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 (
|
2022-08-03 11:35:04 +00:00
|
|
|
"context"
|
2020-07-01 18:44:59 +00:00
|
|
|
"fmt"
|
2022-05-13 08:30:40 +00:00
|
|
|
"math/big"
|
2023-08-04 12:36:56 +00:00
|
|
|
"sync"
|
2020-07-01 18:44:59 +00:00
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
"github.com/cerc-io/eth-iterator-utils/tracker"
|
|
|
|
statediff "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/types"
|
2020-07-01 18:44:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2022-03-09 13:37:33 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-07-01 18:44:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-05-12 14:47:11 +00:00
|
|
|
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 {
|
2023-08-04 12:36:56 +00:00
|
|
|
ethDB ethdb.Database
|
|
|
|
stateDB state.Database
|
|
|
|
indexer indexer.Indexer
|
|
|
|
maxBatchSize uint
|
|
|
|
recoveryFile string
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 05:37:27 +00:00
|
|
|
func NewLevelDB(con *EthConfig) (ethdb.Database, error) {
|
2023-08-04 12:36:56 +00:00
|
|
|
kvdb, err := rawdb.NewLevelDBDatabase(con.LevelDBPath, 1024, 256, "ipld-eth-state-snapshot", true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to connect LevelDB: %s", err)
|
|
|
|
}
|
2023-03-31 15:39:27 +00:00
|
|
|
edb, err := rawdb.NewDatabaseWithFreezer(kvdb, con.AncientDBPath, "ipld-eth-state-snapshot", true)
|
2022-03-30 23:57:30 +00:00
|
|
|
if err != nil {
|
2023-08-04 12:36:56 +00:00
|
|
|
return nil, fmt.Errorf("failed to connect LevelDB freezer: %s", err)
|
2022-03-30 23:57:30 +00:00
|
|
|
}
|
|
|
|
return edb, nil
|
2022-01-11 05:37:27 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2022-01-11 05:37:27 +00:00
|
|
|
// NewSnapshotService creates Service.
|
2023-08-04 12:36:56 +00:00
|
|
|
func NewSnapshotService(edb ethdb.Database, indexer indexer.StateDiffIndexer, recoveryFile string) (*Service, error) {
|
2020-07-01 18:44:59 +00:00
|
|
|
return &Service{
|
2023-08-04 12:36:56 +00:00
|
|
|
ethDB: edb,
|
|
|
|
stateDB: state.NewDatabase(edb),
|
|
|
|
indexer: indexer,
|
|
|
|
maxBatchSize: defaultBatchSize,
|
|
|
|
recoveryFile: recoveryFile,
|
2020-07-01 18:44:59 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 10:23:36 +00:00
|
|
|
type SnapshotParams struct {
|
2023-08-04 12:36:56 +00:00
|
|
|
WatchedAddresses []common.Address
|
2022-08-03 11:35:04 +00:00
|
|
|
Height uint64
|
|
|
|
Workers uint
|
2020-08-20 10:23:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 04:38:31 +00:00
|
|
|
func (s *Service) CreateSnapshot(params SnapshotParams) error {
|
2020-07-16 15:31:37 +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
|
2022-03-09 13:37:33 +00:00
|
|
|
log.Infof("Creating snapshot at height %d", params.Height)
|
2020-08-23 04:38:31 +00:00
|
|
|
hash := rawdb.ReadCanonicalHash(s.ethDB, params.Height)
|
|
|
|
header := rawdb.ReadHeader(s.ethDB, hash, params.Height)
|
2020-07-16 15:31:37 +00:00
|
|
|
if header == nil {
|
2020-08-23 04:38:31 +00:00
|
|
|
return fmt.Errorf("unable to read canonical header at height %d", params.Height)
|
2020-07-16 15:31:37 +00:00
|
|
|
}
|
2022-03-09 13:37:33 +00:00
|
|
|
log.Infof("head hash: %s head height: %d", hash.Hex(), params.Height)
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
// Context for snapshot work
|
2022-08-03 11:35:04 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
2023-08-04 12:36:56 +00:00
|
|
|
defer cancelCtx()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
tx := s.indexer.BeginTx(header.Number, ctx)
|
|
|
|
defer tx.RollbackOnFailure(err)
|
2022-02-09 15:19:10 +00:00
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
var headerid string
|
|
|
|
headerid, err = s.indexer.PushHeader(tx, header, big.NewInt(0), big.NewInt(0))
|
2022-02-18 11:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-08 12:08:17 +00:00
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
tracker := tracker.New(s.recoveryFile, params.Workers)
|
|
|
|
tracker.CaptureSignal(cancelCtx)
|
2022-02-18 11:12:53 +00:00
|
|
|
|
|
|
|
defer func() {
|
2023-08-04 12:36:56 +00:00
|
|
|
err := tracker.HaltAndDump()
|
2022-02-18 11:12:53 +00:00
|
|
|
if err != nil {
|
2022-05-26 10:20:42 +00:00
|
|
|
log.Errorf("failed to write recovery file: %v", err)
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
var nodeMtx, ipldMtx sync.Mutex
|
|
|
|
nodeSink := func(node types.StateLeafNode) error {
|
|
|
|
nodeMtx.Lock()
|
|
|
|
defer nodeMtx.Unlock()
|
|
|
|
return s.indexer.PushStateNode(tx, node, headerid)
|
2020-08-20 10:23:36 +00:00
|
|
|
}
|
2023-08-04 12:36:56 +00:00
|
|
|
ipldSink := func(c types.IPLD) error {
|
|
|
|
ipldMtx.Lock()
|
|
|
|
defer ipldMtx.Unlock()
|
|
|
|
return s.indexer.PushIPLD(tx, c)
|
2020-07-16 15:02:16 +00:00
|
|
|
}
|
2020-08-20 10:23:36 +00:00
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
// Build a diff compared against the zero hash to get a full snapshot
|
|
|
|
sdargs := statediff.Args{
|
|
|
|
NewStateRoot: header.Root,
|
|
|
|
BlockHash: header.Hash(),
|
|
|
|
BlockNumber: header.Number,
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2023-08-04 12:36:56 +00:00
|
|
|
sdparams := statediff.Params{
|
|
|
|
WatchedAddresses: params.WatchedAddresses,
|
2022-01-11 00:06:29 +00:00
|
|
|
}
|
2023-08-04 12:36:56 +00:00
|
|
|
sdparams.ComputeWatchedAddressesLeafPaths()
|
|
|
|
builder := statediff.NewBuilder(adapt.GethStateView(s.stateDB))
|
|
|
|
if err = builder.WriteStateDiffTracked(sdargs, sdparams, nodeSink, ipldSink, &tracker); err != nil {
|
2023-05-16 14:22:15 +00:00
|
|
|
return err
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
if err = tx.Submit(); err != nil {
|
|
|
|
return fmt.Errorf("batch transaction submission failed: %w", err)
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
2023-08-04 12:36:56 +00:00
|
|
|
return err
|
2023-04-12 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 12:36:56 +00:00
|
|
|
// CreateLatestSnapshot snapshot at head (ignores height param)
|
|
|
|
func (s *Service) CreateLatestSnapshot(workers uint, watchedAddresses []common.Address) error {
|
|
|
|
log.Info("Creating snapshot at head")
|
|
|
|
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())
|
2023-04-12 18:07:42 +00:00
|
|
|
}
|
2023-08-04 12:36:56 +00:00
|
|
|
return s.CreateSnapshot(SnapshotParams{Height: *height, Workers: workers, WatchedAddresses: watchedAddresses})
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|