Skip to content
Snippets Groups Projects
Commit 9c663a61 authored by Benjamin Bollen's avatar Benjamin Bollen Committed by GitHub
Browse files

Merge pull request #268 from silasdavis/blocking-transactAndHold

Fix blocking event subscription in transactAndHold (develop)
parents 9214f236 6c6e66f7
No related branches found
No related tags found
No related merge requests found
...@@ -205,13 +205,24 @@ func (this *transactor) TransactAndHold(privKey, address, data []byte, gasLimit, ...@@ -205,13 +205,24 @@ func (this *transactor) TransactAndHold(privKey, address, data []byte, gasLimit,
} else { } else {
addr = address addr = address
} }
wc := make(chan *txs.EventDataCall) // We want non-blocking on the first event received (but buffer the value),
// after which we want to block (and then discard the value - see below)
wc := make(chan *txs.EventDataCall, 1)
subId := fmt.Sprintf("%X", rec.TxHash) subId := fmt.Sprintf("%X", rec.TxHash)
this.eventEmitter.Subscribe(subId, txs.EventStringAccCall(addr), this.eventEmitter.Subscribe(subId, txs.EventStringAccCall(addr),
func(evt txs.EventData) { func(evt txs.EventData) {
event := evt.(txs.EventDataCall) eventDataCall := evt.(txs.EventDataCall)
if bytes.Equal(event.TxID, rec.TxHash) { if bytes.Equal(eventDataCall.TxID, rec.TxHash) {
wc <- &event // Beware the contract of go-events subscribe is that we must not be
// blocking in an event callback when we try to unsubscribe!
// We work around this by using a non-blocking send.
select {
// This is a non-blocking send, but since we are using a buffered
// channel of size 1 we will always grab our first event even if we
// haven't read from the channel at the time we receive the first event.
case wc <- &eventDataCall:
default:
}
} }
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment