Skip to content

Commit

Permalink
Extend transaction order test to cover total order of transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbertJordan committed Nov 15, 2024
1 parent 84b9036 commit 05e7d1f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
21 changes: 11 additions & 10 deletions tests/contracts/counter_event_emitter/counter_event_emitter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ pragma solidity ^0.8.20;
contract counter_event_emitter {
int private totalCount = 0;
mapping(address => int) public perAddrCount;
event Count(int count);
event Count(int totalCount, int perAddrCount);

function increment() public {
// We need to check correct order per account
perAddrCount[msg.sender] += 1;
totalCount += 1;
emit Count(perAddrCount[msg.sender]);
emit Count(totalCount, perAddrCount[msg.sender]);
}

function getTotalCount() public view returns (int) {
Expand Down
48 changes: 43 additions & 5 deletions tests/transaction_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package tests

import (
"context"
"github.com/Fantom-foundation/go-opera/tests/contracts/counter_event_emitter"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
"math"
"math/big"
"math/rand/v2"
"testing"

"github.com/Fantom-foundation/go-opera/tests/contracts/counter_event_emitter"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
)

func TestTransactionOrder(t *testing.T) {
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestTransactionOrder(t *testing.T) {
count, err := contract.ParseCount(*receipt.Logs[0])
require.NoError(t, err)
// Nonce starts at 0 and count starts at 1 per account
accCount := count.Count.Uint64()
accCount := count.PerAddrCount.Uint64()
nonce := tx.Nonce() + 1
if accCount != nonce {
t.Fatalf("transactions are not ordered, got idx: %d, want idx: %d", accCount, nonce)
Expand All @@ -99,6 +100,43 @@ func TestTransactionOrder(t *testing.T) {
if got, want := gotCount.Uint64(), numTxs*numBlocks; got != want {
t.Errorf("wrong count, got: %d, want: %d", got, want)
}

// Check that transactions are ordered correctly in the blockchain and that
// for each transaction a correct receipt is available.
globalCounter := uint64(0)
context := context.Background()
lastBlock, err := client.BlockNumber(context)
require.NoError(t, err)
for i := range lastBlock + 1 {
block, err := client.BlockByNumber(context, big.NewInt(int64(i)))
require.NoError(t, err)
for i, tx := range block.Transactions() {
receipt, err := client.TransactionReceipt(context, tx.Hash())
require.NoError(t, err)

// Check that the receipt matches to the transaction.
require.Equal(t, receipt.Status, types.ReceiptStatusSuccessful)
require.Equal(t, receipt.TxHash, tx.Hash())
require.Equal(t, receipt.BlockHash, block.Hash())
require.Equal(t, receipt.BlockNumber, block.Number())
require.Equal(t, receipt.TransactionIndex, uint(i))

// Check whether the receipt is for a counter transaction.
if len(receipt.Logs) != 1 {
continue
}
count, err := contract.ParseCount(*receipt.Logs[0])
if err != nil {
continue
}

// Check that transactions have been processed in order.
require.Equal(t, count.PerAddrCount.Uint64(), tx.Nonce()+1)
require.Equal(t, count.TotalCount.Uint64(), globalCounter+1)
globalCounter++
}
}
require.Equal(t, globalCounter, numTxs*numBlocks)
}

// makeAccountWithMaxBalance creates a new account and endows it with math.MaxInt64 balance.
Expand Down

0 comments on commit 05e7d1f

Please sign in to comment.