From 9656387e5129bcfdb97c8d6a11993872544115ae Mon Sep 17 00:00:00 2001 From: Silas Davis <silas@erisindustries.com> Date: Fri, 15 Jul 2016 13:58:58 +0100 Subject: [PATCH] Generate receipts and fix broadcast test --- account/account.go | 7 +---- manager/eris-mint/state/execution.go | 2 +- manager/eris-mint/transactor.go | 2 +- rpc/tendermint/test/client_ws_test.go | 2 +- rpc/tendermint/test/tests.go | 38 ++++++++++++++++++--------- txs/tx.go | 14 +++++++--- txs/tx_test.go | 35 +++++++++++++++++++----- 7 files changed, 68 insertions(+), 32 deletions(-) diff --git a/account/account.go b/account/account.go index 9a0121eb..99b545f2 100644 --- a/account/account.go +++ b/account/account.go @@ -28,7 +28,6 @@ import ( ptypes "github.com/eris-ltd/eris-db/permission/types" . "github.com/tendermint/go-common" "github.com/tendermint/go-crypto" - "github.com/tendermint/go-merkle" "github.com/tendermint/go-wire" ) @@ -45,12 +44,8 @@ func SignBytes(chainID string, o Signable) []byte { if *err != nil { PanicCrisis(err) } - return buf.Bytes() -} -// HashSignBytes is a convenience method for getting the hash of the bytes of a signable -func HashSignBytes(chainID string, o Signable) []byte { - return merkle.SimpleHashFromBinary(SignBytes(chainID, o)) + return buf.Bytes() } //----------------------------------------------------------------------------- diff --git a/manager/eris-mint/state/execution.go b/manager/eris-mint/state/execution.go index e76aa35a..9d2aaa2e 100644 --- a/manager/eris-mint/state/execution.go +++ b/manager/eris-mint/state/execution.go @@ -474,7 +474,7 @@ func ExecTx(blockCache *BlockCache, tx txs.Tx, runCall bool, evc events.Fireable // Write caller/callee to txCache. txCache.UpdateAccount(caller) txCache.UpdateAccount(callee) - vmach := vm.NewVM(txCache, params, caller.Address, txs.TxID(_s.ChainID, tx)) + vmach := vm.NewVM(txCache, params, caller.Address, txs.TxHash(_s.ChainID, tx)) vmach.SetFireable(evc) // NOTE: Call() transfers the value from caller to callee iff call succeeds. ret, err = vmach.Call(caller, callee, code, tx.Data, value, &gas) diff --git a/manager/eris-mint/transactor.go b/manager/eris-mint/transactor.go index a3e547e2..e9882f77 100644 --- a/manager/eris-mint/transactor.go +++ b/manager/eris-mint/transactor.go @@ -127,7 +127,7 @@ func (this *transactor) BroadcastTx(tx txs.Tx) (*txs.Receipt, error) { return nil, fmt.Errorf("Error broadcasting transaction: %v", err) } - txHash := txs.TxID(this.chainID, tx) + txHash := txs.TxHash(this.chainID, tx) var createsContract uint8 var contractAddr []byte // check if creates new contract diff --git a/rpc/tendermint/test/client_ws_test.go b/rpc/tendermint/test/client_ws_test.go index 7cb64ecd..d0034372 100644 --- a/rpc/tendermint/test/client_ws_test.go +++ b/rpc/tendermint/test/client_ws_test.go @@ -207,6 +207,6 @@ func TestWSCallCall(t *testing.T) { waitForEvent(t, wsc, eid1, true, func() { tx := makeDefaultCallTx(t, wsTyp, contractAddr2, nil, amt, gasLim, fee) broadcastTx(t, wsTyp, tx) - *txid = txs.TxID(chainID, tx) + *txid = txs.TxHash(chainID, tx) }, unmarshalValidateCall(user[0].Address, returnVal, txid)) } diff --git a/rpc/tendermint/test/tests.go b/rpc/tendermint/test/tests.go index 2b8a83a3..7bcce656 100644 --- a/rpc/tendermint/test/tests.go +++ b/rpc/tendermint/test/tests.go @@ -8,7 +8,8 @@ import ( edbcli "github.com/eris-ltd/eris-db/rpc/tendermint/client" "github.com/eris-ltd/eris-db/txs" - . "github.com/tendermint/go-common" + tm_common "github.com/tendermint/go-common" + "golang.org/x/crypto/ripemd160" ) var doNothing = func(eid string, b interface{}) error { return nil } @@ -39,9 +40,9 @@ func testGetAccount(t *testing.T, typ string) { func testOneSignTx(t *testing.T, typ string, addr []byte, amt int64) { tx := makeDefaultSendTx(t, typ, addr, amt) tx2 := signTx(t, typ, tx, user[0]) - tx2hash := txs.TxID(chainID, tx2) + tx2hash := txs.TxHash(chainID, tx2) tx.SignInput(chainID, 0, user[0]) - txhash := txs.TxID(chainID, tx) + txhash := txs.TxHash(chainID, tx) if bytes.Compare(txhash, tx2hash) != 0 { t.Fatal("Got different signatures for signing via rpc vs tx_utils") } @@ -63,10 +64,17 @@ func testBroadcastTx(t *testing.T, typ string) { t.Fatal("Failed to compute tx hash") } n, err := new(int), new(error) - buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer) - tx.WriteSignBytes(chainID, buf1, n, err) - if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 { - t.Fatal("inconsistent hashes for mempool tx and sent tx") + buf := new(bytes.Buffer) + hasher := ripemd160.New() + tx.WriteSignBytes(chainID, buf, n, err) + // [Silas] Currently tx.TxHash uses go-wire, we we drop that we can drop the prefix here + goWireBytes := append([]byte{0x01, 0xcf}, buf.Bytes()...) + hasher.Write(goWireBytes) + txHashExpected := hasher.Sum(nil) + if bytes.Compare(receipt.TxHash, txHashExpected) != 0 { + t.Fatalf("The receipt hash '%x' does not equal the ripemd160 hash of the " + + "transaction signed bytes calculated in the test: '%x'", + receipt.TxHash, txHashExpected) } } @@ -99,10 +107,11 @@ func testGetStorage(t *testing.T, typ string) { mempoolCount = 0 v := getStorage(t, typ, contractAddr, []byte{0x1}) - got := LeftPadWord256(v) - expected := LeftPadWord256([]byte{0x5}) + got := tm_common.LeftPadWord256(v) + expected := tm_common.LeftPadWord256([]byte{0x5}) if got.Compare(expected) != 0 { - t.Fatalf("Wrong storage value. Got %x, expected %x", got.Bytes(), expected.Bytes()) + t.Fatalf("Wrong storage value. Got %x, expected %x", got.Bytes(), + expected.Bytes()) } } @@ -110,14 +119,17 @@ func testCallCode(t *testing.T, typ string) { client := clients[typ] // add two integers and return the result - code := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3} + code := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, + 0x0, 0xf3} data := []byte{} expected := []byte{0xb} callCode(t, client, user[0].PubKey.Address(), code, data, expected) // pass two ints as calldata, add, and return the result - code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3} - data = append(LeftPadWord256([]byte{0x5}).Bytes(), LeftPadWord256([]byte{0x6}).Bytes()...) + code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60, + 0x20, 0x60, 0x0, 0xf3} + data = append(tm_common.LeftPadWord256([]byte{0x5}).Bytes(), + tm_common.LeftPadWord256([]byte{0x6}).Bytes()...) expected = []byte{0xb} callCode(t, client, user[0].PubKey.Address(), code, data, expected) } diff --git a/txs/tx.go b/txs/tx.go index 10151dcb..b97c95d6 100644 --- a/txs/tx.go +++ b/txs/tx.go @@ -377,12 +377,20 @@ func (tx *PermissionsTx) String() string { //----------------------------------------------------------------------------- -// This should match the leaf hashes of Block.Data.Hash()'s SimpleMerkleTree. -func TxID(chainID string, tx Tx) []byte { +func TxHash(chainID string, tx Tx) []byte { signBytes := acm.SignBytes(chainID, tx) return wire.BinaryRipemd160(signBytes) } +// [Silas] Leaving this implementation here for when we transition +func TxHashFuture(chainID string, tx Tx) []byte { + signBytes := acm.SignBytes(chainID, tx) + hasher := ripemd160.New() + hasher.Write(signBytes) + // Calling Sum(nil) just gives us the digest with nothing prefixed + return hasher.Sum(nil) +} + //----------------------------------------------------------------------------- func EncodeTx(tx Tx) []byte { @@ -417,7 +425,7 @@ func DecodeTx(txBytes []byte) Tx { func GenerateReceipt(chainId string, tx Tx) Receipt { receipt := Receipt{ - TxHash: TxID(chainId, tx), + TxHash: TxHash(chainId, tx), CreatesContract: 0, ContractAddr: nil, } diff --git a/txs/tx_test.go b/txs/tx_test.go index 5a73d351..da6009c8 100644 --- a/txs/tx_test.go +++ b/txs/tx_test.go @@ -38,7 +38,10 @@ func TestSendTxSignable(t *testing.T) { }, }, } - signBytes := acm.SignBytes(chainID, sendTx) + signBytes, err := acm.SignBytes(chainID, bondTx) + if err != nil { + t.Fatal(err) + } signStr := string(signBytes) expected := Fmt(`{"chain_id":"%s","tx":[1,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"outputs":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`, chainID) @@ -60,7 +63,10 @@ func TestCallTxSignable(t *testing.T) { Fee: 222, Data: []byte("data1"), } - signBytes := acm.SignBytes(chainID, callTx) + signBytes, err := acm.SignBytes(chainID, bondTx) + if err != nil { + t.Fatal(err) + } signStr := string(signBytes) expected := Fmt(`{"chain_id":"%s","tx":[2,{"address":"636F6E747261637431","data":"6461746131","fee":222,"gas_limit":111,"input":{"address":"696E70757431","amount":12345,"sequence":67890}}]}`, chainID) @@ -80,7 +86,10 @@ func TestNameTxSignable(t *testing.T) { Data: "secretly.not.google.com", Fee: 1000, } - signBytes := acm.SignBytes(chainID, nameTx) + signBytes, err := acm.SignBytes(chainID, bondTx) + if err != nil { + t.Fatal(err) + } signStr := string(signBytes) expected := Fmt(`{"chain_id":"%s","tx":[3,{"data":"secretly.not.google.com","fee":1000,"input":{"address":"696E70757431","amount":12345,"sequence":250},"name":"google.com"}]}`, chainID) @@ -117,7 +126,10 @@ func TestBondTxSignable(t *testing.T) { }, }, } - signBytes := acm.SignBytes(chainID, bondTx) + signBytes, err := acm.SignBytes(chainID, bondTx) + if err != nil { + t.Fatal(err) + } signStr := string(signBytes) expected := Fmt(`{"chain_id":"%s","tx":[17,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"pub_key":"3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29","unbond_to":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`, chainID) @@ -131,7 +143,10 @@ func TestUnbondTxSignable(t *testing.T) { Address: []byte("address1"), Height: 111, } - signBytes := acm.SignBytes(chainID, unbondTx) + signBytes, err := acm.SignBytes(chainID, bondTx) + if err != nil { + t.Fatal(err) + } signStr := string(signBytes) expected := Fmt(`{"chain_id":"%s","tx":[18,{"address":"6164647265737331","height":111}]}`, chainID) @@ -145,7 +160,10 @@ func TestRebondTxSignable(t *testing.T) { Address: []byte("address1"), Height: 111, } - signBytes := acm.SignBytes(chainID, rebondTx) + signBytes, err := acm.SignBytes(chainID, bondTx) + if err != nil { + t.Fatal(err) + } signStr := string(signBytes) expected := Fmt(`{"chain_id":"%s","tx":[19,{"address":"6164647265737331","height":111}]}`, chainID) @@ -168,7 +186,10 @@ func TestPermissionsTxSignable(t *testing.T) { }, } - signBytes := acm.SignBytes(chainID, permsTx) + signBytes, err := acm.SignBytes(chainID, bondTx) + if err != nil { + t.Fatal(err) + } signStr := string(signBytes) expected := Fmt(`{"chain_id":"%s","tx":[32,{"args":"[2,{"address":"6164647265737331","permission":1,"value":true}]","input":{"address":"696E70757431","amount":12345,"sequence":250}}]}`, chainID) -- GitLab