Skip to content
Snippets Groups Projects
Unverified Commit 9656387e authored by Silas Davis's avatar Silas Davis
Browse files

Generate receipts and fix broadcast test

parent a2184ea1
No related branches found
No related tags found
No related merge requests found
...@@ -28,7 +28,6 @@ import ( ...@@ -28,7 +28,6 @@ import (
ptypes "github.com/eris-ltd/eris-db/permission/types" ptypes "github.com/eris-ltd/eris-db/permission/types"
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto"
"github.com/tendermint/go-merkle"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
) )
...@@ -45,12 +44,8 @@ func SignBytes(chainID string, o Signable) []byte { ...@@ -45,12 +44,8 @@ func SignBytes(chainID string, o Signable) []byte {
if *err != nil { if *err != nil {
PanicCrisis(err) PanicCrisis(err)
} }
return buf.Bytes()
}
// HashSignBytes is a convenience method for getting the hash of the bytes of a signable return buf.Bytes()
func HashSignBytes(chainID string, o Signable) []byte {
return merkle.SimpleHashFromBinary(SignBytes(chainID, o))
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
......
...@@ -474,7 +474,7 @@ func ExecTx(blockCache *BlockCache, tx txs.Tx, runCall bool, evc events.Fireable ...@@ -474,7 +474,7 @@ func ExecTx(blockCache *BlockCache, tx txs.Tx, runCall bool, evc events.Fireable
// Write caller/callee to txCache. // Write caller/callee to txCache.
txCache.UpdateAccount(caller) txCache.UpdateAccount(caller)
txCache.UpdateAccount(callee) 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) vmach.SetFireable(evc)
// NOTE: Call() transfers the value from caller to callee iff call succeeds. // NOTE: Call() transfers the value from caller to callee iff call succeeds.
ret, err = vmach.Call(caller, callee, code, tx.Data, value, &gas) ret, err = vmach.Call(caller, callee, code, tx.Data, value, &gas)
......
...@@ -127,7 +127,7 @@ func (this *transactor) BroadcastTx(tx txs.Tx) (*txs.Receipt, error) { ...@@ -127,7 +127,7 @@ func (this *transactor) BroadcastTx(tx txs.Tx) (*txs.Receipt, error) {
return nil, fmt.Errorf("Error broadcasting transaction: %v", err) 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 createsContract uint8
var contractAddr []byte var contractAddr []byte
// check if creates new contract // check if creates new contract
......
...@@ -207,6 +207,6 @@ func TestWSCallCall(t *testing.T) { ...@@ -207,6 +207,6 @@ func TestWSCallCall(t *testing.T) {
waitForEvent(t, wsc, eid1, true, func() { waitForEvent(t, wsc, eid1, true, func() {
tx := makeDefaultCallTx(t, wsTyp, contractAddr2, nil, amt, gasLim, fee) tx := makeDefaultCallTx(t, wsTyp, contractAddr2, nil, amt, gasLim, fee)
broadcastTx(t, wsTyp, tx) broadcastTx(t, wsTyp, tx)
*txid = txs.TxID(chainID, tx) *txid = txs.TxHash(chainID, tx)
}, unmarshalValidateCall(user[0].Address, returnVal, txid)) }, unmarshalValidateCall(user[0].Address, returnVal, txid))
} }
...@@ -8,7 +8,8 @@ import ( ...@@ -8,7 +8,8 @@ import (
edbcli "github.com/eris-ltd/eris-db/rpc/tendermint/client" edbcli "github.com/eris-ltd/eris-db/rpc/tendermint/client"
"github.com/eris-ltd/eris-db/txs" "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 } var doNothing = func(eid string, b interface{}) error { return nil }
...@@ -39,9 +40,9 @@ func testGetAccount(t *testing.T, typ string) { ...@@ -39,9 +40,9 @@ func testGetAccount(t *testing.T, typ string) {
func testOneSignTx(t *testing.T, typ string, addr []byte, amt int64) { func testOneSignTx(t *testing.T, typ string, addr []byte, amt int64) {
tx := makeDefaultSendTx(t, typ, addr, amt) tx := makeDefaultSendTx(t, typ, addr, amt)
tx2 := signTx(t, typ, tx, user[0]) tx2 := signTx(t, typ, tx, user[0])
tx2hash := txs.TxID(chainID, tx2) tx2hash := txs.TxHash(chainID, tx2)
tx.SignInput(chainID, 0, user[0]) tx.SignInput(chainID, 0, user[0])
txhash := txs.TxID(chainID, tx) txhash := txs.TxHash(chainID, tx)
if bytes.Compare(txhash, tx2hash) != 0 { if bytes.Compare(txhash, tx2hash) != 0 {
t.Fatal("Got different signatures for signing via rpc vs tx_utils") t.Fatal("Got different signatures for signing via rpc vs tx_utils")
} }
...@@ -63,10 +64,17 @@ func testBroadcastTx(t *testing.T, typ string) { ...@@ -63,10 +64,17 @@ func testBroadcastTx(t *testing.T, typ string) {
t.Fatal("Failed to compute tx hash") t.Fatal("Failed to compute tx hash")
} }
n, err := new(int), new(error) n, err := new(int), new(error)
buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer) buf := new(bytes.Buffer)
tx.WriteSignBytes(chainID, buf1, n, err) hasher := ripemd160.New()
if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 { tx.WriteSignBytes(chainID, buf, n, err)
t.Fatal("inconsistent hashes for mempool tx and sent tx") // [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) { ...@@ -99,10 +107,11 @@ func testGetStorage(t *testing.T, typ string) {
mempoolCount = 0 mempoolCount = 0
v := getStorage(t, typ, contractAddr, []byte{0x1}) v := getStorage(t, typ, contractAddr, []byte{0x1})
got := LeftPadWord256(v) got := tm_common.LeftPadWord256(v)
expected := LeftPadWord256([]byte{0x5}) expected := tm_common.LeftPadWord256([]byte{0x5})
if got.Compare(expected) != 0 { 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) { ...@@ -110,14 +119,17 @@ func testCallCode(t *testing.T, typ string) {
client := clients[typ] client := clients[typ]
// add two integers and return the result // 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{} data := []byte{}
expected := []byte{0xb} expected := []byte{0xb}
callCode(t, client, user[0].PubKey.Address(), code, data, expected) callCode(t, client, user[0].PubKey.Address(), code, data, expected)
// pass two ints as calldata, add, and return the result // 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} code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60,
data = append(LeftPadWord256([]byte{0x5}).Bytes(), LeftPadWord256([]byte{0x6}).Bytes()...) 0x20, 0x60, 0x0, 0xf3}
data = append(tm_common.LeftPadWord256([]byte{0x5}).Bytes(),
tm_common.LeftPadWord256([]byte{0x6}).Bytes()...)
expected = []byte{0xb} expected = []byte{0xb}
callCode(t, client, user[0].PubKey.Address(), code, data, expected) callCode(t, client, user[0].PubKey.Address(), code, data, expected)
} }
......
...@@ -377,12 +377,20 @@ func (tx *PermissionsTx) String() string { ...@@ -377,12 +377,20 @@ func (tx *PermissionsTx) String() string {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// This should match the leaf hashes of Block.Data.Hash()'s SimpleMerkleTree. func TxHash(chainID string, tx Tx) []byte {
func TxID(chainID string, tx Tx) []byte {
signBytes := acm.SignBytes(chainID, tx) signBytes := acm.SignBytes(chainID, tx)
return wire.BinaryRipemd160(signBytes) 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 { func EncodeTx(tx Tx) []byte {
...@@ -417,7 +425,7 @@ func DecodeTx(txBytes []byte) Tx { ...@@ -417,7 +425,7 @@ func DecodeTx(txBytes []byte) Tx {
func GenerateReceipt(chainId string, tx Tx) Receipt { func GenerateReceipt(chainId string, tx Tx) Receipt {
receipt := Receipt{ receipt := Receipt{
TxHash: TxID(chainId, tx), TxHash: TxHash(chainId, tx),
CreatesContract: 0, CreatesContract: 0,
ContractAddr: nil, ContractAddr: nil,
} }
......
...@@ -38,7 +38,10 @@ func TestSendTxSignable(t *testing.T) { ...@@ -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) 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}]}]}`, 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) chainID)
...@@ -60,7 +63,10 @@ func TestCallTxSignable(t *testing.T) { ...@@ -60,7 +63,10 @@ func TestCallTxSignable(t *testing.T) {
Fee: 222, Fee: 222,
Data: []byte("data1"), Data: []byte("data1"),
} }
signBytes := acm.SignBytes(chainID, callTx) signBytes, err := acm.SignBytes(chainID, bondTx)
if err != nil {
t.Fatal(err)
}
signStr := string(signBytes) 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}}]}`, expected := Fmt(`{"chain_id":"%s","tx":[2,{"address":"636F6E747261637431","data":"6461746131","fee":222,"gas_limit":111,"input":{"address":"696E70757431","amount":12345,"sequence":67890}}]}`,
chainID) chainID)
...@@ -80,7 +86,10 @@ func TestNameTxSignable(t *testing.T) { ...@@ -80,7 +86,10 @@ func TestNameTxSignable(t *testing.T) {
Data: "secretly.not.google.com", Data: "secretly.not.google.com",
Fee: 1000, Fee: 1000,
} }
signBytes := acm.SignBytes(chainID, nameTx) signBytes, err := acm.SignBytes(chainID, bondTx)
if err != nil {
t.Fatal(err)
}
signStr := string(signBytes) 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"}]}`, 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) chainID)
...@@ -117,7 +126,10 @@ func TestBondTxSignable(t *testing.T) { ...@@ -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) 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}]}]}`, 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) chainID)
...@@ -131,7 +143,10 @@ func TestUnbondTxSignable(t *testing.T) { ...@@ -131,7 +143,10 @@ func TestUnbondTxSignable(t *testing.T) {
Address: []byte("address1"), Address: []byte("address1"),
Height: 111, Height: 111,
} }
signBytes := acm.SignBytes(chainID, unbondTx) signBytes, err := acm.SignBytes(chainID, bondTx)
if err != nil {
t.Fatal(err)
}
signStr := string(signBytes) signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%s","tx":[18,{"address":"6164647265737331","height":111}]}`, expected := Fmt(`{"chain_id":"%s","tx":[18,{"address":"6164647265737331","height":111}]}`,
chainID) chainID)
...@@ -145,7 +160,10 @@ func TestRebondTxSignable(t *testing.T) { ...@@ -145,7 +160,10 @@ func TestRebondTxSignable(t *testing.T) {
Address: []byte("address1"), Address: []byte("address1"),
Height: 111, Height: 111,
} }
signBytes := acm.SignBytes(chainID, rebondTx) signBytes, err := acm.SignBytes(chainID, bondTx)
if err != nil {
t.Fatal(err)
}
signStr := string(signBytes) signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%s","tx":[19,{"address":"6164647265737331","height":111}]}`, expected := Fmt(`{"chain_id":"%s","tx":[19,{"address":"6164647265737331","height":111}]}`,
chainID) chainID)
...@@ -168,7 +186,10 @@ func TestPermissionsTxSignable(t *testing.T) { ...@@ -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) 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}}]}`, expected := Fmt(`{"chain_id":"%s","tx":[32,{"args":"[2,{"address":"6164647265737331","permission":1,"value":true}]","input":{"address":"696E70757431","amount":12345,"sequence":250}}]}`,
chainID) chainID)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment