2023-04-14 06:26:46 +00:00
|
|
|
package test_helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
|
2023-11-01 20:04:37 +00:00
|
|
|
statediff "github.com/cerc-io/plugeth-statediff"
|
2023-09-21 06:55:26 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff/adapt"
|
2023-04-14 06:26:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
|
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/shared"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IndexChainParams struct {
|
|
|
|
Blocks []*types.Block
|
|
|
|
Receipts []types.Receipts
|
|
|
|
StateCache state.Database
|
|
|
|
ChainConfig *params.ChainConfig
|
|
|
|
|
|
|
|
StateDiffParams statediff.Params
|
|
|
|
TotalDifficulty *big.Int
|
|
|
|
// Whether to skip indexing state nodes (state_cids, storage_cids)
|
|
|
|
SkipStateNodes bool
|
|
|
|
// Whether to skip indexing IPLD blocks
|
|
|
|
SkipIPLDs bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func IndexChain(params IndexChainParams) error {
|
|
|
|
indexer := shared.SetupTestStateDiffIndexer(context.Background(), params.ChainConfig, Genesis.Hash())
|
2023-09-21 06:55:26 +00:00
|
|
|
builder := statediff.NewBuilder(adapt.GethStateView(params.StateCache))
|
2023-04-14 06:26:46 +00:00
|
|
|
// iterate over the blocks, generating statediff payloads, and transforming the data into Postgres
|
|
|
|
for i, block := range params.Blocks {
|
|
|
|
var args statediff.Args
|
|
|
|
var rcts types.Receipts
|
|
|
|
if i == 0 {
|
|
|
|
args = statediff.Args{
|
|
|
|
OldStateRoot: common.Hash{},
|
|
|
|
NewStateRoot: block.Root(),
|
|
|
|
BlockNumber: block.Number(),
|
|
|
|
BlockHash: block.Hash(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args = statediff.Args{
|
|
|
|
OldStateRoot: params.Blocks[i-1].Root(),
|
|
|
|
NewStateRoot: block.Root(),
|
|
|
|
BlockNumber: block.Number(),
|
|
|
|
BlockHash: block.Hash(),
|
|
|
|
}
|
|
|
|
rcts = params.Receipts[i-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
diff, err := builder.BuildStateDiffObject(args, params.StateDiffParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx, err := indexer.PushBlock(block, rcts, params.TotalDifficulty)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
2023-04-14 06:26:46 +00:00
|
|
|
|
|
|
|
if !params.SkipStateNodes {
|
|
|
|
for _, node := range diff.Nodes {
|
|
|
|
if err = indexer.PushStateNode(tx, node, block.Hash().String()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !params.SkipIPLDs {
|
|
|
|
for _, ipld := range diff.IPLDs {
|
|
|
|
if err := indexer.PushIPLD(tx, ipld); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 14:37:53 +00:00
|
|
|
if err = tx.Submit(); err != nil {
|
2023-04-14 06:26:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|