Skip to content

Commit

Permalink
refactor: aeternity.WaitSynchronous()
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Jan 21, 2020
1 parent e94898a commit 13b8b55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 5 additions & 6 deletions aeternity/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ func (c *Context) NodeInfo() (networkID string, version string) {
}

// SignBroadcastWait signs, sends and waits for the transaction to be mined.
func (c *Context) SignBroadcastWait(tx transactions.Transaction, blocks uint64) (txReceipt *TxReceipt, err error) {
func (c *Context) SignBroadcastWait(tx transactions.Transaction, blocks uint64) (mined bool, txReceipt *TxReceipt, err error) {
networkID, _ := c.txSender.Info()
txReceipt, err := SignBroadcast(tx, c.SigningAccount, c.txSender, networkID)
txReceipt, err = SignBroadcast(tx, c.SigningAccount, c.txSender, networkID)
if err != nil {
return
}
minedChan := make(chan bool)
go txReceipt.Watch(minedChan, blocks, c.txSender)
mined = <-minedChan
return mined, txReceipt

mined, err = Wait(txReceipt, blocks, c.txSender)
return mined, txReceipt, err
}

// SetCompiler changes the Context's compiler instance.
Expand Down
10 changes: 10 additions & 0 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ func SignBroadcast(tx transactions.Transaction, signingAccount *account.Account,
return
}

// WaitSynchronous blocks until TxReceipt.Watch() reports that a transaction was
// mined/not mined. It is intended as a convenience function since it makes an
// asynchronous operation synchronous.
func WaitSynchronous(txReceipt *TxReceipt, waitBlocks uint64, n getTransactionByHashHeighter) (mined bool, err error) {
minedChan := make(chan bool)
go txReceipt.Watch(minedChan, waitBlocks, n)
mined = <-minedChan
return mined, txReceipt.Error
}

// TxReceipt represents the status of a sent transaction
type TxReceipt struct {
Tx *transactions.Transaction
Expand Down

0 comments on commit 13b8b55

Please sign in to comment.