From 9b6f168a556c50f678480dd9d46496f1add7f73d Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 9 Feb 2024 19:26:53 +1000 Subject: [PATCH] TestTxIndexing() shows the effect of the fix --- gossip/txposition_test.go | 67 ++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/gossip/txposition_test.go b/gossip/txposition_test.go index e277d7071..8651d2ae3 100644 --- a/gossip/txposition_test.go +++ b/gossip/txposition_test.go @@ -1,10 +1,10 @@ package gossip import ( - "fmt" "math/big" "testing" + "github.com/Fantom-foundation/lachesis-base/inter/idx" "github.com/ethereum/go-ethereum/core/types" "github.com/stretchr/testify/require" @@ -17,16 +17,21 @@ func TestTxIndexing(t *testing.T) { logger.SetLevel("debug") require := require.New(t) - env := newTestEnv(2, 3) + var ( + epoch = idx.Epoch(256) + validators = idx.Validator(3) + proposals = [][32]byte{ + ballotOption("Option 1"), + ballotOption("Option 2"), + ballotOption("Option 3"), + } + ) + + env := newTestEnv(epoch, validators) defer env.Close() - proposals := [][32]byte{ - ballotOption("Option 1"), - ballotOption("Option 2"), - ballotOption("Option 3"), - } + // Preparing: - // preparing _, tx1pre, cBallot, err := ballot.DeployBallot(env.Pay(1), env, proposals) require.NoError(err) require.NotNil(cBallot) @@ -34,7 +39,7 @@ func TestTxIndexing(t *testing.T) { tx2pre, err := cBallot.GiveRightToVote(env.Pay(1), env.Address(3)) require.NoError(err) require.NotNil(tx2pre) - receipts, err := env.BlockTxs(nextEpoch, + receipts, err := env.ApplyTxs(sameEpoch, tx1pre, tx2pre, ) @@ -43,6 +48,9 @@ func TestTxIndexing(t *testing.T) { for i, r := range receipts { require.Equal(types.ReceiptStatusSuccessful, r.Status, i) } + + // Testing: + // invalid tx tx1reverted, err := cBallot.Vote(env.Pay(2), big.NewInt(0)) require.NoError(err) @@ -52,23 +60,46 @@ func TestTxIndexing(t *testing.T) { require.NoError(err) require.NotNil(tx2ok) // skipped tx - _, tx3skipped, _, err := ballot.DeployBallot(withLowGas(env.Pay(1)), env, proposals) + tx3skipped := tx2ok + // valid tx + tx4ok, err := cBallot.GiveRightToVote(env.Pay(1), env.Address(2)) require.NoError(err) - require.NotNil(tx3skipped) + require.NotNil(tx1reverted) - receipts, err = env.BlockTxs(nextEpoch, + receipts, err = env.BlockTxs(sameEpoch, tx1reverted, tx2ok, tx3skipped, - ) + tx4ok) require.NoError(err) require.Len(receipts, 3) - var block *big.Int + + var blockN *big.Int for i, r := range receipts { - if block == nil { - block = r.BlockNumber + if blockN == nil { + blockN = r.BlockNumber } - require.Equal(block.Uint64(), r.BlockNumber.Uint64(), i) - } + require.Equal(blockN.Uint64(), r.BlockNumber.Uint64(), i) + txPos := env.store.evm.GetTxPosition(r.TxHash) + require.NotNil(txPos) + + switch r.TxHash { + case tx1reverted.Hash(): + require.Equal(types.ReceiptStatusFailed, r.Status, i) + require.Equal(txPos.BlockOffset, uint32(0)) + case tx2ok.Hash(): + require.Equal(types.ReceiptStatusSuccessful, r.Status, i) + require.Equal(txPos.BlockOffset, uint32(1)) + case tx3skipped.Hash(): + t.Fatal("skipped tx's receipt found") + case tx4ok.Hash(): + require.Equal(types.ReceiptStatusSuccessful, r.Status, i) + require.Equal(txPos.BlockOffset, uint32(3)) // THAT shows the effect of the fix #524 + } + + for j, l := range r.Logs { + require.Equal(txPos.BlockOffset, l.TxIndex, j) + } + } }