2022-01-11 00:59:15 +00:00
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
2022-02-18 11:12:53 +00:00
|
|
|
"errors"
|
2022-08-03 11:35:04 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2022-02-18 11:12:53 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-08-03 11:35:04 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
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
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-05-12 15:07:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/models"
|
2022-01-11 23:49:04 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
|
2022-09-20 17:47:34 +00:00
|
|
|
fixt "github.com/cerc-io/ipld-eth-state-snapshot/fixture"
|
|
|
|
mock "github.com/cerc-io/ipld-eth-state-snapshot/mocks/snapshot"
|
|
|
|
snapt "github.com/cerc-io/ipld-eth-state-snapshot/pkg/types"
|
|
|
|
"github.com/cerc-io/ipld-eth-state-snapshot/test"
|
2022-01-11 00:59:15 +00:00
|
|
|
)
|
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
var (
|
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)"
|
|
|
|
)
|
|
|
|
|
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,
|
2022-02-09 15:19:10 +00:00
|
|
|
NodeInfo: test.DefaultNodeInfo,
|
|
|
|
},
|
|
|
|
DB: &DBConfig{
|
|
|
|
URI: test.DefaultPgConfig.DbConnectionString(),
|
|
|
|
ConnConfig: test.DefaultPgConfig,
|
2022-01-11 00:59:26 +00:00
|
|
|
},
|
2022-01-11 00:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
func makeMocks(t *testing.T) (*mock.MockPublisher, *mock.MockTx) {
|
|
|
|
ctl := gomock.NewController(t)
|
|
|
|
pub := mock.NewMockPublisher(ctl)
|
|
|
|
tx := mock.NewMockTx(ctl)
|
|
|
|
return pub, tx
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:59:15 +00:00
|
|
|
func TestCreateSnapshot(t *testing.T) {
|
2022-02-18 11:12:53 +00:00
|
|
|
runCase := func(t *testing.T, workers int) {
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStateLeafKeys := sync.Map{}
|
|
|
|
for _, key := range fixt.Block1_StateNodeLeafKeys {
|
|
|
|
expectedStateLeafKeys.Store(key, struct{}{})
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
pub, tx := makeMocks(t)
|
|
|
|
pub.EXPECT().PublishHeader(gomock.Eq(&fixt.Block1_Header))
|
|
|
|
pub.EXPECT().BeginTx().Return(tx, nil).
|
|
|
|
Times(workers)
|
|
|
|
pub.EXPECT().PrepareTxForBatch(gomock.Any(), gomock.Any()).Return(tx, nil).
|
|
|
|
AnyTimes()
|
2022-08-03 11:35:04 +00:00
|
|
|
tx.EXPECT().Commit().
|
|
|
|
Times(workers)
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishStateLeafNode(
|
2022-08-03 11:35:04 +00:00
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
2023-05-12 15:07:28 +00:00
|
|
|
Do(func(stateNode *models.StateNodeModel, _ snapt.Tx) error {
|
|
|
|
if stateNode.BlockNumber != fixt.Block1_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if stateNode.HeaderID != fixt.Block1_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if _, ok := expectedStateLeafKeys.Load(stateNode.StateKey); ok {
|
|
|
|
expectedStateLeafKeys.Delete(stateNode.StateKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
2023-05-12 15:07:28 +00:00
|
|
|
AnyTimes()
|
|
|
|
pub.EXPECT().PublishIPLD(gomock.Any(), gomock.Any(), gomock.Eq(fixt.Block1_Header.Number), gomock.Eq(tx)).
|
|
|
|
AnyTimes()
|
|
|
|
// Note: block 1 doesn't have storage nodes. TODO: add fixtures with storage nodes
|
2022-01-11 23:49:04 +00:00
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
chainDataPath, ancientDataPath := fixt.GetChainDataPath("chaindata")
|
|
|
|
config := testConfig(chainDataPath, ancientDataPath)
|
2022-02-18 11:12:53 +00:00
|
|
|
edb, err := NewLevelDB(config.Eth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer edb.Close()
|
|
|
|
|
|
|
|
recovery := filepath.Join(t.TempDir(), "recover.csv")
|
|
|
|
service, err := NewSnapshotService(edb, pub, recovery)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := SnapshotParams{Height: 1, Workers: uint(workers)}
|
|
|
|
err = service.CreateSnapshot(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
// Check if all expected state nodes are indexed
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStateLeafKeys.Range(func(key, value any) bool {
|
|
|
|
t.Fatalf(stateNodeNotIndexedErr, key.(string))
|
2022-08-03 11:35:04 +00:00
|
|
|
return true
|
|
|
|
})
|
2022-01-11 23:49:04 +00:00
|
|
|
}
|
2022-02-09 15:19:10 +00:00
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
testCases := []int{1, 4, 8, 16, 32}
|
2022-02-18 11:12:53 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run("case", func(t *testing.T) { runCase(t, tc) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:07:28 +00:00
|
|
|
type indexedStateLeafNode struct {
|
|
|
|
value models.StateNodeModel
|
|
|
|
isIndexed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type indexedStorageLeafNode struct {
|
|
|
|
value models.StorageNodeModel
|
2022-08-03 11:35:04 +00:00
|
|
|
isIndexed bool
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
type storageNodeKey struct {
|
2023-05-12 15:07:28 +00:00
|
|
|
stateKey string
|
|
|
|
storageKey string
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountSelectiveSnapshot(t *testing.T) {
|
|
|
|
snapShotHeight := uint64(32)
|
|
|
|
watchedAddresses := map[common.Address]struct{}{
|
|
|
|
common.HexToAddress("0x825a6eec09e44Cb0fa19b84353ad0f7858d7F61a"): {},
|
|
|
|
common.HexToAddress("0x0616F59D291a898e796a1FAD044C5926ed2103eC"): {},
|
|
|
|
}
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStateNodeIndexes := []int{0, 4}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-05-12 15:07:28 +00:00
|
|
|
stateKey33 := common.HexToHash("0x33153abc667e873b6036c8a46bdd847e2ade3f89b9331c78ef2553fea194c50d").String()
|
|
|
|
expectedStorageNodeIndexes33 := []int{0, 1, 2, 4, 6}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-05-12 15:07:28 +00:00
|
|
|
stateKey12 := common.HexToHash("0xcabc5edb305583e33f66322ceee43088aa99277da772feb5053512d03a0a702b").String()
|
|
|
|
expectedStorageNodeIndexes12 := []int{9, 11}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
runCase := func(t *testing.T, workers int) {
|
2022-08-03 11:35:04 +00:00
|
|
|
expectedStateNodes := sync.Map{}
|
|
|
|
|
|
|
|
for _, expectedStateNodeIndex := range expectedStateNodeIndexes {
|
2023-05-12 15:07:28 +00:00
|
|
|
key := fixt.Chain2_Block32_StateNodes[expectedStateNodeIndex].StateKey
|
|
|
|
expectedStateNodes.Store(key, indexedStateLeafNode{
|
2022-08-03 11:35:04 +00:00
|
|
|
value: fixt.Chain2_Block32_StateNodes[expectedStateNodeIndex],
|
|
|
|
isIndexed: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedStorageNodes := sync.Map{}
|
|
|
|
|
|
|
|
for _, expectedStorageNodeIndex := range expectedStorageNodeIndexes33 {
|
2023-05-12 15:07:28 +00:00
|
|
|
key := fixt.Chain2_Block32_StorageNodes[expectedStorageNodeIndex].StorageKey
|
|
|
|
keys := storageNodeKey{
|
|
|
|
stateKey: stateKey33,
|
|
|
|
storageKey: key,
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-05-12 15:07:28 +00:00
|
|
|
value := indexedStorageLeafNode{
|
|
|
|
value: fixt.Chain2_Block32_StorageNodes[expectedStorageNodeIndex],
|
2022-08-03 11:35:04 +00:00
|
|
|
isIndexed: false,
|
|
|
|
}
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStorageNodes.Store(keys, value)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, expectedStorageNodeIndex := range expectedStorageNodeIndexes12 {
|
2023-05-12 15:07:28 +00:00
|
|
|
key := fixt.Chain2_Block32_StorageNodes[expectedStorageNodeIndex].StorageKey
|
|
|
|
keys := storageNodeKey{
|
|
|
|
stateKey: stateKey12,
|
|
|
|
storageKey: key,
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-05-12 15:07:28 +00:00
|
|
|
value := indexedStorageLeafNode{
|
|
|
|
value: fixt.Chain2_Block32_StorageNodes[expectedStorageNodeIndex],
|
2022-08-03 11:35:04 +00:00
|
|
|
isIndexed: false,
|
|
|
|
}
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStorageNodes.Store(keys, value)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 15:07:28 +00:00
|
|
|
var count int
|
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
pub, tx := makeMocks(t)
|
2022-08-03 11:35:04 +00:00
|
|
|
pub.EXPECT().PublishHeader(gomock.Eq(&fixt.Chain2_Block32_Header))
|
|
|
|
pub.EXPECT().BeginTx().Return(tx, nil).
|
|
|
|
Times(workers)
|
|
|
|
pub.EXPECT().PrepareTxForBatch(gomock.Any(), gomock.Any()).Return(tx, nil).
|
|
|
|
AnyTimes()
|
|
|
|
tx.EXPECT().Commit().
|
|
|
|
Times(workers)
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishStateLeafNode(
|
2022-08-03 11:35:04 +00:00
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
2023-05-12 15:07:28 +00:00
|
|
|
Do(func(stateNode *models.StateNodeModel, _ snapt.Tx) error {
|
|
|
|
count++
|
|
|
|
if stateNode.BlockNumber != fixt.Chain2_Block32_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if stateNode.HeaderID != fixt.Chain2_Block32_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
key := stateNode.StateKey
|
2022-08-03 11:35:04 +00:00
|
|
|
// Check published nodes
|
|
|
|
if expectedStateNode, ok := expectedStateNodes.Load(key); ok {
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedVal := expectedStateNode.(indexedStateLeafNode).value
|
|
|
|
test.ExpectEqual(t, expectedVal, *stateNode)
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
// Mark expected node as indexed
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStateNodes.Store(key, indexedStateLeafNode{
|
2022-08-03 11:35:04 +00:00
|
|
|
value: expectedVal,
|
|
|
|
isIndexed: true,
|
|
|
|
})
|
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
|
|
|
AnyTimes()
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishStorageLeafNode(
|
2022-08-03 11:35:04 +00:00
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
2023-05-12 15:07:28 +00:00
|
|
|
Do(func(storageNode *models.StorageNodeModel, _ snapt.Tx) error {
|
|
|
|
if storageNode.BlockNumber != fixt.Chain2_Block32_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
|
|
|
}
|
|
|
|
if storageNode.HeaderID != fixt.Chain2_Block32_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
key := storageNodeKey{
|
2023-05-12 15:07:28 +00:00
|
|
|
stateKey: storageNode.StateKey,
|
|
|
|
storageKey: storageNode.StorageKey,
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
// Check published nodes
|
|
|
|
if expectedStorageNode, ok := expectedStorageNodes.Load(key); ok {
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedVal := expectedStorageNode.(indexedStorageLeafNode).value
|
|
|
|
test.ExpectEqual(t, expectedVal, *storageNode)
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
// Mark expected node as indexed
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStorageNodes.Store(key, indexedStorageLeafNode{
|
2022-08-03 11:35:04 +00:00
|
|
|
value: expectedVal,
|
|
|
|
isIndexed: true,
|
|
|
|
})
|
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
|
|
|
AnyTimes()
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishIPLD(gomock.Any(), gomock.Any(), gomock.Eq(fixt.Chain2_Block32_Header.Number), gomock.Eq(tx)).
|
|
|
|
AnyTimes()
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
chainDataPath, ancientDataPath := fixt.GetChainDataPath("chain2data")
|
|
|
|
config := testConfig(chainDataPath, ancientDataPath)
|
|
|
|
edb, err := NewLevelDB(config.Eth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer edb.Close()
|
|
|
|
|
|
|
|
recovery := filepath.Join(t.TempDir(), "recover.csv")
|
|
|
|
service, err := NewSnapshotService(edb, pub, recovery)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := SnapshotParams{Height: snapShotHeight, Workers: uint(workers), WatchedAddresses: watchedAddresses}
|
|
|
|
err = service.CreateSnapshot(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedStateNodes.Range(func(key, value any) bool {
|
2023-05-12 15:07:28 +00:00
|
|
|
if !value.(indexedStateLeafNode).isIndexed {
|
|
|
|
t.Fatalf(stateNodeNotIndexedErr, key)
|
2022-08-03 11:35:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
expectedStorageNodes.Range(func(key, value any) bool {
|
2023-05-12 15:07:28 +00:00
|
|
|
if !value.(indexedStorageLeafNode).isIndexed {
|
|
|
|
t.Fatalf(storageNodeNotIndexedErr, key.(storageNodeKey).stateKey, key.(storageNodeKey).storageKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []int{1, 4, 8, 16, 32}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run("case", func(t *testing.T) { runCase(t, tc) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecovery(t *testing.T) {
|
|
|
|
runCase := func(t *testing.T, workers int, interruptAt int32) {
|
|
|
|
// map: expected state path -> number of times it got published
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStateNodeKeys := sync.Map{}
|
|
|
|
for _, key := range fixt.Block1_StateNodeLeafKeys {
|
|
|
|
expectedStateNodeKeys.Store(key, 0)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
var indexedStateNodesCount int32
|
|
|
|
|
|
|
|
pub, tx := makeMocks(t)
|
|
|
|
pub.EXPECT().PublishHeader(gomock.Eq(&fixt.Block1_Header))
|
|
|
|
pub.EXPECT().BeginTx().Return(tx, nil).MaxTimes(workers)
|
2022-02-18 11:12:53 +00:00
|
|
|
pub.EXPECT().PrepareTxForBatch(gomock.Any(), gomock.Any()).Return(tx, nil).AnyTimes()
|
2022-08-03 11:35:04 +00:00
|
|
|
tx.EXPECT().Commit().MaxTimes(workers)
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishStateLeafNode(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
|
|
|
DoAndReturn(func(stateNode *models.StateNodeModel, _ snapt.Tx) error {
|
|
|
|
if stateNode.BlockNumber != fixt.Block1_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if stateNode.HeaderID != fixt.Block1_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
// Start throwing an error after a certain number of state nodes have been indexed
|
|
|
|
if indexedStateNodesCount >= interruptAt {
|
2023-05-12 15:07:28 +00:00
|
|
|
return errors.New("failingPublishStateLeafNode")
|
2022-08-03 11:35:04 +00:00
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
if prevCount, ok := expectedStateNodeKeys.Load(stateNode.StateKey); ok {
|
|
|
|
expectedStateNodeKeys.Store(stateNode.StateKey, prevCount.(int)+1)
|
2022-08-03 11:35:04 +00:00
|
|
|
atomic.AddInt32(&indexedStateNodesCount, 1)
|
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
|
|
|
MaxTimes(int(interruptAt) + workers)
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishIPLD(gomock.Any(), gomock.Any(), gomock.Eq(fixt.Block1_Header.Number), gomock.Eq(tx)).
|
|
|
|
AnyTimes()
|
2022-02-18 11:12:53 +00:00
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
chainDataPath, ancientDataPath := fixt.GetChainDataPath("chaindata")
|
|
|
|
config := testConfig(chainDataPath, ancientDataPath)
|
2022-02-18 11:12:53 +00:00
|
|
|
edb, err := NewLevelDB(config.Eth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer edb.Close()
|
|
|
|
|
|
|
|
recovery := filepath.Join(t.TempDir(), "recover.csv")
|
|
|
|
service, err := NewSnapshotService(edb, pub, recovery)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := SnapshotParams{Height: 1, Workers: uint(workers)}
|
|
|
|
err = service.CreateSnapshot(params)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(recovery); err != nil {
|
|
|
|
t.Fatal("cannot stat recovery file:", err)
|
|
|
|
}
|
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
// Create new mocks for recovery
|
|
|
|
recoveryPub, tx := makeMocks(t)
|
|
|
|
recoveryPub.EXPECT().PublishHeader(gomock.Eq(&fixt.Block1_Header))
|
|
|
|
recoveryPub.EXPECT().BeginTx().Return(tx, nil).AnyTimes()
|
|
|
|
recoveryPub.EXPECT().PrepareTxForBatch(gomock.Any(), gomock.Any()).Return(tx, nil).AnyTimes()
|
|
|
|
tx.EXPECT().Commit().AnyTimes()
|
2023-05-12 15:07:28 +00:00
|
|
|
recoveryPub.EXPECT().PublishStateLeafNode(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
|
|
|
DoAndReturn(func(stateNode *models.StateNodeModel, _ snapt.Tx) error {
|
|
|
|
if stateNode.BlockNumber != fixt.Block1_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if stateNode.HeaderID != fixt.Block1_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if prevCount, ok := expectedStateNodeKeys.Load(stateNode.StateKey); ok {
|
|
|
|
expectedStateNodeKeys.Store(stateNode.StateKey, prevCount.(int)+1)
|
2022-08-03 11:35:04 +00:00
|
|
|
atomic.AddInt32(&indexedStateNodesCount, 1)
|
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
|
|
|
AnyTimes()
|
2023-05-12 15:07:28 +00:00
|
|
|
recoveryPub.EXPECT().PublishIPLD(gomock.Any(), gomock.Any(), gomock.Eq(fixt.Block1_Header.Number), gomock.Eq(tx)).
|
|
|
|
AnyTimes()
|
2022-07-13 06:09:36 +00:00
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
// Create a new snapshot service for recovery
|
|
|
|
recoveryService, err := NewSnapshotService(edb, recoveryPub, recovery)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = recoveryService.CreateSnapshot(params)
|
2022-02-18 11:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-11 23:49:04 +00:00
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
// Check if recovery file has been deleted
|
2022-02-18 11:12:53 +00:00
|
|
|
_, err = os.Stat(recovery)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("recovery file still present")
|
|
|
|
} else {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
// Check if all state nodes are indexed after recovery
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStateNodeKeys.Range(func(key, value any) bool {
|
2022-08-03 11:35:04 +00:00
|
|
|
if value.(int) == 0 {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(stateNodeNotIndexedErr, key.(string))
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
// nodes along the recovery path get reindexed
|
2023-05-12 15:07:28 +00:00
|
|
|
maxStateNodesCount := len(fixt.Block1_StateNodeLeafKeys)
|
2022-08-03 11:35:04 +00:00
|
|
|
if indexedStateNodesCount > int32(maxStateNodesCount) {
|
|
|
|
t.Fatalf(extraNodesIndexedErr, indexedStateNodesCount, maxStateNodesCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []int{1, 2, 4, 8, 16, 32}
|
|
|
|
numInterrupts := 3
|
|
|
|
interrupts := make([]int32, numInterrupts)
|
|
|
|
for i := 0; i < numInterrupts; i++ {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2023-05-12 15:07:28 +00:00
|
|
|
interrupts[i] = rand.Int31n(int32(len(fixt.Block1_StateNodeLeafKeys) / 2))
|
2022-01-11 00:59:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
for _, tc := range testCases {
|
2022-08-03 11:35:04 +00:00
|
|
|
for _, interrupt := range interrupts {
|
|
|
|
t.Run(fmt.Sprint("case", tc, interrupt), func(t *testing.T) { runCase(t, tc, interrupt) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountSelectiveRecovery(t *testing.T) {
|
|
|
|
snapShotHeight := uint64(32)
|
|
|
|
watchedAddresses := map[common.Address]struct{}{
|
|
|
|
common.HexToAddress("0x825a6eec09e44Cb0fa19b84353ad0f7858d7F61a"): {},
|
|
|
|
common.HexToAddress("0x0616F59D291a898e796a1FAD044C5926ed2103eC"): {},
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStateNodeIndexes := []int{0, 4}
|
|
|
|
expectedStorageNodeIndexes := []int{0, 1, 2, 4, 6, 9, 11}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
runCase := func(t *testing.T, workers int, interruptAt int32) {
|
|
|
|
// map: expected state path -> number of times it got published
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStateNodeKeys := sync.Map{}
|
2022-08-03 11:35:04 +00:00
|
|
|
for _, expectedStateNodeIndex := range expectedStateNodeIndexes {
|
2023-05-12 15:07:28 +00:00
|
|
|
key := fixt.Chain2_Block32_StateNodes[expectedStateNodeIndex].StateKey
|
|
|
|
expectedStateNodeKeys.Store(key, 0)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-05-12 15:07:28 +00:00
|
|
|
expectedStorageNodeKeys := sync.Map{}
|
|
|
|
for _, expectedStorageNodeIndex := range expectedStorageNodeIndexes {
|
|
|
|
stateKey := fixt.Chain2_Block32_StorageNodes[expectedStorageNodeIndex].StateKey
|
|
|
|
storageKey := fixt.Chain2_Block32_StorageNodes[expectedStorageNodeIndex].StorageKey
|
|
|
|
keys := storageNodeKey{
|
|
|
|
stateKey: stateKey,
|
|
|
|
storageKey: storageKey,
|
|
|
|
}
|
|
|
|
expectedStorageNodeKeys.Store(keys, 0)
|
|
|
|
}
|
|
|
|
var indexedStateNodesCount, indexedStorageNodesCount int32
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
pub, tx := makeMocks(t)
|
|
|
|
pub.EXPECT().PublishHeader(gomock.Eq(&fixt.Chain2_Block32_Header))
|
|
|
|
pub.EXPECT().BeginTx().Return(tx, nil).Times(workers)
|
|
|
|
pub.EXPECT().PrepareTxForBatch(gomock.Any(), gomock.Any()).Return(tx, nil).AnyTimes()
|
|
|
|
tx.EXPECT().Commit().Times(workers)
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishStateLeafNode(
|
2022-08-03 11:35:04 +00:00
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
2023-05-12 15:07:28 +00:00
|
|
|
DoAndReturn(func(stateNode *models.StateNodeModel, _ snapt.Tx) error {
|
|
|
|
if stateNode.BlockNumber != fixt.Chain2_Block32_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if stateNode.HeaderID != fixt.Chain2_Block32_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
// Start throwing an error after a certain number of state nodes have been indexed
|
|
|
|
if indexedStateNodesCount >= interruptAt {
|
2023-05-12 15:07:28 +00:00
|
|
|
return errors.New("failingPublishStateLeafNode")
|
2022-08-03 11:35:04 +00:00
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
if prevCount, ok := expectedStateNodeKeys.Load(stateNode.StateKey); ok {
|
|
|
|
expectedStateNodeKeys.Store(stateNode.StateKey, prevCount.(int)+1)
|
2022-08-03 11:35:04 +00:00
|
|
|
atomic.AddInt32(&indexedStateNodesCount, 1)
|
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
|
|
|
MaxTimes(int(interruptAt) + workers)
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishStorageLeafNode(
|
2022-08-03 11:35:04 +00:00
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
2023-05-12 15:07:28 +00:00
|
|
|
Do(func(storageNode *models.StorageNodeModel, _ snapt.Tx) error {
|
|
|
|
if storageNode.BlockNumber != fixt.Chain2_Block32_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
|
|
|
}
|
|
|
|
if storageNode.HeaderID != fixt.Chain2_Block32_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
|
|
|
}
|
|
|
|
keys := storageNodeKey{
|
|
|
|
stateKey: storageNode.StateKey,
|
|
|
|
storageKey: storageNode.StorageKey,
|
|
|
|
}
|
|
|
|
if prevCount, ok := expectedStorageNodeKeys.Load(keys); ok {
|
|
|
|
expectedStorageNodeKeys.Store(keys, prevCount.(int)+1)
|
|
|
|
atomic.AddInt32(&indexedStorageNodesCount, 1)
|
|
|
|
} else {
|
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
2022-08-03 11:35:04 +00:00
|
|
|
AnyTimes()
|
2023-05-12 15:07:28 +00:00
|
|
|
pub.EXPECT().PublishIPLD(gomock.Any(), gomock.Any(), gomock.Eq(fixt.Chain2_Block32_Header.Number), gomock.Eq(tx)).
|
2022-08-03 11:35:04 +00:00
|
|
|
AnyTimes()
|
|
|
|
|
|
|
|
chainDataPath, ancientDataPath := fixt.GetChainDataPath("chain2data")
|
|
|
|
config := testConfig(chainDataPath, ancientDataPath)
|
|
|
|
edb, err := NewLevelDB(config.Eth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer edb.Close()
|
|
|
|
|
|
|
|
recovery := filepath.Join(t.TempDir(), "recover.csv")
|
|
|
|
service, err := NewSnapshotService(edb, pub, recovery)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := SnapshotParams{Height: snapShotHeight, Workers: uint(workers), WatchedAddresses: watchedAddresses}
|
|
|
|
err = service.CreateSnapshot(params)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(recovery); err != nil {
|
|
|
|
t.Fatal("cannot stat recovery file:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new mocks for recovery
|
|
|
|
recoveryPub, tx := makeMocks(t)
|
|
|
|
recoveryPub.EXPECT().PublishHeader(gomock.Eq(&fixt.Chain2_Block32_Header))
|
|
|
|
recoveryPub.EXPECT().BeginTx().Return(tx, nil).MaxTimes(workers)
|
|
|
|
recoveryPub.EXPECT().PrepareTxForBatch(gomock.Any(), gomock.Any()).Return(tx, nil).AnyTimes()
|
|
|
|
tx.EXPECT().Commit().MaxTimes(workers)
|
2023-05-12 15:07:28 +00:00
|
|
|
recoveryPub.EXPECT().PublishStateLeafNode(
|
2022-08-03 11:35:04 +00:00
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
2023-05-12 15:07:28 +00:00
|
|
|
DoAndReturn(func(stateNode *models.StateNodeModel, _ snapt.Tx) error {
|
|
|
|
if stateNode.BlockNumber != fixt.Chain2_Block32_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if stateNode.HeaderID != fixt.Chain2_Block32_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
|
|
|
}
|
|
|
|
if prevCount, ok := expectedStateNodeKeys.Load(stateNode.StateKey); ok {
|
|
|
|
expectedStateNodeKeys.Store(stateNode.StateKey, prevCount.(int)+1)
|
2022-08-03 11:35:04 +00:00
|
|
|
atomic.AddInt32(&indexedStateNodesCount, 1)
|
|
|
|
} else {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(unexpectedStateNodeErr, stateNode.StateKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
|
|
|
AnyTimes()
|
2023-05-12 15:07:28 +00:00
|
|
|
recoveryPub.EXPECT().PublishStorageLeafNode(
|
2022-08-03 11:35:04 +00:00
|
|
|
gomock.Any(),
|
|
|
|
gomock.Eq(tx)).
|
2023-05-12 15:07:28 +00:00
|
|
|
Do(func(storageNode *models.StorageNodeModel, _ snapt.Tx) error {
|
|
|
|
if storageNode.BlockNumber != fixt.Chain2_Block32_Header.Number.String() {
|
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
|
|
|
}
|
|
|
|
if storageNode.HeaderID != fixt.Chain2_Block32_Header.Hash().String() {
|
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
|
|
|
}
|
|
|
|
keys := storageNodeKey{
|
|
|
|
stateKey: storageNode.StateKey,
|
|
|
|
storageKey: storageNode.StorageKey,
|
|
|
|
}
|
|
|
|
if prevCount, ok := expectedStorageNodeKeys.Load(keys); ok {
|
|
|
|
expectedStorageNodeKeys.Store(keys, prevCount.(int)+1)
|
|
|
|
atomic.AddInt32(&indexedStorageNodesCount, 1)
|
|
|
|
} else {
|
|
|
|
t.Fatalf(unexpectedStorageNodeErr, storageNode.StateKey, storageNode.StorageKey)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).
|
2022-08-03 11:35:04 +00:00
|
|
|
AnyTimes()
|
2023-05-12 15:07:28 +00:00
|
|
|
recoveryPub.EXPECT().PublishIPLD(gomock.Any(), gomock.Any(), gomock.Eq(fixt.Chain2_Block32_Header.Number), gomock.Eq(tx)).
|
2022-08-03 11:35:04 +00:00
|
|
|
AnyTimes()
|
|
|
|
|
|
|
|
// Create a new snapshot service for recovery
|
|
|
|
recoveryService, err := NewSnapshotService(edb, recoveryPub, recovery)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = recoveryService.CreateSnapshot(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if recovery file has been deleted
|
|
|
|
_, err = os.Stat(recovery)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("recovery file still present")
|
|
|
|
} else {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:07:28 +00:00
|
|
|
// Check if all expected state nodes are indexed after recovery, but not in duplicate
|
|
|
|
expectedStateNodeKeys.Range(func(key, value any) bool {
|
|
|
|
if value.(int) == 0 {
|
|
|
|
t.Fatalf(stateNodeNotIndexedErr, key.(string))
|
|
|
|
}
|
|
|
|
/* TODO: fix/figure out
|
|
|
|
if value.(int) > 1 {
|
|
|
|
t.Fatalf(stateNodeDuplicateErr, value.(int), key.(string))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
expectedStorageNodeKeys.Range(func(key, value any) bool {
|
2022-08-03 11:35:04 +00:00
|
|
|
if value.(int) == 0 {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Fatalf(storageNodeNotIndexedErr, key.(storageNodeKey).stateKey, key.(storageNodeKey).storageKey)
|
|
|
|
}
|
|
|
|
/* TODO: fix/figure out
|
|
|
|
if value.(int) > 1 {
|
|
|
|
t.Fatalf(storageNodeDuplicateErr, value.(int), key.(storageNodeKey).stateKey, key.(storageNodeKey).storageKey)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-05-12 15:07:28 +00:00
|
|
|
*/
|
2022-08-03 11:35:04 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2023-05-12 15:07:28 +00:00
|
|
|
maxStateNodesCount := len(expectedStateNodeIndexes) + workers
|
2022-08-03 11:35:04 +00:00
|
|
|
if indexedStateNodesCount > int32(maxStateNodesCount) {
|
|
|
|
t.Fatalf(extraNodesIndexedErr, indexedStateNodesCount, maxStateNodesCount)
|
|
|
|
}
|
2022-01-11 00:59:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
testCases := []int{1, 2, 4, 8, 16, 32}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2023-05-12 15:07:28 +00:00
|
|
|
t.Run(fmt.Sprint("case", tc, 1), func(t *testing.T) { runCase(t, tc, 1) })
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2022-01-11 00:59:15 +00:00
|
|
|
}
|