diff --git a/api/service/stagedstreamsync/adapter.go b/api/service/stagedstreamsync/adapter.go index ca9c6a678..56c42b661 100644 --- a/api/service/stagedstreamsync/adapter.go +++ b/api/service/stagedstreamsync/adapter.go @@ -9,6 +9,7 @@ import ( "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/p2p/stream/common/streammanager" syncproto "github.com/harmony-one/harmony/p2p/stream/protocols/sync" + "github.com/harmony-one/harmony/p2p/stream/protocols/sync/message" sttypes "github.com/harmony-one/harmony/p2p/stream/types" ) @@ -20,6 +21,10 @@ type syncProtocol interface { GetBlocksByHashes(ctx context.Context, hs []common.Hash, opts ...syncproto.Option) ([]*types.Block, sttypes.StreamID, error) GetReceipts(ctx context.Context, hs []common.Hash, opts ...syncproto.Option) (receipts []types.Receipts, stid sttypes.StreamID, err error) GetNodeData(ctx context.Context, hs []common.Hash, opts ...syncproto.Option) (data [][]byte, stid sttypes.StreamID, err error) + GetAccountRange(ctx context.Context, root common.Hash, origin common.Hash, limit common.Hash, bytes uint64, opts ...syncproto.Option) (accounts []*message.AccountData, proof [][]byte, stid sttypes.StreamID, err error) + GetStorageRanges(ctx context.Context, root common.Hash, accounts []common.Hash, origin common.Hash, limit common.Hash, bytes uint64, opts ...syncproto.Option) (slots [][]*message.StorageData, proof [][]byte, stid sttypes.StreamID, err error) + GetByteCodes(ctx context.Context, hs []common.Hash, bytes uint64, opts ...syncproto.Option) (codes [][]byte, stid sttypes.StreamID, err error) + GetTrieNodes(ctx context.Context, root common.Hash, paths []*message.TrieNodePathSet, bytes uint64, opts ...syncproto.Option) (nodes [][]byte, stid sttypes.StreamID, err error) RemoveStream(stID sttypes.StreamID) // If a stream delivers invalid data, remove the stream StreamFailed(stID sttypes.StreamID, reason string) diff --git a/api/service/stagedstreamsync/block_manager.go b/api/service/stagedstreamsync/block_manager.go index 28c966b4d..d614d2420 100644 --- a/api/service/stagedstreamsync/block_manager.go +++ b/api/service/stagedstreamsync/block_manager.go @@ -1,8 +1,10 @@ package stagedstreamsync import ( + "fmt" "sync" + "github.com/ethereum/go-ethereum/common" sttypes "github.com/harmony-one/harmony/p2p/stream/types" "github.com/ledgerwatch/erigon-lib/kv" "github.com/rs/zerolog" @@ -11,6 +13,7 @@ import ( type BlockDownloadDetails struct { loopID int streamID sttypes.StreamID + rootHash common.Hash } // blockDownloadManager is the helper structure for get blocks request management @@ -19,11 +22,11 @@ type blockDownloadManager struct { tx kv.RwTx targetBN uint64 - requesting map[uint64]struct{} // block numbers that have been assigned to workers but not received - processing map[uint64]struct{} // block numbers received requests but not inserted - retries *prioritizedNumbers // requests where error happens - rq *resultQueue // result queue wait to be inserted into blockchain - bdd map[uint64]BlockDownloadDetails // details about how this block was downloaded + requesting map[uint64]struct{} // block numbers that have been assigned to workers but not received + processing map[uint64]struct{} // block numbers received requests but not inserted + retries *prioritizedNumbers // requests where error happens + rq *resultQueue // result queue wait to be inserted into blockchain + bdd map[uint64]*BlockDownloadDetails // details about how this block was downloaded logger zerolog.Logger lock sync.Mutex @@ -38,26 +41,26 @@ func newBlockDownloadManager(tx kv.RwTx, chain blockChain, targetBN uint64, logg processing: make(map[uint64]struct{}), retries: newPrioritizedNumbers(), rq: newResultQueue(), - bdd: make(map[uint64]BlockDownloadDetails), + bdd: make(map[uint64]*BlockDownloadDetails), logger: logger, } } // GetNextBatch get the next block numbers batch -func (gbm *blockDownloadManager) GetNextBatch() []uint64 { +func (gbm *blockDownloadManager) GetNextBatch(curHeight uint64) []uint64 { gbm.lock.Lock() defer gbm.lock.Unlock() cap := BlocksPerRequest - bns := gbm.getBatchFromRetries(cap) + bns := gbm.getBatchFromRetries(cap, curHeight) if len(bns) > 0 { cap -= len(bns) gbm.addBatchToRequesting(bns) } if gbm.availableForMoreTasks() { - addBNs := gbm.getBatchFromUnprocessed(cap) + addBNs := gbm.getBatchFromUnprocessed(cap, curHeight) gbm.addBatchToRequesting(addBNs) bns = append(bns, addBNs...) } @@ -88,7 +91,7 @@ func (gbm *blockDownloadManager) HandleRequestResult(bns []uint64, blockBytes [] gbm.retries.push(bn) } else { gbm.processing[bn] = struct{}{} - gbm.bdd[bn] = BlockDownloadDetails{ + gbm.bdd[bn] = &BlockDownloadDetails{ loopID: loopID, streamID: streamID, } @@ -107,7 +110,7 @@ func (gbm *blockDownloadManager) SetDownloadDetails(bns []uint64, loopID int, st defer gbm.lock.Unlock() for _, bn := range bns { - gbm.bdd[bn] = BlockDownloadDetails{ + gbm.bdd[bn] = &BlockDownloadDetails{ loopID: loopID, streamID: streamID, } @@ -116,25 +119,43 @@ func (gbm *blockDownloadManager) SetDownloadDetails(bns []uint64, loopID int, st } // GetDownloadDetails returns the download details for a block -func (gbm *blockDownloadManager) GetDownloadDetails(blockNumber uint64) (loopID int, streamID sttypes.StreamID) { +func (gbm *blockDownloadManager) GetDownloadDetails(blockNumber uint64) (loopID int, streamID sttypes.StreamID, err error) { gbm.lock.Lock() defer gbm.lock.Unlock() - return gbm.bdd[blockNumber].loopID, gbm.bdd[blockNumber].streamID + if dm, exist := gbm.bdd[blockNumber]; exist { + return dm.loopID, dm.streamID, nil + } + return 0, sttypes.StreamID(fmt.Sprint(0)), fmt.Errorf("there is no download details for the block number: %d", blockNumber) +} + +// SetRootHash sets the root hash for a specific block +func (gbm *blockDownloadManager) SetRootHash(blockNumber uint64, root common.Hash) { + gbm.lock.Lock() + defer gbm.lock.Unlock() + + gbm.bdd[blockNumber].rootHash = root +} + +// GetRootHash returns the root hash for a specific block +func (gbm *blockDownloadManager) GetRootHash(blockNumber uint64) common.Hash { + gbm.lock.Lock() + defer gbm.lock.Unlock() + + return gbm.bdd[blockNumber].rootHash } // getBatchFromRetries get the block number batch to be requested from retries. -func (gbm *blockDownloadManager) getBatchFromRetries(cap int) []uint64 { +func (gbm *blockDownloadManager) getBatchFromRetries(cap int, fromBlockNumber uint64) []uint64 { var ( requestBNs []uint64 - curHeight = gbm.chain.CurrentBlock().NumberU64() ) for cnt := 0; cnt < cap; cnt++ { bn := gbm.retries.pop() if bn == 0 { break // no more retries } - if bn <= curHeight { + if bn <= fromBlockNumber { continue } requestBNs = append(requestBNs, bn) @@ -143,10 +164,9 @@ func (gbm *blockDownloadManager) getBatchFromRetries(cap int) []uint64 { } // getBatchFromUnprocessed returns a batch of block numbers to be requested from unprocessed. -func (gbm *blockDownloadManager) getBatchFromUnprocessed(cap int) []uint64 { +func (gbm *blockDownloadManager) getBatchFromUnprocessed(cap int, curHeight uint64) []uint64 { var ( requestBNs []uint64 - curHeight = gbm.chain.CurrentBlock().NumberU64() ) bn := curHeight + 1 // TODO: this algorithm can be potentially optimized. diff --git a/api/service/stagedstreamsync/const.go b/api/service/stagedstreamsync/const.go index 048b5d812..2789bfb1e 100644 --- a/api/service/stagedstreamsync/const.go +++ b/api/service/stagedstreamsync/const.go @@ -23,9 +23,35 @@ const ( // no more request will be assigned to workers to wait for InsertChain to finish. SoftQueueCap int = 100 + // number of get nodes by hashes for each request + StatesPerRequest int = 100 + + // maximum number of blocks for get receipts request + ReceiptsPerRequest int = 10 + + // DefaultConcurrency is the default settings for concurrency + DefaultConcurrency int = 4 + + // MaxTriesToFetchNodeData is the maximum number of tries to fetch node data + MaxTriesToFetchNodeData int = 5 + // ShortRangeTimeout is the timeout for each short range sync, which allow short range sync // to restart automatically when stuck in `getBlockHashes` ShortRangeTimeout time.Duration = 1 * time.Minute + + // pivot block distance ranges + MinPivotDistanceToHead uint64 = 1024 + MaxPivotDistanceToHead uint64 = 2048 +) + +// SyncMode represents the synchronization mode of the downloader. +// It is a uint32 as it is used with atomic operations. +type SyncMode uint32 + +const ( + FullSync SyncMode = iota // Synchronize the entire blockchain history from full blocks + FastSync // Download all blocks and states + SnapSync // Download the chain and the state via compact snapshots ) type ( @@ -35,6 +61,9 @@ type ( // TODO: remove this when stream sync is fully up. ServerOnly bool + // Synchronization mode of the downloader + SyncMode SyncMode + // parameters Network nodeconfig.NetworkType Concurrency int // Number of concurrent sync requests diff --git a/api/service/stagedstreamsync/default_stages.go b/api/service/stagedstreamsync/default_stages.go index 55986ff6e..f869ee5fe 100644 --- a/api/service/stagedstreamsync/default_stages.go +++ b/api/service/stagedstreamsync/default_stages.go @@ -8,35 +8,91 @@ type ForwardOrder []SyncStageID type RevertOrder []SyncStageID type CleanUpOrder []SyncStageID -var DefaultForwardOrder = ForwardOrder{ - Heads, - SyncEpoch, - ShortRange, - BlockBodies, - // Stages below don't use Internet - States, - LastMile, - Finish, +var ( + StagesForwardOrder ForwardOrder + StagesRevertOrder RevertOrder + StagesCleanUpOrder CleanUpOrder +) + +func initStagesOrder(syncMode SyncMode) { + switch syncMode { + case FullSync: + initFullSyncStagesOrder() + case FastSync: + initFastSyncStagesOrder() + default: + panic("not supported sync mode") + } } -var DefaultRevertOrder = RevertOrder{ - Finish, - LastMile, - States, - BlockBodies, - ShortRange, - SyncEpoch, - Heads, +func initFullSyncStagesOrder() { + StagesForwardOrder = ForwardOrder{ + Heads, + SyncEpoch, + ShortRange, + BlockBodies, + States, + LastMile, + Finish, + } + + StagesRevertOrder = RevertOrder{ + Finish, + LastMile, + States, + BlockBodies, + ShortRange, + SyncEpoch, + Heads, + } + + StagesCleanUpOrder = CleanUpOrder{ + Finish, + LastMile, + States, + BlockBodies, + ShortRange, + SyncEpoch, + Heads, + } } -var DefaultCleanUpOrder = CleanUpOrder{ - Finish, - LastMile, - States, - BlockBodies, - ShortRange, - SyncEpoch, - Heads, +func initFastSyncStagesOrder() { + StagesForwardOrder = ForwardOrder{ + Heads, + SyncEpoch, + ShortRange, + BlockBodies, + Receipts, + StateSync, + States, + LastMile, + Finish, + } + + StagesRevertOrder = RevertOrder{ + Finish, + LastMile, + States, + StateSync, + Receipts, + BlockBodies, + ShortRange, + SyncEpoch, + Heads, + } + + StagesCleanUpOrder = CleanUpOrder{ + Finish, + LastMile, + States, + StateSync, + Receipts, + BlockBodies, + ShortRange, + SyncEpoch, + Heads, + } } func DefaultStages(ctx context.Context, @@ -44,7 +100,9 @@ func DefaultStages(ctx context.Context, seCfg StageEpochCfg, srCfg StageShortRangeCfg, bodiesCfg StageBodiesCfg, + stateSyncCfg StageStateSyncCfg, statesCfg StageStatesCfg, + receiptsCfg StageReceiptsCfg, lastMileCfg StageLastMileCfg, finishCfg StageFinishCfg, ) []*Stage { @@ -54,6 +112,8 @@ func DefaultStages(ctx context.Context, handlerStageEpochSync := NewStageEpoch(seCfg) handlerStageBodies := NewStageBodies(bodiesCfg) handlerStageStates := NewStageStates(statesCfg) + handlerStageStateSync := NewStageStateSync(stateSyncCfg) + handlerStageReceipts := NewStageReceipts(receiptsCfg) handlerStageLastMile := NewStageLastMile(lastMileCfg) handlerStageFinish := NewStageFinish(finishCfg) @@ -83,6 +143,16 @@ func DefaultStages(ctx context.Context, Description: "Update Blockchain State", Handler: handlerStageStates, }, + { + ID: StateSync, + Description: "Retrieve States", + Handler: handlerStageStateSync, + }, + { + ID: Receipts, + Description: "Retrieve Receipts", + Handler: handlerStageReceipts, + }, { ID: LastMile, Description: "update status for blocks after sync and update last mile blocks as well", diff --git a/api/service/stagedstreamsync/helpers.go b/api/service/stagedstreamsync/helpers.go index 75e504214..96c1c22b0 100644 --- a/api/service/stagedstreamsync/helpers.go +++ b/api/service/stagedstreamsync/helpers.go @@ -73,6 +73,27 @@ func checkGetBlockByHashesResult(blocks []*types.Block, hashes []common.Hash) er return nil } +func getBlockByMaxVote(blocks []*types.Block) (*types.Block, error) { + hashesVote := make(map[common.Hash]int) + maxVote := int(-1) + maxVotedBlockIndex := int(0) + + for i, block := range blocks { + if block == nil { + continue + } + hashesVote[block.Header().Hash()]++ + if hashesVote[block.Header().Hash()] > maxVote { + maxVote = hashesVote[block.Header().Hash()] + maxVotedBlockIndex = i + } + } + if maxVote < 0 { + return nil, ErrInvalidBlockBytes + } + return blocks[maxVotedBlockIndex], nil +} + func countHashMaxVote(m map[sttypes.StreamID]common.Hash, whitelist map[sttypes.StreamID]struct{}) (common.Hash, map[sttypes.StreamID]struct{}) { var ( voteM = make(map[common.Hash]int) diff --git a/api/service/stagedstreamsync/proof.go b/api/service/stagedstreamsync/proof.go new file mode 100644 index 000000000..216d797d4 --- /dev/null +++ b/api/service/stagedstreamsync/proof.go @@ -0,0 +1,146 @@ +package stagedstreamsync + +import ( + "errors" + "sync" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/rlp" +) + +// ProofSet stores a set of trie nodes. It implements trie.Database and can also +// act as a cache for another trie.Database. +type ProofSet struct { + nodes map[string][]byte + order []string + + dataSize int + lock sync.RWMutex +} + +// NewProofSet creates an empty node set +func NewProofSet() *ProofSet { + return &ProofSet{ + nodes: make(map[string][]byte), + } +} + +// Put stores a new node in the set +func (db *ProofSet) Put(key []byte, value []byte) error { + db.lock.Lock() + defer db.lock.Unlock() + + if _, ok := db.nodes[string(key)]; ok { + return nil + } + keystr := string(key) + + db.nodes[keystr] = common.CopyBytes(value) + db.order = append(db.order, keystr) + db.dataSize += len(value) + + return nil +} + +// Delete removes a node from the set +func (db *ProofSet) Delete(key []byte) error { + db.lock.Lock() + defer db.lock.Unlock() + + delete(db.nodes, string(key)) + return nil +} + +// Get returns a stored node +func (db *ProofSet) Get(key []byte) ([]byte, error) { + db.lock.RLock() + defer db.lock.RUnlock() + + if entry, ok := db.nodes[string(key)]; ok { + return entry, nil + } + return nil, errors.New("not found") +} + +// Has returns true if the node set contains the given key +func (db *ProofSet) Has(key []byte) (bool, error) { + _, err := db.Get(key) + return err == nil, nil +} + +// KeyCount returns the number of nodes in the set +func (db *ProofSet) KeyCount() int { + db.lock.RLock() + defer db.lock.RUnlock() + + return len(db.nodes) +} + +// DataSize returns the aggregated data size of nodes in the set +func (db *ProofSet) DataSize() int { + db.lock.RLock() + defer db.lock.RUnlock() + + return db.dataSize +} + +// List converts the node set to a ProofList +func (db *ProofSet) List() ProofList { + db.lock.RLock() + defer db.lock.RUnlock() + + var values ProofList + for _, key := range db.order { + values = append(values, db.nodes[key]) + } + return values +} + +// Store writes the contents of the set to the given database +func (db *ProofSet) Store(target ethdb.KeyValueWriter) { + db.lock.RLock() + defer db.lock.RUnlock() + + for key, value := range db.nodes { + target.Put([]byte(key), value) + } +} + +// ProofList stores an ordered list of trie nodes. It implements ethdb.KeyValueWriter. +type ProofList []rlp.RawValue + +// Store writes the contents of the list to the given database +func (n ProofList) Store(db ethdb.KeyValueWriter) { + for _, node := range n { + db.Put(crypto.Keccak256(node), node) + } +} + +// Set converts the node list to a ProofSet +func (n ProofList) Set() *ProofSet { + db := NewProofSet() + n.Store(db) + return db +} + +// Put stores a new node at the end of the list +func (n *ProofList) Put(key []byte, value []byte) error { + *n = append(*n, value) + return nil +} + +// Delete panics as there's no reason to remove a node from the list. +func (n *ProofList) Delete(key []byte) error { + panic("not supported") +} + +// DataSize returns the aggregated data size of nodes in the list +func (n ProofList) DataSize() int { + var size int + for _, node := range n { + size += len(node) + } + return size +} diff --git a/api/service/stagedstreamsync/range.go b/api/service/stagedstreamsync/range.go new file mode 100644 index 000000000..d05a92ed4 --- /dev/null +++ b/api/service/stagedstreamsync/range.go @@ -0,0 +1,84 @@ +// Copyright 2021 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package stagedstreamsync + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/holiman/uint256" +) + +// hashSpace is the total size of the 256 bit hash space for accounts. +var hashSpace = new(big.Int).Exp(common.Big2, common.Big256, nil) + +// hashRange is a utility to handle ranges of hashes, Split up the +// hash-space into sections, and 'walk' over the sections +type hashRange struct { + current *uint256.Int + step *uint256.Int +} + +// newHashRange creates a new hashRange, initiated at the start position, +// and with the step set to fill the desired 'num' chunks +func newHashRange(start common.Hash, num uint64) *hashRange { + left := new(big.Int).Sub(hashSpace, start.Big()) + step := new(big.Int).Div( + new(big.Int).Add(left, new(big.Int).SetUint64(num-1)), + new(big.Int).SetUint64(num), + ) + step256 := new(uint256.Int) + step256.SetFromBig(step) + + return &hashRange{ + current: new(uint256.Int).SetBytes32(start[:]), + step: step256, + } +} + +// Next pushes the hash range to the next interval. +func (r *hashRange) Next() bool { + next, overflow := new(uint256.Int).AddOverflow(r.current, r.step) + if overflow { + return false + } + r.current = next + return true +} + +// Start returns the first hash in the current interval. +func (r *hashRange) Start() common.Hash { + return r.current.Bytes32() +} + +// End returns the last hash in the current interval. +func (r *hashRange) End() common.Hash { + // If the end overflows (non divisible range), return a shorter interval + next, overflow := new(uint256.Int).AddOverflow(r.current, r.step) + if overflow { + return common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") + } + return next.SubUint64(next, 1).Bytes32() +} + +// incHash returns the next hash, in lexicographical order (a.k.a plus one) +func incHash(h common.Hash) common.Hash { + var a uint256.Int + a.SetBytes32(h[:]) + a.AddUint64(&a, 1) + return common.Hash(a.Bytes32()) +} diff --git a/api/service/stagedstreamsync/receipt_download_manager.go b/api/service/stagedstreamsync/receipt_download_manager.go new file mode 100644 index 000000000..55d949082 --- /dev/null +++ b/api/service/stagedstreamsync/receipt_download_manager.go @@ -0,0 +1,180 @@ +package stagedstreamsync + +import ( + "sync" + + "github.com/harmony-one/harmony/core/types" + sttypes "github.com/harmony-one/harmony/p2p/stream/types" + "github.com/ledgerwatch/erigon-lib/kv" + "github.com/rs/zerolog" +) + +type ReceiptDownloadDetails struct { + streamID sttypes.StreamID +} + +type Received struct { + streamID sttypes.StreamID + block *types.Block + receipts types.Receipts +} + +// receiptDownloadManager is the helper structure for get receipts request management +type receiptDownloadManager struct { + chain blockChain + tx kv.RwTx + + targetBN uint64 + requesting map[uint64]struct{} // receipt numbers that have been assigned to workers but not received + processing map[uint64]struct{} // receipt numbers received requests but not inserted + retries *prioritizedNumbers // requests where error happens + rdd map[uint64]ReceiptDownloadDetails // details about how this receipt was downloaded + + received map[uint64]Received + + logger zerolog.Logger + lock sync.Mutex +} + +func newReceiptDownloadManager(tx kv.RwTx, chain blockChain, targetBN uint64, logger zerolog.Logger) *receiptDownloadManager { + return &receiptDownloadManager{ + chain: chain, + tx: tx, + targetBN: targetBN, + requesting: make(map[uint64]struct{}), + processing: make(map[uint64]struct{}), + retries: newPrioritizedNumbers(), + rdd: make(map[uint64]ReceiptDownloadDetails), + received: make(map[uint64]Received), + + logger: logger, + } +} + +// GetNextBatch get the next receipt numbers batch +func (rdm *receiptDownloadManager) GetNextBatch(curHeight uint64) []uint64 { + rdm.lock.Lock() + defer rdm.lock.Unlock() + + cap := ReceiptsPerRequest + + bns := rdm.getBatchFromRetries(cap, curHeight) + if len(bns) > 0 { + cap -= len(bns) + rdm.addBatchToRequesting(bns) + } + + if rdm.availableForMoreTasks() { + addBNs := rdm.getBatchFromUnprocessed(cap, curHeight) + rdm.addBatchToRequesting(addBNs) + bns = append(bns, addBNs...) + } + + return bns +} + +// HandleRequestError handles the error result +func (rdm *receiptDownloadManager) HandleRequestError(bns []uint64, err error) { + rdm.lock.Lock() + defer rdm.lock.Unlock() + + // add requested receipt numbers to retries + for _, bn := range bns { + delete(rdm.requesting, bn) + rdm.retries.push(bn) + } +} + +// HandleRequestResult handles get receipts result +func (rdm *receiptDownloadManager) HandleRequestResult(bns []uint64, receivedReceipts []types.Receipts, receivedBlocks []*types.Block, streamID sttypes.StreamID) error { + rdm.lock.Lock() + defer rdm.lock.Unlock() + + for i, bn := range bns { + delete(rdm.requesting, bn) + if !indexExists(receivedBlocks, i) || !indexExists(receivedReceipts, i) { + rdm.retries.push(bn) + } else { + rdm.processing[bn] = struct{}{} + rdm.rdd[bn] = ReceiptDownloadDetails{ + streamID: streamID, + } + rdm.received[bn] = Received{ + block: receivedBlocks[i], + receipts: receivedReceipts[i], + } + } + } + return nil +} + +// SetDownloadDetails sets the download details for a batch of blocks +func (rdm *receiptDownloadManager) SetDownloadDetails(bns []uint64, streamID sttypes.StreamID) error { + rdm.lock.Lock() + defer rdm.lock.Unlock() + + for _, bn := range bns { + rdm.rdd[bn] = ReceiptDownloadDetails{ + streamID: streamID, + } + } + return nil +} + +// GetDownloadDetails returns the download details for a certain block number +func (rdm *receiptDownloadManager) GetDownloadDetails(blockNumber uint64) (streamID sttypes.StreamID) { + rdm.lock.Lock() + defer rdm.lock.Unlock() + + return rdm.rdd[blockNumber].streamID +} + +// getBatchFromRetries get the receipt number batch to be requested from retries. +func (rdm *receiptDownloadManager) getBatchFromRetries(cap int, fromBlockNumber uint64) []uint64 { + var ( + requestBNs []uint64 + ) + for cnt := 0; cnt < cap; cnt++ { + bn := rdm.retries.pop() + if bn == 0 { + break // no more retries + } + if bn <= fromBlockNumber { + continue + } + requestBNs = append(requestBNs, bn) + } + return requestBNs +} + +// getBatchFromUnprocessed returns a batch of receipt numbers to be requested from unprocessed. +func (rdm *receiptDownloadManager) getBatchFromUnprocessed(cap int, curHeight uint64) []uint64 { + var ( + requestBNs []uint64 + ) + bn := curHeight + 1 + // TODO: this algorithm can be potentially optimized. + for cnt := 0; cnt < cap && bn <= rdm.targetBN; cnt++ { + for bn <= rdm.targetBN { + _, ok1 := rdm.requesting[bn] + _, ok2 := rdm.processing[bn] + if !ok1 && !ok2 { + requestBNs = append(requestBNs, bn) + bn++ + break + } + bn++ + } + } + return requestBNs +} + +func (rdm *receiptDownloadManager) availableForMoreTasks() bool { + return len(rdm.requesting) < SoftQueueCap +} + +func (rdm *receiptDownloadManager) addBatchToRequesting(bns []uint64) { + for _, bn := range bns { + rdm.requesting[bn] = struct{}{} + } +} diff --git a/api/service/stagedstreamsync/sig_verify.go b/api/service/stagedstreamsync/sig_verify.go index 8de71effc..bdf5a2107 100644 --- a/api/service/stagedstreamsync/sig_verify.go +++ b/api/service/stagedstreamsync/sig_verify.go @@ -29,7 +29,7 @@ func verifyAndInsertBlocks(bc blockChain, blocks types.Blocks) (int, error) { return len(blocks), nil } -func verifyAndInsertBlock(bc blockChain, block *types.Block, nextBlocks ...*types.Block) error { +func verifyBlock(bc blockChain, block *types.Block, nextBlocks ...*types.Block) error { var ( sigBytes bls.SerializedSignature bitmap []byte @@ -61,7 +61,18 @@ func verifyAndInsertBlock(bc blockChain, block *types.Block, nextBlocks ...*type case err != nil: return errors.Wrap(err, "[InsertChain]") default: + } + return nil +} +func verifyAndInsertBlock(bc blockChain, block *types.Block, nextBlocks ...*types.Block) error { + //verify block + if err := verifyBlock(bc, block, nextBlocks...); err != nil { + return err + } + // insert block + if _, err := bc.InsertChain(types.Blocks{block}, false); err != nil { + return errors.Wrap(err, "[InsertChain]") } return nil } diff --git a/api/service/stagedstreamsync/stage_bodies.go b/api/service/stagedstreamsync/stage_bodies.go index b5d92e3a1..9fdf4681a 100644 --- a/api/service/stagedstreamsync/stage_bodies.go +++ b/api/service/stagedstreamsync/stage_bodies.go @@ -6,6 +6,7 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum/rlp" "github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/internal/utils" @@ -20,13 +21,14 @@ type StageBodies struct { } type StageBodiesCfg struct { - bc core.BlockChain - db kv.RwDB - blockDBs []kv.RwDB - concurrency int - protocol syncProtocol - isBeacon bool - logProgress bool + bc core.BlockChain + db kv.RwDB + blockDBs []kv.RwDB + concurrency int + protocol syncProtocol + isBeacon bool + extractReceiptHashes bool + logProgress bool } func NewStageBodies(cfg StageBodiesCfg) *StageBodies { @@ -35,15 +37,16 @@ func NewStageBodies(cfg StageBodiesCfg) *StageBodies { } } -func NewStageBodiesCfg(bc core.BlockChain, db kv.RwDB, blockDBs []kv.RwDB, concurrency int, protocol syncProtocol, isBeacon bool, logProgress bool) StageBodiesCfg { +func NewStageBodiesCfg(bc core.BlockChain, db kv.RwDB, blockDBs []kv.RwDB, concurrency int, protocol syncProtocol, isBeacon bool, extractReceiptHashes bool, logProgress bool) StageBodiesCfg { return StageBodiesCfg{ - bc: bc, - db: db, - blockDBs: blockDBs, - concurrency: concurrency, - protocol: protocol, - isBeacon: isBeacon, - logProgress: logProgress, + bc: bc, + db: db, + blockDBs: blockDBs, + concurrency: concurrency, + protocol: protocol, + isBeacon: isBeacon, + extractReceiptHashes: extractReceiptHashes, + logProgress: logProgress, } } @@ -67,7 +70,7 @@ func (b *StageBodies) Exec(ctx context.Context, firstCycle bool, invalidBlockRev } maxHeight := s.state.status.targetBN - currentHead := b.configs.bc.CurrentBlock().NumberU64() + currentHead := s.state.CurrentBlockNumber() if currentHead >= maxHeight { return nil } @@ -118,7 +121,7 @@ func (b *StageBodies) Exec(ctx context.Context, firstCycle bool, invalidBlockRev for i := 0; i != s.state.config.Concurrency; i++ { wg.Add(1) - go b.runBlockWorkerLoop(ctx, s.state.gbm, &wg, i, startTime) + go b.runBlockWorkerLoop(ctx, s.state.gbm, &wg, i, s, startTime) } wg.Wait() @@ -133,9 +136,9 @@ func (b *StageBodies) Exec(ctx context.Context, firstCycle bool, invalidBlockRev } // runBlockWorkerLoop creates a work loop for download blocks -func (b *StageBodies) runBlockWorkerLoop(ctx context.Context, gbm *blockDownloadManager, wg *sync.WaitGroup, loopID int, startTime time.Time) { +func (b *StageBodies) runBlockWorkerLoop(ctx context.Context, gbm *blockDownloadManager, wg *sync.WaitGroup, loopID int, s *StageState, startTime time.Time) { - currentBlock := int(b.configs.bc.CurrentBlock().NumberU64()) + currentBlock := int(s.state.CurrentBlockNumber()) defer wg.Done() @@ -145,7 +148,8 @@ func (b *StageBodies) runBlockWorkerLoop(ctx context.Context, gbm *blockDownload return default: } - batch := gbm.GetNextBatch() + curHeight := s.state.CurrentBlockNumber() + batch := gbm.GetNextBatch(curHeight) if len(batch) == 0 { select { case <-ctx.Done(): @@ -204,6 +208,34 @@ func (b *StageBodies) runBlockWorkerLoop(ctx context.Context, gbm *blockDownload } } +func (b *StageBodies) verifyBlockAndExtractReceiptsData(batchBlockBytes [][]byte, batchSigBytes [][]byte, s *StageState) error { + var block *types.Block + for i := uint64(0); i < uint64(len(batchBlockBytes)); i++ { + blockBytes := batchBlockBytes[i] + sigBytes := batchSigBytes[i] + if blockBytes == nil { + continue + } + if err := rlp.DecodeBytes(blockBytes, &block); err != nil { + utils.Logger().Error(). + Uint64("block number", i). + Msg("block size invalid") + return ErrInvalidBlockBytes + } + if sigBytes != nil { + block.SetCurrentCommitSig(sigBytes) + } + + // if block.NumberU64() != i { + // return ErrInvalidBlockNumber + // } + if err := verifyBlock(b.configs.bc, block); err != nil { + return err + } + } + return nil +} + // redownloadBadBlock tries to redownload the bad block from other streams func (b *StageBodies) redownloadBadBlock(ctx context.Context, s *StageState) error { @@ -403,7 +435,7 @@ func (b *StageBodies) Revert(ctx context.Context, firstCycle bool, u *RevertStat defer tx.Rollback() } // save progress - currentHead := b.configs.bc.CurrentBlock().NumberU64() + currentHead := s.state.CurrentBlockNumber() if err = s.Update(tx, currentHead); err != nil { utils.Logger().Error(). Err(err). diff --git a/api/service/stagedstreamsync/stage_heads.go b/api/service/stagedstreamsync/stage_heads.go index c917884a3..bf0721aad 100644 --- a/api/service/stagedstreamsync/stage_heads.go +++ b/api/service/stagedstreamsync/stage_heads.go @@ -53,7 +53,7 @@ func (heads *StageHeads) Exec(ctx context.Context, firstCycle bool, invalidBlock maxHeight := s.state.status.targetBN maxBlocksPerSyncCycle := uint64(1024) // TODO: should be in config -> s.state.MaxBlocksPerSyncCycle - currentHeight := heads.configs.bc.CurrentBlock().NumberU64() + currentHeight := s.state.CurrentBlockNumber() s.state.currentCycle.TargetHeight = maxHeight targetHeight := uint64(0) if errV := CreateView(ctx, heads.configs.db, tx, func(etx kv.Tx) (err error) { @@ -89,6 +89,14 @@ func (heads *StageHeads) Exec(ctx context.Context, firstCycle bool, invalidBlock targetHeight = currentHeight + maxBlocksPerSyncCycle } + // check pivot: if chain hasn't reached to pivot yet + if s.state.status.cycleSyncMode != FullSync && s.state.status.pivotBlock != nil { + // set target height on the pivot block + if !s.state.status.statesSynced && targetHeight > s.state.status.pivotBlock.NumberU64() { + targetHeight = s.state.status.pivotBlock.NumberU64() + } + } + s.state.currentCycle.TargetHeight = targetHeight if err := s.Update(tx, targetHeight); err != nil { diff --git a/api/service/stagedstreamsync/stage_receipts.go b/api/service/stagedstreamsync/stage_receipts.go new file mode 100644 index 000000000..4445eb6ba --- /dev/null +++ b/api/service/stagedstreamsync/stage_receipts.go @@ -0,0 +1,398 @@ +package stagedstreamsync + +import ( + "context" + "fmt" + "sync" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" + "github.com/harmony-one/harmony/core" + "github.com/harmony-one/harmony/core/types" + "github.com/harmony-one/harmony/internal/utils" + sttypes "github.com/harmony-one/harmony/p2p/stream/types" + "github.com/ledgerwatch/erigon-lib/kv" + "github.com/pkg/errors" +) + +type StageReceipts struct { + configs StageReceiptsCfg +} + +type StageReceiptsCfg struct { + bc core.BlockChain + db kv.RwDB + blockDBs []kv.RwDB + concurrency int + protocol syncProtocol + isBeacon bool + logProgress bool +} + +func NewStageReceipts(cfg StageReceiptsCfg) *StageReceipts { + return &StageReceipts{ + configs: cfg, + } +} + +func NewStageReceiptsCfg(bc core.BlockChain, db kv.RwDB, blockDBs []kv.RwDB, concurrency int, protocol syncProtocol, isBeacon bool, logProgress bool) StageReceiptsCfg { + return StageReceiptsCfg{ + bc: bc, + db: db, + blockDBs: blockDBs, + concurrency: concurrency, + protocol: protocol, + isBeacon: isBeacon, + logProgress: logProgress, + } +} + +// Exec progresses receipts stage in the forward direction +func (r *StageReceipts) Exec(ctx context.Context, firstCycle bool, invalidBlockRevert bool, s *StageState, reverter Reverter, tx kv.RwTx) (err error) { + + // only execute this stage in fast/snap sync mode + if s.state.status.cycleSyncMode == FullSync { + return nil + } + + useInternalTx := tx == nil + + if invalidBlockRevert { + return nil + } + + // for short range sync, skip this stage + if !s.state.initSync { + return nil + } + + maxHeight := s.state.status.targetBN + currentHead := s.state.CurrentBlockNumber() + if currentHead >= maxHeight { + return nil + } + currProgress := uint64(0) + targetHeight := s.state.currentCycle.TargetHeight + + if errV := CreateView(ctx, r.configs.db, tx, func(etx kv.Tx) error { + if currProgress, err = s.CurrentStageProgress(etx); err != nil { + return err + } + return nil + }); errV != nil { + return errV + } + + if currProgress == 0 { + currProgress = currentHead + } + + if currProgress >= targetHeight { + return nil + } + + // size := uint64(0) + startTime := time.Now() + // startBlock := currProgress + + if r.configs.logProgress { + fmt.Print("\033[s") // save the cursor position + } + + if useInternalTx { + var err error + tx, err = r.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + for { + // check if there is no any more to download break the loop + curBn := s.state.CurrentBlockNumber() + if curBn == targetHeight { + break + } + + // calculate the block numbers range to download + toBn := curBn + uint64(ReceiptsPerRequest*s.state.config.Concurrency) + if toBn > targetHeight { + toBn = targetHeight + } + + // Fetch receipts from connected peers + rdm := newReceiptDownloadManager(tx, r.configs.bc, toBn, s.state.logger) + + // Setup workers to fetch blocks from remote node + var wg sync.WaitGroup + + for i := 0; i < s.state.config.Concurrency; i++ { + wg.Add(1) + go func() { + // prepare db transactions + txs := make([]kv.RwTx, r.configs.concurrency) + for i := 0; i < r.configs.concurrency; i++ { + txs[i], err = r.configs.blockDBs[i].BeginRw(ctx) + if err != nil { + return + } + } + // rollback the transactions after worker loop + defer func() { + for i := 0; i < r.configs.concurrency; i++ { + txs[i].Rollback() + } + }() + + r.runReceiptWorkerLoop(ctx, rdm, &wg, s, txs, startTime) + }() + } + wg.Wait() + // insert all downloaded blocks and receipts to chain + if err := r.insertBlocksAndReceipts(ctx, rdm, toBn, s); err != nil { + utils.Logger().Err(err).Msg(WrapStagedSyncMsg("InsertReceiptChain failed")) + } + } + + if useInternalTx { + if err := tx.Commit(); err != nil { + return err + } + } + + return nil +} + +func (r *StageReceipts) insertBlocksAndReceipts(ctx context.Context, rdm *receiptDownloadManager, toBn uint64, s *StageState) error { + if len(rdm.received) == 0 { + return nil + } + var ( + bns []uint64 + blocks []*types.Block + receipts []types.Receipts + streamIDs []sttypes.StreamID + ) + // populate blocks and receipts in separate array + // this way helps to sort blocks and receipts by block number + for bn := s.state.CurrentBlockNumber() + 1; bn <= toBn; bn++ { + if received, ok := rdm.received[bn]; !ok { + return errors.New("some blocks are missing") + } else { + bns = append(bns, bn) + blocks = append(blocks, received.block) + receipts = append(receipts, received.receipts) + streamIDs = append(streamIDs, received.streamID) + } + } + // insert sorted blocks and receipts to chain + if inserted, err := r.configs.bc.InsertReceiptChain(blocks, receipts); err != nil { + utils.Logger().Err(err). + Interface("streams", streamIDs). + Interface("block numbers", bns). + Msg(WrapStagedSyncMsg("InsertReceiptChain failed")) + rdm.HandleRequestError(bns, err) + return fmt.Errorf("InsertReceiptChain failed: %s", err.Error()) + } else { + if inserted != len(blocks) { + utils.Logger().Warn(). + Interface("block numbers", bns). + Int("inserted", inserted). + Int("blocks to insert", len(blocks)). + Msg(WrapStagedSyncMsg("InsertReceiptChain couldn't insert all downloaded blocks/receipts")) + } + } + return nil +} + +// runReceiptWorkerLoop creates a work loop for download receipts +func (r *StageReceipts) runReceiptWorkerLoop(ctx context.Context, rdm *receiptDownloadManager, wg *sync.WaitGroup, s *StageState, txs []kv.RwTx, startTime time.Time) { + + currentBlock := int(s.state.CurrentBlockNumber()) + gbm := s.state.gbm + + defer wg.Done() + + for { + select { + case <-ctx.Done(): + return + default: + } + // get next batch of block numbers + curHeight := s.state.CurrentBlockNumber() + batch := rdm.GetNextBatch(curHeight) + if len(batch) == 0 { + select { + case <-ctx.Done(): + return + case <-time.After(100 * time.Millisecond): + return + } + } + // retrieve corresponding blocks from cache db + var hashes []common.Hash + var blocks []*types.Block + + for _, bn := range batch { + blkKey := marshalData(bn) + loopID, _, errBDD := gbm.GetDownloadDetails(bn) + if errBDD != nil { + utils.Logger().Warn(). + Err(errBDD). + Interface("block numbers", bn). + Msg(WrapStagedSyncMsg("get block download details failed")) + return + } + blockBytes, err := txs[loopID].GetOne(BlocksBucket, blkKey) + if err != nil { + return + } + sigBytes, err := txs[loopID].GetOne(BlockSignaturesBucket, blkKey) + if err != nil { + return + } + sz := len(blockBytes) + if sz <= 1 { + return + } + var block *types.Block + if err := rlp.DecodeBytes(blockBytes, &block); err != nil { + return + } + if sigBytes != nil { + block.SetCurrentCommitSig(sigBytes) + } + if block.NumberU64() != bn { + return + } + if block.Header().ReceiptHash() == emptyHash { + return + } + // receiptHash := s.state.currentCycle.ReceiptHashes[bn] + gbm.SetRootHash(bn, block.Header().Root()) + hashes = append(hashes, block.Header().Hash()) + blocks = append(blocks, block) + } + + // download receipts + receipts, stid, err := r.downloadReceipts(ctx, hashes) + if err != nil { + if !errors.Is(err, context.Canceled) { + r.configs.protocol.StreamFailed(stid, "downloadRawBlocks failed") + } + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Interface("block numbers", batch). + Msg(WrapStagedSyncMsg("downloadRawBlocks failed")) + err = errors.Wrap(err, "request error") + rdm.HandleRequestError(batch, err) + } else { + // handle request result + rdm.HandleRequestResult(batch, receipts, blocks, stid) + // log progress + if r.configs.logProgress { + //calculating block download speed + dt := time.Now().Sub(startTime).Seconds() + speed := float64(0) + if dt > 0 { + speed = float64(len(rdm.rdd)) / dt + } + blockReceiptSpeed := fmt.Sprintf("%.2f", speed) + + fmt.Print("\033[u\033[K") // restore the cursor position and clear the line + fmt.Println("downloaded blocks and receipts:", currentBlock+len(rdm.rdd), "/", int(rdm.targetBN), "(", blockReceiptSpeed, "BlocksAndReceipts/s", ")") + } + } + } +} + +func (r *StageReceipts) downloadReceipts(ctx context.Context, hs []common.Hash) ([]types.Receipts, sttypes.StreamID, error) { + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + + receipts, stid, err := r.configs.protocol.GetReceipts(ctx, hs) + if err != nil { + return nil, stid, err + } + if err := validateGetReceiptsResult(hs, receipts); err != nil { + return nil, stid, err + } + return receipts, stid, nil +} + +func validateGetReceiptsResult(requested []common.Hash, result []types.Receipts) error { + // TODO: validate each receipt here + + return nil +} + +func (r *StageReceipts) saveProgress(ctx context.Context, s *StageState, progress uint64, tx kv.RwTx) (err error) { + useInternalTx := tx == nil + if useInternalTx { + var err error + tx, err = r.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + // save progress + if err = s.Update(tx, progress); err != nil { + utils.Logger().Error(). + Err(err). + Msgf("[STAGED_SYNC] saving progress for receipt stage failed") + return ErrSavingBodiesProgressFail + } + + if useInternalTx { + if err := tx.Commit(); err != nil { + return err + } + } + return nil +} + +func (r *StageReceipts) Revert(ctx context.Context, firstCycle bool, u *RevertState, s *StageState, tx kv.RwTx) (err error) { + useInternalTx := tx == nil + if useInternalTx { + tx, err = r.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + if err = u.Done(tx); err != nil { + return err + } + + if useInternalTx { + if err = tx.Commit(); err != nil { + return err + } + } + return nil +} + +func (r *StageReceipts) CleanUp(ctx context.Context, firstCycle bool, p *CleanUpState, tx kv.RwTx) (err error) { + useInternalTx := tx == nil + if useInternalTx { + tx, err = r.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + if useInternalTx { + if err = tx.Commit(); err != nil { + return err + } + } + return nil +} diff --git a/api/service/stagedstreamsync/stage_short_range.go b/api/service/stagedstreamsync/stage_short_range.go index ce6cdf36b..d771cd660 100644 --- a/api/service/stagedstreamsync/stage_short_range.go +++ b/api/service/stagedstreamsync/stage_short_range.go @@ -136,6 +136,8 @@ func (sr *StageShortRange) doShortRangeSync(ctx context.Context, s *StageState) sh.streamsFailed(whitelist, "remote nodes cannot provide blocks with target hashes") } + utils.Logger().Info().Int("num blocks", len(blocks)).Msg("getBlockByHashes result") + n, err := verifyAndInsertBlocks(sr.configs.bc, blocks) numBlocksInsertedShortRangeHistogramVec.With(s.state.promLabels()).Observe(float64(n)) if err != nil { diff --git a/api/service/stagedstreamsync/stage_state.go b/api/service/stagedstreamsync/stage_state.go index b8dfb1828..df864d63f 100644 --- a/api/service/stagedstreamsync/stage_state.go +++ b/api/service/stagedstreamsync/stage_state.go @@ -53,6 +53,11 @@ func NewStageStatesCfg( // Exec progresses States stage in the forward direction func (stg *StageStates) Exec(ctx context.Context, firstCycle bool, invalidBlockRevert bool, s *StageState, reverter Reverter, tx kv.RwTx) (err error) { + // only execute this stage in full sync mode + if s.state.status.cycleSyncMode != FullSync { + return nil + } + // for short range sync, skip this step if !s.state.initSync { return nil @@ -64,11 +69,11 @@ func (stg *StageStates) Exec(ctx context.Context, firstCycle bool, invalidBlockR } maxHeight := s.state.status.targetBN - currentHead := stg.configs.bc.CurrentBlock().NumberU64() + currentHead := s.state.CurrentBlockNumber() if currentHead >= maxHeight { return nil } - currProgress := stg.configs.bc.CurrentBlock().NumberU64() + currProgress := currentHead targetHeight := s.state.currentCycle.TargetHeight if currProgress >= targetHeight { return nil @@ -110,7 +115,10 @@ func (stg *StageStates) Exec(ctx context.Context, firstCycle bool, invalidBlockR for i := currProgress + 1; i <= targetHeight; i++ { blkKey := marshalData(i) - loopID, streamID := gbm.GetDownloadDetails(i) + loopID, streamID, errBDD := gbm.GetDownloadDetails(i) + if errBDD != nil { + return errBDD + } blockBytes, err := txs[loopID].GetOne(BlocksBucket, blkKey) if err != nil { diff --git a/api/service/stagedstreamsync/stage_statesync.go b/api/service/stagedstreamsync/stage_statesync.go new file mode 100644 index 000000000..c4e66e10e --- /dev/null +++ b/api/service/stagedstreamsync/stage_statesync.go @@ -0,0 +1,310 @@ +package stagedstreamsync + +import ( + "context" + "fmt" + "sync" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/harmony-one/harmony/core" + "github.com/harmony-one/harmony/internal/utils" + sttypes "github.com/harmony-one/harmony/p2p/stream/types" + "github.com/ledgerwatch/erigon-lib/kv" + "github.com/pkg/errors" + "github.com/prometheus/client_golang/prometheus" + "github.com/rs/zerolog" +) + +type StageStateSync struct { + configs StageStateSyncCfg +} + +type StageStateSyncCfg struct { + bc core.BlockChain + db kv.RwDB + concurrency int + protocol syncProtocol + logger zerolog.Logger + logProgress bool +} + +func NewStageStateSync(cfg StageStateSyncCfg) *StageStateSync { + return &StageStateSync{ + configs: cfg, + } +} + +func NewStageStateSyncCfg(bc core.BlockChain, + db kv.RwDB, + concurrency int, + protocol syncProtocol, + logger zerolog.Logger, + logProgress bool) StageStateSyncCfg { + + return StageStateSyncCfg{ + bc: bc, + db: db, + concurrency: concurrency, + protocol: protocol, + logger: logger, + logProgress: logProgress, + } +} + +// Exec progresses States stage in the forward direction +func (sss *StageStateSync) Exec(ctx context.Context, bool, invalidBlockRevert bool, s *StageState, reverter Reverter, tx kv.RwTx) (err error) { + + // for short range sync, skip this step + if !s.state.initSync { + return nil + } // only execute this stage in fast/snap sync mode and once we reach to pivot + + if s.state.status.pivotBlock == nil || + s.state.CurrentBlockNumber() != s.state.status.pivotBlock.NumberU64() || + s.state.status.statesSynced { + return nil + } + + // maxHeight := s.state.status.targetBN + // currentHead := s.state.CurrentBlockNumber() + // if currentHead >= maxHeight { + // return nil + // } + // currProgress := s.state.CurrentBlockNumber() + // targetHeight := s.state.currentCycle.TargetHeight + + // if errV := CreateView(ctx, sss.configs.db, tx, func(etx kv.Tx) error { + // if currProgress, err = s.CurrentStageProgress(etx); err != nil { + // return err + // } + // return nil + // }); errV != nil { + // return errV + // } + + // if currProgress >= targetHeight { + // return nil + // } + useInternalTx := tx == nil + if useInternalTx { + var err error + tx, err = sss.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + // isLastCycle := targetHeight >= maxHeight + startTime := time.Now() + + if sss.configs.logProgress { + fmt.Print("\033[s") // save the cursor position + } + + // Fetch states from neighbors + // pivotRootHash := s.state.status.pivotBlock.Root() + currentBlockRootHash := s.state.bc.CurrentFastBlock().Root() + sdm := newStateDownloadManager(tx, sss.configs.bc, sss.configs.concurrency, s.state.logger) + sdm.setRootHash(currentBlockRootHash) + var wg sync.WaitGroup + for i := 0; i < s.state.config.Concurrency; i++ { + wg.Add(1) + go sss.runStateWorkerLoop(ctx, sdm, &wg, i, startTime, s) + } + wg.Wait() + + // insert block + if err := sss.configs.bc.WriteHeadBlock(s.state.status.pivotBlock); err != nil { + sss.configs.logger.Warn().Err(err). + Uint64("pivot block number", s.state.status.pivotBlock.NumberU64()). + Msg(WrapStagedSyncMsg("insert pivot block failed")) + // TODO: panic("pivot block is failed to insert in chain.") + return err + } + + // states should be fully synced in this stage + s.state.status.statesSynced = true + + /* + gbm := s.state.gbm + + // Setup workers to fetch states from remote node + var wg sync.WaitGroup + curHeight := s.state.CurrentBlockNumber() + + for bn := curHeight + 1; bn <= gbm.targetBN; bn++ { + root := gbm.GetRootHash(bn) + if root == emptyHash { + continue + } + sdm.setRootHash(root) + for i := 0; i < s.state.config.Concurrency; i++ { + wg.Add(1) + go sss.runStateWorkerLoop(ctx, sdm, &wg, i, startTime, s) + } + wg.Wait() + } + */ + + if useInternalTx { + if err := tx.Commit(); err != nil { + return err + } + } + + return nil +} + +// runStateWorkerLoop creates a work loop for download states +func (sss *StageStateSync) runStateWorkerLoop(ctx context.Context, sdm *StateDownloadManager, wg *sync.WaitGroup, loopID int, startTime time.Time, s *StageState) { + + defer wg.Done() + + for { + select { + case <-ctx.Done(): + return + default: + } + nodes, paths, codes, err := sdm.GetNextBatch() + if len(nodes)+len(codes) == 0 || err != nil { + select { + case <-ctx.Done(): + return + case <-time.After(100 * time.Millisecond): + return + } + } + data, stid, err := sss.downloadStates(ctx, nodes, codes) + if err != nil { + if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + sss.configs.protocol.StreamFailed(stid, "downloadStates failed") + } + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("downloadStates failed")) + err = errors.Wrap(err, "request error") + sdm.HandleRequestError(codes, paths, stid, err) + } else if data == nil || len(data) == 0 { + utils.Logger().Warn(). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("downloadStates failed, received empty data bytes")) + err := errors.New("downloadStates received empty data bytes") + sdm.HandleRequestError(codes, paths, stid, err) + } else { + sdm.HandleRequestResult(nodes, paths, data, loopID, stid) + if sss.configs.logProgress { + //calculating block download speed + dt := time.Now().Sub(startTime).Seconds() + speed := float64(0) + if dt > 0 { + speed = float64(len(data)) / dt + } + stateDownloadSpeed := fmt.Sprintf("%.2f", speed) + + fmt.Print("\033[u\033[K") // restore the cursor position and clear the line + fmt.Println("state download speed:", stateDownloadSpeed, "states/s") + } + } + } +} + +func (sss *StageStateSync) downloadStates(ctx context.Context, nodes []common.Hash, codes []common.Hash) ([][]byte, sttypes.StreamID, error) { + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + + hashes := append(codes, nodes...) + data, stid, err := sss.configs.protocol.GetNodeData(ctx, hashes) + if err != nil { + return nil, stid, err + } + if err := validateGetNodeDataResult(hashes, data); err != nil { + return nil, stid, err + } + return data, stid, nil +} + +func validateGetNodeDataResult(requested []common.Hash, result [][]byte) error { + if len(result) != len(requested) { + return fmt.Errorf("unexpected number of nodes delivered: %v / %v", len(result), len(requested)) + } + return nil +} + +func (stg *StageStateSync) insertChain(gbm *blockDownloadManager, + protocol syncProtocol, + lbls prometheus.Labels, + targetBN uint64) { + +} + +func (stg *StageStateSync) saveProgress(s *StageState, tx kv.RwTx) (err error) { + + useInternalTx := tx == nil + if useInternalTx { + var err error + tx, err = stg.configs.db.BeginRw(context.Background()) + if err != nil { + return err + } + defer tx.Rollback() + } + + // save progress + if err = s.Update(tx, s.state.CurrentBlockNumber()); err != nil { + utils.Logger().Error(). + Err(err). + Msgf("[STAGED_SYNC] saving progress for block States stage failed") + return ErrSaveStateProgressFail + } + + if useInternalTx { + if err := tx.Commit(); err != nil { + return err + } + } + return nil +} + +func (stg *StageStateSync) Revert(ctx context.Context, firstCycle bool, u *RevertState, s *StageState, tx kv.RwTx) (err error) { + useInternalTx := tx == nil + if useInternalTx { + tx, err = stg.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + if err = u.Done(tx); err != nil { + return err + } + + if useInternalTx { + if err = tx.Commit(); err != nil { + return err + } + } + return nil +} + +func (stg *StageStateSync) CleanUp(ctx context.Context, firstCycle bool, p *CleanUpState, tx kv.RwTx) (err error) { + useInternalTx := tx == nil + if useInternalTx { + tx, err = stg.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + if useInternalTx { + if err = tx.Commit(); err != nil { + return err + } + } + return nil +} diff --git a/api/service/stagedstreamsync/stage_statesync_full.go b/api/service/stagedstreamsync/stage_statesync_full.go new file mode 100644 index 000000000..d304ca1c3 --- /dev/null +++ b/api/service/stagedstreamsync/stage_statesync_full.go @@ -0,0 +1,469 @@ +package stagedstreamsync + +import ( + "context" + "fmt" + "sync" + "time" + + "github.com/harmony-one/harmony/core" + "github.com/harmony-one/harmony/internal/utils" + sttypes "github.com/harmony-one/harmony/p2p/stream/types" + "github.com/pkg/errors" + + //sttypes "github.com/harmony-one/harmony/p2p/stream/types" + "github.com/ledgerwatch/erigon-lib/kv" + "github.com/prometheus/client_golang/prometheus" + "github.com/rs/zerolog" +) + +type StageFullStateSync struct { + configs StageFullStateSyncCfg +} + +type StageFullStateSyncCfg struct { + bc core.BlockChain + db kv.RwDB + concurrency int + protocol syncProtocol + logger zerolog.Logger + logProgress bool +} + +func NewStageFullStateSync(cfg StageFullStateSyncCfg) *StageFullStateSync { + return &StageFullStateSync{ + configs: cfg, + } +} + +func NewStageFullStateSyncCfg(bc core.BlockChain, + db kv.RwDB, + concurrency int, + protocol syncProtocol, + logger zerolog.Logger, + logProgress bool) StageFullStateSyncCfg { + + return StageFullStateSyncCfg{ + bc: bc, + db: db, + concurrency: concurrency, + protocol: protocol, + logger: logger, + logProgress: logProgress, + } +} + +// Exec progresses States stage in the forward direction +func (sss *StageFullStateSync) Exec(ctx context.Context, bool, invalidBlockRevert bool, s *StageState, reverter Reverter, tx kv.RwTx) (err error) { + + // for short range sync, skip this step + if !s.state.initSync { + return nil + } // only execute this stage in fast/snap sync mode and once we reach to pivot + + if s.state.status.pivotBlock == nil || + s.state.CurrentBlockNumber() != s.state.status.pivotBlock.NumberU64() || + s.state.status.statesSynced { + return nil + } + + // maxHeight := s.state.status.targetBN + // currentHead := s.state.CurrentBlockNumber() + // if currentHead >= maxHeight { + // return nil + // } + // currProgress := s.state.CurrentBlockNumber() + // targetHeight := s.state.currentCycle.TargetHeight + + // if errV := CreateView(ctx, sss.configs.db, tx, func(etx kv.Tx) error { + // if currProgress, err = s.CurrentStageProgress(etx); err != nil { + // return err + // } + // return nil + // }); errV != nil { + // return errV + // } + + // if currProgress >= targetHeight { + // return nil + // } + useInternalTx := tx == nil + if useInternalTx { + var err error + tx, err = sss.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + // isLastCycle := targetHeight >= maxHeight + startTime := time.Now() + + if sss.configs.logProgress { + fmt.Print("\033[s") // save the cursor position + } + + // Fetch states from neighbors + currentBlockRootHash := s.state.bc.CurrentFastBlock().Root() + scheme := sss.configs.bc.TrieDB().Scheme() + sdm := newFullStateDownloadManager(sss.configs.bc.ChainDb(), scheme, tx, sss.configs.bc, sss.configs.concurrency, s.state.logger) + sdm.setRootHash(currentBlockRootHash) + var wg sync.WaitGroup + for i := 0; i < s.state.config.Concurrency; i++ { + wg.Add(1) + go sss.runStateWorkerLoop(ctx, sdm, &wg, i, startTime, s) + } + wg.Wait() + + // insert block + if err := sss.configs.bc.WriteHeadBlock(s.state.status.pivotBlock); err != nil { + sss.configs.logger.Warn().Err(err). + Uint64("pivot block number", s.state.status.pivotBlock.NumberU64()). + Msg(WrapStagedSyncMsg("insert pivot block failed")) + // TODO: panic("pivot block is failed to insert in chain.") + return err + } + + // states should be fully synced in this stage + s.state.status.statesSynced = true + + /* + gbm := s.state.gbm + + // Setup workers to fetch states from remote node + var wg sync.WaitGroup + curHeight := s.state.CurrentBlockNumber() + + for bn := curHeight + 1; bn <= gbm.targetBN; bn++ { + root := gbm.GetRootHash(bn) + if root == emptyHash { + continue + } + sdm.setRootHash(root) + for i := 0; i < s.state.config.Concurrency; i++ { + wg.Add(1) + go sss.runStateWorkerLoop(ctx, sdm, &wg, i, startTime, s) + } + wg.Wait() + } + */ + + if useInternalTx { + if err := tx.Commit(); err != nil { + return err + } + } + + return nil +} + +// runStateWorkerLoop creates a work loop for download states +func (sss *StageFullStateSync) runStateWorkerLoop(ctx context.Context, sdm *FullStateDownloadManager, wg *sync.WaitGroup, loopID int, startTime time.Time, s *StageState) { + + defer wg.Done() + + for { + select { + case <-ctx.Done(): + return + default: + } + accountTasks, codes, storages, healtask, codetask, err := sdm.GetNextBatch() + if len(accountTasks)+len(codes)+len(storages.accounts)+len(healtask.hashes)+len(codetask.hashes) == 0 || err != nil { + select { + case <-ctx.Done(): + return + case <-time.After(100 * time.Millisecond): + return + } + } + + if len(accountTasks) > 0 { + + task := accountTasks[0] + origin := task.Next + limit := task.Last + root := sdm.root + cap := maxRequestSize + retAccounts, proof, stid, err := sss.configs.protocol.GetAccountRange(ctx, root, origin, limit, uint64(cap)) + if err != nil { + if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + sss.configs.protocol.StreamFailed(stid, "GetAccountRange failed") + } + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetAccountRange failed")) + err = errors.Wrap(err, "request error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } else if retAccounts == nil || len(retAccounts) == 0 { + utils.Logger().Warn(). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetAccountRange failed, received empty accounts")) + err := errors.New("GetAccountRange received empty slots") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + if err := sdm.HandleAccountRequestResult(task, retAccounts, proof, origin[:], limit[:], loopID, stid); err != nil { + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetAccountRange handle result failed")) + err = errors.Wrap(err, "handle result error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + + } else if len(codes) > 0 { + + stid, err := sss.downloadByteCodes(ctx, sdm, codes, loopID) + if err != nil { + if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + sss.configs.protocol.StreamFailed(stid, "downloadByteCodes failed") + } + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("downloadByteCodes failed")) + err = errors.Wrap(err, "request error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + + } else if len(storages.accounts) > 0 { + + root := sdm.root + roots := storages.roots + accounts := storages.accounts + cap := maxRequestSize + origin := storages.origin + limit := storages.limit + mainTask := storages.mainTask + subTask := storages.subtask + + slots, proof, stid, err := sss.configs.protocol.GetStorageRanges(ctx, root, accounts, origin, limit, uint64(cap)) + if err != nil { + if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + sss.configs.protocol.StreamFailed(stid, "GetStorageRanges failed") + } + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetStorageRanges failed")) + err = errors.Wrap(err, "request error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } else if slots == nil || len(slots) == 0 { + utils.Logger().Warn(). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetStorageRanges failed, received empty slots")) + err := errors.New("GetStorageRanges received empty slots") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + if err := sdm.HandleStorageRequestResult(mainTask, subTask, accounts, roots, origin, limit, slots, proof, loopID, stid); err != nil { + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetStorageRanges handle result failed")) + err = errors.Wrap(err, "handle result error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + + } else { + // assign trie node Heal Tasks + if len(healtask.hashes) > 0 { + root := sdm.root + task := healtask.task + hashes := healtask.hashes + pathsets := healtask.pathsets + paths := healtask.paths + + nodes, stid, err := sss.configs.protocol.GetTrieNodes(ctx, root, pathsets, maxRequestSize) + if err != nil { + if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + sss.configs.protocol.StreamFailed(stid, "GetTrieNodes failed") + } + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetTrieNodes failed")) + err = errors.Wrap(err, "request error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } else if nodes == nil || len(nodes) == 0 { + utils.Logger().Warn(). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetTrieNodes failed, received empty nodes")) + err := errors.New("GetTrieNodes received empty nodes") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + if err := sdm.HandleTrieNodeHealRequestResult(task, paths, hashes, nodes, loopID, stid); err != nil { + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetTrieNodes handle result failed")) + err = errors.Wrap(err, "handle result error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + } + + if len(codetask.hashes) > 0 { + task := codetask.task + hashes := codetask.hashes + retCodes, stid, err := sss.configs.protocol.GetByteCodes(ctx, hashes, maxRequestSize) + if err != nil { + if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { + sss.configs.protocol.StreamFailed(stid, "GetByteCodes failed") + } + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetByteCodes failed")) + err = errors.Wrap(err, "request error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } else if retCodes == nil || len(retCodes) == 0 { + utils.Logger().Warn(). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetByteCodes failed, received empty codes")) + err := errors.New("GetByteCodes received empty codes") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + if err := sdm.HandleBytecodeRequestResult(task, hashes, retCodes, loopID, stid); err != nil { + utils.Logger().Error(). + Err(err). + Str("stream", string(stid)). + Msg(WrapStagedSyncMsg("GetByteCodes handle result failed")) + err = errors.Wrap(err, "handle result error") + sdm.HandleRequestError(accountTasks, codes, storages, healtask, codetask, stid, err) + return + } + } + } + } +} + +func (sss *StageFullStateSync) downloadByteCodes(ctx context.Context, sdm *FullStateDownloadManager, codeTasks []*byteCodeTasksBundle, loopID int) (stid sttypes.StreamID, err error) { + for _, codeTask := range codeTasks { + // try to get byte codes from remote peer + // if any of them failed, the stid will be the id of the failed stream + retCodes, stid, err := sss.configs.protocol.GetByteCodes(ctx, codeTask.hashes, maxRequestSize) + if err != nil { + return stid, err + } + if len(retCodes) == 0 { + return stid, errors.New("empty codes array") + } + if err = sdm.HandleBytecodeRequestResult(codeTask.task, codeTask.hashes, retCodes, loopID, stid); err != nil { + return stid, err + } + } + return +} + +// func (sss *StageFullStateSync) downloadStates(ctx context.Context, +// root common.Hash, +// origin common.Hash, +// accounts []*accountTask, +// codes []common.Hash, +// storages *storageTaskBundle) ([][]byte, sttypes.StreamID, error) { + +// ctx, cancel := context.WithTimeout(ctx, 10*time.Second) +// defer cancel() + +// // if there is any account task, first we have to complete that +// if len(accounts) > 0 { + +// } +// // hashes := append(codes, nodes...) +// // data, stid, err := sss.configs.protocol.GetNodeData(ctx, hashes) +// // if err != nil { +// // return nil, stid, err +// // } +// // if err := validateGetNodeDataResult(hashes, data); err != nil { +// // return nil, stid, err +// // } +// return data, stid, nil +// } + +func (stg *StageFullStateSync) insertChain(gbm *blockDownloadManager, + protocol syncProtocol, + lbls prometheus.Labels, + targetBN uint64) { + +} + +func (stg *StageFullStateSync) saveProgress(s *StageState, tx kv.RwTx) (err error) { + + useInternalTx := tx == nil + if useInternalTx { + var err error + tx, err = stg.configs.db.BeginRw(context.Background()) + if err != nil { + return err + } + defer tx.Rollback() + } + + // save progress + if err = s.Update(tx, s.state.CurrentBlockNumber()); err != nil { + utils.Logger().Error(). + Err(err). + Msgf("[STAGED_SYNC] saving progress for block States stage failed") + return ErrSaveStateProgressFail + } + + if useInternalTx { + if err := tx.Commit(); err != nil { + return err + } + } + return nil +} + +func (stg *StageFullStateSync) Revert(ctx context.Context, firstCycle bool, u *RevertState, s *StageState, tx kv.RwTx) (err error) { + useInternalTx := tx == nil + if useInternalTx { + tx, err = stg.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + if err = u.Done(tx); err != nil { + return err + } + + if useInternalTx { + if err = tx.Commit(); err != nil { + return err + } + } + return nil +} + +func (stg *StageFullStateSync) CleanUp(ctx context.Context, firstCycle bool, p *CleanUpState, tx kv.RwTx) (err error) { + useInternalTx := tx == nil + if useInternalTx { + tx, err = stg.configs.db.BeginRw(ctx) + if err != nil { + return err + } + defer tx.Rollback() + } + + if useInternalTx { + if err = tx.Commit(); err != nil { + return err + } + } + return nil +} diff --git a/api/service/stagedstreamsync/staged_stream_sync.go b/api/service/stagedstreamsync/staged_stream_sync.go index 0a14d0cb3..03340eb15 100644 --- a/api/service/stagedstreamsync/staged_stream_sync.go +++ b/api/service/stagedstreamsync/staged_stream_sync.go @@ -59,23 +59,22 @@ func (ib *InvalidBlock) addBadStream(bsID sttypes.StreamID) { } type StagedStreamSync struct { - bc core.BlockChain - consensus *consensus.Consensus - isBeacon bool - isExplorer bool - db kv.RwDB - protocol syncProtocol - isBeaconNode bool - gbm *blockDownloadManager // initialized when finished get block number - lastMileBlocks []*types.Block // last mile blocks to catch up with the consensus - lastMileMux sync.Mutex - inserted int - config Config - logger zerolog.Logger - status *status //TODO: merge this with currentSyncCycle - initSync bool // if sets to true, node start long range syncing - UseMemDB bool - + bc core.BlockChain + consensus *consensus.Consensus + isBeacon bool + isExplorer bool + db kv.RwDB + protocol syncProtocol + isBeaconNode bool + gbm *blockDownloadManager // initialized when finished get block number + lastMileBlocks []*types.Block // last mile blocks to catch up with the consensus + lastMileMux sync.Mutex + inserted int + config Config + logger zerolog.Logger + status *status //TODO: merge this with currentSyncCycle + initSync bool // if sets to true, node start long range syncing + UseMemDB bool revertPoint *uint64 // used to run stages prevRevertPoint *uint64 // used to get value from outside of staged sync after cycle (for example to notify RPCDaemon) invalidBlock InvalidBlock @@ -267,8 +266,18 @@ func New( logger zerolog.Logger, ) *StagedStreamSync { - revertStages := make([]*Stage, len(stagesList)) - for i, stageIndex := range DefaultRevertOrder { + forwardStages := make([]*Stage, len(StagesForwardOrder)) + for i, stageIndex := range StagesForwardOrder { + for _, s := range stagesList { + if s.ID == stageIndex { + forwardStages[i] = s + break + } + } + } + + revertStages := make([]*Stage, len(StagesRevertOrder)) + for i, stageIndex := range StagesRevertOrder { for _, s := range stagesList { if s.ID == stageIndex { revertStages[i] = s @@ -276,8 +285,9 @@ func New( } } } - pruneStages := make([]*Stage, len(stagesList)) - for i, stageIndex := range DefaultCleanUpOrder { + + pruneStages := make([]*Stage, len(StagesCleanUpOrder)) + for i, stageIndex := range StagesCleanUpOrder { for _, s := range stagesList { if s.ID == stageIndex { pruneStages[i] = s @@ -306,7 +316,7 @@ func New( inserted: 0, config: config, logger: logger, - stages: stagesList, + stages: forwardStages, currentStage: 0, revertOrder: revertStages, pruningOrder: pruneStages, @@ -327,6 +337,18 @@ func (s *StagedStreamSync) doGetCurrentNumberRequest(ctx context.Context) (uint6 return bn, stid, nil } +// doGetBlockByNumberRequest returns block by its number and corresponding stream +func (s *StagedStreamSync) doGetBlockByNumberRequest(ctx context.Context, bn uint64) (*types.Block, sttypes.StreamID, error) { + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + + blocks, stid, err := s.protocol.GetBlocksByNumber(ctx, []uint64{bn}, syncproto.WithHighPriority()) + if err != nil || len(blocks) != 1 { + return nil, stid, err + } + return blocks[0], stid, nil +} + // promLabels returns a prometheus labels for current shard id func (s *StagedStreamSync) promLabels() prometheus.Labels { sid := s.bc.ShardID() @@ -472,7 +494,6 @@ func (s *StagedStreamSync) runStage(ctx context.Context, stage *Stage, db kv.RwD if err != nil { return err } - if err = stage.Handler.Exec(ctx, firstCycle, invalidBlockRevert, stageState, s, tx); err != nil { utils.Logger().Error(). Err(err). diff --git a/api/service/stagedstreamsync/stages.go b/api/service/stagedstreamsync/stages.go index 6a21fe707..6ad9e4519 100644 --- a/api/service/stagedstreamsync/stages.go +++ b/api/service/stagedstreamsync/stages.go @@ -13,6 +13,8 @@ const ( SyncEpoch SyncStageID = "SyncEpoch" // epoch sync BlockBodies SyncStageID = "BlockBodies" // Block bodies are downloaded, TxHash and UncleHash are getting verified States SyncStageID = "States" // will construct most recent state from downloaded blocks + StateSync SyncStageID = "StateSync" // State sync + Receipts SyncStageID = "Receipts" // Receipts LastMile SyncStageID = "LastMile" // update blocks after sync and update last mile blocks as well Finish SyncStageID = "Finish" // Nominal stage after all other stages ) diff --git a/api/service/stagedstreamsync/state_download_manager.go b/api/service/stagedstreamsync/state_download_manager.go new file mode 100644 index 000000000..51eccb8ec --- /dev/null +++ b/api/service/stagedstreamsync/state_download_manager.go @@ -0,0 +1,432 @@ +package stagedstreamsync + +import ( + "fmt" + "sync" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/trie" + "github.com/harmony-one/harmony/core" + "github.com/harmony-one/harmony/core/rawdb" + "github.com/harmony-one/harmony/core/state" + "github.com/harmony-one/harmony/internal/utils" + sttypes "github.com/harmony-one/harmony/p2p/stream/types" + "github.com/ledgerwatch/erigon-lib/kv" + "github.com/rs/zerolog" + "golang.org/x/crypto/sha3" +) + +// codeTask represents a single byte code download task, containing a set of +// peers already attempted retrieval from to detect stalled syncs and abort. +type codeTask struct { + attempts map[sttypes.StreamID]int +} + +// trieTask represents a single trie node download task, containing a set of +// peers already attempted retrieval from to detect stalled syncs and abort. +type trieTask struct { + hash common.Hash + path [][]byte + attempts map[sttypes.StreamID]int +} + +type task struct { + trieTasks map[string]*trieTask // Set of trie node tasks currently queued for retrieval, indexed by path + codeTasks map[common.Hash]*codeTask // Set of byte code tasks currently queued for retrieval, indexed by hash +} + +func newTask() *task { + return &task{ + trieTasks: make(map[string]*trieTask), + codeTasks: make(map[common.Hash]*codeTask), + } +} + +func (t *task) addCodeTask(h common.Hash, ct *codeTask) { + t.codeTasks[h] = &codeTask{ + attempts: ct.attempts, + } +} + +func (t *task) getCodeTask(h common.Hash) *codeTask { + if task, ok := t.codeTasks[h]; ok { + return task + } + return nil +} + +func (t *task) addNewCodeTask(h common.Hash) { + t.codeTasks[h] = &codeTask{ + attempts: make(map[sttypes.StreamID]int), + } +} + +func (t *task) deleteCodeTask(hash common.Hash) { + if _, ok := t.codeTasks[hash]; ok { + delete(t.codeTasks, hash) + } +} + +func (t *task) deleteCodeTaskAttempts(h common.Hash, stID sttypes.StreamID) { + if task, ok := t.codeTasks[h]; ok { + if _, ok := task.attempts[stID]; ok { + delete(t.codeTasks[h].attempts, stID) + } + } +} + +func (t *task) addTrieTask(path string, tt *trieTask) { + t.trieTasks[path] = &trieTask{ + hash: tt.hash, + path: tt.path, + attempts: tt.attempts, + } +} + +func (t *task) getTrieTask(path string) *trieTask { + if task, ok := t.trieTasks[path]; ok { + return task + } + return nil +} + +func (t *task) addNewTrieTask(hash common.Hash, path string) { + t.trieTasks[path] = &trieTask{ + hash: hash, + path: trie.NewSyncPath([]byte(path)), + attempts: make(map[sttypes.StreamID]int), + } +} + +func (t *task) deleteTrieTask(path string) { + if _, ok := t.trieTasks[path]; ok { + delete(t.trieTasks, path) + } +} + +func (t *task) deleteTrieTaskAttempts(path string, stID sttypes.StreamID) { + if task, ok := t.trieTasks[path]; ok { + if _, ok := task.attempts[stID]; ok { + delete(t.trieTasks[path].attempts, stID) + } + } +} + +// StateDownloadManager is the helper structure for get blocks request management +type StateDownloadManager struct { + bc core.BlockChain + tx kv.RwTx + + protocol syncProtocol + root common.Hash // State root currently being synced + sched *trie.Sync // State trie sync scheduler defining the tasks + keccak crypto.KeccakState // Keccak256 hasher to verify deliveries with + concurrency int + logger zerolog.Logger + lock sync.Mutex + + numUncommitted int + bytesUncommitted int + + tasks *task + requesting *task + processing *task + retries *task +} + +func newStateDownloadManager(tx kv.RwTx, + bc core.BlockChain, + concurrency int, + logger zerolog.Logger) *StateDownloadManager { + + return &StateDownloadManager{ + bc: bc, + tx: tx, + keccak: sha3.NewLegacyKeccak256().(crypto.KeccakState), + concurrency: concurrency, + logger: logger, + tasks: newTask(), + requesting: newTask(), + processing: newTask(), + retries: newTask(), + } +} + +func (s *StateDownloadManager) setRootHash(root common.Hash) { + s.root = root + s.sched = state.NewStateSync(root, s.bc.ChainDb(), nil, rawdb.HashScheme) +} + +// fillTasks fills the tasks to send to the remote peer. +func (s *StateDownloadManager) fillTasks(n int) error { + if fill := n - (len(s.tasks.trieTasks) + len(s.tasks.codeTasks)); fill > 0 { + paths, hashes, codes := s.sched.Missing(fill) + for i, path := range paths { + s.tasks.addNewTrieTask(hashes[i], path) + } + for _, hash := range codes { + s.tasks.addNewCodeTask(hash) + } + } + return nil +} + +// getNextBatch returns objects with a maximum of n state download +// tasks to send to the remote peer. +func (s *StateDownloadManager) GetNextBatch() (nodes []common.Hash, paths []string, codes []common.Hash, err error) { + s.lock.Lock() + defer s.lock.Unlock() + + cap := StatesPerRequest + + nodes, paths, codes = s.getBatchFromRetries(cap) + nItems := len(nodes) + len(codes) + cap -= nItems + + if cap > 0 { + // Refill available tasks from the scheduler. + if s.sched.Pending() == 0 { + return + } + + if err = s.commit(false); err != nil { + return + } + + if err = s.fillTasks(cap); err != nil { + return + } + newNodes, newPaths, newCodes := s.getBatchFromUnprocessed(cap) + nodes = append(nodes, newNodes...) + paths = append(paths, newPaths...) + codes = append(codes, newCodes...) + } + return +} + +func (s *StateDownloadManager) commit(force bool) error { + if !force && s.bytesUncommitted < ethdb.IdealBatchSize { + return nil + } + start := time.Now() + b := s.bc.ChainDb().NewBatch() + if err := s.sched.Commit(b); err != nil { + return err + } + if err := b.Write(); err != nil { + return fmt.Errorf("DB write error: %v", err) + } + s.updateStats(s.numUncommitted, 0, 0, time.Since(start)) + s.numUncommitted = 0 + s.bytesUncommitted = 0 + return nil +} + +// updateStats bumps the various state sync progress counters and displays a log +// message for the user to see. +func (s *StateDownloadManager) updateStats(written, duplicate, unexpected int, duration time.Duration) { + // TODO: here it updates the stats for total pending, processed, duplicates and unexpected + + // for now, we just jog current stats + if written > 0 || duplicate > 0 || unexpected > 0 { + utils.Logger().Info(). + Int("count", written). + Int("duplicate", duplicate). + Int("unexpected", unexpected). + Msg("Imported new state entries") + } +} + +// getBatchFromUnprocessed returns objects with a maximum of n unprocessed state download +// tasks to send to the remote peer. +func (s *StateDownloadManager) getBatchFromUnprocessed(n int) (nodes []common.Hash, paths []string, codes []common.Hash) { + // over trie nodes as those can be written to disk and forgotten about. + nodes = make([]common.Hash, 0, n) + paths = make([]string, 0, n) + codes = make([]common.Hash, 0, n) + + for hash, t := range s.tasks.codeTasks { + // Stop when we've gathered enough requests + if len(nodes)+len(codes) == n { + break + } + codes = append(codes, hash) + s.requesting.addCodeTask(hash, t) + s.tasks.deleteCodeTask(hash) + } + for path, t := range s.tasks.trieTasks { + // Stop when we've gathered enough requests + if len(nodes)+len(codes) == n { + break + } + nodes = append(nodes, t.hash) + paths = append(paths, path) + s.requesting.addTrieTask(path, t) + s.tasks.deleteTrieTask(path) + } + return nodes, paths, codes +} + +// getBatchFromRetries get the block number batch to be requested from retries. +func (s *StateDownloadManager) getBatchFromRetries(n int) ([]common.Hash, []string, []common.Hash) { + // over trie nodes as those can be written to disk and forgotten about. + nodes := make([]common.Hash, 0, n) + paths := make([]string, 0, n) + codes := make([]common.Hash, 0, n) + + for hash, t := range s.retries.codeTasks { + // Stop when we've gathered enough requests + if len(nodes)+len(codes) == n { + break + } + codes = append(codes, hash) + s.requesting.addCodeTask(hash, t) + s.retries.deleteCodeTask(hash) + } + for path, t := range s.retries.trieTasks { + // Stop when we've gathered enough requests + if len(nodes)+len(codes) == n { + break + } + nodes = append(nodes, t.hash) + paths = append(paths, path) + s.requesting.addTrieTask(path, t) + s.retries.deleteTrieTask(path) + } + return nodes, paths, codes +} + +// HandleRequestError handles the error result +func (s *StateDownloadManager) HandleRequestError(codeHashes []common.Hash, triePaths []string, streamID sttypes.StreamID, err error) { + s.lock.Lock() + defer s.lock.Unlock() + + // add requested code hashes to retries + for _, h := range codeHashes { + task := s.requesting.getCodeTask(h) + s.retries.addCodeTask(h, task) + s.requesting.deleteCodeTask(h) + } + + // add requested trie paths to retries + for _, path := range triePaths { + task := s.requesting.getTrieTask(path) + s.retries.addTrieTask(path, task) + s.requesting.deleteTrieTask(path) + } +} + +// HandleRequestResult handles get trie paths and code hashes result +func (s *StateDownloadManager) HandleRequestResult(codeHashes []common.Hash, triePaths []string, response [][]byte, loopID int, streamID sttypes.StreamID) error { + s.lock.Lock() + defer s.lock.Unlock() + + // Collect processing stats and update progress if valid data was received + duplicate, unexpected, successful := 0, 0, 0 + + for _, blob := range response { + hash, err := s.processNodeData(codeHashes, triePaths, blob) + switch err { + case nil: + s.numUncommitted++ + s.bytesUncommitted += len(blob) + successful++ + case trie.ErrNotRequested: + unexpected++ + case trie.ErrAlreadyProcessed: + duplicate++ + default: + return fmt.Errorf("invalid state node %s: %v", hash.TerminalString(), err) + } + } + + for _, path := range triePaths { + task := s.requesting.getTrieTask(path) + if task == nil { + // it is already removed from requesting + // either it has been completed and deleted by processNodeData or it does not exist + continue + } + // If the node did deliver something, missing items may be due to a protocol + // limit or a previous timeout + delayed delivery. Both cases should permit + // the node to retry the missing items (to avoid single-peer stalls). + if len(response) > 0 { //TODO: if timeout also do same + s.requesting.deleteTrieTaskAttempts(path, streamID) + } else if task.attempts[streamID] >= MaxTriesToFetchNodeData { + // If we've requested the node too many times already, it may be a malicious + // sync where nobody has the right data. Abort. + return fmt.Errorf("trie node %s failed with peer %s (%d tries)", task.hash.TerminalString(), streamID, task.attempts[streamID]) + } + // Missing item, place into the retry queue. + s.retries.addTrieTask(path, task) + s.requesting.deleteTrieTask(path) + } + + for _, hash := range codeHashes { + task := s.requesting.getCodeTask(hash) + if task == nil { + // it is already removed from requesting + // either it has been completed and deleted by processNodeData or it does not exist + continue + } + // If the node did deliver something, missing items may be due to a protocol + // limit or a previous timeout + delayed delivery. Both cases should permit + // the node to retry the missing items (to avoid single-peer stalls). + if len(response) > 0 { //TODO: if timeout also do same + s.requesting.deleteCodeTaskAttempts(hash, streamID) //TODO: do we need delete attempts??? + } else if task.attempts[streamID] >= MaxTriesToFetchNodeData { + // If we've requested the node too many times already, it may be a malicious + // sync where nobody has the right data. Abort. + return fmt.Errorf("byte code %s failed with peer %s (%d tries)", hash.TerminalString(), streamID, task.attempts[streamID]) + } + // Missing item, place into the retry queue. + s.retries.addCodeTask(hash, task) + s.requesting.deleteCodeTask(hash) + } + + return nil +} + +// processNodeData tries to inject a trie node data blob delivered from a remote +// peer into the state trie, returning whether anything useful was written or any +// error occurred. +// +// If multiple requests correspond to the same hash, this method will inject the +// blob as a result for the first one only, leaving the remaining duplicates to +// be fetched again. +func (s *StateDownloadManager) processNodeData(codeHashes []common.Hash, triePaths []string, responseData []byte) (common.Hash, error) { + var hash common.Hash + s.keccak.Reset() + s.keccak.Write(responseData) + s.keccak.Read(hash[:]) + + //TODO: remove from requesting + if _, present := s.requesting.codeTasks[hash]; present { + err := s.sched.ProcessCode(trie.CodeSyncResult{ + Hash: hash, + Data: responseData, + }) + s.requesting.deleteCodeTask(hash) + return hash, err + } + for _, path := range triePaths { + task := s.requesting.getTrieTask(path) + if task == nil { + // this shouldn't happen while the path is given from triPaths and triPaths + // are given from requesting queue + continue + } + if task.hash == hash { + err := s.sched.ProcessNode(trie.NodeSyncResult{ + Path: path, + Data: responseData, + }) + s.requesting.deleteTrieTask(path) + return hash, err + } + } + return common.Hash{}, trie.ErrNotRequested +} diff --git a/api/service/stagedstreamsync/state_sync_full.go b/api/service/stagedstreamsync/state_sync_full.go new file mode 100644 index 000000000..c98dcbafd --- /dev/null +++ b/api/service/stagedstreamsync/state_sync_full.go @@ -0,0 +1,2418 @@ +package stagedstreamsync + +import ( + "bytes" + "encoding/json" + "fmt" + gomath "math" + "math/big" + "math/rand" + "sort" + "sync" + "sync/atomic" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" + + //"github.com/ethereum/go-ethereum/trie/trienode" + "github.com/harmony-one/harmony/common/math" + "github.com/harmony-one/harmony/core" + "github.com/harmony-one/harmony/core/rawdb" + "github.com/harmony-one/harmony/core/state" + "github.com/harmony-one/harmony/internal/utils" + "github.com/harmony-one/harmony/p2p/stream/protocols/sync/message" + sttypes "github.com/harmony-one/harmony/p2p/stream/types" + "github.com/ledgerwatch/erigon-lib/kv" + "github.com/ledgerwatch/log/v3" + "github.com/pkg/errors" + "github.com/rs/zerolog" + "golang.org/x/crypto/sha3" + // "github.com/ethereum/go-ethereum/eth/protocols/snap/range" +) + +const ( + // minRequestSize is the minimum number of bytes to request from a remote peer. + // This number is used as the low cap for account and storage range requests. + // Bytecode and trienode are limited inherently by item count (1). + minRequestSize = 64 * 1024 + + // maxRequestSize is the maximum number of bytes to request from a remote peer. + // This number is used as the high cap for account and storage range requests. + // Bytecode and trienode are limited more explicitly by the caps below. + maxRequestSize = 512 * 1024 + + // maxCodeRequestCount is the maximum number of bytecode blobs to request in a + // single query. If this number is too low, we're not filling responses fully + // and waste round trip times. If it's too high, we're capping responses and + // waste bandwidth. + // + // Deployed bytecodes are currently capped at 24KB, so the minimum request + // size should be maxRequestSize / 24K. Assuming that most contracts do not + // come close to that, requesting 4x should be a good approximation. + maxCodeRequestCount = maxRequestSize / (24 * 1024) * 4 + + // maxTrieRequestCount is the maximum number of trie node blobs to request in + // a single query. If this number is too low, we're not filling responses fully + // and waste round trip times. If it's too high, we're capping responses and + // waste bandwidth. + maxTrieRequestCount = maxRequestSize / 512 + + // trienodeHealRateMeasurementImpact is the impact a single measurement has on + // the local node's trienode processing capacity. A value closer to 0 reacts + // slower to sudden changes, but it is also more stable against temporary hiccups. + trienodeHealRateMeasurementImpact = 0.005 + + // minTrienodeHealThrottle is the minimum divisor for throttling trie node + // heal requests to avoid overloading the local node and excessively expanding + // the state trie breadth wise. + minTrienodeHealThrottle = 1 + + // maxTrienodeHealThrottle is the maximum divisor for throttling trie node + // heal requests to avoid overloading the local node and exessively expanding + // the state trie bedth wise. + maxTrienodeHealThrottle = maxTrieRequestCount + + // trienodeHealThrottleIncrease is the multiplier for the throttle when the + // rate of arriving data is higher than the rate of processing it. + trienodeHealThrottleIncrease = 1.33 + + // trienodeHealThrottleDecrease is the divisor for the throttle when the + // rate of arriving data is lower than the rate of processing it. + trienodeHealThrottleDecrease = 1.25 +) + +// of only the account path. There's no need to be able to address both an +// account node and a storage node in the same request as it cannot happen +// that a slot is accessed before the account path is fully expanded. +type TrieNodePathSet [][]byte + +var ( + // accountConcurrency is the number of chunks to split the account trie into + // to allow concurrent retrievals. + accountConcurrency = 16 + + // storageConcurrency is the number of chunks to split the a large contract + // storage trie into to allow concurrent retrievals. + storageConcurrency = 16 + + // MaxHash represents the maximum possible hash value. + MaxHash = common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") +) + +// accountTask represents the sync task for a chunk of the account snapshot. +type accountTask struct { + id uint64 //unique id for account task + + // These fields get serialized to leveldb on shutdown + Next common.Hash // Next account to sync in this interval + Last common.Hash // Last account to sync in this interval + SubTasks map[common.Hash][]*storageTask // Storage intervals needing fetching for large contracts + + pend int // Number of pending subtasks for this round + + needCode []bool // Flags whether the filling accounts need code retrieval + needState []bool // Flags whether the filling accounts need storage retrieval + needHeal []bool // Flags whether the filling accounts's state was chunked and need healing + + codeTasks map[common.Hash]struct{} // Code hashes that need retrieval + stateTasks map[common.Hash]common.Hash // Account hashes->roots that need full state retrieval + + genBatch ethdb.Batch // Batch used by the node generator + genTrie *trie.StackTrie // Node generator from storage slots + + requested bool + done bool // Flag whether the task can be removed + + res *accountResponse +} + +// accountResponse is an already Merkle-verified remote response to an account +// range request. It contains the subtrie for the requested account range and +// the database that's going to be filled with the internal nodes on commit. +type accountResponse struct { + task *accountTask // Task which this request is filling + hashes []common.Hash // Account hashes in the returned range + accounts []*types.StateAccount // Expanded accounts in the returned range + cont bool // Whether the account range has a continuation +} + +// storageTask represents the sync task for a chunk of the storage snapshot. +type storageTask struct { + Next common.Hash // Next account to sync in this interval + Last common.Hash // Last account to sync in this interval + root common.Hash // Storage root hash for this instance + genBatch ethdb.Batch // Batch used by the node generator + genTrie *trie.StackTrie // Node generator from storage slots + requested bool + done bool // Flag whether the task can be removed +} + +// healRequestSort implements the Sort interface, allowing sorting trienode +// heal requests, which is a prerequisite for merging storage-requests. +type healRequestSort struct { + paths []string + hashes []common.Hash + syncPaths []trie.SyncPath +} + +func (t *healRequestSort) Len() int { + return len(t.hashes) +} + +func (t *healRequestSort) Less(i, j int) bool { + a := t.syncPaths[i] + b := t.syncPaths[j] + switch bytes.Compare(a[0], b[0]) { + case -1: + return true + case 1: + return false + } + // identical first part + if len(a) < len(b) { + return true + } + if len(b) < len(a) { + return false + } + if len(a) == 2 { + return bytes.Compare(a[1], b[1]) < 0 + } + return false +} + +func (t *healRequestSort) Swap(i, j int) { + t.paths[i], t.paths[j] = t.paths[j], t.paths[i] + t.hashes[i], t.hashes[j] = t.hashes[j], t.hashes[i] + t.syncPaths[i], t.syncPaths[j] = t.syncPaths[j], t.syncPaths[i] +} + +// Merge merges the pathsets, so that several storage requests concerning the +// same account are merged into one, to reduce bandwidth. +// This operation is moot if t has not first been sorted. +func (t *healRequestSort) Merge() []*message.TrieNodePathSet { + var result []TrieNodePathSet + for _, path := range t.syncPaths { + pathset := TrieNodePathSet(path) + if len(path) == 1 { + // It's an account reference. + result = append(result, pathset) + } else { + // It's a storage reference. + end := len(result) - 1 + if len(result) == 0 || !bytes.Equal(pathset[0], result[end][0]) { + // The account doesn't match last, create a new entry. + result = append(result, pathset) + } else { + // It's the same account as the previous one, add to the storage + // paths of that request. + result[end] = append(result[end], pathset[1]) + } + } + } + // convert to array of pointers + result_ptr := make([]*message.TrieNodePathSet, 0) + for _, p := range result { + result_ptr = append(result_ptr, &message.TrieNodePathSet{ + Pathset: p, + }) + } + return result_ptr +} + +type byteCodeTasksBundle struct { + id uint64 //unique id for bytecode task bundle + task *accountTask + hashes []common.Hash +} + +type storageTaskBundle struct { + id uint64 //unique id for storage task bundle + accounts []common.Hash + roots []common.Hash + mainTask *accountTask + subtask *storageTask + origin common.Hash + limit common.Hash +} + +// healTask represents the sync task for healing the snap-synced chunk boundaries. +type healTask struct { + id uint64 + trieTasks map[string]common.Hash // Set of trie node tasks currently queued for retrieval, indexed by node path + codeTasks map[common.Hash]struct{} // Set of byte code tasks currently queued for retrieval, indexed by code hash + paths []string + hashes []common.Hash + pathsets []*message.TrieNodePathSet + task *healTask + root common.Hash + byteCodeReq bool +} + +type tasks struct { + accountTasks map[uint64]*accountTask // Current account task set being synced + storageTasks map[uint64]*storageTaskBundle // Set of trie node tasks currently queued for retrieval, indexed by path + codeTasks map[uint64]*byteCodeTasksBundle // Set of byte code tasks currently queued for retrieval, indexed by hash + healer map[uint64]*healTask + snapped bool // Flag to signal that snap phase is done +} + +func newTasks() *tasks { + return &tasks{ + accountTasks: make(map[uint64]*accountTask, 0), + storageTasks: make(map[uint64]*storageTaskBundle, 0), + codeTasks: make(map[uint64]*byteCodeTasksBundle), + healer: make(map[uint64]*healTask, 0), + snapped: false, + } +} + +func (t *tasks) addAccountTask(accountTaskIndex uint64, ct *accountTask) { + t.accountTasks[accountTaskIndex] = ct +} + +func (t *tasks) getAccountTask(accountTaskIndex uint64) *accountTask { + if _, ok := t.accountTasks[accountTaskIndex]; ok { + return t.accountTasks[accountTaskIndex] + } + return nil +} + +func (t *tasks) deleteAccountTask(accountTaskIndex uint64) { + if _, ok := t.accountTasks[accountTaskIndex]; ok { + delete(t.accountTasks, accountTaskIndex) + } +} + +func (t *tasks) addCodeTask(id uint64, bytecodeTask *byteCodeTasksBundle) { + t.codeTasks[id] = bytecodeTask +} + +func (t *tasks) deleteCodeTask(id uint64) { + if _, ok := t.codeTasks[id]; ok { + delete(t.codeTasks, id) + } +} + +func (t *tasks) addStorageTaskBundle(storageBundleIndex uint64, storages *storageTaskBundle) { + t.storageTasks[storageBundleIndex] = storages +} + +func (t *tasks) deleteStorageTaskBundle(storageBundleIndex uint64) { + if _, ok := t.storageTasks[storageBundleIndex]; ok { + delete(t.storageTasks, storageBundleIndex) + } +} + +func (t *tasks) addHealerTask(taskID uint64, task *healTask) { + t.healer[taskID] = task +} + +func (t *tasks) deleteHealerTask(taskID uint64) { + if _, ok := t.healer[taskID]; ok { + delete(t.healer, taskID) + } +} + +func (t *tasks) addHealerTrieTask(taskID uint64, path string, h common.Hash) { + if _, ok := t.healer[taskID]; ok { + t.healer[taskID].trieTasks[path] = h + } +} + +func (t *tasks) getHealerTrieTask(taskID uint64, path string) common.Hash { + if _, ok := t.healer[taskID]; ok { + return t.healer[taskID].trieTasks[path] + } + return common.Hash{} +} + +func (t *tasks) addHealerTrieCodeTask(taskID uint64, hash common.Hash, v struct{}) { + if _, ok := t.healer[taskID]; ok { + t.healer[taskID].codeTasks[hash] = v + } +} + +func (t *tasks) getHealerTrieCodeTask(taskID uint64, h common.Hash) struct{} { + if _, ok := t.healer[taskID]; ok { + return t.healer[taskID].codeTasks[h] + } + return struct{}{} +} + +// SyncProgress is a database entry to allow suspending and resuming a snapshot state +// sync. Opposed to full and fast sync, there is no way to restart a suspended +// snap sync without prior knowledge of the suspension point. +type SyncProgress struct { + Tasks map[uint64]*accountTask // The suspended account tasks (contract tasks within) + + // Status report during syncing phase + AccountSynced uint64 // Number of accounts downloaded + AccountBytes common.StorageSize // Number of account trie bytes persisted to disk + BytecodeSynced uint64 // Number of bytecodes downloaded + BytecodeBytes common.StorageSize // Number of bytecode bytes downloaded + StorageSynced uint64 // Number of storage slots downloaded + StorageBytes common.StorageSize // Number of storage trie bytes persisted to disk + + // Status report during healing phase + TrienodeHealSynced uint64 // Number of state trie nodes downloaded + TrienodeHealBytes common.StorageSize // Number of state trie bytes persisted to disk + BytecodeHealSynced uint64 // Number of bytecodes downloaded + BytecodeHealBytes common.StorageSize // Number of bytecodes persisted to disk +} + +// FullStateDownloadManager is the helper structure for get blocks request management +type FullStateDownloadManager struct { + bc core.BlockChain + tx kv.RwTx + + db ethdb.KeyValueStore // Database to store the trie nodes into (and dedup) + scheme string // Node scheme used in node database + + tasks *tasks + requesting *tasks + processing *tasks + retries *tasks + + root common.Hash // Current state trie root being synced + snapped bool // Flag to signal that snap phase is done + + protocol syncProtocol + scheduler *trie.Sync // State trie sync scheduler defining the tasks + keccak crypto.KeccakState // Keccak256 hasher to verify deliveries with + concurrency int + logger zerolog.Logger + lock sync.RWMutex + + numUncommitted int + bytesUncommitted int + + accountSynced uint64 // Number of accounts downloaded + accountBytes common.StorageSize // Number of account trie bytes persisted to disk + bytecodeSynced uint64 // Number of bytecodes downloaded + bytecodeBytes common.StorageSize // Number of bytecode bytes downloaded + storageSynced uint64 // Number of storage slots downloaded + storageBytes common.StorageSize // Number of storage trie bytes persisted to disk + + pend sync.WaitGroup // Tracks network request goroutines for graceful shutdown + + stateWriter ethdb.Batch // Shared batch writer used for persisting raw states + accountHealed uint64 // Number of accounts downloaded during the healing stage + accountHealedBytes common.StorageSize // Number of raw account bytes persisted to disk during the healing stage + storageHealed uint64 // Number of storage slots downloaded during the healing stage + storageHealedBytes common.StorageSize // Number of raw storage bytes persisted to disk during the healing stage + + trienodeHealRate float64 // Average heal rate for processing trie node data + trienodeHealPend atomic.Uint64 // Number of trie nodes currently pending for processing + trienodeHealThrottle float64 // Divisor for throttling the amount of trienode heal data requested + trienodeHealThrottled time.Time // Timestamp the last time the throttle was updated + + trienodeHealSynced uint64 // Number of state trie nodes downloaded + trienodeHealBytes common.StorageSize // Number of state trie bytes persisted to disk + trienodeHealDups uint64 // Number of state trie nodes already processed + trienodeHealNops uint64 // Number of state trie nodes not requested + bytecodeHealSynced uint64 // Number of bytecodes downloaded + bytecodeHealBytes common.StorageSize // Number of bytecodes persisted to disk + bytecodeHealDups uint64 // Number of bytecodes already processed + bytecodeHealNops uint64 // Number of bytecodes not requested +} + +func newFullStateDownloadManager(db ethdb.KeyValueStore, + scheme string, + tx kv.RwTx, + bc core.BlockChain, + concurrency int, + logger zerolog.Logger) *FullStateDownloadManager { + + return &FullStateDownloadManager{ + db: db, + scheme: scheme, + bc: bc, + stateWriter: db.NewBatch(), + tx: tx, + keccak: sha3.NewLegacyKeccak256().(crypto.KeccakState), + concurrency: concurrency, + logger: logger, + tasks: newTasks(), + requesting: newTasks(), + processing: newTasks(), + retries: newTasks(), + } +} + +func (s *FullStateDownloadManager) setRootHash(root common.Hash) { + s.root = root + s.scheduler = state.NewStateSync(root, s.db, s.onHealState, s.scheme) + s.loadSyncStatus() +} + +func (s *FullStateDownloadManager) taskDone(taskID uint64) { + s.tasks.accountTasks[taskID].done = true +} + +// SlimAccount is a modified version of an Account, where the root is replaced +// with a byte slice. This format can be used to represent full-consensus format +// or slim format which replaces the empty root and code hash as nil byte slice. +type SlimAccount struct { + Nonce uint64 + Balance *big.Int + Root []byte // Nil if root equals to types.EmptyRootHash + CodeHash []byte // Nil if hash equals to types.EmptyCodeHash +} + +// SlimAccountRLP encodes the state account in 'slim RLP' format. +func (s *FullStateDownloadManager) SlimAccountRLP(account types.StateAccount) []byte { + slim := SlimAccount{ + Nonce: account.Nonce, + Balance: account.Balance, + } + if account.Root != types.EmptyRootHash { + slim.Root = account.Root[:] + } + if !bytes.Equal(account.CodeHash, types.EmptyCodeHash[:]) { + slim.CodeHash = account.CodeHash + } + data, err := rlp.EncodeToBytes(slim) + if err != nil { + panic(err) + } + return data +} + +// FullAccount decodes the data on the 'slim RLP' format and returns +// the consensus format account. +func FullAccount(data []byte) (*types.StateAccount, error) { + var slim SlimAccount + if err := rlp.DecodeBytes(data, &slim); err != nil { + return nil, err + } + var account types.StateAccount + account.Nonce, account.Balance = slim.Nonce, slim.Balance + + // Interpret the storage root and code hash in slim format. + if len(slim.Root) == 0 { + account.Root = types.EmptyRootHash + } else { + account.Root = common.BytesToHash(slim.Root) + } + if len(slim.CodeHash) == 0 { + account.CodeHash = types.EmptyCodeHash[:] + } else { + account.CodeHash = slim.CodeHash + } + return &account, nil +} + +// FullAccountRLP converts data on the 'slim RLP' format into the full RLP-format. +func FullAccountRLP(data []byte) ([]byte, error) { + account, err := FullAccount(data) + if err != nil { + return nil, err + } + return rlp.EncodeToBytes(account) +} + +func (s *FullStateDownloadManager) commitHealer(force bool) { + if !force && s.scheduler.MemSize() < ethdb.IdealBatchSize { + return + } + batch := s.db.NewBatch() + if err := s.scheduler.Commit(batch); err != nil { + utils.Logger().Error().Err(err).Msg("Failed to commit healing data") + } + if err := batch.Write(); err != nil { + log.Crit("Failed to persist healing data", "err", err) + } + utils.Logger().Debug().Str("type", "trienodes").Interface("bytes", common.StorageSize(batch.ValueSize())).Msg("Persisted set of healing data") +} + +func (s *FullStateDownloadManager) SyncCompleted() { + defer func() { // Persist any progress, independent of failure + for _, task := range s.tasks.accountTasks { + s.forwardAccountTask(task) + } + s.cleanAccountTasks() + s.saveSyncStatus() + }() + + // Flush out the last committed raw states + defer func() { + if s.stateWriter.ValueSize() > 0 { + s.stateWriter.Write() + s.stateWriter.Reset() + } + }() + + // commit any trie- and bytecode-healing data. + defer s.commitHealer(true) + + // Whether sync completed or not, disregard any future packets + defer func() { + utils.Logger().Debug().Interface("root", s.root).Msg("Terminating snapshot sync cycle") + }() + + utils.Logger().Debug().Msg("Snapshot sync already completed") +} + +// getNextBatch returns objects with a maximum of n state download +// tasks to send to the remote peer. +func (s *FullStateDownloadManager) GetNextBatch() (accounts []*accountTask, + codes []*byteCodeTasksBundle, + storages *storageTaskBundle, + healtask *healTask, + codetask *healTask, + err error) { + + s.lock.Lock() + defer s.lock.Unlock() + + accounts, codes, storages, healtask, codetask = s.getBatchFromRetries() + nItems := len(accounts) + len(codes) + len(storages.roots) + len(healtask.hashes) + len(codetask.hashes) + + if nItems > 0 { + return + } + + if len(s.tasks.accountTasks) == 0 && s.scheduler.Pending() == 0 { + if nItems == 0 { + s.SyncCompleted() + } + return + } + + // Refill available tasks from the scheduler. + withHealTasks := true + if healtask != nil || codetask != nil { + withHealTasks = false + } + newAccounts, newCodes, newStorageTaskBundle, newHealTask, newCodeTask := s.getBatchFromUnprocessed(withHealTasks) + accounts = append(accounts, newAccounts...) + codes = append(codes, newCodes...) + storages = newStorageTaskBundle + if withHealTasks { + healtask = newHealTask + codetask = newCodeTask + } + + return +} + +// saveSyncStatus marshals the remaining sync tasks into leveldb. +func (s *FullStateDownloadManager) saveSyncStatus() { + // Serialize any partial progress to disk before spinning down + for _, task := range s.tasks.accountTasks { + if err := task.genBatch.Write(); err != nil { + utils.Logger().Debug(). + Err(err). + Msg("Failed to persist account slots") + } + for _, subtasks := range task.SubTasks { + for _, subtask := range subtasks { + if err := subtask.genBatch.Write(); err != nil { + utils.Logger().Debug(). + Err(err). + Msg("Failed to persist storage slots") + } + } + } + } + // Store the actual progress markers + progress := &SyncProgress{ + Tasks: s.tasks.accountTasks, + AccountSynced: s.accountSynced, + AccountBytes: s.accountBytes, + BytecodeSynced: s.bytecodeSynced, + BytecodeBytes: s.bytecodeBytes, + StorageSynced: s.storageSynced, + StorageBytes: s.storageBytes, + TrienodeHealSynced: s.trienodeHealSynced, + TrienodeHealBytes: s.trienodeHealBytes, + BytecodeHealSynced: s.bytecodeHealSynced, + BytecodeHealBytes: s.bytecodeHealBytes, + } + status, err := json.Marshal(progress) + if err != nil { + panic(err) // This can only fail during implementation + } + rawdb.WriteSnapshotSyncStatus(s.db, status) +} + +// loadSyncStatus retrieves a previously aborted sync status from the database, +// or generates a fresh one if none is available. +func (s *FullStateDownloadManager) loadSyncStatus() { + var progress SyncProgress + + if status := rawdb.ReadSnapshotSyncStatus(s.db); status != nil { + if err := json.Unmarshal(status, &progress); err != nil { + utils.Logger().Error(). + Err(err). + Msg("Failed to decode snap sync status") + } else { + for _, task := range progress.Tasks { + utils.Logger().Debug(). + Interface("from", task.Next). + Interface("last", task.Last). + Msg("Scheduled account sync task") + } + s.tasks.accountTasks = progress.Tasks + for _, task := range s.tasks.accountTasks { + task := task // closure for task.genBatch in the stacktrie writer callback + + task.genBatch = ethdb.HookedBatch{ + Batch: s.db.NewBatch(), + OnPut: func(key []byte, value []byte) { + s.accountBytes += common.StorageSize(len(key) + len(value)) + }, + } + // options := trie.NewStackTrieOptions() + writeFn := func(owner common.Hash, path []byte, hash common.Hash, blob []byte) { + rawdb.WriteTrieNode(task.genBatch, common.Hash{}, path, hash, blob, s.scheme) + } + task.genTrie = trie.NewStackTrie(writeFn) + for accountHash, subtasks := range task.SubTasks { + for _, subtask := range subtasks { + subtask := subtask // closure for subtask.genBatch in the stacktrie writer callback + + subtask.genBatch = ethdb.HookedBatch{ + Batch: s.db.NewBatch(), + OnPut: func(key []byte, value []byte) { + s.storageBytes += common.StorageSize(len(key) + len(value)) + }, + } + // owner := accountHash // local assignment for stacktrie writer closure + writeFn = func(owner common.Hash, path []byte, hash common.Hash, blob []byte) { + rawdb.WriteTrieNode(subtask.genBatch, accountHash, path, hash, blob, s.scheme) + } + subtask.genTrie = trie.NewStackTrie(writeFn) + } + } + } + s.lock.Lock() + defer s.lock.Unlock() + + s.snapped = len(s.tasks.accountTasks) == 0 + + s.accountSynced = progress.AccountSynced + s.accountBytes = progress.AccountBytes + s.bytecodeSynced = progress.BytecodeSynced + s.bytecodeBytes = progress.BytecodeBytes + s.storageSynced = progress.StorageSynced + s.storageBytes = progress.StorageBytes + + s.trienodeHealSynced = progress.TrienodeHealSynced + s.trienodeHealBytes = progress.TrienodeHealBytes + s.bytecodeHealSynced = progress.BytecodeHealSynced + s.bytecodeHealBytes = progress.BytecodeHealBytes + return + } + } + // Either we've failed to decode the previous state, or there was none. + // Start a fresh sync by chunking up the account range and scheduling + // them for retrieval. + s.tasks.accountTasks = nil + s.accountSynced, s.accountBytes = 0, 0 + s.bytecodeSynced, s.bytecodeBytes = 0, 0 + s.storageSynced, s.storageBytes = 0, 0 + s.trienodeHealSynced, s.trienodeHealBytes = 0, 0 + s.bytecodeHealSynced, s.bytecodeHealBytes = 0, 0 + + var next common.Hash + step := new(big.Int).Sub( + new(big.Int).Div( + new(big.Int).Exp(common.Big2, common.Big256, nil), + big.NewInt(int64(accountConcurrency)), + ), common.Big1, + ) + for i := 0; i < accountConcurrency; i++ { + last := common.BigToHash(new(big.Int).Add(next.Big(), step)) + if i == accountConcurrency-1 { + // Make sure we don't overflow if the step is not a proper divisor + last = MaxHash + } + batch := ethdb.HookedBatch{ + Batch: s.db.NewBatch(), + OnPut: func(key []byte, value []byte) { + s.accountBytes += common.StorageSize(len(key) + len(value)) + }, + } + // options := trie.NewStackTrieOptions() + writeFn := func(owner common.Hash, path []byte, hash common.Hash, blob []byte) { + rawdb.WriteTrieNode(batch, common.Hash{}, path, hash, blob, s.scheme) + } + // create a unique id for task + var taskID uint64 + for { + taskID = uint64(rand.Int63()) + if taskID == 0 { + continue + } + if _, ok := s.tasks.accountTasks[taskID]; ok { + continue + } + break + } + s.tasks.addAccountTask(taskID, &accountTask{ + id: taskID, + Next: next, + Last: last, + SubTasks: make(map[common.Hash][]*storageTask), + genBatch: batch, + genTrie: trie.NewStackTrie(writeFn), + }) + utils.Logger().Debug(). + Interface("from", next). + Interface("last", last). + Msg("Created account sync task") + + next = common.BigToHash(new(big.Int).Add(last.Big(), common.Big1)) + } +} + +// cleanAccountTasks removes account range retrieval tasks that have already been +// completed. +func (s *FullStateDownloadManager) cleanAccountTasks() { + // If the sync was already done before, don't even bother + if len(s.tasks.accountTasks) == 0 { + return + } + // Sync wasn't finished previously, check for any task that can be finalized + for taskID, _ := range s.tasks.accountTasks { + if s.tasks.accountTasks[taskID].done { + s.tasks.deleteAccountTask(taskID) + } + } + // If everything was just finalized just, generate the account trie and start heal + if len(s.tasks.accountTasks) == 0 { + s.lock.Lock() + s.snapped = true + s.lock.Unlock() + + // Push the final sync report + //s.reportSyncProgress(true) + } +} + +// cleanStorageTasks iterates over all the account tasks and storage sub-tasks +// within, cleaning any that have been completed. +func (s *FullStateDownloadManager) cleanStorageTasks() { + for _, task := range s.tasks.accountTasks { + for account, subtasks := range task.SubTasks { + // Remove storage range retrieval tasks that completed + for j := 0; j < len(subtasks); j++ { + if subtasks[j].done { + subtasks = append(subtasks[:j], subtasks[j+1:]...) + j-- + } + } + if len(subtasks) > 0 { + task.SubTasks[account] = subtasks + continue + } + // If all storage chunks are done, mark the account as done too + for j, hash := range task.res.hashes { + if hash == account { + task.needState[j] = false + } + } + delete(task.SubTasks, account) + task.pend-- + + // If this was the last pending task, forward the account task + if task.pend == 0 { + s.forwardAccountTask(task) + } + } + } +} + +// forwardAccountTask takes a filled account task and persists anything available +// into the database, after which it forwards the next account marker so that the +// task's next chunk may be filled. +func (s *FullStateDownloadManager) forwardAccountTask(task *accountTask) { + // Remove any pending delivery + res := task.res + if res == nil { + return // nothing to forward + } + task.res = nil + + // Persist the received account segments. These flat state maybe + // outdated during the sync, but it can be fixed later during the + // snapshot generation. + oldAccountBytes := s.accountBytes + + batch := ethdb.HookedBatch{ + Batch: s.db.NewBatch(), + OnPut: func(key []byte, value []byte) { + s.accountBytes += common.StorageSize(len(key) + len(value)) + }, + } + for i, hash := range res.hashes { + if task.needCode[i] || task.needState[i] { + break + } + slim := s.SlimAccountRLP(*res.accounts[i]) + rawdb.WriteAccountSnapshot(batch, hash, slim) + + // If the task is complete, drop it into the stack trie to generate + // account trie nodes for it + if !task.needHeal[i] { + full, err := FullAccountRLP(slim) // TODO(karalabe): Slim parsing can be omitted + if err != nil { + panic(err) // Really shouldn't ever happen + } + task.genTrie.Update(hash[:], full) + } + } + // Flush anything written just now and update the stats + if err := batch.Write(); err != nil { + utils.Logger().Error().Err(err).Msg("Failed to persist accounts") + } + s.accountSynced += uint64(len(res.accounts)) + + // Task filling persisted, push it the chunk marker forward to the first + // account still missing data. + for i, hash := range res.hashes { + if task.needCode[i] || task.needState[i] { + return + } + task.Next = incHash(hash) + } + // All accounts marked as complete, track if the entire task is done + task.done = !res.cont + + // Stack trie could have generated trie nodes, push them to disk (we need to + // flush after finalizing task.done. It's fine even if we crash and lose this + // write as it will only cause more data to be downloaded during heal. + if task.done { + task.genTrie.Commit() + } + if task.genBatch.ValueSize() > ethdb.IdealBatchSize || task.done { + if err := task.genBatch.Write(); err != nil { + utils.Logger().Error().Err(err).Msg("Failed to persist stack account") + } + task.genBatch.Reset() + } + utils.Logger().Debug(). + Int("accounts", len(res.accounts)). + Float64("bytes", float64(s.accountBytes-oldAccountBytes)). + Msg("Persisted range of accounts") +} + +// updateStats bumps the various state sync progress counters and displays a log +// message for the user to see. +func (s *FullStateDownloadManager) updateStats(written, duplicate, unexpected int, duration time.Duration) { + // TODO: here it updates the stats for total pending, processed, duplicates and unexpected + + // for now, we just jog current stats + if written > 0 || duplicate > 0 || unexpected > 0 { + utils.Logger().Info(). + Int("count", written). + Int("duplicate", duplicate). + Int("unexpected", unexpected). + Msg("Imported new state entries") + } +} + +// getBatchFromUnprocessed returns objects with a maximum of n unprocessed state download +// tasks to send to the remote peer. +func (s *FullStateDownloadManager) getBatchFromUnprocessed(withHealTasks bool) ( + accounts []*accountTask, + codes []*byteCodeTasksBundle, + storages *storageTaskBundle, + healtask *healTask, + codetask *healTask) { + + // over trie nodes as those can be written to disk and forgotten about. + codes = make([]*byteCodeTasksBundle, 0) + accounts = make([]*accountTask, 0) + + for i, task := range s.tasks.accountTasks { + // Stop when we've gathered enough requests + // if len(accounts) == n { + // return + // } + + // if already requested + if task.requested { + continue + } + + // create a unique id for healer task + var taskID uint64 + for { + taskID = uint64(rand.Int63()) + if taskID == 0 { + continue + } + if _, ok := s.tasks.accountTasks[taskID]; ok { + continue + } + break + } + + s.tasks.accountTasks[i].requested = true + accounts = append(accounts, task) + s.requesting.addAccountTask(task.id, task) + s.tasks.addAccountTask(task.id, task) + + // one task account is enough for an stream + return + } + + totalHashes := int(0) + + for _, task := range s.tasks.accountTasks { + // Skip tasks that are already retrieving (or done with) all codes + if len(task.codeTasks) == 0 { + continue + } + + var hashes []common.Hash + for hash := range task.codeTasks { + delete(task.codeTasks, hash) + hashes = append(hashes, hash) + } + totalHashes += len(hashes) + + // create a unique id for task bundle + var taskID uint64 + for { + taskID = uint64(rand.Int63()) + if taskID == 0 { + continue + } + if _, ok := s.tasks.codeTasks[taskID]; ok { + continue + } + break + } + + bytecodeTask := &byteCodeTasksBundle{ + id: taskID, + hashes: hashes, + task: task, + } + codes = append(codes, bytecodeTask) + + s.requesting.addCodeTask(taskID, bytecodeTask) + s.tasks.addCodeTask(taskID, bytecodeTask) + + // Stop when we've gathered enough requests + if totalHashes >= maxCodeRequestCount { + return + } + } + + // if we found some codes, can assign it to node + if totalHashes > 0 { + return + } + + for accTaskID, task := range s.tasks.accountTasks { + // Skip tasks that are already retrieving (or done with) all small states + if len(task.SubTasks) == 0 && len(task.stateTasks) == 0 { + continue + } + + // TODO: check cap calculations (shouldn't give us big chunk) + // if cap > maxRequestSize { + // cap = maxRequestSize + // } + // if cap < minRequestSize { // Don't bother with peers below a bare minimum performance + // cap = minRequestSize + // } + storageSets := maxRequestSize / 1024 + + storages = &storageTaskBundle{ + accounts: make([]common.Hash, 0, storageSets), + roots: make([]common.Hash, 0, storageSets), + mainTask: task, + } + + // create a unique id for task bundle + var taskID uint64 + for { + taskID = uint64(rand.Int63()) + if taskID == 0 { + continue + } + if _, ok := s.tasks.storageTasks[taskID]; ok { + continue + } + break + } + storages.id = taskID + + for account, subtasks := range task.SubTasks { + // find the first subtask which is not requested yet + for i, st := range subtasks { + // Skip any subtasks already filling + if st.requested { + continue + } + // Found an incomplete storage chunk, schedule it + storages.accounts = append(storages.accounts, account) + storages.roots = append(storages.roots, st.root) + storages.subtask = st + s.tasks.accountTasks[accTaskID].SubTasks[account][i].requested = true + break // Large contract chunks are downloaded individually + } + if storages.subtask != nil { + break // Large contract chunks are downloaded individually + } + } + if storages.subtask == nil { + // No large contract required retrieval, but small ones available + for account, root := range task.stateTasks { + delete(task.stateTasks, account) + + storages.accounts = append(storages.accounts, account) + storages.roots = append(storages.roots, root) + + if len(storages.accounts) >= storageSets { + break + } + } + } + // If nothing was found, it means this task is actually already fully + // retrieving, but large contracts are hard to detect. Skip to the next. + if len(storages.accounts) == 0 { + continue + } + if storages.subtask != nil { + storages.origin = storages.subtask.Next + storages.limit = storages.subtask.Last + } + s.tasks.addStorageTaskBundle(taskID, storages) + s.requesting.addStorageTaskBundle(taskID, storages) + + return + } + + if len(storages.accounts) > 0 { + return + } + + if !withHealTasks { + return + } + + // Sync phase done, run heal phase + + // Iterate over pending tasks and try to find a peer to retrieve with + for (len(s.tasks.healer) > 0 && len(s.tasks.healer[0].hashes) > 0) || s.scheduler.Pending() > 0 { + // If there are not enough trie tasks queued to fully assign, fill the + // queue from the state sync scheduler. The trie synced schedules these + // together with bytecodes, so we need to queue them combined. + + // index 0 keeps all tasks, later we split it into multiple batch + if len(s.tasks.healer) == 0 { + s.tasks.healer[0] = &healTask{ + trieTasks: make(map[string]common.Hash, 0), + codeTasks: make(map[common.Hash]struct{}, 0), + } + } + + mPaths, mHashes, mCodes := s.scheduler.Missing(maxTrieRequestCount) + for i, path := range mPaths { + s.tasks.healer[0].trieTasks[path] = mHashes[i] + } + for _, hash := range mCodes { + s.tasks.healer[0].codeTasks[hash] = struct{}{} + } + + // If all the heal tasks are bytecodes or already downloading, bail + if len(s.tasks.healer[0].trieTasks) == 0 { + return + } + // Generate the network query and send it to the peer + // if cap > maxTrieRequestCount { + // cap = maxTrieRequestCount + // } + cap := int(float64(maxTrieRequestCount) / s.trienodeHealThrottle) + if cap <= 0 { + cap = 1 + } + var ( + hashes = make([]common.Hash, 0, cap) + paths = make([]string, 0, cap) + pathsets = make([]*message.TrieNodePathSet, 0, cap) + ) + for path, hash := range s.tasks.healer[0].trieTasks { + delete(s.tasks.healer[0].trieTasks, path) + + paths = append(paths, path) + hashes = append(hashes, hash) + if len(paths) >= cap { + break + } + } + + // Group requests by account hash + paths, hashes, _, pathsets = sortByAccountPath(paths, hashes) + + // create a unique id for healer task + var taskID uint64 + for { + taskID = uint64(rand.Int63()) + if taskID == 0 { + continue + } + if _, ok := s.tasks.healer[taskID]; ok { + continue + } + break + } + + healtask = &healTask{ + id: taskID, + hashes: hashes, + paths: paths, + pathsets: pathsets, + root: s.root, + task: s.tasks.healer[0], + byteCodeReq: false, + } + + s.tasks.healer[taskID] = healtask + s.requesting.addHealerTask(taskID, healtask) + + if len(hashes) > 0 { + return + } + } + + // trying to get bytecodes + // Iterate over pending tasks and try to find a peer to retrieve with + for (len(s.tasks.healer) > 0 && len(s.tasks.healer[0].codeTasks) > 0) || s.scheduler.Pending() > 0 { + // If there are not enough trie tasks queued to fully assign, fill the + // queue from the state sync scheduler. The trie synced schedules these + // together with trie nodes, so we need to queue them combined. + + mPaths, mHashes, mCodes := s.scheduler.Missing(maxTrieRequestCount) + for i, path := range mPaths { + s.tasks.healer[0].trieTasks[path] = mHashes[i] + } + for _, hash := range mCodes { + s.tasks.healer[0].codeTasks[hash] = struct{}{} + } + + // If all the heal tasks are trienodes or already downloading, bail + if len(s.tasks.healer[0].codeTasks) == 0 { + return + } + // Task pending retrieval, try to find an idle peer. If no such peer + // exists, we probably assigned tasks for all (or they are stateless). + // Abort the entire assignment mechanism. + + // Generate the network query and send it to the peer + // if cap > maxCodeRequestCount { + // cap = maxCodeRequestCount + // } + cap := maxCodeRequestCount + hashes := make([]common.Hash, 0, cap) + for hash := range s.tasks.healer[0].codeTasks { + delete(s.tasks.healer[0].codeTasks, hash) + + hashes = append(hashes, hash) + if len(hashes) >= cap { + break + } + } + + // create a unique id for healer task + var taskID uint64 + for { + taskID = uint64(rand.Int63()) + if taskID == 0 { + continue + } + if _, ok := s.tasks.healer[taskID]; ok { + continue + } + break + } + + codetask = &healTask{ + id: taskID, + hashes: hashes, + task: s.tasks.healer[0], + byteCodeReq: true, + } + + s.tasks.healer[taskID] = codetask + s.requesting.addHealerTask(taskID, healtask) + } + + return +} + +// sortByAccountPath takes hashes and paths, and sorts them. After that, it generates +// the TrieNodePaths and merges paths which belongs to the same account path. +func sortByAccountPath(paths []string, hashes []common.Hash) ([]string, []common.Hash, []trie.SyncPath, []*message.TrieNodePathSet) { + var syncPaths []trie.SyncPath + for _, path := range paths { + syncPaths = append(syncPaths, trie.NewSyncPath([]byte(path))) + } + n := &healRequestSort{paths, hashes, syncPaths} + sort.Sort(n) + pathsets := n.Merge() + return n.paths, n.hashes, n.syncPaths, pathsets +} + +// getBatchFromRetries get the block number batch to be requested from retries. +func (s *FullStateDownloadManager) getBatchFromRetries() ( + accounts []*accountTask, + codes []*byteCodeTasksBundle, + storages *storageTaskBundle, + healtask *healTask, + codetask *healTask) { + + // over trie nodes as those can be written to disk and forgotten about. + accounts = make([]*accountTask, 0) + codes = make([]*byteCodeTasksBundle, 0) + + for _, task := range s.retries.accountTasks { + // Stop when we've gathered enough requests + // if len(accounts) == n { + // return + // } + accounts = append(accounts, task) + s.requesting.addAccountTask(task.id, task) + s.retries.deleteAccountTask(task.id) + return + } + + if len(accounts) > 0 { + return + } + + for _, code := range s.retries.codeTasks { + codes = append(codes, code) + s.requesting.addCodeTask(code.id, code) + s.retries.deleteCodeTask(code.id) + return + } + + if len(codes) > 0 { + return + } + + if s.retries.storageTasks != nil && len(s.retries.storageTasks) > 0 { + storages = &storageTaskBundle{ + id: s.retries.storageTasks[0].id, + accounts: s.retries.storageTasks[0].accounts, + roots: s.retries.storageTasks[0].roots, + mainTask: s.retries.storageTasks[0].mainTask, + subtask: s.retries.storageTasks[0].subtask, + limit: s.retries.storageTasks[0].limit, + origin: s.retries.storageTasks[0].origin, + } + s.requesting.addStorageTaskBundle(storages.id, storages) + s.retries.deleteStorageTaskBundle(storages.id) + return + } + + if len(storages.accounts) > 0 { + return + } + + if s.retries.healer != nil && len(s.retries.healer) > 0 { + + for id, task := range s.retries.healer { + if !task.byteCodeReq { + healtask = &healTask{ + id: id, + hashes: task.hashes, + paths: task.paths, + pathsets: task.pathsets, + root: task.root, + task: task.task, + byteCodeReq: task.byteCodeReq, + } + s.requesting.addHealerTask(id, task) + s.retries.deleteHealerTask(id) + return + } + if task.byteCodeReq { + codetask = &healTask{ + id: id, + hashes: task.hashes, + paths: task.paths, + pathsets: task.pathsets, + root: task.root, + task: task.task, + byteCodeReq: task.byteCodeReq, + } + s.requesting.addHealerTask(id, task) + s.retries.deleteHealerTask(id) + return + } + } + } + + return +} + +// HandleRequestError handles the error result +func (s *FullStateDownloadManager) HandleRequestError(accounts []*accountTask, + codes []*byteCodeTasksBundle, + storages *storageTaskBundle, + healtask *healTask, + codetask *healTask, + streamID sttypes.StreamID, err error) { + + s.lock.Lock() + defer s.lock.Unlock() + + for _, task := range accounts { + s.requesting.deleteAccountTask(task.id) + s.retries.addAccountTask(task.id, task) + } + + for _, code := range codes { + s.requesting.deleteCodeTask(code.id) + s.retries.addCodeTask(code.id, code) + } + + if storages != nil { + s.requesting.addStorageTaskBundle(storages.id, storages) + s.retries.deleteStorageTaskBundle(storages.id) + } + + if healtask != nil { + s.retries.addHealerTask(healtask.id, healtask) + s.requesting.deleteHealerTask(healtask.id) + } + + if codetask != nil { + s.retries.addHealerTask(codetask.id, codetask) + s.requesting.deleteHealerTask(codetask.id) + } +} + +// UnpackAccountRanges retrieves the accounts from the range packet and converts from slim +// wire representation to consensus format. The returned data is RLP encoded +// since it's expected to be serialized to disk without further interpretation. +// +// Note, this method does a round of RLP decoding and re-encoding, so only use it +// once and cache the results if need be. Ideally discard the packet afterwards +// to not double the memory use. +func (s *FullStateDownloadManager) UnpackAccountRanges(retAccounts []*message.AccountData) ([]common.Hash, [][]byte, error) { + var ( + hashes = make([]common.Hash, len(retAccounts)) + accounts = make([][]byte, len(retAccounts)) + ) + for i, acc := range retAccounts { + val, err := FullAccountRLP(acc.Body) + if err != nil { + return nil, nil, fmt.Errorf("invalid account %x: %v", acc.Body, err) + } + hashes[i] = common.BytesToHash(acc.Hash) + accounts[i] = val + } + return hashes, accounts, nil +} + +// HandleAccountRequestResult handles get account ranges result +func (s *FullStateDownloadManager) HandleAccountRequestResult(task *accountTask, + retAccounts []*message.AccountData, + proof [][]byte, + origin []byte, + last []byte, + loopID int, + streamID sttypes.StreamID) error { + + hashes, accounts, err := s.UnpackAccountRanges(retAccounts) + if err != nil { + return err + } + + size := common.StorageSize(len(hashes) * common.HashLength) + for _, account := range accounts { + size += common.StorageSize(len(account)) + } + for _, node := range proof { + size += common.StorageSize(len(node)) + } + utils.Logger().Trace(). + Int("hashes", len(hashes)). + Int("accounts", len(accounts)). + Int("proofs", len(proof)). + Interface("bytes", size). + Msg("Delivering range of accounts") + + s.lock.Lock() + defer s.lock.Unlock() + + // Response is valid, but check if peer is signalling that it does not have + // the requested data. For account range queries that means the state being + // retrieved was either already pruned remotely, or the peer is not yet + // synced to our head. + if len(hashes) == 0 && len(accounts) == 0 && len(proof) == 0 { + utils.Logger().Debug(). + Interface("root", s.root). + Msg("Peer rejected account range request") + s.lock.Unlock() + return nil + } + root := s.root + s.lock.Unlock() + + // Reconstruct a partial trie from the response and verify it + keys := make([][]byte, len(hashes)) + for i, key := range hashes { + keys[i] = common.CopyBytes(key[:]) + } + nodes := make(ProofList, len(proof)) + for i, node := range proof { + nodes[i] = node + } + cont, err := trie.VerifyRangeProof(root, origin[:], last[:], keys, accounts, nodes.Set()) + if err != nil { + utils.Logger().Warn().Err(err).Msg("Account range failed proof") + // Signal this request as failed, and ready for rescheduling + return err + } + accs := make([]*types.StateAccount, len(accounts)) + for i, account := range accounts { + acc := new(types.StateAccount) + if err := rlp.DecodeBytes(account, acc); err != nil { + panic(err) // We created these blobs, we must be able to decode them + } + accs[i] = acc + } + + if err := s.processAccountResponse(task, hashes, accs, cont); err != nil { + return err + } + + return nil +} + +// processAccountResponse integrates an already validated account range response +// into the account tasks. +func (s *FullStateDownloadManager) processAccountResponse(task *accountTask, // Task which this request is filling + hashes []common.Hash, // Account hashes in the returned range + accounts []*types.StateAccount, // Expanded accounts in the returned range + cont bool, // Whether the account range has a continuation +) error { + + if _, ok := s.tasks.accountTasks[task.id]; ok { + s.tasks.accountTasks[task.id].res = &accountResponse{ + task: task, + hashes: hashes, + accounts: accounts, + cont: cont, + } + } + + // Ensure that the response doesn't overflow into the subsequent task + last := task.Last.Big() + for i, hash := range hashes { + // Mark the range complete if the last is already included. + // Keep iteration to delete the extra states if exists. + cmp := hash.Big().Cmp(last) + if cmp == 0 { + cont = false + continue + } + if cmp > 0 { + // Chunk overflown, cut off excess + hashes = hashes[:i] + accounts = accounts[:i] + cont = false // Mark range completed + break + } + } + // Iterate over all the accounts and assemble which ones need further sub- + // filling before the entire account range can be persisted. + task.needCode = make([]bool, len(accounts)) + task.needState = make([]bool, len(accounts)) + task.needHeal = make([]bool, len(accounts)) + + task.codeTasks = make(map[common.Hash]struct{}) + task.stateTasks = make(map[common.Hash]common.Hash) + + resumed := make(map[common.Hash]struct{}) + + task.pend = 0 + for i, account := range accounts { + // Check if the account is a contract with an unknown code + if !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) { + if !rawdb.HasCodeWithPrefix(s.db, common.BytesToHash(account.CodeHash)) { + task.codeTasks[common.BytesToHash(account.CodeHash)] = struct{}{} + task.needCode[i] = true + task.pend++ + } + } + // Check if the account is a contract with an unknown storage trie + if account.Root != types.EmptyRootHash { + if !rawdb.HasTrieNode(s.db, hashes[i], nil, account.Root, s.scheme) { + // If there was a previous large state retrieval in progress, + // don't restart it from scratch. This happens if a sync cycle + // is interrupted and resumed later. However, *do* update the + // previous root hash. + if subtasks, ok := task.SubTasks[hashes[i]]; ok { + utils.Logger().Debug().Interface("account", hashes[i]).Interface("root", account.Root).Msg("Resuming large storage retrieval") + for _, subtask := range subtasks { + subtask.root = account.Root + } + task.needHeal[i] = true + resumed[hashes[i]] = struct{}{} + } else { + task.stateTasks[hashes[i]] = account.Root + } + task.needState[i] = true + task.pend++ + } + } + } + // Delete any subtasks that have been aborted but not resumed. This may undo + // some progress if a new peer gives us less accounts than an old one, but for + // now we have to live with that. + for hash := range task.SubTasks { + if _, ok := resumed[hash]; !ok { + utils.Logger().Debug().Interface("account", hash).Msg("Aborting suspended storage retrieval") + delete(task.SubTasks, hash) + } + } + // If the account range contained no contracts, or all have been fully filled + // beforehand, short circuit storage filling and forward to the next task + if task.pend == 0 { + s.forwardAccountTask(task) + return nil + } + // Some accounts are incomplete, leave as is for the storage and contract + // task assigners to pick up and fill + return nil +} + +// HandleBytecodeRequestResult handles get bytecode result +// it is a callback method to invoke when a batch of contract +// bytes codes are received from a remote peer. +func (s *FullStateDownloadManager) HandleBytecodeRequestResult(task interface{}, // Task which this request is filling + reqHashes []common.Hash, // Hashes of the bytecode to avoid double hashing + bytecodes [][]byte, // Actual bytecodes to store into the database (nil = missing) + loopID int, + streamID sttypes.StreamID) error { + + s.lock.RLock() + syncing := !s.snapped + s.lock.RUnlock() + + if syncing { + return s.onByteCodes(task.(*accountTask), bytecodes, reqHashes) + } + return s.onHealByteCodes(task.(*healTask), reqHashes, bytecodes) +} + +// onByteCodes is a callback method to invoke when a batch of contract +// bytes codes are received from a remote peer in the syncing phase. +func (s *FullStateDownloadManager) onByteCodes(task *accountTask, bytecodes [][]byte, reqHashes []common.Hash) error { + var size common.StorageSize + for _, code := range bytecodes { + size += common.StorageSize(len(code)) + } + + utils.Logger().Trace().Int("bytecodes", len(bytecodes)).Interface("bytes", size).Msg("Delivering set of bytecodes") + + s.lock.Lock() + defer s.lock.Unlock() + + // Response is valid, but check if peer is signalling that it does not have + // the requested data. For bytecode range queries that means the peer is not + // yet synced. + if len(bytecodes) == 0 { + utils.Logger().Debug().Msg("Peer rejected bytecode request") + return nil + } + + // Cross reference the requested bytecodes with the response to find gaps + // that the serving node is missing + hasher := sha3.NewLegacyKeccak256().(crypto.KeccakState) + hash := make([]byte, 32) + + codes := make([][]byte, len(reqHashes)) + for i, j := 0, 0; i < len(bytecodes); i++ { + // Find the next hash that we've been served, leaving misses with nils + hasher.Reset() + hasher.Write(bytecodes[i]) + hasher.Read(hash) + + for j < len(reqHashes) && !bytes.Equal(hash, reqHashes[j][:]) { + j++ + } + if j < len(reqHashes) { + codes[j] = bytecodes[i] + j++ + continue + } + // We've either ran out of hashes, or got unrequested data + utils.Logger().Warn().Int("count", len(bytecodes)-i).Msg("Unexpected bytecodes") + // Signal this request as failed, and ready for rescheduling + return errors.New("unexpected bytecode") + } + // Response validated, send it to the scheduler for filling + if err := s.processBytecodeResponse(task, reqHashes, codes); err != nil { + return err + } + + return nil +} + +// processBytecodeResponse integrates an already validated bytecode response +// into the account tasks. +func (s *FullStateDownloadManager) processBytecodeResponse(task *accountTask, // Task which this request is filling + hashes []common.Hash, // Hashes of the bytecode to avoid double hashing + bytecodes [][]byte, // Actual bytecodes to store into the database (nil = missing) +) error { + batch := s.db.NewBatch() + + var ( + codes uint64 + ) + for i, hash := range hashes { + code := bytecodes[i] + + // If the bytecode was not delivered, reschedule it + if code == nil { + task.codeTasks[hash] = struct{}{} + continue + } + // Code was delivered, mark it not needed any more + for j, account := range task.res.accounts { + if task.needCode[j] && hash == common.BytesToHash(account.CodeHash) { + task.needCode[j] = false + task.pend-- + } + } + // Push the bytecode into a database batch + codes++ + rawdb.WriteCode(batch, hash, code) + } + bytes := common.StorageSize(batch.ValueSize()) + if err := batch.Write(); err != nil { + log.Crit("Failed to persist bytecodes", "err", err) + } + s.bytecodeSynced += codes + s.bytecodeBytes += bytes + + utils.Logger().Debug().Interface("count", codes).Float64("bytes", float64(bytes)).Msg("Persisted set of bytecodes") + + // If this delivery completed the last pending task, forward the account task + // to the next chunk + if task.pend == 0 { + s.forwardAccountTask(task) + return nil + } + // Some accounts are still incomplete, leave as is for the storage and contract + // task assigners to pick up and fill. + + return nil +} + +// estimateRemainingSlots tries to determine roughly how many slots are left in +// a contract storage, based on the number of keys and the last hash. This method +// assumes that the hashes are lexicographically ordered and evenly distributed. +func estimateRemainingSlots(hashes int, last common.Hash) (uint64, error) { + if last == (common.Hash{}) { + return 0, errors.New("last hash empty") + } + space := new(big.Int).Mul(math.MaxBig256, big.NewInt(int64(hashes))) + space.Div(space, last.Big()) + if !space.IsUint64() { + // Gigantic address space probably due to too few or malicious slots + return 0, errors.New("too few slots for estimation") + } + return space.Uint64() - uint64(hashes), nil +} + +// Unpack retrieves the storage slots from the range packet and returns them in +// a split flat format that's more consistent with the internal data structures. +func (s *FullStateDownloadManager) UnpackStorages(slots [][]*message.StorageData) ([][]common.Hash, [][][]byte) { + var ( + hashset = make([][]common.Hash, len(slots)) + slotset = make([][][]byte, len(slots)) + ) + for i, slots := range slots { + hashset[i] = make([]common.Hash, len(slots)) + slotset[i] = make([][]byte, len(slots)) + for j, slot := range slots { + hashset[i][j] = common.BytesToHash(slot.Hash) + slotset[i][j] = slot.Body + } + } + return hashset, slotset +} + +// HandleStorageRequestResult handles get storages result when ranges of storage slots +// are received from a remote peer. +func (s *FullStateDownloadManager) HandleStorageRequestResult(mainTask *accountTask, + subTask *storageTask, + reqAccounts []common.Hash, + roots []common.Hash, + origin common.Hash, + limit common.Hash, + receivedSlots [][]*message.StorageData, + proof [][]byte, + loopID int, + streamID sttypes.StreamID) error { + + s.lock.Lock() + defer s.lock.Unlock() + + hashes, slots := s.UnpackStorages(receivedSlots) + + // Gather some trace stats to aid in debugging issues + var ( + hashCount int + slotCount int + size common.StorageSize + ) + for _, hashset := range hashes { + size += common.StorageSize(common.HashLength * len(hashset)) + hashCount += len(hashset) + } + for _, slotset := range slots { + for _, slot := range slotset { + size += common.StorageSize(len(slot)) + } + slotCount += len(slotset) + } + for _, node := range proof { + size += common.StorageSize(len(node)) + } + + utils.Logger().Trace(). + Int("accounts", len(hashes)). + Int("hashes", hashCount). + Int("slots", slotCount). + Int("proofs", len(proof)). + Interface("size", size). + Msg("Delivering ranges of storage slots") + + s.lock.Lock() + defer s.lock.Unlock() + + // Reject the response if the hash sets and slot sets don't match, or if the + // peer sent more data than requested. + if len(hashes) != len(slots) { + utils.Logger().Warn(). + Int("hashset", len(hashes)). + Int("slotset", len(slots)). + Msg("Hash and slot set size mismatch") + return errors.New("hash and slot set size mismatch") + } + if len(hashes) > len(reqAccounts) { + utils.Logger().Warn(). + Int("hashset", len(hashes)). + Int("requested", len(reqAccounts)). + Msg("Hash set larger than requested") + return errors.New("hash set larger than requested") + } + // Response is valid, but check if peer is signalling that it does not have + // the requested data. For storage range queries that means the state being + // retrieved was either already pruned remotely, or the peer is not yet + // synced to our head. + if len(hashes) == 0 && len(proof) == 0 { + utils.Logger().Debug().Msg("Peer rejected storage request") + return nil + } + + // Reconstruct the partial tries from the response and verify them + var cont bool + + // If a proof was attached while the response is empty, it indicates that the + // requested range specified with 'origin' is empty. Construct an empty state + // response locally to finalize the range. + if len(hashes) == 0 && len(proof) > 0 { + hashes = append(hashes, []common.Hash{}) + slots = append(slots, [][]byte{}) + } + for i := 0; i < len(hashes); i++ { + // Convert the keys and proofs into an internal format + keys := make([][]byte, len(hashes[i])) + for j, key := range hashes[i] { + keys[j] = common.CopyBytes(key[:]) + } + nodes := make(ProofList, 0, len(proof)) + if i == len(hashes)-1 { + for _, node := range proof { + nodes = append(nodes, node) + } + } + var err error + if len(nodes) == 0 { + // No proof has been attached, the response must cover the entire key + // space and hash to the origin root. + _, err = trie.VerifyRangeProof(roots[i], nil, nil, keys, slots[i], nil) + if err != nil { + utils.Logger().Warn().Err(err).Msg("Storage slots failed proof") + return err + } + } else { + // A proof was attached, the response is only partial, check that the + // returned data is indeed part of the storage trie + proofdb := nodes.Set() + + cont, err = trie.VerifyRangeProof(roots[i], origin[:], limit[:], keys, slots[i], proofdb) + if err != nil { + utils.Logger().Warn().Err(err).Msg("Storage range failed proof") + return err + } + } + } + + if err := s.processStorageResponse(mainTask, subTask, reqAccounts, roots, hashes, slots, cont); err != nil { + return err + } + + return nil +} + +// processStorageResponse integrates an already validated storage response +// into the account tasks. +func (s *FullStateDownloadManager) processStorageResponse(mainTask *accountTask, // Task which this response belongs to + subTask *storageTask, // Task which this response is filling + accounts []common.Hash, // Account hashes requested, may be only partially filled + roots []common.Hash, // Storage roots requested, may be only partially filled + hashes [][]common.Hash, // Storage slot hashes in the returned range + storageSlots [][][]byte, // Storage slot values in the returned range + cont bool, // Whether the last storage range has a continuation +) error { + batch := ethdb.HookedBatch{ + Batch: s.db.NewBatch(), + OnPut: func(key []byte, value []byte) { + s.storageBytes += common.StorageSize(len(key) + len(value)) + }, + } + var ( + slots int + oldStorageBytes = s.storageBytes + ) + // Iterate over all the accounts and reconstruct their storage tries from the + // delivered slots + for i, account := range accounts { + // If the account was not delivered, reschedule it + if i >= len(hashes) { + mainTask.stateTasks[account] = roots[i] + continue + } + // State was delivered, if complete mark as not needed any more, otherwise + // mark the account as needing healing + for j, hash := range mainTask.res.hashes { + if account != hash { + continue + } + acc := mainTask.res.accounts[j] + + // If the packet contains multiple contract storage slots, all + // but the last are surely complete. The last contract may be + // chunked, so check it's continuation flag. + if subTask == nil && mainTask.needState[j] && (i < len(hashes)-1 || !cont) { + mainTask.needState[j] = false + mainTask.pend-- + } + // If the last contract was chunked, mark it as needing healing + // to avoid writing it out to disk prematurely. + if subTask == nil && !mainTask.needHeal[j] && i == len(hashes)-1 && cont { + mainTask.needHeal[j] = true + } + // If the last contract was chunked, we need to switch to large + // contract handling mode + if subTask == nil && i == len(hashes)-1 && cont { + // If we haven't yet started a large-contract retrieval, create + // the subtasks for it within the main account task + if tasks, ok := mainTask.SubTasks[account]; !ok { + var ( + keys = hashes[i] + chunks = uint64(storageConcurrency) + lastKey common.Hash + ) + if len(keys) > 0 { + lastKey = keys[len(keys)-1] + } + // If the number of slots remaining is low, decrease the + // number of chunks. Somewhere on the order of 10-15K slots + // fit into a packet of 500KB. A key/slot pair is maximum 64 + // bytes, so pessimistically maxRequestSize/64 = 8K. + // + // Chunk so that at least 2 packets are needed to fill a task. + if estimate, err := estimateRemainingSlots(len(keys), lastKey); err == nil { + if n := estimate / (2 * (maxRequestSize / 64)); n+1 < chunks { + chunks = n + 1 + } + utils.Logger().Debug(). + Int("initiators", len(keys)). + Interface("tail", lastKey). + Uint64("remaining", estimate). + Uint64("chunks", chunks). + Msg("Chunked large contract") + } else { + utils.Logger().Debug(). + Int("initiators", len(keys)). + Interface("tail", lastKey). + Uint64("chunks", chunks). + Msg("Chunked large contract") + } + r := newHashRange(lastKey, chunks) + + // Our first task is the one that was just filled by this response. + batch := ethdb.HookedBatch{ + Batch: s.db.NewBatch(), + OnPut: func(key []byte, value []byte) { + s.storageBytes += common.StorageSize(len(key) + len(value)) + }, + } + ownerAccount := account // local assignment for stacktrie writer closure + // options := trie.NewStackTrieOptions() + writeFn := func(owner common.Hash, path []byte, hash common.Hash, blob []byte) { + rawdb.WriteTrieNode(batch, ownerAccount, path, hash, blob, s.scheme) + } + tasks = append(tasks, &storageTask{ + Next: common.Hash{}, + Last: r.End(), + root: acc.Root, + genBatch: batch, + genTrie: trie.NewStackTrie(writeFn), + }) + for r.Next() { + batch := ethdb.HookedBatch{ + Batch: s.db.NewBatch(), + OnPut: func(key []byte, value []byte) { + s.storageBytes += common.StorageSize(len(key) + len(value)) + }, + } + // options := trie.NewStackTrieOptions() + writeFn := func(owner common.Hash, path []byte, hash common.Hash, blob []byte) { + rawdb.WriteTrieNode(batch, ownerAccount, path, hash, blob, s.scheme) + } + tasks = append(tasks, &storageTask{ + Next: r.Start(), + Last: r.End(), + root: acc.Root, + genBatch: batch, + genTrie: trie.NewStackTrie(writeFn), + }) + } + for _, task := range tasks { + utils.Logger().Debug(). + Interface("from", task.Next). + Interface("last", task.Last). + Interface("root", acc.Root). + Interface("account", account). + Msg("Created storage sync task") + } + mainTask.SubTasks[account] = tasks + + // Since we've just created the sub-tasks, this response + // is surely for the first one (zero origin) + subTask = tasks[0] + } + } + // If we're in large contract delivery mode, forward the subtask + if subTask != nil { + // Ensure the response doesn't overflow into the subsequent task + last := subTask.Last.Big() + // Find the first overflowing key. While at it, mark res as complete + // if we find the range to include or pass the 'last' + index := sort.Search(len(hashes[i]), func(k int) bool { + cmp := hashes[i][k].Big().Cmp(last) + if cmp >= 0 { + cont = false + } + return cmp > 0 + }) + if index >= 0 { + // cut off excess + hashes[i] = hashes[i][:index] + storageSlots[i] = storageSlots[i][:index] + } + // Forward the relevant storage chunk (even if created just now) + if cont { + subTask.Next = incHash(hashes[i][len(hashes[i])-1]) + } else { + subTask.done = true + } + } + } + // Iterate over all the complete contracts, reconstruct the trie nodes and + // push them to disk. If the contract is chunked, the trie nodes will be + // reconstructed later. + slots += len(hashes[i]) + + if i < len(hashes)-1 || subTask == nil { + // no need to make local reassignment of account: this closure does not outlive the loop + // options := trie.NewStackTrieOptions() + writeFn := func(owner common.Hash, path []byte, hash common.Hash, blob []byte) { + rawdb.WriteTrieNode(batch, account, path, hash, blob, s.scheme) + } + tr := trie.NewStackTrie(writeFn) + for j := 0; j < len(hashes[i]); j++ { + tr.Update(hashes[i][j][:], storageSlots[i][j]) + } + tr.Commit() + } + // Persist the received storage segments. These flat state maybe + // outdated during the sync, but it can be fixed later during the + // snapshot generation. + for j := 0; j < len(hashes[i]); j++ { + rawdb.WriteStorageSnapshot(batch, account, hashes[i][j], storageSlots[i][j]) + + // If we're storing large contracts, generate the trie nodes + // on the fly to not trash the gluing points + if i == len(hashes)-1 && subTask != nil { + subTask.genTrie.Update(hashes[i][j][:], storageSlots[i][j]) + } + } + } + // Large contracts could have generated new trie nodes, flush them to disk + if subTask != nil { + if subTask.done { + root, _ := subTask.genTrie.Commit() + if root == subTask.root { + // If the chunk's root is an overflown but full delivery, clear the heal request + for i, account := range mainTask.res.hashes { + if account == accounts[len(accounts)-1] { + mainTask.needHeal[i] = false + } + } + } + } + if subTask.genBatch.ValueSize() > ethdb.IdealBatchSize || subTask.done { + if err := subTask.genBatch.Write(); err != nil { + log.Error("Failed to persist stack slots", "err", err) + } + subTask.genBatch.Reset() + } + } + // Flush anything written just now and update the stats + if err := batch.Write(); err != nil { + log.Crit("Failed to persist storage slots", "err", err) + } + s.storageSynced += uint64(slots) + + utils.Logger().Debug(). + Int("accounts", len(hashes)). + Int("slots", slots). + Interface("bytes", s.storageBytes-oldStorageBytes). + Msg("Persisted set of storage slots") + + // If this delivery completed the last pending task, forward the account task + // to the next chunk + if mainTask.pend == 0 { + s.forwardAccountTask(mainTask) + return nil + } + // Some accounts are still incomplete, leave as is for the storage and contract + // task assigners to pick up and fill. + + return nil +} + +// HandleTrieNodeHealRequestResult handles get trie nodes heal result when a batch of trie nodes +// are received from a remote peer. +func (s *FullStateDownloadManager) HandleTrieNodeHealRequestResult(task *healTask, // Task which this request is filling + reqPaths []string, + reqHashes []common.Hash, + trienodes [][]byte, + loopID int, + streamID sttypes.StreamID) error { + + s.lock.Lock() + defer s.lock.Unlock() + + var size common.StorageSize + for _, node := range trienodes { + size += common.StorageSize(len(node)) + } + + utils.Logger().Trace(). + Int("trienodes", len(trienodes)). + Interface("bytes", size). + Msg("Delivering set of healing trienodes") + + // Response is valid, but check if peer is signalling that it does not have + // the requested data. For bytecode range queries that means the peer is not + // yet synced. + if len(trienodes) == 0 { + utils.Logger().Debug().Msg("Peer rejected trienode heal request") + return nil + } + + // Cross reference the requested trienodes with the response to find gaps + // that the serving node is missing + var ( + hasher = sha3.NewLegacyKeccak256().(crypto.KeccakState) + hash = make([]byte, 32) + nodes = make([][]byte, len(reqHashes)) + fills uint64 + ) + for i, j := 0, 0; i < len(trienodes); i++ { + // Find the next hash that we've been served, leaving misses with nils + hasher.Reset() + hasher.Write(trienodes[i]) + hasher.Read(hash) + + for j < len(reqHashes) && !bytes.Equal(hash, reqHashes[j][:]) { + j++ + } + if j < len(reqHashes) { + nodes[j] = trienodes[i] + fills++ + j++ + continue + } + // We've either ran out of hashes, or got unrequested data + utils.Logger().Warn().Int("count", len(trienodes)-i).Msg("Unexpected healing trienodes") + + // Signal this request as failed, and ready for rescheduling + return errors.New("unexpected healing trienode") + } + // Response validated, send it to the scheduler for filling + s.trienodeHealPend.Add(fills) + defer func() { + s.trienodeHealPend.Add(^(fills - 1)) + }() + + if err := s.processTrienodeHealResponse(task, reqPaths, reqHashes, nodes); err != nil { + return err + } + + return nil +} + +// processTrienodeHealResponse integrates an already validated trienode response +// into the healer tasks. +func (s *FullStateDownloadManager) processTrienodeHealResponse(task *healTask, // Task which this request is filling + paths []string, // Paths of the trie nodes + hashes []common.Hash, // Hashes of the trie nodes to avoid double hashing + nodes [][]byte, // Actual trie nodes to store into the database (nil = missing) +) error { + var ( + start = time.Now() + fills int + ) + for i, hash := range hashes { + node := nodes[i] + + // If the trie node was not delivered, reschedule it + if node == nil { + task.trieTasks[paths[i]] = hashes[i] + continue + } + fills++ + + // Push the trie node into the state syncer + s.trienodeHealSynced++ + s.trienodeHealBytes += common.StorageSize(len(node)) + + err := s.scheduler.ProcessNode(trie.NodeSyncResult{Path: paths[i], Data: node}) + switch err { + case nil: + case trie.ErrAlreadyProcessed: + s.trienodeHealDups++ + case trie.ErrNotRequested: + s.trienodeHealNops++ + default: + utils.Logger().Err(err).Interface("hash", hash).Msg("Invalid trienode processed") + } + } + s.commitHealer(false) + + // Calculate the processing rate of one filled trie node + rate := float64(fills) / (float64(time.Since(start)) / float64(time.Second)) + + // Update the currently measured trienode queueing and processing throughput. + // + // The processing rate needs to be updated uniformly independent if we've + // processed 1x100 trie nodes or 100x1 to keep the rate consistent even in + // the face of varying network packets. As such, we cannot just measure the + // time it took to process N trie nodes and update once, we need one update + // per trie node. + // + // Naively, that would be: + // + // for i:=0; i time.Second { + // Periodically adjust the trie node throttler + if float64(pending) > 2*s.trienodeHealRate { + s.trienodeHealThrottle *= trienodeHealThrottleIncrease + } else { + s.trienodeHealThrottle /= trienodeHealThrottleDecrease + } + if s.trienodeHealThrottle > maxTrienodeHealThrottle { + s.trienodeHealThrottle = maxTrienodeHealThrottle + } else if s.trienodeHealThrottle < minTrienodeHealThrottle { + s.trienodeHealThrottle = minTrienodeHealThrottle + } + s.trienodeHealThrottled = time.Now() + + utils.Logger().Debug(). + Float64("rate", s.trienodeHealRate). + Uint64("pending", pending). + Float64("throttle", s.trienodeHealThrottle). + Msg("Updated trie node heal throttler") + } + + return nil +} + +// HandleByteCodeHealRequestResult handles get byte codes heal result +func (s *FullStateDownloadManager) HandleByteCodeHealRequestResult(task *healTask, // Task which this request is filling + hashes []common.Hash, // Hashes of the bytecode to avoid double hashing + codes [][]byte, // Actual bytecodes to store into the database (nil = missing) + loopID int, + streamID sttypes.StreamID) error { + + s.lock.Lock() + defer s.lock.Unlock() + + if err := s.processBytecodeHealResponse(task, hashes, codes); err != nil { + return err + } + + return nil +} + +// onHealByteCodes is a callback method to invoke when a batch of contract +// bytes codes are received from a remote peer in the healing phase. +func (s *FullStateDownloadManager) onHealByteCodes(task *healTask, + reqHashes []common.Hash, + bytecodes [][]byte) error { + + var size common.StorageSize + for _, code := range bytecodes { + size += common.StorageSize(len(code)) + } + + utils.Logger().Trace(). + Int("bytecodes", len(bytecodes)). + Interface("bytes", size). + Msg("Delivering set of healing bytecodes") + + s.lock.Lock() + s.lock.Unlock() + + // Response is valid, but check if peer is signalling that it does not have + // the requested data. For bytecode range queries that means the peer is not + // yet synced. + if len(bytecodes) == 0 { + utils.Logger().Debug().Msg("Peer rejected bytecode heal request") + return nil + } + + // Cross reference the requested bytecodes with the response to find gaps + // that the serving node is missing + hasher := sha3.NewLegacyKeccak256().(crypto.KeccakState) + hash := make([]byte, 32) + + codes := make([][]byte, len(reqHashes)) + for i, j := 0, 0; i < len(bytecodes); i++ { + // Find the next hash that we've been served, leaving misses with nils + hasher.Reset() + hasher.Write(bytecodes[i]) + hasher.Read(hash) + + for j < len(reqHashes) && !bytes.Equal(hash, reqHashes[j][:]) { + j++ + } + if j < len(reqHashes) { + codes[j] = bytecodes[i] + j++ + continue + } + // We've either ran out of hashes, or got unrequested data + utils.Logger().Warn().Int("count", len(bytecodes)-i).Msg("Unexpected healing bytecodes") + + // Signal this request as failed, and ready for rescheduling + return errors.New("unexpected healing bytecode") + } + + if err := s.processBytecodeHealResponse(task, reqHashes, codes); err != nil { + return err + } + + return nil +} + +// processBytecodeHealResponse integrates an already validated bytecode response +// into the healer tasks. +func (s *FullStateDownloadManager) processBytecodeHealResponse(task *healTask, // Task which this request is filling + hashes []common.Hash, // Hashes of the bytecode to avoid double hashing + codes [][]byte, // Actual bytecodes to store into the database (nil = missing) +) error { + for i, hash := range hashes { + node := codes[i] + + // If the trie node was not delivered, reschedule it + if node == nil { + task.codeTasks[hash] = struct{}{} + continue + } + // Push the trie node into the state syncer + s.bytecodeHealSynced++ + s.bytecodeHealBytes += common.StorageSize(len(node)) + + err := s.scheduler.ProcessCode(trie.CodeSyncResult{Hash: hash, Data: node}) + switch err { + case nil: + case trie.ErrAlreadyProcessed: + s.bytecodeHealDups++ + case trie.ErrNotRequested: + s.bytecodeHealNops++ + default: + log.Error("Invalid bytecode processed", "hash", hash, "err", err) + } + } + s.commitHealer(false) + + return nil +} + +// onHealState is a callback method to invoke when a flat state(account +// or storage slot) is downloaded during the healing stage. The flat states +// can be persisted blindly and can be fixed later in the generation stage. +// Note it's not concurrent safe, please handle the concurrent issue outside. +func (s *FullStateDownloadManager) onHealState(paths [][]byte, value []byte) error { + if len(paths) == 1 { + var account types.StateAccount + if err := rlp.DecodeBytes(value, &account); err != nil { + return nil // Returning the error here would drop the remote peer + } + blob := s.SlimAccountRLP(account) + rawdb.WriteAccountSnapshot(s.stateWriter, common.BytesToHash(paths[0]), blob) + s.accountHealed += 1 + s.accountHealedBytes += common.StorageSize(1 + common.HashLength + len(blob)) + } + if len(paths) == 2 { + rawdb.WriteStorageSnapshot(s.stateWriter, common.BytesToHash(paths[0]), common.BytesToHash(paths[1]), value) + s.storageHealed += 1 + s.storageHealedBytes += common.StorageSize(1 + 2*common.HashLength + len(value)) + } + if s.stateWriter.ValueSize() > ethdb.IdealBatchSize { + s.stateWriter.Write() // It's fine to ignore the error here + s.stateWriter.Reset() + } + return nil +} diff --git a/api/service/stagedstreamsync/syncing.go b/api/service/stagedstreamsync/syncing.go index 9e8926468..e6879a523 100644 --- a/api/service/stagedstreamsync/syncing.go +++ b/api/service/stagedstreamsync/syncing.go @@ -11,6 +11,8 @@ import ( "github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/core" + "github.com/harmony-one/harmony/core/rawdb" + "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/internal/utils" sttypes "github.com/harmony-one/harmony/p2p/stream/types" "github.com/ledgerwatch/erigon-lib/kv" @@ -81,20 +83,28 @@ func CreateStagedSync(ctx context.Context, return nil, errInitDB } + extractReceiptHashes := config.SyncMode == FastSync || config.SyncMode == SnapSync stageHeadsCfg := NewStageHeadersCfg(bc, mainDB) stageShortRangeCfg := NewStageShortRangeCfg(bc, mainDB) stageSyncEpochCfg := NewStageEpochCfg(bc, mainDB) - stageBodiesCfg := NewStageBodiesCfg(bc, mainDB, dbs, config.Concurrency, protocol, isBeaconNode, config.LogProgress) + stageBodiesCfg := NewStageBodiesCfg(bc, mainDB, dbs, config.Concurrency, protocol, isBeaconNode, extractReceiptHashes, config.LogProgress) stageStatesCfg := NewStageStatesCfg(bc, mainDB, dbs, config.Concurrency, logger, config.LogProgress) + stageStateSyncCfg := NewStageStateSyncCfg(bc, mainDB, config.Concurrency, protocol, logger, config.LogProgress) + stageReceiptsCfg := NewStageReceiptsCfg(bc, mainDB, dbs, config.Concurrency, protocol, isBeaconNode, config.LogProgress) lastMileCfg := NewStageLastMileCfg(ctx, bc, mainDB) stageFinishCfg := NewStageFinishCfg(mainDB) - stages := DefaultStages(ctx, + // init stages order based on sync mode + initStagesOrder(config.SyncMode) + + defaultStages := DefaultStages(ctx, stageHeadsCfg, stageSyncEpochCfg, stageShortRangeCfg, stageBodiesCfg, + stageStateSyncCfg, stageStatesCfg, + stageReceiptsCfg, lastMileCfg, stageFinishCfg, ) @@ -112,7 +122,7 @@ func CreateStagedSync(ctx context.Context, bc, consensus, mainDB, - stages, + defaultStages, isBeaconNode, protocol, isBeaconNode, @@ -214,6 +224,65 @@ func (s *StagedStreamSync) Debug(source string, msg interface{}) { } } +// checkPivot checks pivot block and returns pivot block and cycle Sync mode +func (s *StagedStreamSync) checkPivot(ctx context.Context, estimatedHeight uint64, initSync bool) (*types.Block, SyncMode, error) { + + if s.config.SyncMode == FullSync { + return nil, FullSync, nil + } + + // do full sync if chain is at early stage + if initSync && estimatedHeight < MaxPivotDistanceToHead { + return nil, FullSync, nil + } + + pivotBlockNumber := uint64(0) + var curPivot *uint64 + if curPivot = rawdb.ReadLastPivotNumber(s.bc.ChainDb()); curPivot != nil { + // if head is behind pivot, that means it is still on fast/snap sync mode + if head := s.CurrentBlockNumber(); head < *curPivot { + pivotBlockNumber = *curPivot + // pivot could be moved forward if it is far from head + if pivotBlockNumber < estimatedHeight-MaxPivotDistanceToHead { + pivotBlockNumber = estimatedHeight - MinPivotDistanceToHead + } + } + } else { + if head := s.CurrentBlockNumber(); s.config.SyncMode == FastSync && head <= 1 { + pivotBlockNumber = estimatedHeight - MinPivotDistanceToHead + if err := rawdb.WriteLastPivotNumber(s.bc.ChainDb(), pivotBlockNumber); err != nil { + s.logger.Warn().Err(err). + Uint64("new pivot number", pivotBlockNumber). + Msg(WrapStagedSyncMsg("update pivot number failed")) + } + } + } + if pivotBlockNumber > 0 { + if block, err := s.queryAllPeersForBlockByNumber(ctx, pivotBlockNumber); err != nil { + s.logger.Error().Err(err). + Uint64("pivot", pivotBlockNumber). + Msg(WrapStagedSyncMsg("query peers for pivot block failed")) + return block, FastSync, err + } else { + if curPivot == nil || pivotBlockNumber != *curPivot { + if err := rawdb.WriteLastPivotNumber(s.bc.ChainDb(), pivotBlockNumber); err != nil { + s.logger.Warn().Err(err). + Uint64("new pivot number", pivotBlockNumber). + Msg(WrapStagedSyncMsg("update pivot number failed")) + return block, FastSync, err + } + } + s.status.pivotBlock = block + s.logger.Info(). + Uint64("estimatedHeight", estimatedHeight). + Uint64("pivot number", pivotBlockNumber). + Msg(WrapStagedSyncMsg("fast/snap sync mode, pivot is set successfully")) + return block, FastSync, nil + } + } + return nil, FullSync, nil +} + // doSync does the long range sync. // One LongRangeSync consists of several iterations. // For each iteration, estimate the current block number, then fetch block & insert to blockchain @@ -224,7 +293,6 @@ func (s *StagedStreamSync) doSync(downloaderContext context.Context, initSync bo var totalInserted int s.initSync = initSync - if err := s.checkPrerequisites(); err != nil { return 0, 0, err } @@ -238,13 +306,23 @@ func (s *StagedStreamSync) doSync(downloaderContext context.Context, initSync bo //TODO: use directly currentCycle var s.status.setTargetBN(estimatedHeight) } - if curBN := s.bc.CurrentBlock().NumberU64(); estimatedHeight <= curBN { + if curBN := s.CurrentBlockNumber(); estimatedHeight <= curBN { s.logger.Info().Uint64("current number", curBN).Uint64("target number", estimatedHeight). Msg(WrapStagedSyncMsg("early return of long range sync (chain is already ahead of target height)")) return estimatedHeight, 0, nil } } + // We are probably in full sync, but we might have rewound to before the + // fast/snap sync pivot, check if we should reenable + if pivotBlock, cycleSyncMode, err := s.checkPivot(downloaderContext, estimatedHeight, initSync); err != nil { + s.logger.Error().Err(err).Msg(WrapStagedSyncMsg("check pivot failed")) + return 0, 0, err + } else { + s.status.cycleSyncMode = cycleSyncMode + s.status.pivotBlock = pivotBlock + } + s.startSyncing() defer s.finishSyncing() @@ -289,7 +367,7 @@ func (s *StagedStreamSync) doSync(downloaderContext context.Context, initSync bo } // add consensus last mile blocks - if s.consensus != nil { + if s.consensus != nil && s.isBeaconNode { if hashes, err := s.addConsensusLastMile(s.Blockchain(), s.consensus); err != nil { utils.Logger().Error().Err(err). Msg("[STAGED_STREAM_SYNC] Add consensus last mile failed") @@ -315,7 +393,7 @@ func (s *StagedStreamSync) doSyncCycle(ctx context.Context) (int, error) { var totalInserted int s.inserted = 0 - startHead := s.bc.CurrentBlock().NumberU64() + startHead := s.CurrentBlockNumber() canRunCycleInOneTransaction := false var tx kv.RwTx @@ -379,6 +457,36 @@ func (s *StagedStreamSync) checkPrerequisites() error { return s.checkHaveEnoughStreams() } +func (s *StagedStreamSync) CurrentBlockNumber() uint64 { + // if current head is ahead of pivot block, return chain head regardless of sync mode + if s.status.pivotBlock != nil && s.bc.CurrentBlock().NumberU64() >= s.status.pivotBlock.NumberU64() { + return s.bc.CurrentBlock().NumberU64() + } + + current := uint64(0) + switch s.config.SyncMode { + case FullSync: + current = s.bc.CurrentBlock().NumberU64() + case FastSync: + current = s.bc.CurrentFastBlock().NumberU64() + case SnapSync: + current = s.bc.CurrentHeader().Number().Uint64() + } + return current +} + +func (s *StagedStreamSync) stateSyncStage() bool { + switch s.config.SyncMode { + case FullSync: + return false + case FastSync: + return s.status.pivotBlock != nil && s.bc.CurrentFastBlock().NumberU64() == s.status.pivotBlock.NumberU64()-1 + case SnapSync: + return false + } + return false +} + // estimateCurrentNumber roughly estimates the current block number. // The block number does not need to be exact, but just a temporary target of the iteration func (s *StagedStreamSync) estimateCurrentNumber(ctx context.Context) (uint64, error) { @@ -418,3 +526,45 @@ func (s *StagedStreamSync) estimateCurrentNumber(ctx context.Context) (uint64, e bn := computeBlockNumberByMaxVote(cnResults) return bn, nil } + +// queryAllPeersForBlockByNumber queries all connected streams for a block by its number. +func (s *StagedStreamSync) queryAllPeersForBlockByNumber(ctx context.Context, bn uint64) (*types.Block, error) { + var ( + blkResults []*types.Block + lock sync.Mutex + wg sync.WaitGroup + ) + wg.Add(s.config.Concurrency) + for i := 0; i != s.config.Concurrency; i++ { + go func() { + defer wg.Done() + block, stid, err := s.doGetBlockByNumberRequest(ctx, bn) + if err != nil { + s.logger.Err(err).Str("streamID", string(stid)). + Msg(WrapStagedSyncMsg("getBlockByNumber request failed")) + if !errors.Is(err, context.Canceled) { + s.protocol.StreamFailed(stid, "getBlockByNumber request failed") + } + return + } + lock.Lock() + blkResults = append(blkResults, block) + lock.Unlock() + }() + } + wg.Wait() + + if len(blkResults) == 0 { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + return nil, ErrZeroBlockResponse + } + block, err := getBlockByMaxVote(blkResults) + if err != nil { + return nil, err + } + return block, nil +} diff --git a/api/service/stagedstreamsync/types.go b/api/service/stagedstreamsync/types.go index 6d6326452..e46b61429 100644 --- a/api/service/stagedstreamsync/types.go +++ b/api/service/stagedstreamsync/types.go @@ -14,9 +14,12 @@ var ( ) type status struct { - isSyncing bool - targetBN uint64 - lock sync.Mutex + isSyncing bool + targetBN uint64 + pivotBlock *types.Block + cycleSyncMode SyncMode + statesSynced bool + lock sync.Mutex } func newStatus() status { diff --git a/cmd/harmony/default.go b/cmd/harmony/default.go index 986a2f7f6..86ed4226a 100644 --- a/cmd/harmony/default.go +++ b/cmd/harmony/default.go @@ -192,6 +192,7 @@ var defaultStagedSyncConfig = harmonyconfig.StagedSyncConfig{ var ( defaultMainnetSyncConfig = harmonyconfig.SyncConfig{ Enabled: false, + SyncMode: 0, Downloader: false, StagedSync: false, StagedSyncCfg: defaultStagedSyncConfig, @@ -207,6 +208,7 @@ var ( defaultTestNetSyncConfig = harmonyconfig.SyncConfig{ Enabled: true, + SyncMode: 0, Downloader: false, StagedSync: false, StagedSyncCfg: defaultStagedSyncConfig, @@ -222,6 +224,7 @@ var ( defaultLocalNetSyncConfig = harmonyconfig.SyncConfig{ Enabled: true, + SyncMode: 0, Downloader: true, StagedSync: true, StagedSyncCfg: defaultStagedSyncConfig, @@ -237,6 +240,7 @@ var ( defaultPartnerSyncConfig = harmonyconfig.SyncConfig{ Enabled: true, + SyncMode: 0, Downloader: true, StagedSync: false, StagedSyncCfg: defaultStagedSyncConfig, @@ -252,6 +256,7 @@ var ( defaultElseSyncConfig = harmonyconfig.SyncConfig{ Enabled: true, + SyncMode: 0, Downloader: true, StagedSync: false, StagedSyncCfg: defaultStagedSyncConfig, diff --git a/cmd/harmony/flags.go b/cmd/harmony/flags.go index 46a1decb0..2af21cb24 100644 --- a/cmd/harmony/flags.go +++ b/cmd/harmony/flags.go @@ -238,6 +238,7 @@ var ( syncFlags = []cli.Flag{ syncStreamEnabledFlag, + syncModeFlag, syncDownloaderFlag, syncStagedSyncFlag, syncConcurrencyFlag, @@ -1876,6 +1877,13 @@ var ( Usage: "Enable the stream sync protocol (experimental feature)", DefValue: false, } + + syncModeFlag = cli.IntFlag{ + Name: "sync.mode", + Usage: "synchronization mode of the downloader (0=FullSync, 1=FastSync, 2=SnapSync)", + DefValue: 0, + } + // TODO: Deprecate this flag, and always set to true after stream sync is fully up. syncDownloaderFlag = cli.BoolFlag{ Name: "sync.downloader", @@ -1937,6 +1945,10 @@ func applySyncFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) { config.Sync.Enabled = cli.GetBoolFlagValue(cmd, syncStreamEnabledFlag) } + if cli.IsFlagChanged(cmd, syncModeFlag) { + config.Sync.SyncMode = uint32(cli.GetIntFlagValue(cmd, syncModeFlag)) + } + if cli.IsFlagChanged(cmd, syncDownloaderFlag) { config.Sync.Downloader = cli.GetBoolFlagValue(cmd, syncDownloaderFlag) } diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 549237d1c..ec05e2419 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -1005,6 +1005,7 @@ func setupStagedSyncService(node *node.Node, host p2p.Host, hc harmonyconfig.Har sConfig := stagedstreamsync.Config{ ServerOnly: !hc.Sync.Downloader, + SyncMode: stagedstreamsync.SyncMode(hc.Sync.SyncMode), Network: nodeconfig.NetworkType(hc.Network.NetworkType), Concurrency: hc.Sync.Concurrency, MinStreams: hc.Sync.MinPeers, @@ -1016,7 +1017,7 @@ func setupStagedSyncService(node *node.Node, host p2p.Host, hc harmonyconfig.Har SmDiscBatch: hc.Sync.DiscBatch, UseMemDB: hc.Sync.StagedSyncCfg.UseMemDB, LogProgress: hc.Sync.StagedSyncCfg.LogProgress, - DebugMode: hc.Sync.StagedSyncCfg.DebugMode, + DebugMode: true, // hc.Sync.StagedSyncCfg.DebugMode, } // If we are running side chain, we will need to do some extra works for beacon diff --git a/core/blockchain.go b/core/blockchain.go index 41f72a9a2..f47133bad 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -52,6 +52,11 @@ type BlockChain interface { // CurrentBlock retrieves the current head block of the canonical chain. The // block is retrieved from the blockchain's internal cache. CurrentBlock() *types.Block + // CurrentFastBlock retrieves the current fast-sync head block of the canonical + // block is retrieved from the blockchain's internal cache. + CurrentFastBlock() *types.Block + // Validator returns the current validator. + Validator() Validator // Processor returns the current processor. Processor() Processor // State returns a new mutable state based on the current HEAD block. @@ -100,6 +105,20 @@ type BlockChain interface { // Rollback is designed to remove a chain of links from the database that aren't // certain enough to be valid. Rollback(chain []common.Hash) error + // writeHeadBlock writes a new head block + WriteHeadBlock(block *types.Block) error + // WriteBlockWithoutState writes only the block and its metadata to the database, + // but does not write any state. This is used to construct competing side forks + // up to the point where they exceed the canonical total difficulty. + WriteBlockWithoutState(block *types.Block) (err error) + // WriteBlockWithState writes the block and all associated state to the database. + WriteBlockWithState( + block *types.Block, receipts []*types.Receipt, + cxReceipts []*types.CXReceipt, + stakeMsgs []types2.StakeMsg, + paid reward.Reader, + state *state.DB, + ) (status WriteStatus, err error) // GetMaxGarbageCollectedBlockNumber .. GetMaxGarbageCollectedBlockNumber() int64 // InsertChain attempts to insert the given batch of blocks in to the canonical @@ -109,7 +128,10 @@ type BlockChain interface { // // After insertion is done, all accumulated events will be fired. InsertChain(chain types.Blocks, verifyHeaders bool) (int, error) - // LeaderRotationMeta returns info about leader rotation. + // InsertReceiptChain attempts to complete an already existing header chain with + // transaction and receipt data. + InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) + // LeaderRotationMeta returns the number of continuous blocks by the leader. LeaderRotationMeta() LeaderRotationMeta // BadBlocks returns a list of the last 'bad blocks' that // the client has seen on the network. diff --git a/core/blockchain_impl.go b/core/blockchain_impl.go index cc3031567..c7f01d413 100644 --- a/core/blockchain_impl.go +++ b/core/blockchain_impl.go @@ -34,6 +34,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/common/prque" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/metrics" @@ -69,8 +70,9 @@ import ( ) var ( - headBlockGauge = metrics.NewRegisteredGauge("chain/head/block", nil) - headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil) + headBlockGauge = metrics.NewRegisteredGauge("chain/head/block", nil) + headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil) + headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil) accountReadTimer = metrics.NewRegisteredTimer("chain/account/reads", nil) accountHashTimer = metrics.NewRegisteredTimer("chain/account/hashes", nil) @@ -185,7 +187,8 @@ type BlockChainImpl struct { pendingCrossLinksMutex sync.RWMutex // pending crosslinks lock pendingSlashingCandidatesMU sync.RWMutex // pending slashing candidates - currentBlock atomic.Value // Current head of the block chain + currentBlock atomic.Value // Current head of the block chain + currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!) stateCache state.Database // State database to reuse between imports (contains state cache) bodyCache *lru.Cache // Cache for the most recent block bodies @@ -319,6 +322,7 @@ func newBlockChainWithOptions( } var nilBlock *types.Block bc.currentBlock.Store(nilBlock) + bc.currentFastBlock.Store(nilBlock) if err := bc.loadLastState(); err != nil { return nil, err } @@ -612,8 +616,22 @@ func (bc *BlockChainImpl) loadLastState() error { return errors.Wrap(err, "headerChain SetCurrentHeader") } + // Restore the last known head fast block + bc.currentFastBlock.Store(currentBlock) + headFastBlockGauge.Update(int64(currentBlock.NumberU64())) + if head := rawdb.ReadHeadFastBlockHash(bc.db); head != (common.Hash{}) { + if block := bc.GetBlockByHash(head); block != nil { + bc.currentFastBlock.Store(block) + headFastBlockGauge.Update(int64(block.NumberU64())) + } + } + + // Issue a status log for the user + currentFastBlock := bc.CurrentFastBlock() + headerTd := bc.GetTd(currentHeader.Hash(), currentHeader.Number().Uint64()) blockTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) + fastTd := bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64()) utils.Logger().Info(). Str("number", currentHeader.Number().String()). @@ -627,6 +645,12 @@ func (bc *BlockChainImpl) loadLastState() error { Str("td", blockTd.String()). Str("age", common.PrettyAge(time.Unix(currentBlock.Time().Int64(), 0)).String()). Msg("Loaded most recent local full block") + utils.Logger().Info(). + Str("number", currentFastBlock.Number().String()). + Str("hash", currentFastBlock.Hash().Hex()). + Str("td", fastTd.String()). + Str("age", common.PrettyAge(time.Unix(currentFastBlock.Time().Int64(), 0)).String()). + Msg("Loaded most recent local fast block") return nil } @@ -663,16 +687,30 @@ func (bc *BlockChainImpl) setHead(head uint64) error { headBlockGauge.Update(int64(bc.genesisBlock.NumberU64())) } } + // Rewind the fast block in a simpleton way to the target head + if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock != nil && currentHeader.Number().Uint64() < currentFastBlock.NumberU64() { + newHeadFastBlock := bc.GetBlock(currentHeader.Hash(), currentHeader.Number().Uint64()) + bc.currentFastBlock.Store(newHeadFastBlock) + headFastBlockGauge.Update(int64(newHeadFastBlock.NumberU64())) + } // If either blocks reached nil, reset to the genesis state if currentBlock := bc.CurrentBlock(); currentBlock == nil { bc.currentBlock.Store(bc.genesisBlock) headBlockGauge.Update(int64(bc.genesisBlock.NumberU64())) } + if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock == nil { + bc.currentFastBlock.Store(bc.genesisBlock) + headFastBlockGauge.Update(int64(bc.genesisBlock.NumberU64())) + } currentBlock := bc.CurrentBlock() + currentFastBlock := bc.CurrentFastBlock() if err := rawdb.WriteHeadBlockHash(bc.db, currentBlock.Hash()); err != nil { return err } + if err := rawdb.WriteHeadFastBlockHash(bc.db, currentFastBlock.Hash()); err != nil { + return err + } return bc.loadLastState() } @@ -685,6 +723,17 @@ func (bc *BlockChainImpl) CurrentBlock() *types.Block { return bc.currentBlock.Load().(*types.Block) } +// CurrentFastBlock retrieves the current fast-sync head block of the canonical +// chain. The block is retrieved from the blockchain's internal cache. +func (bc *BlockChainImpl) CurrentFastBlock() *types.Block { + return bc.currentFastBlock.Load().(*types.Block) +} + +// Validator returns the current validator. +func (bc *BlockChainImpl) Validator() Validator { + return bc.validator +} + func (bc *BlockChainImpl) Processor() Processor { return bc.processor } @@ -727,6 +776,8 @@ func (bc *BlockChainImpl) resetWithGenesisBlock(genesis *types.Block) error { } bc.currentBlock.Store(bc.genesisBlock) headBlockGauge.Update(int64(bc.genesisBlock.NumberU64())) + bc.currentFastBlock.Store(bc.genesisBlock) + headFastBlockGauge.Update(int64(bc.genesisBlock.NumberU64())) return nil } @@ -828,6 +879,10 @@ func (bc *BlockChainImpl) ExportN(w io.Writer, first uint64, last uint64) error return nil } +func (bc *BlockChainImpl) WriteHeadBlock(block *types.Block) error { + return bc.writeHeadBlock(block) +} + // writeHeadBlock writes a new head block func (bc *BlockChainImpl) writeHeadBlock(block *types.Block) error { // If the block is on a side chain or an unknown one, force other heads onto it too @@ -841,6 +896,20 @@ func (bc *BlockChainImpl) writeHeadBlock(block *types.Block) error { if err := rawdb.WriteHeadBlockHash(batch, block.Hash()); err != nil { return err } + if err := rawdb.WriteHeadHeaderHash(batch, block.Hash()); err != nil { + return err + } + + isNewEpoch := block.IsLastBlockInEpoch() + if isNewEpoch { + epoch := block.Header().Epoch() + nextEpoch := epoch.Add(epoch, common.Big1) + if err := rawdb.WriteShardStateBytes(batch, nextEpoch, block.Header().ShardState()); err != nil { + utils.Logger().Error().Err(err).Msg("failed to store shard state") + return err + } + } + if err := batch.Write(); err != nil { return err } @@ -856,6 +925,9 @@ func (bc *BlockChainImpl) writeHeadBlock(block *types.Block) error { if err := rawdb.WriteHeadFastBlockHash(bc.db, block.Hash()); err != nil { return err } + + bc.currentFastBlock.Store(block) + headFastBlockGauge.Update(int64(block.NumberU64())) } return nil } @@ -869,6 +941,9 @@ func (bc *BlockChainImpl) tikvFastForward(block *types.Block, logs []*types.Log) return errors.Wrap(err, "HeaderChain SetCurrentHeader") } + bc.currentFastBlock.Store(block) + headFastBlockGauge.Update(int64(block.NumberU64())) + var events []interface{} events = append(events, ChainEvent{block, block.Hash(), logs}) events = append(events, ChainHeadEvent{block}) @@ -1170,6 +1245,14 @@ func (bc *BlockChainImpl) Rollback(chain []common.Hash) error { } } } + if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock != nil && currentFastBlock.Hash() == hash { + newFastBlock := bc.GetBlock(currentFastBlock.ParentHash(), currentFastBlock.NumberU64()-1) + if newFastBlock != nil { + bc.currentFastBlock.Store(newFastBlock) + headFastBlockGauge.Update(int64(newFastBlock.NumberU64())) + rawdb.WriteHeadFastBlockHash(bc.db, newFastBlock.Hash()) + } + } if currentBlock := bc.CurrentBlock(); currentBlock != nil && currentBlock.Hash() == hash { newBlock := bc.GetBlock(currentBlock.ParentHash(), currentBlock.NumberU64()-1) if newBlock != nil { @@ -1192,9 +1275,191 @@ func (bc *BlockChainImpl) Rollback(chain []common.Hash) error { return bc.removeInValidatorList(valsToRemove) } +// SetReceiptsData computes all the non-consensus fields of the receipts +func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts types.Receipts) error { + signer := types.MakeSigner(config, block.Epoch()) + ethSigner := types.NewEIP155Signer(config.EthCompatibleChainID) + + transactions, stakingTransactions, logIndex := block.Transactions(), block.StakingTransactions(), uint(0) + if len(transactions)+len(stakingTransactions) != len(receipts) { + return errors.New("transaction+stakingTransactions and receipt count mismatch") + } + + // The used gas can be calculated based on previous receipts + if len(receipts) > 0 && len(transactions) > 0 { + receipts[0].GasUsed = receipts[0].CumulativeGasUsed + } + for j := 1; j < len(transactions); j++ { + // The transaction hash can be retrieved from the transaction itself + receipts[j].TxHash = transactions[j].Hash() + receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed + // The contract address can be derived from the transaction itself + if transactions[j].To() == nil { + // Deriving the signer is expensive, only do if it's actually needed + var from common.Address + if transactions[j].IsEthCompatible() { + from, _ = types.Sender(ethSigner, transactions[j]) + } else { + from, _ = types.Sender(signer, transactions[j]) + } + receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce()) + } + // The derived log fields can simply be set from the block and transaction + for k := 0; k < len(receipts[j].Logs); k++ { + receipts[j].Logs[k].BlockNumber = block.NumberU64() + receipts[j].Logs[k].BlockHash = block.Hash() + receipts[j].Logs[k].TxHash = receipts[j].TxHash + receipts[j].Logs[k].TxIndex = uint(j) + receipts[j].Logs[k].Index = logIndex + logIndex++ + } + } + + // The used gas can be calculated based on previous receipts + if len(receipts) > len(transactions) && len(stakingTransactions) > 0 { + receipts[len(transactions)].GasUsed = receipts[len(transactions)].CumulativeGasUsed + } + // in a block, txns are processed before staking txns + for j := len(transactions) + 1; j < len(transactions)+len(stakingTransactions); j++ { + // The transaction hash can be retrieved from the staking transaction itself + receipts[j].TxHash = stakingTransactions[j].Hash() + receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed + // The derived log fields can simply be set from the block and transaction + for k := 0; k < len(receipts[j].Logs); k++ { + receipts[j].Logs[k].BlockNumber = block.NumberU64() + receipts[j].Logs[k].BlockHash = block.Hash() + receipts[j].Logs[k].TxHash = receipts[j].TxHash + receipts[j].Logs[k].TxIndex = uint(j) + uint(len(transactions)) + receipts[j].Logs[k].Index = logIndex + logIndex++ + } + } + return nil +} + +// InsertReceiptChain attempts to complete an already existing header chain with +// transaction and receipt data. +func (bc *BlockChainImpl) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) { + // Do a sanity check that the provided chain is actually ordered and linked + for i := 1; i < len(blockChain); i++ { + if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() { + utils.Logger().Error(). + Str("number", blockChain[i].Number().String()). + Str("hash", blockChain[i].Hash().Hex()). + Str("parent", blockChain[i].ParentHash().Hex()). + Str("prevnumber", blockChain[i-1].Number().String()). + Str("prevhash", blockChain[i-1].Hash().Hex()). + Msg("Non contiguous receipt insert") + return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, blockChain[i-1].NumberU64(), + blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4]) + } + } + + bc.chainmu.Lock() + defer bc.chainmu.Unlock() + + var ( + stats = struct{ processed, ignored int32 }{} + start = time.Now() + bytes = 0 + batch = bc.db.NewBatch() + ) + for i, block := range blockChain { + receipts := receiptChain[i] + // Short circuit insertion if shutting down or processing failed + if atomic.LoadInt32(&bc.procInterrupt) == 1 { + return 0, fmt.Errorf("Premature abort during blocks processing") + } + // Add header if the owner header is unknown + if !bc.HasHeader(block.Hash(), block.NumberU64()) { + if err := rawdb.WriteHeader(batch, block.Header()); err != nil { + return 0, err + } + // return 0, fmt.Errorf("containing header #%d [%x…] unknown", block.Number(), block.Hash().Bytes()[:4]) + } + // Skip if the entire data is already known + if bc.HasBlock(block.Hash(), block.NumberU64()) { + stats.ignored++ + continue + } + // Compute all the non-consensus fields of the receipts + if err := SetReceiptsData(bc.chainConfig, block, receipts); err != nil { + return 0, fmt.Errorf("failed to set receipts data: %v", err) + } + // Write all the data out into the database + if err := rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body()); err != nil { + return 0, err + } + if err := rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts); err != nil { + return 0, err + } + if err := rawdb.WriteBlockTxLookUpEntries(batch, block); err != nil { + return 0, err + } + if err := rawdb.WriteBlockStxLookUpEntries(batch, block); err != nil { + return 0, err + } + + isNewEpoch := block.IsLastBlockInEpoch() + if isNewEpoch { + epoch := block.Header().Epoch() + nextEpoch := epoch.Add(epoch, common.Big1) + err := rawdb.WriteShardStateBytes(batch, nextEpoch, block.Header().ShardState()) + if err != nil { + utils.Logger().Error().Err(err).Msg("failed to store shard state") + return 0, err + } + } + + stats.processed++ + + if batch.ValueSize() >= ethdb.IdealBatchSize { + if err := batch.Write(); err != nil { + return 0, err + } + bytes += batch.ValueSize() + batch.Reset() + } + } + if batch.ValueSize() > 0 { + bytes += batch.ValueSize() + if err := batch.Write(); err != nil { + return 0, err + } + } + + // Update the head fast sync block if better + head := blockChain[len(blockChain)-1] + rawdb.WriteHeadFastBlockHash(bc.db, head.Hash()) + bc.currentFastBlock.Store(head) + + utils.Logger().Info(). + Int32("count", stats.processed). + Str("elapsed", common.PrettyDuration(time.Since(start)).String()). + Str("age", common.PrettyAge(time.Unix(head.Time().Int64(), 0)).String()). + Str("head", head.Number().String()). + Str("hash", head.Hash().Hex()). + Str("size", common.StorageSize(bytes).String()). + Int32("ignored", stats.ignored). + Msg("Imported new block receipts") + + return int(stats.processed), nil +} + var lastWrite uint64 -func (bc *BlockChainImpl) writeBlockWithState( +func (bc *BlockChainImpl) WriteBlockWithoutState(block *types.Block) (err error) { + bc.chainmu.Lock() + defer bc.chainmu.Unlock() + + if err := rawdb.WriteBlock(bc.db, block); err != nil { + return err + } + + return nil +} + +func (bc *BlockChainImpl) WriteBlockWithState( block *types.Block, receipts []*types.Receipt, cxReceipts []*types.CXReceipt, stakeMsgs []staking.StakeMsg, @@ -1506,7 +1771,9 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i // Prune in case non-empty winner chain if len(winner) > 0 { // Import all the pruned blocks to make the state available + bc.chainmu.Unlock() _, evs, logs, err := bc.insertChain(winner, true /* verifyHeaders */) + bc.chainmu.Lock() events, coalescedLogs = evs, logs if err != nil { @@ -1583,7 +1850,7 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i // Write the block to the chain and get the status. substart = time.Now() - status, err := bc.writeBlockWithState( + status, err := bc.WriteBlockWithState( block, receipts, cxReceipts, stakeMsgs, payout, state, ) if err != nil { diff --git a/core/blockchain_stub.go b/core/blockchain_stub.go index e9ef10ce9..437bc32e7 100644 --- a/core/blockchain_stub.go +++ b/core/blockchain_stub.go @@ -49,6 +49,10 @@ func (a Stub) CurrentBlock() *types.Block { return nil } +func (a Stub) CurrentFastBlock() *types.Block { + return nil +} + func (a Stub) Validator() Validator { return nil } @@ -120,7 +124,7 @@ func (a Stub) Rollback(chain []common.Hash) error { return errors.Errorf("method Rollback not implemented for %s", a.Name) } -func (a Stub) WriteBlockWithoutState(block *types.Block, td *big.Int) (err error) { +func (a Stub) WriteBlockWithoutState(block *types.Block) (err error) { return errors.Errorf("method WriteBlockWithoutState not implemented for %s", a.Name) } @@ -136,6 +140,10 @@ func (a Stub) InsertChain(chain types.Blocks, verifyHeaders bool) (int, error) { return 0, errors.Errorf("method InsertChain not implemented for %s", a.Name) } +func (a Stub) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) { + return 0, errors.Errorf("method InsertReceiptChain not implemented for %s", a.Name) +} + func (a Stub) BadBlocks() []BadBlock { return nil } diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 72ce358e2..b01dc0965 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -597,14 +597,17 @@ func ReadLastPivotNumber(db ethdb.KeyValueReader) *uint64 { } // WriteLastPivotNumber stores the number of the last pivot block. -func WriteLastPivotNumber(db ethdb.KeyValueWriter, pivot uint64) { +func WriteLastPivotNumber(db ethdb.KeyValueWriter, pivot uint64) error { enc, err := rlp.EncodeToBytes(pivot) if err != nil { utils.Logger().Error().Err(err).Msg("Failed to encode pivot block number") + return err } if err := db.Put(lastPivotKey, enc); err != nil { utils.Logger().Error().Err(err).Msg("Failed to store pivot block number") + return err } + return nil } // ReadTxIndexTail retrieves the number of oldest indexed block diff --git a/core/rawdb/accessors_offchain.go b/core/rawdb/accessors_offchain.go index 4808c8c23..05a2321a2 100644 --- a/core/rawdb/accessors_offchain.go +++ b/core/rawdb/accessors_offchain.go @@ -22,7 +22,7 @@ func ReadShardState( data, err := db.Get(shardStateKey(epoch)) if err != nil { return nil, errors.Errorf( - MsgNoShardStateFromDB, "epoch: %d", epoch, + MsgNoShardStateFromDB, "epoch: %d", epoch.Uint64(), ) } ss, err2 := shard.DecodeWrapper(data) diff --git a/internal/configs/harmony/harmony.go b/internal/configs/harmony/harmony.go index 2fcb200c4..7ff250148 100644 --- a/internal/configs/harmony/harmony.go +++ b/internal/configs/harmony/harmony.go @@ -329,6 +329,7 @@ type PrometheusConfig struct { type SyncConfig struct { // TODO: Remove this bool after stream sync is fully up. Enabled bool // enable the stream sync protocol + SyncMode uint32 // sync mode (default:Full sync, 1: Fast Sync, 2: Snap Sync(not implemented yet)) Downloader bool // start the sync downloader client StagedSync bool // use staged sync StagedSyncCfg StagedSyncConfig // staged sync configurations diff --git a/p2p/stream/protocols/sync/chain.go b/p2p/stream/protocols/sync/chain.go index efabd9307..451952bcc 100644 --- a/p2p/stream/protocols/sync/chain.go +++ b/p2p/stream/protocols/sync/chain.go @@ -171,7 +171,10 @@ func (ch *chainHelperImpl) getNodeData(hs []common.Hash) ([][]byte, error) { entry, err = ch.chain.ValidatorCode(hash) } } - if err == nil && len(entry) > 0 { + if err != nil { + return nil, err + } + if len(entry) > 0 { nodes = append(nodes, entry) bytes += len(entry) } @@ -196,7 +199,7 @@ func (ch *chainHelperImpl) getReceipts(hs []common.Hash) ([]types.Receipts, erro return receipts, nil } -// getAccountRangeRequest +// getAccountRange func (ch *chainHelperImpl) getAccountRange(root common.Hash, origin common.Hash, limit common.Hash, bytes uint64) ([]*message.AccountData, [][]byte, error) { if bytes > softResponseLimit { bytes = softResponseLimit diff --git a/p2p/stream/protocols/sync/client.go b/p2p/stream/protocols/sync/client.go index 9024142ce..45707e119 100644 --- a/p2p/stream/protocols/sync/client.go +++ b/p2p/stream/protocols/sync/client.go @@ -184,7 +184,7 @@ func (p *Protocol) GetNodeData(ctx context.Context, hs []common.Hash, opts ...Op // GetAccountRange do getAccountRange through sync stream protocol. // returns the accounts along with proofs as result, target stream id, and error -func (p *Protocol) GetAccountRange(ctx context.Context, root common.Hash, origin common.Hash, limit common.Hash, bytes uint64, opts ...Option) (accounts []*message.AccountData, proof []common.Hash, stid sttypes.StreamID, err error) { +func (p *Protocol) GetAccountRange(ctx context.Context, root common.Hash, origin common.Hash, limit common.Hash, bytes uint64, opts ...Option) (accounts []*message.AccountData, proof [][]byte, stid sttypes.StreamID, err error) { timer := p.doMetricClientRequest("getAccountRange") defer p.doMetricPostClientRequest("getAccountRange", err, timer) @@ -207,7 +207,7 @@ func (p *Protocol) GetAccountRange(ctx context.Context, root common.Hash, origin // GetStorageRanges do getStorageRanges through sync stream protocol. // returns the slots along with proofs as result, target stream id, and error -func (p *Protocol) GetStorageRanges(ctx context.Context, root common.Hash, accounts []common.Hash, origin common.Hash, limit common.Hash, bytes uint64, opts ...Option) (slots []*message.StorageData, proof []common.Hash, stid sttypes.StreamID, err error) { +func (p *Protocol) GetStorageRanges(ctx context.Context, root common.Hash, accounts []common.Hash, origin common.Hash, limit common.Hash, bytes uint64, opts ...Option) (slots [][]*message.StorageData, proof [][]byte, stid sttypes.StreamID, err error) { timer := p.doMetricClientRequest("getStorageRanges") defer p.doMetricPostClientRequest("getStorageRanges", err, timer) @@ -233,11 +233,9 @@ func (p *Protocol) GetStorageRanges(ctx context.Context, root common.Hash, accou if err != nil { return } - slots = make([]*message.StorageData, 0) + slots = make([][]*message.StorageData, 0) for _, storage := range storages { - for _, data := range storage.Data { - slots = append(slots, data) - } + slots = append(slots, storage.Data) } return } @@ -735,8 +733,7 @@ func (req *getAccountRangeRequest) Encode() ([]byte, error) { return protobuf.Marshal(msg) } -// []*message.AccountData, []common.Hash -func (req *getAccountRangeRequest) getAccountRangeFromResponse(resp sttypes.Response) ([]*message.AccountData, []common.Hash, error) { +func (req *getAccountRangeRequest) getAccountRangeFromResponse(resp sttypes.Response) ([]*message.AccountData, [][]byte, error) { sResp, ok := resp.(*syncResponse) if !ok || sResp == nil { return nil, nil, errors.New("not sync response") @@ -744,7 +741,7 @@ func (req *getAccountRangeRequest) getAccountRangeFromResponse(resp sttypes.Resp return req.parseGetAccountRangeResponse(sResp) } -func (req *getAccountRangeRequest) parseGetAccountRangeResponse(resp *syncResponse) ([]*message.AccountData, []common.Hash, error) { +func (req *getAccountRangeRequest) parseGetAccountRangeResponse(resp *syncResponse) ([]*message.AccountData, [][]byte, error) { if errResp := resp.pb.GetErrorResponse(); errResp != nil { return nil, nil, errors.New(errResp.Error) } @@ -752,9 +749,9 @@ func (req *getAccountRangeRequest) parseGetAccountRangeResponse(resp *syncRespon if grResp == nil { return nil, nil, errors.New("response not GetAccountRange") } - proofs := make([]common.Hash, 0) + proofs := make([][]byte, 0) for _, proofBytes := range grResp.Proof { - var proof common.Hash + var proof []byte if err := rlp.DecodeBytes(proofBytes, &proof); err != nil { return nil, nil, errors.Wrap(err, "[GetAccountRangeResponse]") } @@ -817,7 +814,7 @@ func (req *getStorageRangesRequest) Encode() ([]byte, error) { } // []*message.AccountData, []common.Hash -func (req *getStorageRangesRequest) getStorageRangesFromResponse(resp sttypes.Response) ([]*message.StoragesData, []common.Hash, error) { +func (req *getStorageRangesRequest) getStorageRangesFromResponse(resp sttypes.Response) ([]*message.StoragesData, [][]byte, error) { sResp, ok := resp.(*syncResponse) if !ok || sResp == nil { return nil, nil, errors.New("not sync response") @@ -825,7 +822,7 @@ func (req *getStorageRangesRequest) getStorageRangesFromResponse(resp sttypes.Re return req.parseGetStorageRangesResponse(sResp) } -func (req *getStorageRangesRequest) parseGetStorageRangesResponse(resp *syncResponse) ([]*message.StoragesData, []common.Hash, error) { +func (req *getStorageRangesRequest) parseGetStorageRangesResponse(resp *syncResponse) ([]*message.StoragesData, [][]byte, error) { if errResp := resp.pb.GetErrorResponse(); errResp != nil { return nil, nil, errors.New(errResp.Error) } @@ -833,9 +830,9 @@ func (req *getStorageRangesRequest) parseGetStorageRangesResponse(resp *syncResp if grResp == nil { return nil, nil, errors.New("response not GetStorageRanges") } - proofs := make([]common.Hash, 0) + proofs := make([][]byte, 0) for _, proofBytes := range grResp.Proof { - var proof common.Hash + var proof []byte if err := rlp.DecodeBytes(proofBytes, &proof); err != nil { return nil, nil, errors.Wrap(err, "[GetStorageRangesResponse]") }