Skip to content

Commit 017831d

Browse files
karalabeMariusVanDerWijdenholiman
authored
core, eth: split eth package, implement snap protocol (#21482)
This commit splits the eth package, separating the handling of eth and snap protocols. It also includes the capability to run snap sync (https://github.com/ethereum/devp2p/blob/master/caps/snap.md) , but does not enable it by default. Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> Co-authored-by: Martin Holst Swende <martin@swende.se>
1 parent 00d10e6 commit 017831d

74 files changed

Lines changed: 8224 additions & 3389 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/geth/misccmd.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"github.com/ethereum/go-ethereum/cmd/utils"
2727
"github.com/ethereum/go-ethereum/consensus/ethash"
28-
"github.com/ethereum/go-ethereum/eth"
2928
"github.com/ethereum/go-ethereum/params"
3029
"gopkg.in/urfave/cli.v1"
3130
)
@@ -143,7 +142,6 @@ func version(ctx *cli.Context) error {
143142
fmt.Println("Git Commit Date:", gitDate)
144143
}
145144
fmt.Println("Architecture:", runtime.GOARCH)
146-
fmt.Println("Protocol Versions:", eth.ProtocolVersions)
147145
fmt.Println("Go Version:", runtime.Version())
148146
fmt.Println("Operating System:", runtime.GOOS)
149147
fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))

