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