func randomSendTx(errs *int) types.Tx { acc, i, nonce, amt := pickAccountNonceAmount(errs) if acc == nil { return nil } tx := types.NewSendTx() tx.AddInputWithNonce(acc.PubKey, amt, nonce) // pick receiver j := rand.Intn(len(keys)) for ; j == i; j = rand.Intn(len(keys)) { } tx.AddOutput(keys[j].Address, amt) tx.SignInput(chainID, 0, acc) return tx }
func TestTxSequence(t *testing.T) { state, privAccounts, _ := RandGenesisState(3, true, 1000, 1, true, 1000) acc0 := state.GetAccount(privAccounts[0].PubKey.Address()) acc0PubKey := privAccounts[0].PubKey acc1 := state.GetAccount(privAccounts[1].PubKey.Address()) // Test a variety of sequence numbers for the tx. // The tx should only pass when i == 1. for i := -1; i < 3; i++ { sequence := acc0.Sequence + i 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) if i == 1 { // Sequence is good. if err != nil { t.Errorf("Expected good sequence to pass: %v", err) } // Check acc.Sequence. newAcc0 := stateCopy.GetAccount(acc0.Address) if newAcc0.Sequence != sequence { t.Errorf("Expected account sequence to change to %v, got %v", sequence, newAcc0.Sequence) } } else { // Sequence is bad. if err == nil { t.Errorf("Expected bad sequence to fail") } // Check acc.Sequence. (shouldn't have changed) newAcc0 := stateCopy.GetAccount(acc0.Address) if newAcc0.Sequence != acc0.Sequence { t.Errorf("Expected account sequence to not change from %v, got %v", acc0.Sequence, newAcc0.Sequence) } } } }
func Send(nodeAddr, signAddr, pubkey, addr, toAddr, amtS, nonceS string) (*types.SendTx, error) { pub, amt, nonce, err := checkCommon(nodeAddr, signAddr, pubkey, addr, amtS, nonceS) if err != nil { return nil, err } if toAddr == "" { return nil, fmt.Errorf("destination address must be given with --to flag") } toAddrBytes, err := hex.DecodeString(toAddr) if err != nil { return nil, fmt.Errorf("toAddr is bad hex: %v", err) } tx := types.NewSendTx() tx.AddInputWithNonce(pub, amt, int(nonce)) tx.AddOutput(toAddrBytes, amt) return tx, nil }