4
4
"fmt"
5
5
"math/big"
6
6
"sort"
7
+ "sync"
7
8
8
9
"github.com/ethereum/go-ethereum/core"
9
10
"github.com/ethereum/go-ethereum/core/types"
@@ -55,6 +56,7 @@ type Agent interface {
55
56
}
56
57
57
58
type worker struct {
59
+ mu sync.Mutex
58
60
agents []Agent
59
61
recv chan Work
60
62
mux * event.TypeMux
115
117
select {
116
118
case event := <- events .Chan ():
117
119
switch event .(type ) {
118
- case core.ChainEvent :
119
- self .commitNewWork ()
120
- case core.TxPreEvent :
120
+ case core.ChainEvent , core.TxPreEvent :
121
121
self .commitNewWork ()
122
122
}
123
123
case <- self .quit :
@@ -163,6 +163,9 @@ func (self *worker) push() {
163
163
}
164
164
165
165
func (self * worker ) commitNewWork () {
166
+ self .mu .Lock ()
167
+ defer self .mu .Unlock ()
168
+
166
169
self .current = env (self .chain .NewBlock (self .coinbase ), self .eth )
167
170
parent := self .chain .GetBlock (self .current .block .ParentHash ())
168
171
self .current .coinbase .SetGasPool (core .CalcGasLimit (parent , self .current .block ))
@@ -176,12 +179,11 @@ func (self *worker) commitNewWork() {
176
179
err := self .commitTransaction (tx )
177
180
switch {
178
181
case core .IsNonceErr (err ):
182
+ // Remove invalid transactions
179
183
remove = append (remove , tx )
180
184
case core .IsGasLimitErr (err ):
181
185
// Break on gas limit
182
186
break
183
- default :
184
- remove = append (remove , tx )
185
187
}
186
188
187
189
if err != nil {
@@ -227,16 +229,13 @@ func (self *worker) commitUncle(uncle *types.Header) error {
227
229
228
230
func (self * worker ) commitTransaction (tx * types.Transaction ) error {
229
231
snapshot := self .current .state .Copy ()
230
- receipt , txGas , err := self .proc .ApplyTransaction (self .current .coinbase , self .current .state , self .current .block , tx , self .current .totalUsedGas , true )
231
- if err != nil {
232
- if core .IsNonceErr (err ) || core .IsGasLimitErr (err ) {
233
- self .current .state .Set (snapshot )
234
- }
232
+ receipt , _ , err := self .proc .ApplyTransaction (self .current .coinbase , self .current .state , self .current .block , tx , self .current .totalUsedGas , true )
233
+ if err != nil && (core .IsNonceErr (err ) || core .IsGasLimitErr (err )) {
234
+ self .current .state .Set (snapshot )
235
235
236
236
return err
237
237
}
238
238
239
- self .current .totalUsedGas .Add (self .current .totalUsedGas , txGas )
240
239
self .current .block .AddTransaction (tx )
241
240
self .current .block .AddReceipt (receipt )
242
241
0 commit comments