-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathexecution_test.go
More file actions
127 lines (103 loc) · 3.7 KB
/
execution_test.go
File metadata and controls
127 lines (103 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//go:build !integration
package node
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
coreexecutor "github.com/evstack/ev-node/core/execution"
testmocks "github.com/evstack/ev-node/test/mocks"
"github.com/evstack/ev-node/types"
)
func TestBasicExecutionFlow(t *testing.T) {
require := require.New(t)
node, cleanup := createNodeWithCleanup(t, getTestConfig(t, 1))
defer cleanup()
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
go func() {
_ = node.Run(ctx)
}()
// Wait for node initialization
err := waitForNodeInitialization(node)
require.NoError(err)
// Get the original executor to retrieve transactions
executor := getExecutorFromNode(t, node)
require.NotNil(executor, "Executor should not be nil")
txs := getTransactions(t, executor, t.Context())
// Use the generated mock executor for testing execution steps
mockExec := testmocks.NewMockExecutor(t)
// Define expected state and parameters
expectedInitialStateRoot := []byte("initial state root")
expectedNewStateRoot := []byte("new state root")
blockHeight := uint64(1)
chainID := "test-chain"
// Set expectations on the mock executor
mockExec.On("InitChain", mock.Anything, mock.AnythingOfType("time.Time"), blockHeight, chainID).
Return(expectedInitialStateRoot, nil).Once()
mockExec.On("ExecuteTxs", mock.Anything, txs, blockHeight, mock.AnythingOfType("time.Time"), expectedInitialStateRoot).
Return(expectedNewStateRoot, nil).Once()
mockExec.On("SetFinal", mock.Anything, blockHeight).
Return(nil).Once()
// Call helper functions with the mock executor
stateRoot := initializeChain(t, mockExec, t.Context())
require.Equal(expectedInitialStateRoot, stateRoot)
newStateRoot := executeTransactions(t, mockExec, t.Context(), txs, stateRoot)
require.Equal(expectedNewStateRoot, newStateRoot)
finalizeExecution(t, mockExec, t.Context())
require.NotEmpty(newStateRoot)
cancel()
time.Sleep(100 * time.Millisecond) // grace period for node shutdown and cleanup
}
func waitForNodeInitialization(node *FullNode) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if node.IsRunning() {
return nil
}
case <-ctx.Done():
return errors.New("timeout waiting for node initialization")
}
}
}
func getExecutorFromNode(t *testing.T, node *FullNode) coreexecutor.Executor {
le := node.leaderElection
sle, ok := le.(*singleRoleElector)
require.True(t, ok, "Leader election is not singleRoleElector")
state := sle.state()
require.NotNil(t, state)
require.NotNil(t, state.bc)
require.NotNil(t, state.bc.Executor)
return state.bc.Executor.GetCoreExecutor()
}
func getTransactions(t *testing.T, executor coreexecutor.Executor, ctx context.Context) [][]byte {
txs, err := executor.GetTxs(ctx)
require.NoError(t, err)
return txs
}
func initializeChain(t *testing.T, executor coreexecutor.Executor, ctx context.Context) []byte {
genesisTime := time.Now()
initialHeight := uint64(1)
chainID := "test-chain"
stateRoot, err := executor.InitChain(ctx, genesisTime, initialHeight, chainID)
require.NoError(t, err)
return stateRoot
}
func executeTransactions(t *testing.T, executor coreexecutor.Executor, ctx context.Context, txs [][]byte, stateRoot types.Hash) []byte {
blockHeight := uint64(1)
timestamp := time.Now()
newStateRoot, err := executor.ExecuteTxs(ctx, txs, blockHeight, timestamp, stateRoot)
require.NoError(t, err)
return newStateRoot
}
func finalizeExecution(t *testing.T, executor coreexecutor.Executor, ctx context.Context) {
err := executor.SetFinal(ctx, 1)
require.NoError(t, err)
}