@@ -25,22 +25,24 @@ const (
25
25
26
26
// Manager provides means to manage internal Status Backend (injected into LES)
27
27
type Manager struct {
28
- nodeManager common.NodeManager
29
- accountManager common.AccountManager
30
- txQueue * queue.TxQueue
31
- ethTxClient EthTransactor
32
- addrLock * AddrLocker
33
- notify bool
28
+ nodeManager common.NodeManager
29
+ accountManager common.AccountManager
30
+ txQueue * queue.TxQueue
31
+ ethTxClient EthTransactor
32
+ addrLock * AddrLocker
33
+ notify bool
34
+ sendCompletionTimeout time.Duration
34
35
}
35
36
36
37
// NewManager returns a new Manager.
37
38
func NewManager (nodeManager common.NodeManager , accountManager common.AccountManager ) * Manager {
38
39
return & Manager {
39
- nodeManager : nodeManager ,
40
- accountManager : accountManager ,
41
- txQueue : queue .NewQueue (),
42
- addrLock : & AddrLocker {},
43
- notify : true ,
40
+ nodeManager : nodeManager ,
41
+ accountManager : accountManager ,
42
+ txQueue : queue .NewQueue (),
43
+ addrLock : & AddrLocker {},
44
+ notify : true ,
45
+ sendCompletionTimeout : DefaultTxSendCompletionTimeout * time .Second ,
44
46
}
45
47
}
46
48
@@ -75,34 +77,38 @@ func (m *Manager) QueueTransaction(tx *common.QueuedTx) error {
75
77
to = tx .Args .To .Hex ()
76
78
}
77
79
log .Info ("queue a new transaction" , "id" , tx .ID , "from" , tx .Args .From .Hex (), "to" , to )
78
- err := m .txQueue .Enqueue (tx )
80
+ if err := m .txQueue .Enqueue (tx ); err != nil {
81
+ return err
82
+ }
79
83
if m .notify {
80
84
NotifyOnEnqueue (tx )
81
85
}
82
- return err
86
+ return nil
83
87
}
84
88
85
89
func (m * Manager ) txDone (tx * common.QueuedTx , hash gethcommon.Hash , err error ) {
86
90
m .txQueue .Done (tx .ID , hash , err ) //nolint: errcheck
87
91
if m .notify {
88
- NotifyOnReturn (tx )
92
+ NotifyOnReturn (tx , err )
89
93
}
90
94
}
91
95
92
96
// WaitForTransaction adds a transaction to the queue and blocks
93
97
// until it's completed, discarded or times out.
94
- func (m * Manager ) WaitForTransaction (tx * common.QueuedTx ) error {
98
+ func (m * Manager ) WaitForTransaction (tx * common.QueuedTx ) common. RawCompleteTransactionResult {
95
99
log .Info ("wait for transaction" , "id" , tx .ID )
96
100
// now wait up until transaction is:
97
101
// - completed (via CompleteQueuedTransaction),
98
102
// - discarded (via DiscardQueuedTransaction)
99
103
// - or times out
100
- select {
101
- case <- tx .Done :
102
- case <- time .After (DefaultTxSendCompletionTimeout * time .Second ):
103
- m .txDone (tx , gethcommon.Hash {}, queue .ErrQueuedTxTimedOut )
104
+ for {
105
+ select {
106
+ case rst := <- tx .Result :
107
+ return rst
108
+ case <- time .After (m .sendCompletionTimeout ):
109
+ m .txDone (tx , gethcommon.Hash {}, queue .ErrQueuedTxTimedOut )
110
+ }
104
111
}
105
- return tx .Err
106
112
}
107
113
108
114
// CompleteTransaction instructs backend to complete sending of a given transaction.
@@ -242,7 +248,7 @@ func (m *Manager) DiscardTransaction(id common.QueuedTxID) error {
242
248
}
243
249
err = m .txQueue .Done (id , gethcommon.Hash {}, queue .ErrQueuedTxDiscarded )
244
250
if m .notify {
245
- NotifyOnReturn (tx )
251
+ NotifyOnReturn (tx , queue . ErrQueuedTxDiscarded )
246
252
}
247
253
return err
248
254
}
@@ -267,19 +273,17 @@ func (m *Manager) DiscardTransactions(ids []common.QueuedTxID) map[common.Queued
267
273
// It accepts one param which is a slice with a map of transaction params.
268
274
func (m * Manager ) SendTransactionRPCHandler (ctx context.Context , args ... interface {}) (interface {}, error ) {
269
275
log .Info ("SendTransactionRPCHandler called" )
270
-
271
276
// TODO(adam): it's a hack to parse arguments as common.RPCCall can do that.
272
277
// We should refactor parsing these params to a separate struct.
273
278
rpcCall := common.RPCCall {Params : args }
274
279
tx := common .CreateTransaction (ctx , rpcCall .ToSendTxArgs ())
275
-
276
280
if err := m .QueueTransaction (tx ); err != nil {
277
281
return nil , err
278
282
}
279
-
280
- if err := m . WaitForTransaction ( tx ); err != nil {
281
- return nil , err
283
+ rst := m . WaitForTransaction ( tx )
284
+ if rst . Error != nil {
285
+ return nil , rst . Error
282
286
}
283
-
284
- return tx .Hash .Hex (), nil
287
+ // handle empty hash
288
+ return rst .Hash .Hex (), nil
285
289
}
0 commit comments