Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syncervm_test: test refactor #590

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions plugin/evm/syncervm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package evm

import (
"context"
"encoding/json"
"fmt"
"math/big"
"math/rand"
Expand Down Expand Up @@ -144,7 +145,7 @@ func TestStateSyncToggleEnabledToDisabled(t *testing.T) {
context.Background(),
vmSetup.syncerVM.ctx,
vmSetup.syncerDB,
[]byte(genesisJSONLatest),
vmSetup.genesisBytes,
nil,
[]byte(stateSyncDisabledConfigJSON),
vmSetup.syncerVM.toEngine,
Expand Down Expand Up @@ -207,7 +208,7 @@ func TestStateSyncToggleEnabledToDisabled(t *testing.T) {
context.Background(),
vmSetup.syncerVM.ctx,
vmSetup.syncerDB,
[]byte(genesisJSONLatest),
vmSetup.genesisBytes,
nil,
[]byte(configJSON),
vmSetup.syncerVM.toEngine,
Expand Down Expand Up @@ -281,17 +282,24 @@ func createSyncServerAndClientVMs(t *testing.T, test syncTest, numBlocks int) *s
alloc = map[ids.ShortID]uint64{
testShortIDAddrs[0]: importAmount,
}
g core.Genesis
)
err := json.Unmarshal([]byte(genesisJSONLatest), &g)
require.NoError(err)
g.Config.ChainID = params.AvalancheLocalChainID
genesisBytes, err := json.Marshal(g)
require.NoError(err)
config := fmt.Sprintf(`{"commit-interval":%d}`, test.syncableInterval)

_, serverVM, _, serverAtomicMemory, serverAppSender := GenesisVMWithUTXOs(
t, true, "", "", "", alloc,
t, true, string(genesisBytes), config, "", alloc,
)
t.Cleanup(func() {
log.Info("Shutting down server VM")
require.NoError(serverVM.Shutdown(context.Background()))
})
var (
importTx, exportTx *Tx
err error
)
generateAndAcceptBlocks(t, serverVM, numBlocks, func(i int, gen *core.BlockGen) {
b, err := predicate.NewResults().Bytes()
Expand Down Expand Up @@ -326,12 +334,11 @@ func createSyncServerAndClientVMs(t *testing.T, test syncTest, numBlocks int) *s
}
}, nil)

// override serverAtomicTrie's commitInterval so the call to [serverAtomicTrie.Index]
// creates a commit at the height [syncableInterval]. This is necessary to support
// Create a commit at the height [syncableInterval]. This is necessary to support
// fetching a state summary.
serverAtomicTrie := serverVM.atomicTrie.(*atomicTrie)
serverAtomicTrie.commitInterval = test.syncableInterval
require.NoError(serverAtomicTrie.commit(test.syncableInterval, serverAtomicTrie.LastAcceptedRoot()))
serverAtomicTrie := serverVM.atomicTrie
_, err = serverAtomicTrie.AcceptTrie(test.syncableInterval, serverAtomicTrie.LastAcceptedRoot())
require.NoError(err)
require.NoError(serverVM.db.Commit())

serverSharedMemories := newSharedMemories(serverAtomicMemory, serverVM.ctx.ChainID, serverVM.ctx.XChainID)
Expand All @@ -358,9 +365,12 @@ func createSyncServerAndClientVMs(t *testing.T, test syncTest, numBlocks int) *s
serverVM.StateSyncServer.(*stateSyncServer).syncableInterval = test.syncableInterval

// initialise [syncerVM] with blank genesis state
stateSyncEnabledJSON := fmt.Sprintf(`{"state-sync-enabled":true, "state-sync-min-blocks": %d, "tx-lookup-limit": %d}`, test.stateSyncMinBlocks, 4)
stateSyncEnabledJSON := fmt.Sprintf(
`{"state-sync-enabled":true, "state-sync-min-blocks": %d, "tx-lookup-limit": %d, "commit-interval": %d}`,
test.stateSyncMinBlocks, 4, test.syncableInterval,
)
syncerEngineChan, syncerVM, syncerDB, syncerAtomicMemory, syncerAppSender := GenesisVMWithUTXOs(
t, false, "", stateSyncEnabledJSON, "", alloc,
t, false, string(genesisBytes), stateSyncEnabledJSON, "", alloc,
)
shutdownOnceSyncerVM := &shutdownOnceVM{VM: syncerVM}
t.Cleanup(func() {
Expand All @@ -371,9 +381,6 @@ func createSyncServerAndClientVMs(t *testing.T, test syncTest, numBlocks int) *s
require.NoError(err)
require.True(enabled)

// override [syncerVM]'s commit interval so the atomic trie works correctly.
syncerVM.atomicTrie.(*atomicTrie).commitInterval = test.syncableInterval

// override [serverVM]'s SendAppResponse function to trigger AppResponse on [syncerVM]
serverAppSender.SendAppResponseF = func(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error {
if test.responseIntercept == nil {
Expand Down Expand Up @@ -416,6 +423,7 @@ func createSyncServerAndClientVMs(t *testing.T, test syncTest, numBlocks int) *s
syncerEngineChan: syncerEngineChan,
syncerAtomicMemory: syncerAtomicMemory,
shutdownOnceSyncerVM: shutdownOnceSyncerVM,
genesisBytes: genesisBytes,
}
}

Expand All @@ -433,6 +441,7 @@ type syncVMSetup struct {
syncerEngineChan <-chan commonEng.Message
syncerAtomicMemory *atomic.Memory
shutdownOnceSyncerVM *shutdownOnceVM
genesisBytes []byte
}

type shutdownOnceVM struct {
Expand Down
Loading