Skip to content

Commit f7ef016

Browse files
committed
fast node verification and fix conflicts
Signed-off-by: kyrie-yl <[email protected]>
1 parent 1e46537 commit f7ef016

17 files changed

+517
-47
lines changed

cmd/geth/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ var (
7171
utils.NoUSBFlag,
7272
utils.DirectBroadcastFlag,
7373
utils.DisableSnapProtocolFlag,
74+
utils.DisableDiffProtocolFlag,
75+
utils.EnableTrustProtocolFlag,
7476
utils.DiffSyncFlag,
7577
utils.PipeCommitFlag,
7678
utils.RangeLimitFlag,
@@ -98,6 +100,7 @@ var (
98100
utils.TxPoolLifetimeFlag,
99101
utils.TxPoolReannounceTimeFlag,
100102
utils.SyncModeFlag,
103+
utils.TriesVerifyModeFlag,
101104
utils.ExitWhenSyncedFlag,
102105
utils.GCModeFlag,
103106
utils.SnapshotFlag,
@@ -115,7 +118,6 @@ var (
115118
utils.WhitelistFlag,
116119
utils.BloomFilterSizeFlag,
117120
utils.TriesInMemoryFlag,
118-
utils.AllowInsecureNoTriesFlag,
119121
utils.CacheFlag,
120122
utils.CacheDatabaseFlag,
121123
utils.CacheTrieFlag,

cmd/geth/usage.go

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ var AppHelpFlagGroups = []flags.FlagGroup{
4141
utils.NoUSBFlag,
4242
utils.DirectBroadcastFlag,
4343
utils.DisableSnapProtocolFlag,
44+
utils.DisableDiffProtocolFlag,
45+
utils.EnableTrustProtocolFlag,
4446
utils.RangeLimitFlag,
4547
utils.SmartCardDaemonPathFlag,
4648
utils.NetworkIdFlag,
@@ -50,6 +52,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
5052
utils.YoloV3Flag,
5153
utils.RopstenFlag,
5254
utils.SyncModeFlag,
55+
utils.TriesVerifyModeFlag,
5356
utils.ExitWhenSyncedFlag,
5457
utils.GCModeFlag,
5558
utils.TxLookupLimitFlag,

cmd/utils/flags.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ var (
122122
Name: "disablesnapprotocol",
123123
Usage: "Disable snap protocol",
124124
}
125+
DisableDiffProtocolFlag = cli.BoolFlag{
126+
Name: "disablediffprotocol",
127+
Usage: "Disable diff protocol",
128+
}
129+
EnableTrustProtocolFlag = cli.BoolFlag{
130+
Name: "enabletrustprotocol",
131+
Usage: "Enable trust protocol",
132+
}
125133
DiffSyncFlag = cli.BoolFlag{
126134
Name: "diffsync",
127135
Usage: "Enable diffy sync, Please note that enable diffsync will improve the syncing speed, " +
@@ -264,9 +272,11 @@ var (
264272
Usage: "The layer of tries trees that keep in memory",
265273
Value: 128,
266274
}
267-
AllowInsecureNoTriesFlag = cli.BoolTFlag{
268-
Name: "allow-insecure-no-tries",
269-
Usage: `Disable the tries state root verification, the state consistency is no longer 100% guaranteed, diffsync is not allowed if enabled. Do not enable it unless you know exactly what the consequence it will cause.`,
275+
defaultVerifyMode = ethconfig.Defaults.TriesVerifyMode
276+
TriesVerifyModeFlag = TextMarshalerFlag{
277+
Name: "tries-verify-mode",
278+
Usage: `tries verify mode: "local", "full", "insecure", "none"`,
279+
Value: &defaultVerifyMode,
270280
}
271281
OverrideBerlinFlag = cli.Uint64Flag{
272282
Name: "override.berlin",
@@ -1637,6 +1647,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16371647
if ctx.GlobalIsSet(DisableSnapProtocolFlag.Name) {
16381648
cfg.DisableSnapProtocol = ctx.GlobalBool(DisableSnapProtocolFlag.Name)
16391649
}
1650+
if ctx.GlobalIsSet(DisableDiffProtocolFlag.Name) {
1651+
cfg.DisableDiffProtocol = ctx.GlobalIsSet(DisableDiffProtocolFlag.Name)
1652+
}
1653+
if ctx.GlobalIsSet(EnableTrustProtocolFlag.Name) {
1654+
cfg.EnableTrustProtocol = ctx.GlobalIsSet(EnableTrustProtocolFlag.Name)
1655+
}
16401656
if ctx.GlobalIsSet(DiffSyncFlag.Name) {
16411657
cfg.DiffSync = ctx.GlobalBool(DiffSyncFlag.Name)
16421658
}
@@ -1670,8 +1686,13 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16701686
if ctx.GlobalIsSet(TriesInMemoryFlag.Name) {
16711687
cfg.TriesInMemory = ctx.GlobalUint64(TriesInMemoryFlag.Name)
16721688
}
1673-
if ctx.GlobalIsSet(AllowInsecureNoTriesFlag.Name) {
1674-
cfg.NoTries = ctx.GlobalBool(AllowInsecureNoTriesFlag.Name)
1689+
if ctx.GlobalIsSet(TriesVerifyModeFlag.Name) {
1690+
cfg.TriesVerifyMode = *GlobalTextMarshaler(ctx, TriesVerifyModeFlag.Name).(*core.VerifyMode)
1691+
// If a node sets verify mode to full or light, it's a fast node and need
1692+
// to verify blocks from verify nodes, then it should enable trust protocol.
1693+
if cfg.TriesVerifyMode.NeedRemoteVerify() {
1694+
cfg.EnableTrustProtocol = true
1695+
}
16751696
}
16761697
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheSnapshotFlag.Name) {
16771698
cfg.SnapshotCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheSnapshotFlag.Name) / 100

core/block_validator.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,23 @@ const badBlockCacheExpire = 30 * time.Second
3434
//
3535
// BlockValidator implements Validator.
3636
type BlockValidator struct {
37-
config *params.ChainConfig // Chain configuration options
38-
bc *BlockChain // Canonical block chain
39-
engine consensus.Engine // Consensus engine used for validating
37+
config *params.ChainConfig // Chain configuration options
38+
bc *BlockChain // Canonical block chain
39+
engine consensus.Engine // Consensus engine used for validating
40+
remoteValidator *remoteVerifyManager
4041
}
4142

4243
// NewBlockValidator returns a new block validator which is safe for re-use
43-
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator {
44+
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, mode VerifyMode, peers verifyPeers) *BlockValidator {
4445
validator := &BlockValidator{
4546
config: config,
4647
engine: engine,
4748
bc: blockchain,
4849
}
50+
if mode.NeedRemoteVerify() {
51+
validator.remoteValidator = NewVerifyManager(blockchain, peers, mode == InsecureVerify)
52+
go validator.remoteValidator.mainLoop()
53+
}
4954
return validator
5055
}
5156

@@ -85,6 +90,13 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
8590
}
8691
return nil
8792
},
93+
// for fast node which verify trie from remote verify peers, a block's H-11 ancestor should have been verify.
94+
func() error {
95+
if v.remoteValidator != nil && !v.remoteValidator.AncestorVerified(v.bc.GetHeaderByNumber(header.Number.Uint64())) {
96+
return fmt.Errorf("block's ancessor %x has not been verified", block.Hash())
97+
}
98+
return nil
99+
},
88100
}
89101
validateRes := make(chan error, len(validateFuns))
90102
for _, f := range validateFuns {
@@ -164,6 +176,10 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
164176
return err
165177
}
166178

179+
func (v *BlockValidator) RemoteVerifyManager() *remoteVerifyManager {
180+
return v.remoteValidator
181+
}
182+
167183
// CalcGasLimit computes the gas limit of the next block after parent. It aims
168184
// to keep the baseline gas above the provided floor, and increase it towards the
169185
// ceil if the blocks are full. If the ceil is exceeded, it will always decrease

core/blockchain.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
311311
diffNumToBlockHashes: make(map[uint64]map[common.Hash]struct{}),
312312
diffPeersToDiffHashes: make(map[string]map[common.Hash]struct{}),
313313
}
314+
314315
bc.prefetcher = NewStatePrefetcher(chainConfig, bc, engine)
315-
bc.validator = NewBlockValidator(chainConfig, bc, engine)
316316
bc.processor = NewStateProcessor(chainConfig, bc, engine)
317317

318318
var err error
@@ -3135,8 +3135,15 @@ func EnablePersistDiff(limit uint64) BlockChainOption {
31353135
}
31363136
}
31373137

3138-
func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) *types.VerifyResult {
3139-
var res types.VerifyResult
3138+
func EnableBlockValidator(chainConfig *params.ChainConfig, engine consensus.Engine, mode VerifyMode, peers verifyPeers) BlockChainOption {
3139+
return func(bc *BlockChain) *BlockChain {
3140+
bc.validator = NewBlockValidator(chainConfig, bc, engine, mode, peers)
3141+
return bc
3142+
}
3143+
}
3144+
3145+
func (bc *BlockChain) GetRootByDiffHash(blockNumber uint64, blockHash common.Hash, diffHash common.Hash) *VerifyResult {
3146+
var res VerifyResult
31403147
res.BlockNumber = blockNumber
31413148
res.BlockHash = blockHash
31423149

core/blockchain_diff_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ func testGetRootByDiffHash(t *testing.T, chain1, chain2 *BlockChain, blockNumber
584584
if block2 == nil {
585585
t.Fatalf("failed to find block, number: %v", blockNumber)
586586
}
587-
expect := types.VerifyResult{
587+
expect := VerifyResult{
588588
Status: status,
589589
BlockNumber: blockNumber,
590590
BlockHash: block2.Hash(),

0 commit comments

Comments
 (0)