2023-06-21 23:25:27 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-10 09:02:50 +00:00
|
|
|
"fmt"
|
2023-06-21 23:25:27 +00:00
|
|
|
"math/big"
|
|
|
|
|
2023-09-09 08:05:51 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/adapt"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/interfaces"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/node"
|
2023-06-21 23:25:27 +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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateDiffIndexer(ctx context.Context, chainConfig *params.ChainConfig, genHash common.Hash) (interfaces.StateDiffIndexer, error) {
|
|
|
|
testInfo := node.Info{
|
|
|
|
GenesisBlock: genHash.String(),
|
|
|
|
NetworkID: "1",
|
|
|
|
ID: "1",
|
|
|
|
ClientName: "geth",
|
|
|
|
ChainID: chainConfig.ChainID.Uint64(),
|
|
|
|
}
|
|
|
|
_, indexer, err := indexer.NewStateDiffIndexer(ctx, chainConfig, testInfo, TestDBConfig)
|
|
|
|
return indexer, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexChainParams struct {
|
|
|
|
Blocks []*types.Block
|
|
|
|
Receipts []types.Receipts
|
|
|
|
StateCache state.Database
|
|
|
|
|
|
|
|
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(indexer interfaces.StateDiffIndexer, params IndexChainParams) error {
|
2023-09-09 08:05:51 +00:00
|
|
|
builder := statediff.NewBuilder(adapt.GethStateView(params.StateCache))
|
2023-06-21 23:25:27 +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 {
|
2023-08-10 09:02:50 +00:00
|
|
|
return fmt.Errorf("failed to build diff (block %d): %w", block.Number(), err)
|
2023-06-21 23:25:27 +00:00
|
|
|
}
|
|
|
|
tx, err := indexer.PushBlock(block, rcts, params.TotalDifficulty)
|
|
|
|
if err != nil {
|
2023-08-10 09:02:50 +00:00
|
|
|
return fmt.Errorf("failed to index block (block %d): %w", block.Number(), err)
|
2023-06-21 23:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !params.SkipStateNodes {
|
|
|
|
for _, node := range diff.Nodes {
|
|
|
|
if err = indexer.PushStateNode(tx, node, block.Hash().String()); err != nil {
|
2023-08-10 09:02:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to index state node: %w", err)
|
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !params.SkipIPLDs {
|
|
|
|
for _, ipld := range diff.IPLDs {
|
|
|
|
if err := indexer.PushIPLD(tx, ipld); err != nil {
|
2023-08-10 09:02:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to index IPLD: %w", err)
|
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = tx.Submit(err); err != nil {
|
2023-08-10 09:02:50 +00:00
|
|
|
return fmt.Errorf("failed to commit diff: %w", err)
|
2023-06-21 23:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|