From 02f4219079803c2d22e155d1f1d02b7f94ab97d5 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 15 May 2015 16:12:34 -0400 Subject: [PATCH] cleanup calldepth=0 events logic --- rpc/test/client_ws_test.go | 14 +++++++++----- rpc/test/ws_helpers_test.go | 1 - state/execution.go | 7 ++++++- vm/vm.go | 5 +---- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/rpc/test/client_ws_test.go b/rpc/test/client_ws_test.go index 85cb2ceeb..c926b6124 100644 --- a/rpc/test/client_ws_test.go +++ b/rpc/test/client_ws_test.go @@ -230,7 +230,7 @@ func TestWSCallWait(t *testing.T) { // susbscribe to the new contract amt = uint64(10001) - eid2 := types.EventStringAccReceive(contractAddr) + eid2 := types.EventStringAccOutput(contractAddr) subscribe(t, con, eid2) defer func() { unsubscribe(t, con, eid2) @@ -254,7 +254,7 @@ func TestWSCallNoWait(t *testing.T) { // susbscribe to the new contract amt = uint64(10001) - eid := types.EventStringAccReceive(contractAddr) + eid := types.EventStringAccOutput(contractAddr) subscribe(t, con, eid) defer func() { unsubscribe(t, con, eid) @@ -284,16 +284,20 @@ func TestWSCallCall(t *testing.T) { // susbscribe to the new contracts amt = uint64(10001) eid1 := types.EventStringAccReceive(contractAddr1) - eid2 := types.EventStringAccReceive(contractAddr2) subscribe(t, con, eid1) - subscribe(t, con, eid2) defer func() { unsubscribe(t, con, eid1) - unsubscribe(t, con, eid2) con.Close() }() // call contract2, which should call contract1, and wait for ev1 data := []byte{0x1} // just needs to be non empty for this to be a CallTx + + // let the contract get created first + waitForEvent(t, con, eid1, true, func() { + }, func(eid string, b []byte) error { + return nil + }) + // call it waitForEvent(t, con, eid1, true, func() { tx, _ := broadcastTx(t, "JSONRPC", userByteAddr, contractAddr2, data, userBytePriv, amt, 1000, 1000) *txid = account.HashSignBytes(tx) diff --git a/rpc/test/ws_helpers_test.go b/rpc/test/ws_helpers_test.go index 2eacbfcca..241e7a7ba 100644 --- a/rpc/test/ws_helpers_test.go +++ b/rpc/test/ws_helpers_test.go @@ -130,7 +130,6 @@ func unmarshalValidateCallCall(origin, returnCode []byte, txid *[]byte) func(str if bytes.Compare(response.Data.TxId, *txid) != 0 { return fmt.Errorf("TxIds do not match up! Got %x, expected %x", response.Data.TxId, *txid) } - // calldata := response.Data.CallData return nil } } diff --git a/state/execution.go b/state/execution.go index d1cdc485e..dabe80160 100644 --- a/state/execution.go +++ b/state/execution.go @@ -406,6 +406,11 @@ func ExecTx(blockCache *BlockCache, tx_ types.Tx, runCall bool, evc events.Firea if outAcc == nil || len(outAcc.Code) == 0 { // if you call an account that doesn't exist // or an account with no code then we take fees (sorry pal) + // NOTE: it's fine to create a contract and call it within one + // block (nonce will prevent re-ordering of those txs) + // but to create with one account and call with another + // you have to wait a block to avoid a re-ordering attack + // that will take your fees inAcc.Balance -= tx.Fee blockCache.UpdateAccount(inAcc) if outAcc == nil { @@ -457,7 +462,7 @@ func ExecTx(blockCache *BlockCache, tx_ types.Tx, runCall bool, evc events.Firea // a separate event will be fired from vm for each additional call if evc != nil { evc.FireEvent(types.EventStringAccInput(tx.Input.Address), types.EventMsgCallTx{tx, ret, exception}) - evc.FireEvent(types.EventStringAccReceive(tx.Address), types.EventMsgCallTx{tx, ret, exception}) + evc.FireEvent(types.EventStringAccOutput(tx.Address), types.EventMsgCallTx{tx, ret, exception}) } } else { // The mempool does not call txs until diff --git a/vm/vm.go b/vm/vm.go index 961638682..6a814655b 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -76,10 +76,7 @@ func (vm *VM) Call(caller, callee *Account, code, input []byte, value uint64, ga exception := new(string) defer func() { - // if callDepth is 0 the event is fired from ExecTx (along with the Input event) - // otherwise, we fire from here. - if vm.callDepth != 0 && vm.evc != nil { - fmt.Println("FIRE AWAY!", types.EventStringAccReceive(callee.Address.Postfix(20))) + if vm.evc != nil { vm.evc.FireEvent(types.EventStringAccReceive(callee.Address.Postfix(20)), types.EventMsgCall{ &types.CallData{caller.Address.Postfix(20), callee.Address.Postfix(20), input, value, *gas}, vm.origin.Postfix(20),