Skip to content

Commit f89fe52

Browse files
committed
add test code
1 parent c6e62e1 commit f89fe52

File tree

9 files changed

+41
-52
lines changed

9 files changed

+41
-52
lines changed

cmd/utils/flags.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ var (
123123
}
124124
DiffSyncFlag = cli.BoolFlag{
125125
Name: "diffsync",
126-
Usage: "Enable difflayer sync, Please note that enable diffsync will improve the syncing speed, " +
126+
Usage: "Enable diffy sync, Please note that enable diffsync will improve the syncing speed, " +
127127
"but will degrade the security to light client level",
128128
}
129129
RangeLimitFlag = cli.BoolFlag{

consensus/parlia/parlia.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ const (
5555

5656
validatorBytesLength = common.AddressLength
5757
wiggleTime = uint64(1) // second, Random delay (per signer) to allow concurrent signers
58-
// TODO this is a hardfork change, just for tuning so far, recover it late
59-
initialBackOffTime = uint64(2) // second
58+
initialBackOffTime = uint64(1) // second
6059

6160
systemRewardPercent = 4 // it means 1/2^4 = 1/16 percentage of gas fee incoming will be distributed to system
6261

@@ -800,9 +799,9 @@ func (p *Parlia) Delay(chain consensus.ChainReader, header *types.Header) *time.
800799
return nil
801800
}
802801
delay := p.delayForRamanujanFork(snap, header)
803-
// The blocking time should be no more than half of epoch
804-
if delay > time.Duration(p.config.Period)*time.Second*4/5 {
805-
delay = time.Duration(p.config.Period) * time.Second * 4 / 5
802+
// The blocking time should be no more than half of period
803+
if delay > time.Duration(p.config.Period)*time.Second/2 {
804+
delay = time.Duration(p.config.Period) * time.Second / 2
806805
}
807806
return &delay
808807
}
@@ -894,19 +893,9 @@ func (p *Parlia) AllowLightProcess(chain consensus.ChainReader, currentHeader *t
894893
}
895894

896895
idx := snap.indexOfVal(p.val)
897-
if idx < 0 {
898-
return true
899-
}
900-
validators := snap.validators()
901-
902-
validatorNum := int64(len(validators))
903-
// It is not allowed if only two validators
904-
if validatorNum <= 2 {
905-
return false
906-
}
896+
// validator is not allowed to diff sync
897+
return idx < 0
907898

908-
offset := (int64(snap.Number) + 2) % validatorNum
909-
return validators[offset] == p.val
910899
}
911900

912901
func (p *Parlia) IsLocalBlock(header *types.Header) bool {

core/blockchain.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -2654,14 +2654,12 @@ func (bc *BlockChain) HandleDiffLayer(diffLayer *types.DiffLayer, pid string, fu
26542654
if _, alreadyHas := bc.diffPeersToDiffHashes[pid][diffLayer.DiffHash]; alreadyHas {
26552655
return nil
26562656
}
2657+
} else {
2658+
bc.diffPeersToDiffHashes[pid] = make(map[common.Hash]struct{})
26572659
}
2658-
bc.diffPeersToDiffHashes[pid] = make(map[common.Hash]struct{})
26592660
bc.diffPeersToDiffHashes[pid][diffLayer.DiffHash] = struct{}{}
26602661
if _, exist := bc.diffNumToBlockHashes[diffLayer.Number]; !exist {
26612662
bc.diffNumToBlockHashes[diffLayer.Number] = make(map[common.Hash]struct{})
2662-
}
2663-
if len(bc.diffNumToBlockHashes[diffLayer.Number]) > 4 {
2664-
26652663
}
26662664
bc.diffNumToBlockHashes[diffLayer.Number][diffLayer.BlockHash] = struct{}{}
26672665

@@ -2929,7 +2927,7 @@ func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscr
29292927
return bc.scope.Track(bc.blockProcFeed.Subscribe(ch))
29302928
}
29312929

