Skip to content

Commit

Permalink
TestTxIndexing() shows the effect of the fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rus-alex committed Feb 11, 2024
1 parent f0b3bdd commit 9b6f168
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions gossip/txposition_test.go
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -17,24 +17,29 @@ 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)
require.NotNil(tx1pre)
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,
)
Expand All @@ -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)
Expand All @@ -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)
}
}
}

0 comments on commit 9b6f168

Please sign in to comment.