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-09-29 18:43:26 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2020-07-01 18:44:59 +00:00
|
|
|
|
2023-09-29 18:43:26 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-state-snapshot/pkg/prom"
|
|
|
|
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-09-29 18:43:26 +00:00
|
|
|
ethDB ethdb.Database
|
|
|
|
stateDB state.Database
|
|
|
|
indexer indexer.Indexer
|
|
|
|
maxBatchSize uint
|
|
|
|
recoveryFile string
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 13:17:00 +00:00
|
|
|
func NewEthDB(con *EthDBConfig) (ethdb.Database, error) {
|
|
|
|
return rawdb.Open(rawdb.OpenOptions{
|
|
|
|
Directory: con.DBPath,
|
|
|
|
AncientsDirectory: con.AncientDBPath,
|
|
|
|
Namespace: "ipld-eth-state-snapshot",
|
|
|
|
Cache: 1024,
|
|
|
|
Handles: 256,
|
|
|
|
ReadOnly: true,
|
|
|
|
})
|
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-09-29 18:43:26 +00:00
|
|
|
func NewSnapshotService(edb ethdb.Database, indexer indexer.Indexer, recoveryFile string) (*Service, error) {
|
2020-07-01 18:44:59 +00:00
|
|
|
return &Service{
|
2023-09-29 18:43:26 +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-09-29 18:43:26 +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
|
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
|
|
|
}
|
2023-09-29 18:43:26 +00:00
|
|
|
log.WithField("height", params.Height).WithField("hash", hash).Info("Creating snapshot")
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2023-09-29 18:43:26 +00:00
|
|
|
// Context for snapshot work
|
2022-08-03 11:35:04 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
2023-09-29 18:43:26 +00:00
|
|
|
defer cancelCtx()
|
|
|
|
// Cancel context on receiving a signal. On cancellation, all tracked iterators complete
|
|
|
|
// processing of their current node before stopping.
|
|
|
|
captureSignal(cancelCtx)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
tx := s.indexer.BeginTx(header.Number, ctx)
|
|
|
|
defer tx.RollbackOnFailure(err)
|
2022-02-09 15:19:10 +00:00
|
|
|
|
2023-09-29 18:43:26 +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-09-29 18:43:26 +00:00
|
|
|
tr := prom.NewTracker(s.recoveryFile, params.Workers)
|
2022-02-18 11:12:53 +00:00
|
|
|
defer func() {
|
2023-09-29 18:43:26 +00:00
|
|
|
err := tr.CloseAndSave()
|
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-09-29 18:43:26 +00:00
|
|
|
var nodeMtx, ipldMtx sync.Mutex
|
|
|
|
nodeSink := func(node types.StateLeafNode) error {
|
|
|
|
nodeMtx.Lock()
|
|
|
|
defer nodeMtx.Unlock()
|
2023-10-09 21:17:20 +00:00
|
|
|
prom.IncStateNodeCount()
|
|
|
|
prom.AddStorageNodeCount(len(node.StorageDiff))
|
2023-09-29 18:43:26 +00:00
|
|
|
return s.indexer.PushStateNode(tx, node, headerid)
|
2020-08-20 10:23:36 +00:00
|
|
|
}
|
2023-09-29 18:43:26 +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-09-29 18:43:26 +00:00
|
|
|
sdparams := statediff.Params{
|
|
|
|
WatchedAddresses: params.WatchedAddresses,
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2023-09-29 18:43:26 +00:00
|
|
|
sdparams.ComputeWatchedAddressesLeafPaths()
|
|
|
|
builder := statediff.NewBuilder(adapt.GethStateView(s.stateDB))
|
|
|
|
builder.SetSubtrieWorkers(params.Workers)
|
2024-08-05 13:17:00 +00:00
|
|
|
if err = builder.WriteStateSnapshot(ctx, header.Root, sdparams, nodeSink, ipldSink, tr); err != nil {
|
2022-01-11 00:06:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2023-09-29 18:43:26 +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-05-16 14:22:15 +00:00
|
|
|
return err
|
2022-08-03 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 18:43:26 +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)
|
2023-04-12 18:07:42 +00:00
|
|
|
}
|
2023-09-29 18:43:26 +00:00
|
|
|
return s.CreateSnapshot(SnapshotParams{Height: *height, Workers: workers, WatchedAddresses: watchedAddresses})
|
2023-04-12 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 18:43:26 +00:00
|
|
|
func captureSignal(cb func()) {
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
2023-04-12 18:07:42 +00:00
|
|
|
|
2023-09-29 18:43:26 +00:00
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
sig := <-sigChan
|
|
|
|
log.Errorf("Signal received (%v), stopping", sig)
|
|
|
|
cb()
|
|
|
|
}()
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|