Merge pull request #1 from DeBankDeFi/debank_archive

Switch archive node by configuration(dynamically)
This commit is contained in:
barryz 2021-07-21 11:06:08 +08:00 committed by GitHub
commit 0c4892f728
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 6 deletions

View file

@ -11,12 +11,18 @@
GOBIN = ./build/bin GOBIN = ./build/bin
GO ?= latest GO ?= latest
GORUN = env GO111MODULE=on go run GORUN = env GO111MODULE=on go run
PWD = `pwd`
geth: geth:
$(GORUN) build/ci.go install ./cmd/geth $(GORUN) build/ci.go install ./cmd/geth
@echo "Done building." @echo "Done building."
@echo "Run \"$(GOBIN)/geth\" to launch geth." @echo "Run \"$(GOBIN)/geth\" to launch geth."
geth-linux:
docker run --rm -v "$(PWD)":/usr/src/go-ethereum -w /usr/src/go-ethereum golang:1.16.3 $(GORUN) build/ci.go install ./cmd/geth
@echo "Done building."
@echo "Run \"$(GOBIN)/geth\" to launch geth."
all: all:
$(GORUN) build/ci.go install $(GORUN) build/ci.go install
@ -60,9 +66,9 @@ geth-cross: geth-linux geth-darwin geth-windows geth-android geth-ios
@echo "Full cross compilation done:" @echo "Full cross compilation done:"
@ls -ld $(GOBIN)/geth-* @ls -ld $(GOBIN)/geth-*
geth-linux: geth-linux-386 geth-linux-amd64 geth-linux-arm geth-linux-mips64 geth-linux-mips64le # geth-linux: geth-linux-386 geth-linux-amd64 geth-linux-arm geth-linux-mips64 geth-linux-mips64le
@echo "Linux cross compilation done:" # @echo "Linux cross compilation done:"
@ls -ld $(GOBIN)/geth-linux-* # @ls -ld $(GOBIN)/geth-linux-*
geth-linux-386: geth-linux-386:
$(GORUN) build/ci.go xgo -- --go=$(GO) --targets=linux/386 -v ./cmd/geth $(GORUN) build/ci.go xgo -- --go=$(GO) --targets=linux/386 -v ./cmd/geth

View file

