Skip to content

Commit

Permalink
test: refactor state root checks and handle empty transactions.
Browse files Browse the repository at this point in the history
Add a `stateRootChanged` flag to test cases to explicitly verify state root changes. Update logic in `Exec.ExecuteTxs` to handle state root equality checks properly and return previous state root for empty transactions in `dummy.go`.
  • Loading branch information
tzdybal committed Feb 21, 2025
1 parent 69bd770 commit c954062
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func (e *DummyExecutor) ExecuteTxs(ctx context.Context, txs []types.Tx, blockHei
e.mu.Lock()
defer e.mu.Unlock()

if len(txs) == 0 {
e.pendingRoots[blockHeight] = prevStateRoot
return prevStateRoot, e.maxBytes, nil
}

hash := sha512.New()
hash.Write(prevStateRoot)
for _, tx := range txs {
Expand Down
24 changes: 15 additions & 9 deletions test/suite.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"bytes"
"context"
"time"

Expand Down Expand Up @@ -70,20 +71,24 @@ func (s *ExecutorSuite) TestExecuteTxs() {
s.skipIfInjectorNotSet()

cases := []struct {
name string
txs []types.Tx
name string
txs []types.Tx
stateRootChanged bool
}{
{
name: "nil txs",
txs: nil,
name: "nil txs",
txs: nil,
stateRootChanged: false,
},
{
name: "empty txs",
txs: []types.Tx{},
name: "empty txs",
txs: []types.Tx{},
stateRootChanged: false,
},
{
name: "two txs",
txs: []types.Tx{s.TxInjector.InjectRandomTx(), s.TxInjector.InjectRandomTx()},
name: "two txs",
txs: []types.Tx{s.TxInjector.InjectRandomTx(), s.TxInjector.InjectRandomTx()},
stateRootChanged: true,
},
}

Expand All @@ -99,7 +104,7 @@ func (s *ExecutorSuite) TestExecuteTxs() {
stateRoot, maxBytes, err := s.Exec.ExecuteTxs(ctx, c.txs, initialHeight, genesisTime.Add(time.Second), genesisStateRoot)
s.Require().NoError(err)
s.Require().NotEmpty(stateRoot)
s.Require().NotEqual(genesisStateRoot, stateRoot)
s.Require().NotEqual(c.stateRootChanged, bytes.Equal(genesisStateRoot, stateRoot))
s.Require().Greater(maxBytes, uint64(0))
})
}
Expand Down Expand Up @@ -130,6 +135,7 @@ func (s *ExecutorSuite) TestMultipleBlocks() {
genesisTime, prevStateRoot, _ := s.initChain(ctx, initialHeight)

for i := initialHeight; i <= 10; i++ {
s.TxInjector.InjectRandomTx()
txs, err := s.Exec.GetTxs(ctx)
s.Require().NoError(err)

Expand Down

0 comments on commit c954062

Please sign in to comment.