Skip to content

Commit

Permalink
refactor: SignBroadcast() is useful enough that it should be a helper…
Browse files Browse the repository at this point in the history
…, as SignBroadcastTransaction()

WaitForTransactionUntilHeight() required wrapping code, but WaitForTransactionForXBlocks() is more useful
  • Loading branch information
randomshinichi committed Sep 11, 2019
1 parent 18c973c commit eda4f69
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
36 changes: 31 additions & 5 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"
"time"

rlp "github.com/randomshinichi/rlpae"
)

// GetHeightAccountNamer is used by Helper{} methods to describe the
Expand Down Expand Up @@ -348,10 +350,14 @@ func VerifySignedTx(accountID string, txSigned string, networkID string) (valid
return
}

// WaitForTransactionUntilHeight waits for a transaction until heightLimit (inclusive) is reached
func WaitForTransactionUntilHeight(c getTransactionByHashHeighter, txHash string, untilHeight uint64) (blockHeight uint64, blockHash string, err error) {
var nodeHeight uint64
for nodeHeight <= untilHeight {
// WaitForTransactionForXBlocks waits for a transaction until X blocks have gone by
func WaitForTransactionForXBlocks(c getTransactionByHashHeighter, txHash string, x uint64) (blockHeight uint64, blockHash string, err error) {
nodeHeight, err := c.GetHeight()
if err != nil {
return
}
endHeight := nodeHeight + x
for nodeHeight <= endHeight {
nodeHeight, err = c.GetHeight()
if err != nil {
return 0, "", err
Expand All @@ -367,7 +373,7 @@ func WaitForTransactionUntilHeight(c getTransactionByHashHeighter, txHash string
}
time.Sleep(time.Millisecond * time.Duration(Config.Tuning.ChainPollInteval))
}
return 0, "", fmt.Errorf("It is already height %v and %v still isn't in a block", nodeHeight, txHash)
return 0, "", fmt.Errorf("%v blocks have gone by and %v still isn't in a block", x, txHash)
}

// BroadcastTransaction differs from Client.PostTransaction() in that the latter just handles
Expand All @@ -391,3 +397,23 @@ func BroadcastTransaction(c PostTransactioner, txSignedBase64 string) (err error
err = c.PostTransaction(txSignedBase64, signedEncodedTxHash)
return
}

// SignBroadcastTransaction is a convenience function that signs your
// transaction before calling BroadcastTransaction on it
func SignBroadcastTransaction(tx rlp.Encoder, signingAccount *Account, n *Node, networkID string) (signedTxStr, hash, signature string, err error) {
signedTx, hash, signature, err := SignHashTx(signingAccount, tx, networkID)
if err != nil {
return
}

signedTxStr, err = SerializeTx(&signedTx)
if err != nil {
return
}

err = BroadcastTransaction(n, signedTxStr)
if err != nil {
return
}
return
}
4 changes: 2 additions & 2 deletions aeternity/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func (m *mockClient) GetTransactionByHash(hash string) (tx *models.GenericSigned
}
return tx, nil
}
func TestWaitForTransactionUntilHeight(t *testing.T) {
func TestWaitForTransactionForXBlocks(t *testing.T) {
m := new(mockClient)
blockHeight, blockHash, err := WaitForTransactionUntilHeight(m, "th_transactionhash", 10)
blockHeight, blockHash, err := WaitForTransactionForXBlocks(m, "th_transactionhash", 10)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 1 addition & 3 deletions integration_test/testsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ func getHeight(aeNode *aeternity.Node) (h uint64) {
}

func waitForTransaction(aeNode *aeternity.Node, hash string) (height uint64, microblockHash string, err error) {
height = getHeight(aeNode)
// fmt.Println("Waiting for", hash)
height, microblockHash, err = aeternity.WaitForTransactionUntilHeight(aeNode, hash, height+10)
height, microblockHash, err = aeternity.WaitForTransactionForXBlocks(aeNode, hash, 10)
if err != nil {
// Sometimes, the tests want the tx to fail. Return the err to let them know.
return 0, "", err
Expand Down

0 comments on commit eda4f69

Please sign in to comment.