@ -91,6 +91,7 @@ func setupGeth(stack *node.Node) error {
Genesis: &chain.genesis, Genesis: &chain.genesis,
NetworkId: chain.genesis.Config.ChainID.Uint64(), // 19763 NetworkId: chain.genesis.Config.ChainID.Uint64(), // 19763
DatabaseCache: 10, DatabaseCache: 10,
TriesInMemory: 128,
TrieCleanCache: 10, TrieCleanCache: 10,
TrieCleanCacheJournal: "", TrieCleanCacheJournal: "",
TrieCleanCacheRejournal: 60 * time.Minute, TrieCleanCacheRejournal: 60 * time.Minute,

View file

@ -1855,6 +1855,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name) Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name)
} }
cache := &core.CacheConfig{ cache := &core.CacheConfig{
TriesInMemory: ethconfig.Defaults.TriesInMemory,
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache, TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
TrieCleanNoPrefetch: ctx.GlobalBool(CacheNoPrefetchFlag.Name), TrieCleanNoPrefetch: ctx.GlobalBool(CacheNoPrefetchFlag.Name),
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache, TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,

View file

@ -52,6 +52,9 @@ var (
headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil) headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil)
headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil) headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil)
headLastGCBlockGauge = metrics.NewRegisteredGauge("chain/head/lastgc", nil)
headLastPersistGauge = metrics.NewRegisteredGauge("chain/head/lastpersist", nil)
accountReadTimer = metrics.NewRegisteredTimer("chain/account/reads", nil) accountReadTimer = metrics.NewRegisteredTimer("chain/account/reads", nil)
accountHashTimer = metrics.NewRegisteredTimer("chain/account/hashes", nil) accountHashTimer = metrics.NewRegisteredTimer("chain/account/hashes", nil)
accountUpdateTimer = metrics.NewRegisteredTimer("chain/account/updates", nil) accountUpdateTimer = metrics.NewRegisteredTimer("chain/account/updates", nil)
@ -82,6 +85,11 @@ var (
errInsertionInterrupted = errors.New("insertion is interrupted") errInsertionInterrupted = errors.New("insertion is interrupted")
) )
var (
// TriesInMemory Keeps the latest 128 blocks when pruning.
TriesInMemory uint64 = 128
)
const ( const (
bodyCacheLimit = 256 bodyCacheLimit = 256
blockCacheLimit = 256 blockCacheLimit = 256
@ -89,7 +97,6 @@ const (
txLookupCacheLimit = 1024 txLookupCacheLimit = 1024
maxFutureBlocks = 256 maxFutureBlocks = 256
maxTimeFutureBlocks = 30 maxTimeFutureBlocks = 30
TriesInMemory = 128
// BlockChainVersion ensures that an incompatible database forces a resync from scratch. // BlockChainVersion ensures that an incompatible database forces a resync from scratch.
// //
@ -120,6 +127,7 @@ const (
// CacheConfig contains the configuration values for the trie caching/pruning // CacheConfig contains the configuration values for the trie caching/pruning
// that's resident in a blockchain. // that's resident in a blockchain.
type CacheConfig struct { type CacheConfig struct {
TriesInMemory int // Keeps the latest n blocks when pruning.
TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory
TrieCleanJournal string // Disk journal for saving clean cache entries. TrieCleanJournal string // Disk journal for saving clean cache entries.
TrieCleanRejournal time.Duration // Time interval to dump clean cache to disk periodically TrieCleanRejournal time.Duration // Time interval to dump clean cache to disk periodically
@ -136,6 +144,7 @@ type CacheConfig struct {
// defaultCacheConfig are the default caching values if none are specified by the // defaultCacheConfig are the default caching values if none are specified by the
// user (also used during testing). // user (also used during testing).
var defaultCacheConfig = &CacheConfig{ var defaultCacheConfig = &CacheConfig{
TriesInMemory: 128,
TrieCleanLimit: 256, TrieCleanLimit: 256,
TrieDirtyLimit: 256, TrieDirtyLimit: 256,
TrieTimeLimit: 5 * time.Minute, TrieTimeLimit: 5 * time.Minute,
@ -218,6 +227,12 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
if cacheConfig == nil { if cacheConfig == nil {
cacheConfig = defaultCacheConfig cacheConfig = defaultCacheConfig
} }
if cacheConfig.TriesInMemory != 0 {
TriesInMemory = uint64(cacheConfig.TriesInMemory)
}
if TriesInMemory != 128 {
log.Warn("TriesInMemory isn't the default value(128), non-default values may cause system instability", "triesInMemory", TriesInMemory)
}
bodyCache, _ := lru.New(bodyCacheLimit) bodyCache, _ := lru.New(bodyCacheLimit)
bodyRLPCache, _ := lru.New(bodyCacheLimit) bodyRLPCache, _ := lru.New(bodyCacheLimit)
receiptsCache, _ := lru.New(receiptsCacheLimit) receiptsCache, _ := lru.New(receiptsCacheLimit)
@ -1030,11 +1045,14 @@ func (bc *BlockChain) Stop() {
log.Error("Failed to commit recent state trie", "err", err) log.Error("Failed to commit recent state trie", "err", err)
} }
} }
if !bc.triegc.Empty() {
log.Info("Dereference all trie nodes remaining in prqueue", "nodes", bc.triegc.Size())
}
for !bc.triegc.Empty() { for !bc.triegc.Empty() {
triedb.Dereference(bc.triegc.PopItem().(common.Hash)) triedb.Dereference(bc.triegc.PopItem().(common.Hash))
} }
if size, _ := triedb.Size(); size != 0 { if size, _ := triedb.Size(); size != 0 {
log.Error("Dangling trie nodes after full cleanup") log.Error("Dangling trie nodes after full cleanup", "size", size)
} }
} }
// Ensure all live cached entries be saved into disk, so that we can skip // Ensure all live cached entries be saved into disk, so that we can skip
@ -1511,11 +1529,12 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
// If we're exceeding limits but haven't reached a large enough memory gap, // If we're exceeding limits but haven't reached a large enough memory gap,
// warn the user that the system is becoming unstable. // warn the user that the system is becoming unstable.
if chosen < lastWrite+TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit { if chosen < lastWrite+TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit {
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/TriesInMemory) log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/float64(TriesInMemory))
} }
// Flush an entire trie and restart the counters // Flush an entire trie and restart the counters
triedb.Commit(header.Root, true, nil) triedb.Commit(header.Root, true, nil)
lastWrite = chosen lastWrite = chosen
headLastPersistGauge.Update(int64(chosen))
bc.gcproc = 0 bc.gcproc = 0
} }
} }
@ -1526,6 +1545,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
bc.triegc.Push(root, number) bc.triegc.Push(root, number)
break break
} }
headLastGCBlockGauge.Update(-number)
triedb.Dereference(root.(common.Hash)) triedb.Dereference(root.(common.Hash))
} }
} }

