2023-04-03 06:44:31 +00:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
|
2023-09-20 06:16:44 +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/database/sql/postgres"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/node"
|
2023-04-03 06:44:31 +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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ChainConfig = params.TestChainConfig
|
|
|
|
|
|
|
|
mockTD = big.NewInt(1)
|
|
|
|
)
|
|
|
|
|
2023-04-25 09:14:04 +00:00
|
|
|
// IndexStateDiff indexes a single statediff.
|
|
|
|
// - uses TestChainConfig
|
|
|
|
// - block hash/number are left as zero
|
|
|
|
func IndexStateDiff(dbConfig postgres.Config, stateCache state.Database, rootA, rootB common.Hash) error {
|
2023-04-03 06:44:31 +00:00
|
|
|
_, indexer, err := indexer.NewStateDiffIndexer(
|
2023-09-30 05:54:44 +00:00
|
|
|
context.Background(), ChainConfig, node.Info{}, dbConfig, true)
|
2023-04-03 06:44:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer indexer.Close() // fixme: hangs when using PGX driver
|
|
|
|
|
2023-09-20 06:16:44 +00:00
|
|
|
builder := statediff.NewBuilder(adapt.GethStateView(stateCache))
|
2023-04-03 06:44:31 +00:00
|
|
|
block := types.NewBlock(&types.Header{Root: rootB}, nil, nil, nil, NewHasher())
|
|
|
|
|
2023-04-25 09:14:04 +00:00
|
|
|
// uses zero block hash/number, we only need the trie structure here
|
2023-04-03 06:44:31 +00:00
|
|
|
args := statediff.Args{
|
|
|
|
OldStateRoot: rootA,
|
|
|
|
NewStateRoot: rootB,
|
|
|
|
}
|
|
|
|
diff, err := builder.BuildStateDiffObject(args, statediff.Params{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx, err := indexer.PushBlock(block, nil, mockTD)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-25 09:14:04 +00:00
|
|
|
// we don't need to index diff.Nodes since we are just interested in the trie
|
2023-04-03 06:44:31 +00:00
|
|
|
for _, ipld := range diff.IPLDs {
|
|
|
|
if err := indexer.PushIPLD(tx, ipld); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 05:54:44 +00:00
|
|
|
return tx.Submit()
|
2023-04-03 06:44:31 +00:00
|
|
|
}
|