2932-
//Options
2930+
// Options
29332931
func EnableLightProcessor(bc *BlockChain) *BlockChain {
29342932
bc.processor = NewLightStateProcessor(bc.Config(), bc, bc.engine)
29352933
return bc

core/state/statedb.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -1109,9 +1109,11 @@ func (s *StateDB) LightCommit(root common.Hash) (common.Hash, *types.DiffLayer,
11091109
tasksNum := 0
11101110
finishCh := make(chan struct{})
11111111
defer close(finishCh)
1112-
threads := 1
1113-
if len(s.diffTries)/runtime.NumCPU() > minNumberOfAccountPerTask {
1112+
threads := len(s.diffTries) / minNumberOfAccountPerTask
1113+
if threads > runtime.NumCPU() {
11141114
threads = runtime.NumCPU()
1115+
} else if threads == 0 {
1116+
threads = 1
11151117
}
11161118
for i := 0; i < threads; i++ {
11171119
go func() {
@@ -1230,7 +1232,15 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, *types.DiffLayer
12301232
tasksNum := 0
12311233
finishCh := make(chan struct{})
12321234
defer close(finishCh)
1233-
for i := 0; i < runtime.NumCPU(); i++ {
1235+
1236+
threads := len(s.stateObjectsDirty) / minNumberOfAccountPerTask
1237+
if threads > runtime.NumCPU() {
1238+
threads = runtime.NumCPU()
1239+
} else if threads == 0 {
1240+
threads = 1
1241+
}
1242+
1243+
for i := 0; i < threads; i++ {
12341244
go func() {
12351245
codeWriter := s.db.TrieDB().DiskDB().NewBatch()
12361246
for {

core/state_processor.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ import (
4444
const (
4545
fullProcessCheck = 21 // On diff sync mode, will do full process every fullProcessCheck randomly
4646
minNumberOfAccountPerTask = 5
47-
diffLayerTimeout = 50
47+
recentTime = 2048 * 3
48+
recentDiffLayerTimeout = 20
49+
farDiffLayerTimeout = 2
4850
)
4951

5052
// StateProcessor is a basic Processor, which takes care of transitioning
@@ -83,7 +85,6 @@ func (p *LightStateProcessor) Process(block *types.Block, statedb *state.StateDB
8385
allowLightProcess := true
8486
if posa, ok := p.engine.(consensus.PoSA); ok {
8587
allowLightProcess = posa.AllowLightProcess(p.bc, block.Header())
86-
log.Error("===debug, allow to light process?", "allow", allowLightProcess)
8788
}
8889
// random fallback to full process
8990
if check := p.randomGenerator.Int63n(fullProcessCheck); allowLightProcess && check != 0 && len(block.Transactions()) != 0 {
@@ -92,28 +93,25 @@ func (p *LightStateProcessor) Process(block *types.Block, statedb *state.StateDB
9293
pid = peer.ID()
9394
}
9495
var diffLayer *types.DiffLayer
95-
//TODO This is just for debug
96+
var diffLayerTimeout = recentDiffLayerTimeout
97+
if time.Now().Unix()-int64(block.Time()) > recentTime {
98+
diffLayerTimeout = farDiffLayerTimeout
99+
}
96100
for tried := 0; tried < diffLayerTimeout; tried++ {
97101
// wait a bit for the diff layer
98102
diffLayer = p.bc.GetUnTrustedDiffLayer(block.Hash(), pid)
99103
if diffLayer != nil {
100-
log.Error("===debug find it", "idx", tried)
101104
break
102105
}
103106
time.Sleep(time.Millisecond)
104107
}
105108
if diffLayer != nil {
106-
if err := diffLayer.Receipts.DeriveFields(p.bc.chainConfig, block.Hash(), block.NumberU64(), block.Transactions()); err != nil {
107-
log.Error("Failed to derive block receipts fields", "hash", block.Hash(), "number", block.NumberU64(), "err", err)
108-
// fallback to full process
109-
return p.StateProcessor.Process(block, statedb, cfg)
110-
}
111-
receipts, logs, gasUsed, err := p.LightProcess(diffLayer, block, statedb, cfg)
109+
receipts, logs, gasUsed, err := p.LightProcess(diffLayer, block, statedb)
112110
if err == nil {
113111
log.Info("do light process success at block", "num", block.NumberU64())
114112
return statedb, receipts, logs, gasUsed, nil
115113
} else {
116-
log.Error("do light process err at block\n", "num", block.NumberU64(), "err", err)
114+
log.Error("do light process err at block", "num", block.NumberU64(), "err", err)
117115
p.bc.removeDiffLayers(diffLayer.DiffHash)
118116
// prepare new statedb
119117
statedb.StopPrefetcher()
@@ -131,7 +129,7 @@ func (p *LightStateProcessor) Process(block *types.Block, statedb *state.StateDB
131129
return p.StateProcessor.Process(block, statedb, cfg)
132130
}
133131

134-
func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
132+
func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *types.Block, statedb *state.StateDB) (types.Receipts, []*types.Log, uint64, error) {
135133
statedb.MarkLightProcessed()
136134
fullDiffCode := make(map[common.Hash][]byte, len(diffLayer.Codes))
137135
diffTries := make(map[common.Address]state.Trie)
@@ -149,9 +147,11 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
149147
for des := range snapDestructs {
150148
statedb.Trie().TryDelete(des[:])
151149
}
152-
threads := 1
153-
if len(snapAccounts)/runtime.NumCPU() > minNumberOfAccountPerTask {
150+
threads := len(snapAccounts) / minNumberOfAccountPerTask
151+
if threads > runtime.NumCPU() {
154152
threads = runtime.NumCPU()
153+
} else if threads == 0 {
154+
threads = 1
155155
}
156156

157157
iteAccounts := make([]common.Address, 0, len(snapAccounts))
@@ -236,7 +236,7 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
236236
!bytes.Equal(latestAccount.CodeHash, types.EmptyCodeHash) {
237237
if code, exist := fullDiffCode[codeHash]; exist {
238238
if crypto.Keccak256Hash(code) != codeHash {
239-
errChan <- err
239+
errChan <- fmt.Errorf("code and code hash mismatch, account %s", diffAccount.String())
240240
return
241241
}
242242
diffMux.Lock()
@@ -245,7 +245,7 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
245245
} else {
246246
rawCode := rawdb.ReadCode(p.bc.db, codeHash)
247247
if len(rawCode) == 0 {
248-
errChan <- err
248+
errChan <- fmt.Errorf("missing code, account %s", diffAccount.String())
249249
return
250250
}
251251
}

core/tx_pool.go

+1
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ func (pool *TxPool) scheduleReorgLoop() {
10191019
pool.reorgDoneCh <- nextDone
10201020

10211021
case req := <-pool.reqPromoteCh:
1022+
log.Error("=== debug receive reqPromote notice")
10221023
// Promote request: update address set if request is already pending.
10231024
if dirtyAccounts == nil {
10241025
dirtyAccounts = req

eth/handler.go

-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ func (h *handler) BroadcastBlock(block *types.Block, propagate bool) {
486486
for _, peer := range transfer {
487487
if len(diff) != 0 && peer.diffExt != nil {
488488
// difflayer should send before block
489-
log.Error("===debug Broadcast block", "number", block.Number(), "hash", hash)
490489
peer.diffExt.SendDiffLayers([]rlp.RawValue{diff})
491490
}
492491
peer.AsyncSendNewBlock(block, td)

eth/handler_diff.go

-9
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package eth
1919
import (
2020
"fmt"
2121

22-
"github.com/ethereum/go-ethereum/log"
23-
2422
"github.com/ethereum/go-ethereum/core"
2523
"github.com/ethereum/go-ethereum/eth/protocols/diff"
2624
"github.com/ethereum/go-ethereum/p2p/enode"
@@ -73,15 +71,8 @@ func (h *diffHandler) handleDiffLayerPackage(packet *diff.DiffLayersPacket, pid
7371
diffs, err := packet.Unpack()
7472

7573
if err != nil {
76-
log.Error("====unpack err", "number", diffs[0].Number, "hash", diffs[0].BlockHash, "err", err)
7774
return err
7875
}
79-
if len(diffs) > 0 {
80-
log.Error("====debug receive difflayer", "number", diffs[0].Number, "hash", diffs[0].BlockHash)
81-
82-
} else {
83-
log.Error("====debug receive difflayer length 0")
84-
}
8576
for _, d := range diffs {
8677
if d != nil {
8778
if err := d.Validate(); err != nil {

eth/protocols/diff/protocol.go

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func (p *DiffLayersPacket) Unpack() ([]*types.DiffLayer, error) {
9898

9999
type DiffCapPacket struct {
100100
DiffSync bool
101+
Extra rlp.RawValue // for extension
101102
}
102103

103104
type DiffLayersPacket []rlp.RawValue

0 commit comments

Comments
 (0)