From eda4f696b1eb18e26fdaf4cbf4ff90031702aea2 Mon Sep 17 00:00:00 2001 From: Andrew Chiw Date: Thu, 15 Aug 2019 18:02:13 +0200 Subject: [PATCH] refactor: SignBroadcast() is useful enough that it should be a helper, as SignBroadcastTransaction() WaitForTransactionUntilHeight() required wrapping code, but WaitForTransactionForXBlocks() is more useful --- aeternity/helpers.go | 36 ++++++++++++++++++++++++++++++----- aeternity/helpers_test.go | 4 ++-- integration_test/testsetup.go | 4 +--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/aeternity/helpers.go b/aeternity/helpers.go index 36b071c5..4ee01642 100644 --- a/aeternity/helpers.go +++ b/aeternity/helpers.go @@ -7,6 +7,8 @@ import ( "os" "path/filepath" "time" + + rlp "github.com/randomshinichi/rlpae" ) // GetHeightAccountNamer is used by Helper{} methods to describe the @@ -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 @@ -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 @@ -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 +} diff --git a/aeternity/helpers_test.go b/aeternity/helpers_test.go index 110ee329..b7cdcfba 100644 --- a/aeternity/helpers_test.go +++ b/aeternity/helpers_test.go @@ -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) } diff --git a/integration_test/testsetup.go b/integration_test/testsetup.go index b2b13646..dc703a7b 100644 --- a/integration_test/testsetup.go +++ b/integration_test/testsetup.go @@ -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