From 6eb8386c7cbea7b65fbf56f7de149da7a438de98 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sat, 30 May 2015 01:54:05 -0400 Subject: [PATCH] fixes for chain id in nametx sign functions --- rpc/test/helpers.go | 4 ++-- rpc/test/tests.go | 2 +- state/execution.go | 2 +- state/state_test.go | 34 ++++++++++++++++++---------------- types/tx.go | 4 ++-- types/tx_utils.go | 4 ++-- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/rpc/test/helpers.go b/rpc/test/helpers.go index cbbb530e..beab1cf0 100644 --- a/rpc/test/helpers.go +++ b/rpc/test/helpers.go @@ -107,7 +107,7 @@ func makeDefaultSendTxSigned(t *testing.T, typ string, addr []byte, amt uint64) func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim, fee uint64) *types.CallTx { nonce := getNonce(t, typ, user[0].Address) - tx := types.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce) + tx := types.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce+1) tx.Sign(chainID, user[0]) return tx } @@ -115,7 +115,7 @@ func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim, func makeDefaultNameTx(t *testing.T, typ string, name, value string, amt, fee uint64) *types.NameTx { nonce := getNonce(t, typ, user[0].Address) tx := types.NewNameTxWithNonce(user[0].PubKey, name, value, amt, fee, nonce+1) - tx.Sign(user[0]) + tx.Sign(chainID, user[0]) return tx } diff --git a/rpc/test/tests.go b/rpc/test/tests.go index 973a05b3..35aafefb 100644 --- a/rpc/test/tests.go +++ b/rpc/test/tests.go @@ -238,7 +238,7 @@ func testNameReg(t *testing.T, typ string) { nonce := getNonce(t, typ, user[1].Address) data2 := "this is not my beautiful house" tx = types.NewNameTxWithNonce(user[1].PubKey, name, data2, amt, fee, nonce+1) - tx.Sign(user[1]) + tx.Sign(chainID, user[1]) _, err := client.BroadcastTx(tx) if err == nil { t.Fatal("Expected error on NameTx") diff --git a/state/execution.go b/state/execution.go index 304a653a..83cbea42 100644 --- a/state/execution.go +++ b/state/execution.go @@ -492,7 +492,7 @@ func ExecTx(blockCache *BlockCache, tx_ types.Tx, runCall bool, evc events.Firea log.Debug(Fmt("Can't find pubkey for %X", tx.Input.Address)) return err } - signBytes := account.SignBytes(tx) + signBytes := account.SignBytes(_s.ChainID, tx) err := validateInput(inAcc, signBytes, tx.Input) if err != nil { log.Debug(Fmt("validateInput failed on %X:", tx.Input.Address)) diff --git a/state/state_test.go b/state/state_test.go index 7e8ccc2d..acb809fb 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -179,7 +179,9 @@ func TestTxSequence(t *testing.T) { // The tx should only pass when i == 1. for i := -1; i < 3; i++ { sequence := acc0.Sequence + uint(i) - tx := makeSendTx(sequence) + tx := types.NewSendTx() + tx.AddInputWithNonce(acc0PubKey, 1, sequence) + tx.AddOutput(acc1.Address, 1) tx.Inputs[0].Signature = privAccounts[0].Sign(state.ChainID, tx) stateCopy := state.Copy() err := execTxWithState(stateCopy, tx, true) @@ -223,7 +225,7 @@ func TestNameTxs(t *testing.T) { for _, name := range names { amt := fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data) tx, _ := types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[0]) + tx.Sign(state.ChainID, privAccounts[0]) if err := execTxWithState(state, tx, true); err == nil { t.Fatalf("Expected invalid name error from %s", name) @@ -236,7 +238,7 @@ func TestNameTxs(t *testing.T) { for _, data := range datas { amt := fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data) tx, _ := types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[0]) + tx.Sign(state.ChainID, privAccounts[0]) if err := execTxWithState(state, tx, true); err == nil { t.Fatalf("Expected invalid data error from %s", data) @@ -267,7 +269,7 @@ func TestNameTxs(t *testing.T) { data = "on this side of neptune there are 1234567890 people: first is OMNIVORE. Or is it. Ok this is pretty restrictive. No exclamations :(. Faces tho :')" amt := fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data) tx, _ := types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[0]) + tx.Sign(state.ChainID, privAccounts[0]) if err := execTxWithState(state, tx, true); err != nil { t.Fatal(err) } @@ -276,7 +278,7 @@ func TestNameTxs(t *testing.T) { // fail to update it as non-owner, in same block tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[1]) + tx.Sign(state.ChainID, privAccounts[1]) if err := execTxWithState(state, tx, true); err == nil { t.Fatal("Expected error") } @@ -284,7 +286,7 @@ func TestNameTxs(t *testing.T) { // update it as owner, just to increase expiry, in same block // NOTE: we have to resend the data or it will clear it (is this what we want?) tx, _ = types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[0]) + tx.Sign(state.ChainID, privAccounts[0]) if err := execTxWithStateNewBlock(state, tx, true); err != nil { t.Fatal(err) } @@ -293,7 +295,7 @@ func TestNameTxs(t *testing.T) { // update it as owner, just to increase expiry, in next block tx, _ = types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[0]) + tx.Sign(state.ChainID, privAccounts[0]) if err := execTxWithStateNewBlock(state, tx, true); err != nil { t.Fatal(err) } @@ -303,7 +305,7 @@ func TestNameTxs(t *testing.T) { // fail to update it as non-owner state.LastBlockHeight = uint(entry.Expires - 1) tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[1]) + tx.Sign(state.ChainID, privAccounts[1]) if err := execTxWithState(state, tx, true); err == nil { t.Fatal("Expected error") } @@ -311,7 +313,7 @@ func TestNameTxs(t *testing.T) { // once expires, non-owner succeeds state.LastBlockHeight = uint(entry.Expires) tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[1]) + tx.Sign(state.ChainID, privAccounts[1]) if err := execTxWithState(state, tx, true); err != nil { t.Fatal(err) } @@ -324,7 +326,7 @@ func TestNameTxs(t *testing.T) { numDesiredBlocks = 10 amt = fee + (numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data) - oldCredit) tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[1]) + tx.Sign(state.ChainID, privAccounts[1]) if err := execTxWithState(state, tx, true); err != nil { t.Fatal(err) } @@ -335,7 +337,7 @@ func TestNameTxs(t *testing.T) { amt = fee data = "" tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[1]) + tx.Sign(state.ChainID, privAccounts[1]) if err := execTxWithStateNewBlock(state, tx, true); err != nil { t.Fatal(err) } @@ -350,7 +352,7 @@ func TestNameTxs(t *testing.T) { data = "some data" amt = fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data) tx, _ = types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[0]) + tx.Sign(state.ChainID, privAccounts[0]) if err := execTxWithState(state, tx, true); err != nil { t.Fatal(err) } @@ -361,7 +363,7 @@ func TestNameTxs(t *testing.T) { amt = fee data = "" tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee) - tx.Sign(privAccounts[1]) + tx.Sign(state.ChainID, privAccounts[1]) if err := execTxWithStateNewBlock(state, tx, true); err != nil { t.Fatal(err) } @@ -436,7 +438,7 @@ func TestTxs(t *testing.T) { GasLimit: 10, } - tx.Input.Signature = privAccounts[0].Sign(tx) + tx.Input.Signature = privAccounts[0].Sign(state.ChainID, tx) err := execTxWithState(state, tx, true) if err != nil { t.Errorf("Got error in executing call transaction, %v", err) @@ -485,7 +487,7 @@ proof-of-work chain as proof of what happened while they were gone ` Data: entryData, } - tx.Input.Signature = privAccounts[0].Sign(tx) + tx.Input.Signature = privAccounts[0].Sign(state.ChainID, tx) err := execTxWithState(state, tx, true) if err != nil { t.Errorf("Got error in executing call transaction, %v", err) @@ -506,7 +508,7 @@ proof-of-work chain as proof of what happened while they were gone ` // test a bad string tx.Data = string([]byte{0, 1, 2, 3, 127, 128, 129, 200, 251}) tx.Input.Sequence += 1 - tx.Input.Signature = privAccounts[0].Sign(tx) + tx.Input.Signature = privAccounts[0].Sign(state.ChainID, tx) err = execTxWithState(state, tx, true) if err != types.ErrTxInvalidString { t.Errorf("Expected invalid string error. Got: %s", err.Error()) diff --git a/types/tx.go b/types/tx.go index 1665a332..8bb59640 100644 --- a/types/tx.go +++ b/types/tx.go @@ -190,9 +190,9 @@ type NameTx struct { Fee uint64 `json:"fee"` } -func (tx *NameTx) WriteSignBytes(w io.Writer, n *int64, err *error) { +func (tx *NameTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) { // We hex encode the network name so we don't deal with escaping issues. - binary.WriteTo([]byte(Fmt(`{"network":"%X"`, config.GetString("network"))), w, n, err) + binary.WriteTo([]byte(Fmt(`{"network":"%X"`, chainID)), w, n, err) binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"name":"%s","data":"%s"`, TxTypeName, tx.Name, tx.Data)), w, n, err) binary.WriteTo([]byte(Fmt(`,"fee":%v,"input":`, tx.Fee)), w, n, err) tx.Input.WriteSignBytes(w, n, err) diff --git a/types/tx_utils.go b/types/tx_utils.go index 39578f82..0c878e31 100644 --- a/types/tx_utils.go +++ b/types/tx_utils.go @@ -127,9 +127,9 @@ func NewNameTxWithNonce(from account.PubKey, name, data string, amt, fee uint64, } } -func (tx *NameTx) Sign(privAccount *account.PrivAccount) { +func (tx *NameTx) Sign(chainID string, privAccount *account.PrivAccount) { tx.Input.PubKey = privAccount.PubKey - tx.Input.Signature = privAccount.Sign(tx) + tx.Input.Signature = privAccount.Sign(chainID, tx) } //----------------------------------------------------------------------------