2020-07-01 18:44:59 +00:00
|
|
|
// Copyright © 2020 Vulcanize, Inc
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-01 23:07:56 +00:00
|
|
|
"errors"
|
2020-07-01 18:44:59 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2021-12-13 15:01:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2020-07-01 18:44:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2021-12-14 06:50:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/postgres"
|
2020-07-01 18:44:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2021-12-13 15:01:32 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-07-16 15:31:37 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-07-01 18:44:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nullHash = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")
|
|
|
|
emptyNode, _ = rlp.EncodeToBytes([]byte{})
|
2020-07-31 05:31:34 +00:00
|
|
|
emptyCodeHash = crypto.Keccak256([]byte{})
|
2020-07-01 18:44:59 +00:00
|
|
|
emptyContractRoot = crypto.Keccak256Hash(emptyNode)
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
defaultBatchSize = uint(100)
|
2020-07-01 18:44:59 +00:00
|
|
|
)
|
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
// Service holds ethDB and stateDB to read data from lvldb and Publisher
|
|
|
|
// to publish trie in postgres DB.
|
2020-07-01 18:44:59 +00:00
|
|
|
type Service struct {
|
|
|
|
ethDB ethdb.Database
|
|
|
|
stateDB state.Database
|
|
|
|
ipfsPublisher *Publisher
|
2021-12-13 15:01:32 +00:00
|
|
|
maxBatchSize uint
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
// NewSnapshotService creates Service.
|
|
|
|
func NewSnapshotService(con *Config) (*Service, error) {
|
2021-12-13 15:01:32 +00:00
|
|
|
pgDB, err := postgres.NewDB(con.connectionURI, con.DBConfig, con.Node)
|
2020-07-01 18:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
edb, err := rawdb.NewLevelDBDatabaseWithFreezer(con.LevelDBPath, 1024, 256, con.AncientDBPath, "eth-pg-ipfs-state-snapshot", false)
|
2020-07-01 18:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
return &Service{
|
|
|
|
ethDB: edb,
|
|
|
|
stateDB: state.NewDatabase(edb),
|
2021-12-13 15:01:32 +00:00
|
|
|
ipfsPublisher: NewPublisher(pgDB),
|
|
|
|
maxBatchSize: defaultBatchSize,
|
2020-07-01 18:44:59 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
// CreateLatestSnapshot creates snapshot for the latest block.
|
2020-07-16 15:31:37 +00:00
|
|
|
func (s *Service) CreateLatestSnapshot() error {
|
|
|
|
// extract header from lvldb and publish to PG-IPFS
|
|
|
|
// hold onto the headerID so that we can link the state nodes to this header
|
|
|
|
logrus.Info("Creating snapshot at head")
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-16 15:31:37 +00:00
|
|
|
hash := rawdb.ReadHeadHeaderHash(s.ethDB)
|
|
|
|
height := rawdb.ReadHeaderNumber(s.ethDB, hash)
|
|
|
|
if height == nil {
|
|
|
|
return fmt.Errorf("unable to read header height for header hash %s", hash.String())
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-16 15:31:37 +00:00
|
|
|
header := rawdb.ReadHeader(s.ethDB, hash, *height)
|
|
|
|
if header == nil {
|
|
|
|
return fmt.Errorf("unable to read canonical header at height %d", height)
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-16 15:31:37 +00:00
|
|
|
logrus.Infof("head hash: %s head height: %d", hash.Hex(), *height)
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-16 15:31:37 +00:00
|
|
|
headerID, err := s.ipfsPublisher.PublishHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-16 15:31:37 +00:00
|
|
|
t, err := s.stateDB.OpenTrie(header.Root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-16 15:31:37 +00:00
|
|
|
trieDB := s.stateDB.TrieDB()
|
2021-12-13 15:12:02 +00:00
|
|
|
return s.createSnapshot(t.NodeIterator([]byte{}), trieDB, headerID)
|
2020-07-16 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
// CreateSnapshot creates snapshot for given block height.
|
2020-07-15 04:43:11 +00:00
|
|
|
func (s *Service) CreateSnapshot(height uint64) error {
|
2020-07-01 18:44:59 +00:00
|
|
|
// extract header from lvldb and publish to PG-IPFS
|
|
|
|
// hold onto the headerID so that we can link the state nodes to this header
|
2020-07-15 04:43:11 +00:00
|
|
|
logrus.Infof("Creating snapshot at height %d", height)
|
|
|
|
hash := rawdb.ReadCanonicalHash(s.ethDB, height)
|
2020-07-01 18:44:59 +00:00
|
|
|
header := rawdb.ReadHeader(s.ethDB, hash, height)
|
2020-07-16 15:02:16 +00:00
|
|
|
if header == nil {
|
|
|
|
return fmt.Errorf("unable to read canonical header at height %d", height)
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
headerID, err := s.ipfsPublisher.PublishHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
t, err := s.stateDB.OpenTrie(header.Root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
trieDB := s.stateDB.TrieDB()
|
2021-12-13 15:12:02 +00:00
|
|
|
return s.createSnapshot(t.NodeIterator([]byte{}), trieDB, headerID)
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) createSnapshot(it trie.NodeIterator, trieDB *trie.Database, headerID int64) error {
|
2021-12-13 15:01:32 +00:00
|
|
|
tx, err := s.ipfsPublisher.db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = tx.Commit()
|
|
|
|
}()
|
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
for it.Next(true) {
|
|
|
|
if it.Leaf() { // "leaf" nodes are actually "value" nodes, whose parents are the actual leaves
|
|
|
|
continue
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
if bytes.Equal(nullHash.Bytes(), it.Hash().Bytes()) {
|
|
|
|
continue
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
tx, err = s.ipfsPublisher.checkBatchSize(tx, s.maxBatchSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
nodePath := make([]byte, len(it.Path()))
|
|
|
|
copy(nodePath, it.Path())
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
var (
|
2021-12-14 06:50:19 +00:00
|
|
|
nodeData []byte
|
|
|
|
ty nodeType
|
2021-12-13 15:01:32 +00:00
|
|
|
)
|
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
nodeData, err = trieDB.Node(it.Hash())
|
2020-07-01 18:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
var nodeElements []interface{}
|
2021-12-14 06:50:19 +00:00
|
|
|
if err = rlp.DecodeBytes(nodeData, &nodeElements); err != nil {
|
2020-07-01 18:44:59 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
ty, err = CheckKeyType(nodeElements)
|
2020-07-01 18:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
stateNode := &node{
|
|
|
|
nodeType: ty,
|
|
|
|
path: nodePath,
|
|
|
|
value: nodeData,
|
2020-07-01 23:07:56 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
switch ty {
|
2021-12-14 06:50:19 +00:00
|
|
|
case leaf:
|
2020-07-31 05:31:34 +00:00
|
|
|
// if the node is a leaf, decode the account and publish the associated storage trie nodes if there are any
|
2021-12-13 15:01:32 +00:00
|
|
|
var account types.StateAccount
|
2021-12-14 06:50:19 +00:00
|
|
|
if err = rlp.DecodeBytes(nodeElements[1].([]byte), &account); err != nil {
|
|
|
|
return fmt.Errorf("error decoding account for leaf node at path %x nerror: %w", nodePath, err)
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
partialPath := trie.CompactToHex(nodeElements[0].([]byte))
|
|
|
|
valueNodePath := append(nodePath, partialPath...)
|
|
|
|
encodedPath := trie.HexToCompact(valueNodePath)
|
|
|
|
leafKey := encodedPath[1:]
|
2021-12-14 06:50:19 +00:00
|
|
|
stateNode.key = common.BytesToHash(leafKey)
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
stateID, err := s.ipfsPublisher.PublishStateNode(stateNode, headerID, tx)
|
2020-07-01 23:07:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-31 05:31:34 +00:00
|
|
|
// publish any non-nil code referenced by codehash
|
|
|
|
if !bytes.Equal(account.CodeHash, emptyCodeHash) {
|
2021-12-14 06:50:19 +00:00
|
|
|
codeBytes := rawdb.ReadCode(s.ethDB, common.BytesToHash(account.CodeHash))
|
2021-12-13 15:01:32 +00:00
|
|
|
if len(codeBytes) == 0 {
|
|
|
|
logrus.Error("Code is missing", "account", common.BytesToHash(it.LeafKey()))
|
|
|
|
return errors.New("missing code")
|
2020-07-31 05:31:34 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
if err = s.ipfsPublisher.PublishCode(codeBytes, tx); err != nil {
|
2020-07-31 05:31:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
if tx, err = s.storageSnapshot(account.Root, stateID, tx); err != nil {
|
2021-12-14 06:50:19 +00:00
|
|
|
return fmt.Errorf("failed building storage snapshot for account %+v\r\nerror: %w", account, err)
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2021-12-14 06:50:19 +00:00
|
|
|
case extension, branch:
|
|
|
|
stateNode.key = common.BytesToHash([]byte{})
|
2021-12-13 15:01:32 +00:00
|
|
|
if _, err := s.ipfsPublisher.PublishStateNode(stateNode, headerID, tx); err != nil {
|
2020-07-01 23:07:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-01 18:44:59 +00:00
|
|
|
default:
|
2020-07-01 23:07:56 +00:00
|
|
|
return errors.New("unexpected node type")
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 15:46:35 +00:00
|
|
|
return it.Error()
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:01:32 +00:00
|
|
|
func (s *Service) storageSnapshot(sr common.Hash, stateID int64, tx *sqlx.Tx) (*sqlx.Tx, error) {
|
2020-07-01 18:44:59 +00:00
|
|
|
if bytes.Equal(sr.Bytes(), emptyContractRoot.Bytes()) {
|
2021-12-13 15:01:32 +00:00
|
|
|
return tx, nil
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
sTrie, err := s.stateDB.OpenTrie(sr)
|
|
|
|
if err != nil {
|
2021-12-13 15:01:32 +00:00
|
|
|
return nil, err
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
it := sTrie.NodeIterator(make([]byte, 0))
|
|
|
|
for it.Next(true) {
|
|
|
|
// skip value nodes
|
|
|
|
if it.Leaf() {
|
|
|
|
continue
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
if bytes.Equal(nullHash.Bytes(), it.Hash().Bytes()) {
|
|
|
|
continue
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
tx, err = s.ipfsPublisher.checkBatchSize(tx, s.maxBatchSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
nodePath := make([]byte, len(it.Path()))
|
|
|
|
copy(nodePath, it.Path())
|
2021-12-14 06:50:19 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
nodeData []byte
|
|
|
|
ty nodeType
|
|
|
|
)
|
|
|
|
|
|
|
|
nodeData, err = s.stateDB.TrieDB().Node(it.Hash())
|
2020-07-01 18:44:59 +00:00
|
|
|
if err != nil {
|
2021-12-13 15:01:32 +00:00
|
|
|
return nil, err
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
var nodeElements []interface{}
|
2021-12-14 06:50:19 +00:00
|
|
|
if err = rlp.DecodeBytes(nodeData, &nodeElements); err != nil {
|
2021-12-13 15:01:32 +00:00
|
|
|
return nil, err
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
ty, err = CheckKeyType(nodeElements)
|
2020-07-01 18:44:59 +00:00
|
|
|
if err != nil {
|
2021-12-13 15:01:32 +00:00
|
|
|
return nil, err
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
storageNode := &node{
|
|
|
|
nodeType: ty,
|
|
|
|
path: nodePath,
|
|
|
|
value: nodeData,
|
2020-07-01 23:07:56 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2020-07-01 18:44:59 +00:00
|
|
|
switch ty {
|
2021-12-14 06:50:19 +00:00
|
|
|
case leaf:
|
2020-07-01 18:44:59 +00:00
|
|
|
partialPath := trie.CompactToHex(nodeElements[0].([]byte))
|
|
|
|
valueNodePath := append(nodePath, partialPath...)
|
|
|
|
encodedPath := trie.HexToCompact(valueNodePath)
|
|
|
|
leafKey := encodedPath[1:]
|
2021-12-14 06:50:19 +00:00
|
|
|
storageNode.key = common.BytesToHash(leafKey)
|
|
|
|
case extension, branch:
|
|
|
|
storageNode.key = common.BytesToHash([]byte{})
|
2020-07-01 18:44:59 +00:00
|
|
|
default:
|
2021-12-13 15:01:32 +00:00
|
|
|
return nil, errors.New("unexpected node type")
|
2020-07-01 23:07:56 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
if err = s.ipfsPublisher.PublishStorageNode(storageNode, stateID, tx); err != nil {
|
|
|
|
return nil, err
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
return tx, it.Error()
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|