View file

@ -177,6 +177,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
EnablePreimageRecording: config.EnablePreimageRecording, EnablePreimageRecording: config.EnablePreimageRecording,
} }
cacheConfig = &core.CacheConfig{ cacheConfig = &core.CacheConfig{
TriesInMemory: config.TriesInMemory,
TrieCleanLimit: config.TrieCleanCache, TrieCleanLimit: config.TrieCleanCache,
TrieCleanJournal: stack.ResolvePath(config.TrieCleanCacheJournal), TrieCleanJournal: stack.ResolvePath(config.TrieCleanCacheJournal),
TrieCleanRejournal: config.TrieCleanCacheRejournal, TrieCleanRejournal: config.TrieCleanCacheRejournal,

View file

@ -81,6 +81,7 @@ var Defaults = Config{
TrieCleanCacheRejournal: 60 * time.Minute, TrieCleanCacheRejournal: 60 * time.Minute,
TrieDirtyCache: 256, TrieDirtyCache: 256,
TrieTimeout: 60 * time.Minute, TrieTimeout: 60 * time.Minute,
TriesInMemory: 128,
SnapshotCache: 102, SnapshotCache: 102,
Miner: miner.Config{ Miner: miner.Config{
GasFloor: 8000000, GasFloor: 8000000,
@ -165,6 +166,7 @@ type Config struct {
TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache
TrieDirtyCache int TrieDirtyCache int
TrieTimeout time.Duration TrieTimeout time.Duration
TriesInMemory int
SnapshotCache int SnapshotCache int
Preimages bool Preimages bool

View file

@ -46,6 +46,7 @@ func (c Config) MarshalTOML() (interface{}, error) {
TrieCleanCacheRejournal time.Duration `toml:",omitempty"` TrieCleanCacheRejournal time.Duration `toml:",omitempty"`
TrieDirtyCache int TrieDirtyCache int
TrieTimeout time.Duration TrieTimeout time.Duration
TriesInMemory int
SnapshotCache int SnapshotCache int
Preimages bool Preimages bool
Miner miner.Config Miner miner.Config
@ -89,6 +90,7 @@ func (c Config) MarshalTOML() (interface{}, error) {
enc.TrieCleanCacheRejournal = c.TrieCleanCacheRejournal enc.TrieCleanCacheRejournal = c.TrieCleanCacheRejournal
enc.TrieDirtyCache = c.TrieDirtyCache enc.TrieDirtyCache = c.TrieDirtyCache
enc.TrieTimeout = c.TrieTimeout enc.TrieTimeout = c.TrieTimeout
enc.TriesInMemory = c.TriesInMemory
enc.SnapshotCache = c.SnapshotCache enc.SnapshotCache = c.SnapshotCache
enc.Preimages = c.Preimages enc.Preimages = c.Preimages
enc.Miner = c.Miner enc.Miner = c.Miner
@ -136,6 +138,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error {
TrieCleanCacheRejournal *time.Duration `toml:",omitempty"` TrieCleanCacheRejournal *time.Duration `toml:",omitempty"`
TrieDirtyCache *int TrieDirtyCache *int
TrieTimeout *time.Duration TrieTimeout *time.Duration
TriesInMemory *int
SnapshotCache *int SnapshotCache *int
Preimages *bool Preimages *bool
Miner *miner.Config Miner *miner.Config
@ -238,6 +241,9 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error {
if dec.TrieTimeout != nil { if dec.TrieTimeout != nil {
c.TrieTimeout = *dec.TrieTimeout c.TrieTimeout = *dec.TrieTimeout
} }
if dec.TriesInMemory != nil {
c.TriesInMemory = *dec.TriesInMemory
}
if dec.SnapshotCache != nil { if dec.SnapshotCache != nil {
c.SnapshotCache = *dec.SnapshotCache c.SnapshotCache = *dec.SnapshotCache
} }