@@ -44,7 +44,9 @@ import (
44
44
const (
45
45
fullProcessCheck = 21 // On diff sync mode, will do full process every fullProcessCheck randomly
46
46
minNumberOfAccountPerTask = 5
47
- diffLayerTimeout = 50
47
+ recentTime = 2048 * 3
48
+ recentDiffLayerTimeout = 20
49
+ farDiffLayerTimeout = 2
48
50
)
49
51
50
52
// StateProcessor is a basic Processor, which takes care of transitioning
@@ -83,7 +85,6 @@ func (p *LightStateProcessor) Process(block *types.Block, statedb *state.StateDB
83
85
allowLightProcess := true
84
86
if posa , ok := p .engine .(consensus.PoSA ); ok {
85
87
allowLightProcess = posa .AllowLightProcess (p .bc , block .Header ())
86
- log .Error ("===debug, allow to light process?" , "allow" , allowLightProcess )
87
88
}
88
89
// random fallback to full process
89
90
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
92
93
pid = peer .ID ()
93
94
}
94
95
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
+ }
96
100
for tried := 0 ; tried < diffLayerTimeout ; tried ++ {
97
101
// wait a bit for the diff layer
98
102
diffLayer = p .bc .GetUnTrustedDiffLayer (block .Hash (), pid )
99
103
if diffLayer != nil {
100
- log .Error ("===debug find it" , "idx" , tried )
101
104
break
102
105
}
103
106
time .Sleep (time .Millisecond )
104
107
}
105
108
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 )
112
110
if err == nil {
113
111
log .Info ("do light process success at block" , "num" , block .NumberU64 ())
114
112
return statedb , receipts , logs , gasUsed , nil
115
113
} 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 )
117
115
p .bc .removeDiffLayers (diffLayer .DiffHash )
118
116
// prepare new statedb
119
117
statedb .StopPrefetcher ()
@@ -131,7 +129,7 @@ func (p *LightStateProcessor) Process(block *types.Block, statedb *state.StateDB
131
129
return p .StateProcessor .Process (block , statedb , cfg )
132
130
}
133
131
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 ) {
135
133
statedb .MarkLightProcessed ()
136
134
fullDiffCode := make (map [common.Hash ][]byte , len (diffLayer .Codes ))
137
135
diffTries := make (map [common.Address ]state.Trie )
@@ -149,9 +147,11 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
149
147
for des := range snapDestructs {
150
148
statedb .Trie ().TryDelete (des [:])
151
149
}
152
- threads := 1
153
- if len ( snapAccounts ) / runtime .NumCPU () > minNumberOfAccountPerTask {
150
+ threads := len ( snapAccounts ) / minNumberOfAccountPerTask
151
+ if threads > runtime .NumCPU () {
154
152
threads = runtime .NumCPU ()
153
+ } else if threads == 0 {
154
+ threads = 1
155
155
}
156
156
157
157
iteAccounts := make ([]common.Address , 0 , len (snapAccounts ))
@@ -236,7 +236,7 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
236
236
! bytes .Equal (latestAccount .CodeHash , types .EmptyCodeHash ) {
237
237
if code , exist := fullDiffCode [codeHash ]; exist {
238
238
if crypto .Keccak256Hash (code ) != codeHash {
239
- errChan <- err
239
+ errChan <- fmt . Errorf ( "code and code hash mismatch, account %s" , diffAccount . String ())
240
240
return
241
241
}
242
242
diffMux .Lock ()
@@ -245,7 +245,7 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
245
245
} else {
246
246
rawCode := rawdb .ReadCode (p .bc .db , codeHash )
247
247
if len (rawCode ) == 0 {
248
- errChan <- err
248
+ errChan <- fmt . Errorf ( "missing code, account %s" , diffAccount . String ())
249
249
return
250
250
}
251
251
}
0 commit comments