‎cmd/utils/flags.go‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ var (
187187
defaultSyncMode = eth.DefaultConfig.SyncMode
188188
SyncModeFlag = TextMarshalerFlag{
189189
Name: "syncmode",
190-
Usage: `Blockchain sync mode ("fast", "full", or "light")`,
190+
Usage: `Blockchain sync mode ("fast", "full", "snap" or "light")`,
191191
Value: &defaultSyncMode,
192192
}
193193
GCModeFlag = cli.StringFlag{
@@ -1555,8 +1555,14 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15551555
cfg.SnapshotCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheSnapshotFlag.Name) / 100
15561556
}
15571557
if !ctx.GlobalIsSet(SnapshotFlag.Name) {
1558-
cfg.TrieCleanCache += cfg.SnapshotCache
1559-
cfg.SnapshotCache = 0 // Disabled
1558+
// If snap-sync is requested, this flag is also required
1559+
if cfg.SyncMode == downloader.SnapSync {
1560+
log.Info("Snap sync requested, enabling --snapshot")
1561+
ctx.Set(SnapshotFlag.Name, "true")
1562+
} else {
1563+
cfg.TrieCleanCache += cfg.SnapshotCache
1564+
cfg.SnapshotCache = 0 // Disabled
1565+
}
15601566
}
15611567
if ctx.GlobalIsSet(DocRootFlag.Name) {
15621568
cfg.DocRoot = ctx.GlobalString(DocRootFlag.Name)
@@ -1585,16 +1591,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15851591
cfg.RPCTxFeeCap = ctx.GlobalFloat64(RPCGlobalTxFeeCapFlag.Name)
15861592
}
15871593
if ctx.GlobalIsSet(NoDiscoverFlag.Name) {
1588-
cfg.DiscoveryURLs = []string{}
1594+
cfg.EthDiscoveryURLs, cfg.SnapDiscoveryURLs = []string{}, []string{}
15891595
} else if ctx.GlobalIsSet(DNSDiscoveryFlag.Name) {
15901596
urls := ctx.GlobalString(DNSDiscoveryFlag.Name)
15911597
if urls == "" {
1592-
cfg.DiscoveryURLs = []string{}
1598+
cfg.EthDiscoveryURLs = []string{}
15931599
} else {
1594-
cfg.DiscoveryURLs = SplitAndTrim(urls)
1600+
cfg.EthDiscoveryURLs = SplitAndTrim(urls)
15951601
}
15961602
}
1597-
15981603
// Override any default configs for hard coded networks.
15991604
switch {
16001605
case ctx.GlobalBool(LegacyTestnetFlag.Name) || ctx.GlobalBool(RopstenFlag.Name):
@@ -1676,16 +1681,20 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
16761681
// SetDNSDiscoveryDefaults configures DNS discovery with the given URL if
16771682
// no URLs are set.
16781683
func SetDNSDiscoveryDefaults(cfg *eth.Config, genesis common.Hash) {
1679-
if cfg.DiscoveryURLs != nil {
1684+
if cfg.EthDiscoveryURLs != nil {
16801685
return // already set through flags/config
16811686
}
1682-
16831687
protocol := "all"
16841688
if cfg.SyncMode == downloader.LightSync {
16851689
protocol = "les"
16861690
}
16871691
if url := params.KnownDNSNetwork(genesis, protocol); url != "" {
1688-
cfg.DiscoveryURLs = []string{url}
1692+
cfg.EthDiscoveryURLs = []string{url}
1693+
}
1694+
if cfg.SyncMode == downloader.SnapSync {
1695+
if url := params.KnownDNSNetwork(genesis, "snap"); url != "" {
1696+
cfg.SnapDiscoveryURLs = []string{url}
1697+
}
16891698
}
16901699
}
16911700

‎core/blockchain.go‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,8 @@ func (bc *BlockChain) CurrentBlock() *types.Block {
659659
return bc.currentBlock.Load().(*types.Block)
660660
}
661661

662-
// Snapshot returns the blockchain snapshot tree. This method is mainly used for
663-
// testing, to make it possible to verify the snapshot after execution.
664-
//
665-
// Warning: There are no guarantees about the safety of using the returned 'snap' if the
666-
// blockchain is simultaneously importing blocks, so take care.
667-
func (bc *BlockChain) Snapshot() *snapshot.Tree {
662+
// Snapshots returns the blockchain snapshot tree.
663+
func (bc *BlockChain) Snapshots() *snapshot.Tree {
668664
return bc.snaps
669665
}
670666

‎core/blockchain_snapshot_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ func testSnapshot(t *testing.T, tt *snapshotTest) {
751751
t.Fatalf("Failed to recreate chain: %v", err)
752752
}
753753
chain.InsertChain(newBlocks)
754-
chain.Snapshot().Cap(newBlocks[len(newBlocks)-1].Root(), 0)
754+
chain.Snapshots().Cap(newBlocks[len(newBlocks)-1].Root(), 0)
755755

756756
// Simulate the blockchain crash
757757
// Don't call chain.Stop here, so that no snapshot

‎core/forkid/forkid.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ func NewID(config *params.ChainConfig, genesis common.Hash, head uint64) ID {
8484
return ID{Hash: checksumToBytes(hash), Next: next}
8585
}
8686

87+
// NewIDWithChain calculates the Ethereum fork ID from an existing chain instance.
88+
func NewIDWithChain(chain Blockchain) ID {
89+
return NewID(
90+
chain.Config(),
91+
chain.Genesis().Hash(),
92+
chain.CurrentHeader().Number.Uint64(),
93+
)
94+
}
95+
8796
// NewFilter creates a filter that returns if a fork ID should be rejected or not
8897
// based on the local chain's status.
8998
func NewFilter(chain Blockchain) Filter {

‎core/rawdb/accessors_snapshot.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,24 @@ func DeleteSnapshotRecoveryNumber(db ethdb.KeyValueWriter) {
175175
log.Crit("Failed to remove snapshot recovery number", "err", err)
176176
}
177177
}
178+
179+
// ReadSanpshotSyncStatus retrieves the serialized sync status saved at shutdown.
180+
func ReadSanpshotSyncStatus(db ethdb.KeyValueReader) []byte {
181+
data, _ := db.Get(snapshotSyncStatusKey)
182+
return data
183+
}
184+
185+
// WriteSnapshotSyncStatus stores the serialized sync status to save at shutdown.
186+
func WriteSnapshotSyncStatus(db ethdb.KeyValueWriter, status []byte) {
187+
if err := db.Put(snapshotSyncStatusKey, status); err != nil {
188+
log.Crit("Failed to store snapshot sync status", "err", err)
189+
}
190+
}
191+
192+
// DeleteSnapshotSyncStatus deletes the serialized sync status saved at the last
193+
// shutdown
194+
func DeleteSnapshotSyncStatus(db ethdb.KeyValueWriter) {
195+
if err := db.Delete(snapshotSyncStatusKey); err != nil {
196+
log.Crit("Failed to remove snapshot sync status", "err", err)
197+
}
198+
}

‎core/rawdb/schema.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ var (
5757
// snapshotRecoveryKey tracks the snapshot recovery marker across restarts.
5858
snapshotRecoveryKey = []byte("SnapshotRecovery")
5959

60+
// snapshotSyncStatusKey tracks the snapshot sync status across restarts.
61+
snapshotSyncStatusKey = []byte("SnapshotSyncStatus")
62+
6063
// txIndexTailKey tracks the oldest block whose transactions have been indexed.
6164
txIndexTailKey = []byte("TransactionIndexTail")
6265

‎core/state/snapshot/generate.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (dl *diskLayer) generate(stats *generatorStats) {
241241
if acc.Root != emptyRoot {
242242
storeTrie, err := trie.NewSecure(acc.Root, dl.triedb)
243243
if err != nil {
244-
log.Error("Generator failed to access storage trie", "accroot", dl.root, "acchash", common.BytesToHash(accIt.Key), "stroot", acc.Root, "err", err)
244+
log.Error("Generator failed to access storage trie", "root", dl.root, "account", accountHash, "stroot", acc.Root, "err", err)
245245
abort := <-dl.genAbort
246246
abort <- stats
247247
return

‎core/state/statedb.go‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,19 @@ func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
314314
return common.Hash{}
315315
}
316316

317-
// GetProof returns the MerkleProof for a given Account
318-
func (s *StateDB) GetProof(a common.Address) ([][]byte, error) {
317+
// GetProof returns the Merkle proof for a given account.
318+
func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) {
319+
return s.GetProofByHash(crypto.Keccak256Hash(addr.Bytes()))
320+
}
321+
322+
// GetProofByHash returns the Merkle proof for a given account.
323+
func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) {
319324
var proof proofList
320-
err := s.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof)
325+
err := s.trie.Prove(addrHash[:], 0, &proof)
321326
return proof, err
322327
}
323328

324-
// GetStorageProof returns the StorageProof for given key
329+
// GetStorageProof returns the Merkle proof for given storage slot.
325330
func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
326331
var proof proofList
327332
trie := s.StorageTrie(a)
@@ -332,6 +337,17 @@ func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte,
332337
return proof, err
333338
}
334339

340+
// GetStorageProofByHash returns the Merkle proof for given storage slot.
341+
func (s *StateDB) GetStorageProofByHash(a common.Address, key common.Hash) ([][]byte, error) {
342+
var proof proofList
343+
trie := s.StorageTrie(a)
344+
if trie == nil {
345+
return proof, errors.New("storage trie for requested address does not exist")
346+
}
347+
err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
348+
return proof, err
349+
}
350+
335351
// GetCommittedState retrieves a value from the given account's committed storage trie.
336352
func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
337353
stateObject := s.getStateObject(addr)

‎eth/api_backend.go‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (b *EthAPIBackend) CurrentBlock() *types.Block {
5656
}
5757

5858
func (b *EthAPIBackend) SetHead(number uint64) {
59-
b.eth.protocolManager.downloader.Cancel()
59+
b.eth.handler.downloader.Cancel()
6060
b.eth.blockchain.SetHead(number)
6161
}
6262

@@ -272,10 +272,6 @@ func (b *EthAPIBackend) Downloader() *downloader.Downloader {
272272
return b.eth.Downloader()
273273
}
274274

275-
func (b *EthAPIBackend) ProtocolVersion() int {
276-
return b.eth.EthVersion()
277-
}
278-
279275
func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
280276
return b.gpo.SuggestPrice(ctx)
281277
}

0 commit comments

Comments
 (0)