From 19bd3bb2e2204c53a80c3efa1f9894af1a36b08b Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 27 May 2015 15:24:45 -0400 Subject: [PATCH] int/nonce fixes --- rpc/test/helpers.go | 8 ++++---- rpc/test/tests.go | 2 +- types/tx_utils.go | 40 ++++++++++++---------------------------- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/rpc/test/helpers.go b/rpc/test/helpers.go index 768704cf2..cbbb530e9 100644 --- a/rpc/test/helpers.go +++ b/rpc/test/helpers.go @@ -94,7 +94,7 @@ func init() { func makeDefaultSendTx(t *testing.T, typ string, addr []byte, amt uint64) *types.SendTx { nonce := getNonce(t, typ, user[0].Address) tx := types.NewSendTx() - tx.AddInputWithNonce(user[0].PubKey, amt, nonce) + tx.AddInputWithNonce(user[0].PubKey, amt, nonce+1) tx.AddOutput(addr, amt) return tx } @@ -114,7 +114,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) + tx := types.NewNameTxWithNonce(user[0].PubKey, name, value, amt, fee, nonce+1) tx.Sign(user[0]) return tx } @@ -123,7 +123,7 @@ func makeDefaultNameTx(t *testing.T, typ string, name, value string, amt, fee ui // rpc call wrappers (fail on err) // get an account's nonce -func getNonce(t *testing.T, typ string, addr []byte) uint64 { +func getNonce(t *testing.T, typ string, addr []byte) uint { client := clients[typ] ac, err := client.GetAccount(addr) if err != nil { @@ -132,7 +132,7 @@ func getNonce(t *testing.T, typ string, addr []byte) uint64 { if ac.Account == nil { return 0 } - return uint64(ac.Account.Sequence) + return ac.Account.Sequence } // get the account diff --git a/rpc/test/tests.go b/rpc/test/tests.go index fba33b23c..973a05b30 100644 --- a/rpc/test/tests.go +++ b/rpc/test/tests.go @@ -237,7 +237,7 @@ func testNameReg(t *testing.T, typ string) { // try to update as non owner, should fail 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) + tx = types.NewNameTxWithNonce(user[1].PubKey, name, data2, amt, fee, nonce+1) tx.Sign(user[1]) _, err := client.BroadcastTx(tx) if err == nil { diff --git a/types/tx_utils.go b/types/tx_utils.go index a7228c7b4..39578f82c 100644 --- a/types/tx_utils.go +++ b/types/tx_utils.go @@ -25,23 +25,15 @@ func (tx *SendTx) AddInput(st AccountGetter, pubkey account.PubKey, amt uint64) if acc == nil { return fmt.Errorf("Invalid address %X from pubkey %X", addr, pubkey) } - - tx.Inputs = append(tx.Inputs, &TxInput{ - Address: addr, - Amount: amt, - Sequence: uint(acc.Sequence) + 1, - Signature: account.SignatureEd25519{}, - PubKey: pubkey, - }) - return nil + return tx.AddInputWithNonce(pubkey, amt, acc.Sequence+1) } -func (tx *SendTx) AddInputWithNonce(pubkey account.PubKey, amt, nonce uint64) error { +func (tx *SendTx) AddInputWithNonce(pubkey account.PubKey, amt uint64, nonce uint) error { addr := pubkey.Address() tx.Inputs = append(tx.Inputs, &TxInput{ Address: addr, Amount: amt, - Sequence: uint(nonce) + 1, + Sequence: nonce, Signature: account.SignatureEd25519{}, PubKey: pubkey, }) @@ -75,16 +67,16 @@ func NewCallTx(st AccountGetter, from account.PubKey, to, data []byte, amt, gasL return nil, fmt.Errorf("Invalid address %X from pubkey %X", addr, from) } - nonce := uint64(acc.Sequence) + nonce := acc.Sequence + 1 return NewCallTxWithNonce(from, to, data, amt, gasLimit, fee, nonce), nil } -func NewCallTxWithNonce(from account.PubKey, to, data []byte, amt, gasLimit, fee, nonce uint64) *CallTx { +func NewCallTxWithNonce(from account.PubKey, to, data []byte, amt, gasLimit, fee uint64, nonce uint) *CallTx { addr := from.Address() input := &TxInput{ Address: addr, Amount: amt, - Sequence: uint(nonce) + 1, + Sequence: nonce, Signature: account.SignatureEd25519{}, PubKey: from, } @@ -113,16 +105,16 @@ func NewNameTx(st AccountGetter, from account.PubKey, name, data string, amt, fe return nil, fmt.Errorf("Invalid address %X from pubkey %X", addr, from) } - nonce := uint64(acc.Sequence) + nonce := acc.Sequence + 1 return NewNameTxWithNonce(from, name, data, amt, fee, nonce), nil } -func NewNameTxWithNonce(from account.PubKey, name, data string, amt, fee, nonce uint64) *NameTx { +func NewNameTxWithNonce(from account.PubKey, name, data string, amt, fee uint64, nonce uint) *NameTx { addr := from.Address() input := &TxInput{ Address: addr, Amount: amt, - Sequence: uint(nonce) + 1, + Sequence: nonce, Signature: account.SignatureEd25519{}, PubKey: from, } @@ -161,23 +153,15 @@ func (tx *BondTx) AddInput(st AccountGetter, pubkey account.PubKey, amt uint64) if acc == nil { return fmt.Errorf("Invalid address %X from pubkey %X", addr, pubkey) } - - tx.Inputs = append(tx.Inputs, &TxInput{ - Address: addr, - Amount: amt, - Sequence: uint(acc.Sequence) + 1, - Signature: account.SignatureEd25519{}, - PubKey: pubkey, - }) - return nil + return tx.AddInputWithNonce(pubkey, amt, acc.Sequence+1) } -func (tx *BondTx) AddInputWithNonce(pubkey account.PubKey, amt, nonce uint64) error { +func (tx *BondTx) AddInputWithNonce(pubkey account.PubKey, amt uint64, nonce uint) error { addr := pubkey.Address() tx.Inputs = append(tx.Inputs, &TxInput{ Address: addr, Amount: amt, - Sequence: uint(nonce) + 1, + Sequence: nonce, Signature: account.SignatureEd25519{}, PubKey: pubkey, })