2023-08-27 09:49:46 +00:00
|
|
|
package snapshot_test
|
2022-01-11 00:59:15 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-03 11:35:04 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2022-02-18 11:12:53 +00:00
|
|
|
"path/filepath"
|
2023-08-27 09:49:46 +00:00
|
|
|
"sort"
|
2022-01-11 00:59:15 +00:00
|
|
|
"testing"
|
2022-07-13 06:09:36 +00:00
|
|
|
"time"
|
2022-01-11 00:59:15 +00:00
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/models"
|
2022-08-03 11:35:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-08-27 09:49:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-11 23:49:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-state-snapshot/fixture"
|
|
|
|
"github.com/cerc-io/ipld-eth-state-snapshot/internal/mocks"
|
|
|
|
|
|
|
|
. "github.com/cerc-io/ipld-eth-state-snapshot/pkg/snapshot"
|
2022-01-11 00:59:15 +00:00
|
|
|
)
|
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
var (
|
2023-08-27 09:49:46 +00:00
|
|
|
// TODO refactor
|
2023-05-12 15:07:28 +00:00
|
|
|
stateNodeDuplicateErr = "state node indexed multiple times (%d) for state key %v"
|
|
|
|
storageNodeDuplicateErr = "storage node indexed multiple times (%d) for state key %v and storage key %v"
|
|
|
|
stateNodeNotIndexedErr = "state node not indexed for state key %v"
|
|
|
|
storageNodeNotIndexedErr = "storage node not indexed for state key %v, storage key %v"
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-05-12 15:07:28 +00:00
|
|
|
unexpectedStateNodeErr = "got unexpected state node for state key %v"
|
|
|
|
unexpectedStorageNodeErr = "got unexpected storage node for state key %v, storage key %v"
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
extraNodesIndexedErr = "number of nodes indexed (%v) is more than expected (max %v)"
|
2023-08-27 09:49:46 +00:00
|
|
|
|
|
|
|
// Note: block 1 doesn't have storage nodes. TODO: add fixtures with storage nodes
|
|
|
|
chain1block1StateKeys = sliceToSet(fixture.Block1_StateNodeLeafKeys)
|
|
|
|
chain1block1IpldCids = sliceToSet(fixture.Block1_IpldCids)
|
|
|
|
|
|
|
|
subtrieWorkerCases = []uint{1, 4, 8, 16, 32}
|
2022-08-03 11:35:04 +00:00
|
|
|
)
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:59:15 +00:00
|
|
|
func testConfig(leveldbpath, ancientdbpath string) *Config {
|
|
|
|
return &Config{
|
2022-01-11 00:59:26 +00:00
|
|
|
Eth: &EthConfig{
|
|
|
|
LevelDBPath: leveldbpath,
|
|
|
|
AncientDBPath: ancientdbpath,
|
2023-08-27 09:49:46 +00:00
|
|
|
NodeInfo: DefaultNodeInfo,
|
2022-02-09 15:19:10 +00:00
|
|
|
},
|
2023-08-27 09:49:46 +00:00
|
|
|
DB: &DefaultPgConfig,
|
2022-01-11 00:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func sliceToSet[T comparable](slice []T) map[T]struct{} {
|
|
|
|
set := make(map[T]struct{})
|
|
|
|
for _, v := range slice {
|
|
|
|
set[v] = struct{}{}
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
return set
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
type selectiveData struct {
|
|
|
|
StateNodes map[string]*models.StateNodeModel
|
|
|
|
StorageNodes map[string]map[string]*models.StorageNodeModel
|
2023-05-12 15:07:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func verifyChain1Block1(t *testing.T, data mocks.IndexerData) {
|
|
|
|
// Extract indexed keys and sort them for comparison
|
|
|
|
var indexedStateKeys []string
|
|
|
|
for _, stateNode := range data.StateNodes {
|
|
|
|
stateKey := common.BytesToHash(stateNode.AccountWrapper.LeafKey).String()
|
|
|
|
indexedStateKeys = append(indexedStateKeys, stateKey)
|
|
|
|
}
|
|
|
|
sort.Slice(indexedStateKeys, func(i, j int) bool { return indexedStateKeys[i] < indexedStateKeys[j] })
|
|
|
|
require.Equal(t, fixture.Block1_StateNodeLeafKeys, indexedStateKeys)
|
2022-02-18 11:12:53 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
ipldCids := make(map[string]struct{})
|
|
|
|
for _, ipld := range data.IPLDs {
|
|
|
|
ipldCids[ipld.CID] = struct{}{}
|
|
|
|
}
|
|
|
|
require.Equal(t, chain1block1IpldCids, ipldCids)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func chain2WatchedAccountData() ([]common.Address, selectiveData) {
|
2023-08-04 12:36:56 +00:00
|
|
|
watchedAddresses := []common.Address{
|
2023-08-27 09:49:46 +00:00
|
|
|
// hash 0xcabc5edb305583e33f66322ceee43088aa99277da772feb5053512d03a0a702b
|
2023-08-04 12:36:56 +00:00
|
|
|
common.HexToAddress("0x825a6eec09e44Cb0fa19b84353ad0f7858d7F61a"),
|
2023-08-27 09:49:46 +00:00
|
|
|
// hash 0x33153abc667e873b6036c8a46bdd847e2ade3f89b9331c78ef2553fea194c50d
|
2023-08-04 12:36:56 +00:00
|
|
|
common.HexToAddress("0x0616F59D291a898e796a1FAD044C5926ed2103eC"),
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
var expected selectiveData
|
|
|
|
// expectedStateNodeIndexes := []int{0, 4}
|
|
|
|
expected.StateNodes = make(map[string]*models.StateNodeModel)
|
|
|
|
for _, index := range []int{0, 4} {
|
|
|
|
node := &fixture.Chain2_Block32_StateNodes[index]
|
|
|
|
expected.StateNodes[node.StateKey] = node
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
// Map account leaf keys to corresponding storage
|
|
|
|
expectedStorageNodeIndexes := []struct {
|
|
|
|
address common.Address
|
|
|
|
indexes []int
|
|
|
|
}{
|
|
|
|
{watchedAddresses[0], []int{9, 11}},
|
|
|
|
{watchedAddresses[1], []int{0, 1, 2, 4, 6}},
|
|
|
|
}
|
|
|
|
expected.StorageNodes = make(map[string]map[string]*models.StorageNodeModel)
|
|
|
|
for _, account := range expectedStorageNodeIndexes {
|
|
|
|
leafKey := crypto.Keccak256Hash(account.address[:]).String()
|
|
|
|
storageNodes := make(map[string]*models.StorageNodeModel)
|
|
|
|
for _, index := range account.indexes {
|
|
|
|
node := &fixture.Chain2_Block32_StorageNodes[index]
|
|
|
|
storageNodes[node.StorageKey] = node
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
expected.StorageNodes[leafKey] = storageNodes
|
|
|
|
}
|
|
|
|
return watchedAddresses, expected
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func (expected selectiveData) verify(t *testing.T, data mocks.IndexerData) {
|
|
|
|
// check that all indexed nodes are expected and correct
|
|
|
|
indexedStateKeys := make(map[string]struct{})
|
|
|
|
for _, stateNode := range data.StateNodes {
|
|
|
|
stateKey := common.BytesToHash(stateNode.AccountWrapper.LeafKey).String()
|
|
|
|
indexedStateKeys[stateKey] = struct{}{}
|
|
|
|
require.Contains(t, expected.StateNodes, stateKey, "unexpected state node")
|
|
|
|
|
|
|
|
model := expected.StateNodes[stateKey]
|
|
|
|
require.Equal(t, model.CID, stateNode.AccountWrapper.CID)
|
|
|
|
require.Equal(t, model.Balance, stateNode.AccountWrapper.Account.Balance.String())
|
|
|
|
require.Equal(t, model.StorageRoot, stateNode.AccountWrapper.Account.Root.String())
|
|
|
|
|
|
|
|
expectedStorage := expected.StorageNodes[stateKey]
|
|
|
|
indexedStorageKeys := make(map[string]struct{})
|
|
|
|
for _, storageNode := range stateNode.StorageDiff {
|
|
|
|
storageKey := common.BytesToHash(storageNode.LeafKey).String()
|
|
|
|
indexedStorageKeys[storageKey] = struct{}{}
|
|
|
|
require.Contains(t, expectedStorage, storageKey, "unexpected storage node")
|
|
|
|
|
|
|
|
require.Equal(t, expectedStorage[storageKey].CID, storageNode.CID)
|
|
|
|
require.Equal(t, expectedStorage[storageKey].Value, storageNode.Value)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
// check for completeness
|
|
|
|
for storageNode := range expectedStorage {
|
|
|
|
require.Contains(t, indexedStorageKeys, storageNode, "missing storage node")
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
// check for completeness
|
|
|
|
for stateNode := range expected.StateNodes {
|
|
|
|
require.Contains(t, indexedStateKeys, stateNode, "missing state node")
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func doSnapshot(t *testing.T, dataName string, params SnapshotParams) mocks.IndexerData {
|
|
|
|
chainDataPath, ancientDataPath := fixture.GetChainDataPath(dataName)
|
|
|
|
config := testConfig(chainDataPath, ancientDataPath)
|
|
|
|
edb, err := NewLevelDB(config.Eth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer edb.Close()
|
2022-02-18 11:12:53 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
idx := mocks.NewIndexer(t)
|
|
|
|
recovery := filepath.Join(t.TempDir(), "recover.csv")
|
|
|
|
service, err := NewSnapshotService(edb, idx, recovery)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-02-18 11:12:53 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
if err = service.CreateSnapshot(params); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return idx.IndexerData
|
|
|
|
}
|
2022-01-11 23:49:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func doSnapshotWithRecovery(
|
|
|
|
t *testing.T,
|
|
|
|
dataName string,
|
|
|
|
params SnapshotParams,
|
|
|
|
failAfter uint,
|
|
|
|
) mocks.IndexerData {
|
|
|
|
chainDataPath, ancientDataPath := fixture.GetChainDataPath(dataName)
|
|
|
|
config := testConfig(chainDataPath, ancientDataPath)
|
|
|
|
edb, err := NewLevelDB(config.Eth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer edb.Close()
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
indexer := &mocks.InterruptingIndexer{
|
|
|
|
Indexer: mocks.NewIndexer(t),
|
|
|
|
InterruptAfter: failAfter,
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
t.Logf("Will interrupt after %d state nodes", failAfter)
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
recoveryFile := filepath.Join(t.TempDir(), "recover.csv")
|
|
|
|
service, err := NewSnapshotService(edb, indexer, recoveryFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = service.CreateSnapshot(params); err == nil {
|
|
|
|
t.Fatal("expected an error")
|
2022-01-11 00:59:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
require.FileExists(t, recoveryFile)
|
|
|
|
require.LessOrEqual(t, len(indexer.StateNodes), int(indexer.InterruptAfter+params.Workers))
|
|
|
|
|
|
|
|
// use the nested mock indexer, to continue where it left off
|
|
|
|
recoveryIndexer := indexer.Indexer
|
|
|
|
if service, err = NewSnapshotService(edb, recoveryIndexer, recoveryFile); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = service.CreateSnapshot(params); err != nil {
|
|
|
|
t.Fatal(err)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
return recoveryIndexer.IndexerData
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func TestSnapshot(t *testing.T) {
|
|
|
|
runCase := func(t *testing.T, workers uint) {
|
|
|
|
params := SnapshotParams{Height: 1, Workers: workers}
|
|
|
|
data := doSnapshot(t, "chaindata", params)
|
|
|
|
verifyChain1Block1(t, data)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
for _, tc := range subtrieWorkerCases {
|
|
|
|
t.Run(fmt.Sprintf("with %d subtries", tc), func(t *testing.T) { runCase(t, tc) })
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func TestAccountSelectiveSnapshot(t *testing.T) {
|
|
|
|
height := uint64(32)
|
|
|
|
watchedAddresses, expected := chain2WatchedAccountData()
|
|
|
|
|
|
|
|
runCase := func(t *testing.T, workers uint) {
|
|
|
|
params := SnapshotParams{
|
|
|
|
Height: height,
|
|
|
|
Workers: workers,
|
|
|
|
WatchedAddresses: watchedAddresses,
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
data := doSnapshot(t, "chain2data", params)
|
|
|
|
expected.verify(t, data)
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
for _, tc := range subtrieWorkerCases {
|
|
|
|
t.Run(fmt.Sprintf("with %d subtries", tc), func(t *testing.T) { runCase(t, tc) })
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func TestSnapshotRecovery(t *testing.T) {
|
|
|
|
runCase := func(t *testing.T, workers uint, interruptAt uint) {
|
|
|
|
params := SnapshotParams{Height: 1, Workers: workers}
|
|
|
|
data := doSnapshotWithRecovery(t, "chaindata", params, interruptAt)
|
|
|
|
verifyChain1Block1(t, data)
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
interrupts := make([]uint, 4)
|
|
|
|
for i := 0; i < len(interrupts); i++ {
|
|
|
|
N := fixture.Block1_StateNodeLeafKeys
|
|
|
|
interrupts[i] = uint(rand.Intn(len(N)/2) + len(N)/4)
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
for _, tc := range subtrieWorkerCases {
|
|
|
|
for i, interrupt := range interrupts {
|
|
|
|
t.Run(
|
|
|
|
fmt.Sprintf("with %d subtries %d", tc, i),
|
|
|
|
func(t *testing.T) { runCase(t, tc, interrupt) },
|
|
|
|
)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
func TestAccountSelectiveSnapshotRecovery(t *testing.T) {
|
|
|
|
height := uint64(32)
|
|
|
|
watchedAddresses, expected := chain2WatchedAccountData()
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
runCase := func(t *testing.T, workers uint, interruptAt uint) {
|
|
|
|
params := SnapshotParams{
|
|
|
|
Height: height,
|
|
|
|
Workers: workers,
|
|
|
|
WatchedAddresses: watchedAddresses,
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-27 09:49:46 +00:00
|
|
|
data := doSnapshotWithRecovery(t, "chain2data", params, interruptAt)
|
|
|
|
expected.verify(t, data)
|
2022-01-11 00:59:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 09:49:46 +00:00
|
|
|
for _, tc := range subtrieWorkerCases {
|
|
|
|
t.Run(
|
|
|
|
fmt.Sprintf("with %d subtries", tc),
|
|
|
|
func(t *testing.T) { runCase(t, tc, 1) },
|
|
|
|
)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2022-01-11 00:59:15 +00:00
|
|
|
}
|