github.com/koko1123/flow-go-1@v0.29.6/fvm/transactionSequenceNum_test.go (about) 1 package fvm_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/koko1123/flow-go-1/fvm" 9 "github.com/koko1123/flow-go-1/fvm/environment" 10 "github.com/koko1123/flow-go-1/fvm/errors" 11 "github.com/koko1123/flow-go-1/fvm/state" 12 "github.com/koko1123/flow-go-1/fvm/tracing" 13 "github.com/koko1123/flow-go-1/fvm/utils" 14 "github.com/koko1123/flow-go-1/model/flow" 15 "github.com/koko1123/flow-go-1/utils/unittest" 16 ) 17 18 func TestTransactionSequenceNumProcess(t *testing.T) { 19 t.Run("sequence number update (happy path)", func(t *testing.T) { 20 ledger := utils.NewSimpleView() 21 txnState := state.NewTransactionState(ledger, state.DefaultParameters()) 22 accounts := environment.NewAccounts(txnState) 23 24 // create an account 25 address := flow.HexToAddress("1234") 26 privKey, err := unittest.AccountKeyDefaultFixture() 27 require.NoError(t, err) 28 err = accounts.Create([]flow.AccountPublicKey{privKey.PublicKey(1000)}, address) 29 require.NoError(t, err) 30 31 tx := flow.TransactionBody{} 32 tx.SetProposalKey(address, 0, 0) 33 proc := fvm.Transaction(&tx, 0) 34 35 seqChecker := fvm.TransactionSequenceNumberChecker{} 36 err = seqChecker.CheckAndIncrementSequenceNumber( 37 tracing.NewTracerSpan(), 38 proc, 39 txnState) 40 require.NoError(t, err) 41 42 // get fetch the sequence number and it should be updated 43 key, err := accounts.GetPublicKey(address, 0) 44 require.NoError(t, err) 45 require.Equal(t, key.SeqNumber, uint64(1)) 46 }) 47 t.Run("invalid sequence number", func(t *testing.T) { 48 ledger := utils.NewSimpleView() 49 txnState := state.NewTransactionState(ledger, state.DefaultParameters()) 50 accounts := environment.NewAccounts(txnState) 51 52 // create an account 53 address := flow.HexToAddress("1234") 54 privKey, err := unittest.AccountKeyDefaultFixture() 55 require.NoError(t, err) 56 err = accounts.Create([]flow.AccountPublicKey{privKey.PublicKey(1000)}, address) 57 require.NoError(t, err) 58 59 tx := flow.TransactionBody{} 60 // invalid sequence number is 2 61 tx.SetProposalKey(address, 0, 2) 62 proc := fvm.Transaction(&tx, 0) 63 64 seqChecker := fvm.TransactionSequenceNumberChecker{} 65 err = seqChecker.CheckAndIncrementSequenceNumber( 66 tracing.NewTracerSpan(), 67 proc, 68 txnState) 69 require.Error(t, err) 70 require.True(t, errors.HasErrorCode(err, errors.ErrCodeInvalidProposalSeqNumberError)) 71 72 // get fetch the sequence number and check it to be unchanged 73 key, err := accounts.GetPublicKey(address, 0) 74 require.NoError(t, err) 75 require.Equal(t, key.SeqNumber, uint64(0)) 76 }) 77 t.Run("invalid address", func(t *testing.T) { 78 ledger := utils.NewSimpleView() 79 txnState := state.NewTransactionState(ledger, state.DefaultParameters()) 80 accounts := environment.NewAccounts(txnState) 81 82 // create an account 83 address := flow.HexToAddress("1234") 84 privKey, err := unittest.AccountKeyDefaultFixture() 85 require.NoError(t, err) 86 err = accounts.Create([]flow.AccountPublicKey{privKey.PublicKey(1000)}, address) 87 require.NoError(t, err) 88 89 tx := flow.TransactionBody{} 90 // wrong address 91 tx.SetProposalKey(flow.HexToAddress("2222"), 0, 0) 92 proc := fvm.Transaction(&tx, 0) 93 94 seqChecker := &fvm.TransactionSequenceNumberChecker{} 95 err = seqChecker.CheckAndIncrementSequenceNumber( 96 tracing.NewTracerSpan(), 97 proc, 98 txnState) 99 require.Error(t, err) 100 101 // get fetch the sequence number and check it to be unchanged 102 key, err := accounts.GetPublicKey(address, 0) 103 require.NoError(t, err) 104 require.Equal(t, key.SeqNumber, uint64(0)) 105 }) 106 }