Skip to content

Commit

Permalink
refactor: use aeternity.SignBroadcast(), aeternity.WaitSynchronous() …
Browse files Browse the repository at this point in the history
…or ctx.SignBroadcastWait() throughout the codebase
  • Loading branch information
randomshinichi committed Jan 21, 2020
1 parent 13b8b55 commit 74f46d9
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 49 deletions.
6 changes: 3 additions & 3 deletions aeternity/aens.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func NewAENS(ctx ContextInterface) *AENS {

// RegisterName allows one to easily register a name on AENS. It does the
// preclaim, transaction sending, confirmation and claim for you.
func (aens *AENS) RegisterName(name string, nameFee *big.Int) (claimTxReceipt *TxReceipt, err error) {
func (aens *AENS) RegisterName(name string, nameFee *big.Int) (txReceipt *TxReceipt, err error) {
preclaimTx, nameSalt, err := transactions.NewNamePreclaimTx(aens.ctx.SenderAccount(), name, aens.ctx.TTLNoncer())
if err != nil {
return
}
_, err = aens.ctx.SignBroadcastWait(preclaimTx, config.Client.WaitBlocks)
txReceipt, err = aens.ctx.SignBroadcastWait(preclaimTx, config.Client.WaitBlocks)
if err != nil {
return
}
Expand All @@ -32,7 +32,7 @@ func (aens *AENS) RegisterName(name string, nameFee *big.Int) (claimTxReceipt *T
return
}

claimTxReceipt, err = aens.ctx.SignBroadcastWait(claimTx, config.Client.WaitBlocks)
txReceipt, err = aens.ctx.SignBroadcastWait(claimTx, config.Client.WaitBlocks)
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions aeternity/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ContextInterface interface {
TTLNoncer() transactions.TTLNoncer
Compiler() CompileEncoder
NodeInfo() (networkID string, version string)
SignBroadcastWait(tx transactions.Transaction, blocks uint64) (*TxReceipt, error)
SignBroadcastWait(tx transactions.Transaction, blocks uint64) (r *TxReceipt, err error)
SetCompiler(c CompileEncoder)
}

Expand Down Expand Up @@ -77,15 +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) (mined bool, txReceipt *TxReceipt, err error) {
func (c *Context) SignBroadcastWait(tx transactions.Transaction, blocks uint64) (txReceipt *TxReceipt, err error) {
networkID, _ := c.txSender.Info()
txReceipt, err = SignBroadcast(tx, c.SigningAccount, c.txSender, networkID)
if err != nil {
return
}

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

// SetCompiler changes the Context's compiler instance.
Expand Down
9 changes: 6 additions & 3 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ func SignBroadcast(tx transactions.Transaction, signingAccount *account.Account,
// 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) {
func WaitSynchronous(txReceipt *TxReceipt, waitBlocks uint64, n getTransactionByHashHeighter) (err error) {
minedChan := make(chan bool)
go txReceipt.Watch(minedChan, waitBlocks, n)
mined = <-minedChan
return mined, txReceipt.Error
mined := <-minedChan
if !mined {
return txReceipt.Error
}
return nil
}

// TxReceipt represents the status of a sent transaction
Expand Down
10 changes: 7 additions & 3 deletions aeternity/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ func Example() {
fmt.Println("Could not create the SpendTx:", err)
}

spendTxReceipt, err := SignBroadcastWaitTransaction(tx, alice, node, config.Node.NetworkID, 10)
spendTxReceipt, err := SignBroadcast(tx, alice, node, config.Node.NetworkID)
if err != nil {
fmt.Println("SignBroadcastTransaction failed with:", err)
fmt.Println("could not send transaction:", err)
}
fmt.Println(spendTxReceipt)
err = WaitSynchronous(spendTxReceipt, 10, node)
if err != nil {
fmt.Println("transaction was not accepted by the blockchain:", err)
}
fmt.Printf("%#v\n", spendTxReceipt)

// check the recipient's balance
time.Sleep(2 * time.Second)
Expand Down
33 changes: 18 additions & 15 deletions integration_test/aens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,31 @@ func randomName(length int) string {
func TestAENSWorkflow(t *testing.T) {
node := setupNetwork(t, privatenetURL, false)
alice, bob := setupAccounts(t)
ttlnoncer := transactions.NewTTLNoncer(node)
ctx, err := aeternity.NewContext(alice, node)
if err != nil {
t.Fatal(err)
}

name := randomName(int(config.Client.Names.NameAuctionMaxLength + 1))
// Preclaim the name
preclaimTx, nameSalt, err := transactions.NewNamePreclaimTx(alice.Address, name, ttlnoncer)
preclaimTx, nameSalt, err := transactions.NewNamePreclaimTx(alice.Address, name, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Preclaim %+v with name %s \n", preclaimTx, name)
_, err = aeternity.SignBroadcastWaitTransaction(preclaimTx, alice, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(preclaimTx, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}

// Claim the name
nameFee := transactions.CalculateMinNameFee(name)
claimTx, err := transactions.NewNameClaimTx(alice.Address, name, nameSalt, nameFee, ttlnoncer)
claimTx, err := transactions.NewNameClaimTx(alice.Address, name, nameSalt, nameFee, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Claim %+v\n", claimTx)
_, err = aeternity.SignBroadcastWaitTransaction(claimTx, alice, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(claimTx, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -74,12 +77,12 @@ func TestAENSWorkflow(t *testing.T) {
delay(printNameEntry)

// Update the name, make it point to something
updateTx, err := transactions.NewNameUpdateTx(alice.Address, name, []string{alice.Address}, config.Client.Names.ClientTTL, ttlnoncer)
updateTx, err := transactions.NewNameUpdateTx(alice.Address, name, []string{alice.Address}, config.Client.Names.ClientTTL, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Update %+v\n", updateTx)
_, err = aeternity.SignBroadcastWaitTransaction(updateTx, alice, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(updateTx, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -91,34 +94,34 @@ func TestAENSWorkflow(t *testing.T) {
}

// Transfer the name to a recipient
transferTx, err := transactions.NewNameTransferTx(alice.Address, name, bob.Address, ttlnoncer)
transferTx, err := transactions.NewNameTransferTx(alice.Address, name, bob.Address, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Transfer %+v\n", transferTx)
_, err = aeternity.SignBroadcastWaitTransaction(transferTx, alice, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(transferTx, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}

// Receiver updates the name, makes it point to himself
updateTx2, err := transactions.NewNameUpdateTx(bob.Address, name, []string{bob.Address}, config.Client.Names.ClientTTL, ttlnoncer)
updateTx2, err := transactions.NewNameUpdateTx(bob.Address, name, []string{bob.Address}, config.Client.Names.ClientTTL, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Update Signed By Recipient %+v\n", updateTx2)
_, err = aeternity.SignBroadcastWaitTransaction(updateTx2, bob, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(updateTx2, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}

// Revoke the name - shouldn't work because it is signed by the sender, who no longer owns the address
revokeTx, err := transactions.NewNameRevokeTx(alice.Address, name, ttlnoncer)
revokeTx, err := transactions.NewNameRevokeTx(alice.Address, name, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Revoke %+v\n", revokeTx)
_, revokeTxShouldHaveFailed := aeternity.SignBroadcastWaitTransaction(revokeTx, alice, node, networkID, config.Client.WaitBlocks)
_, revokeTxShouldHaveFailed := ctx.SignBroadcastWait(revokeTx, config.Client.WaitBlocks)
if revokeTxShouldHaveFailed == nil {
t.Fatal("After transferring the name to Recipient, the Sender should not have been able to revoke the name")
} else if revokeTxShouldHaveFailed.(aeternity.ErrWaitTransaction).NetworkErr == true {
Expand All @@ -128,12 +131,12 @@ func TestAENSWorkflow(t *testing.T) {
}

// Revoke the name - signed by the recipient
revokeTx2, err := transactions.NewNameRevokeTx(bob.Address, name, ttlnoncer)
revokeTx2, err := transactions.NewNameRevokeTx(bob.Address, name, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Revoke Signed By Recipient %+v\n", revokeTx2)
_, err = aeternity.SignBroadcastWaitTransaction(revokeTx2, bob, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(revokeTx2, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 6 additions & 1 deletion integration_test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ var sentTxs txTypes
var useTestNet bool

func signBroadcastWaitKeepTrackOfTx(t *testing.T, tx transactions.Transaction, acc *account.Account, node *naet.Node) (height uint64, txHash string, mbHash string) {
receipt, err := aeternity.SignBroadcastWaitTransaction(tx, acc, node, networkID, config.Client.WaitBlocks)
receipt, err := aeternity.SignBroadcast(tx, acc, node, networkID)
if err != nil {
t.Fatal(err)
}
err = aeternity.WaitSynchronous(receipt, config.Client.WaitBlocks, node)
if err != nil {
t.Fatal(err)
}

info := txInfo{
height: receipt.BlockHeight,
txHash: receipt.Hash,
Expand Down
13 changes: 8 additions & 5 deletions integration_test/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ import (
func TestContracts(t *testing.T) {
alice, _ := setupAccounts(t)
node := setupNetwork(t, privatenetURL, false)
ttlnoncer := transactions.NewTTLNoncer(node)
ctx, err := aeternity.NewContext(alice, node)
if err != nil {
t.Fatal(err)
}

var ctID string

identityBytecode := string(golden.Get(t, "identity_bytecode.txt"))
identityInitCalldata := string(golden.Get(t, "identity_initcalldata.txt"))
create, err := transactions.NewContractCreateTx(alice.Address, identityBytecode, config.Client.Contracts.VMVersion, config.Client.Contracts.ABIVersion, config.Client.Contracts.Deposit, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, identityInitCalldata, ttlnoncer)
create, err := transactions.NewContractCreateTx(alice.Address, identityBytecode, config.Client.Contracts.VMVersion, config.Client.Contracts.ABIVersion, config.Client.Contracts.Deposit, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, identityInitCalldata, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
ctID, _ = create.ContractID()
createTxReceipt, err := aeternity.SignBroadcastWaitTransaction(create, alice, node, networkID, config.Client.WaitBlocks)
createTxReceipt, err := ctx.SignBroadcastWait(create, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -40,11 +43,11 @@ func TestContracts(t *testing.T) {
delay(getContract)

identityMain42Calldata := string(golden.Get(t, "identity_main42.txt"))
callTx, err := transactions.NewContractCallTx(alice.Address, ctID, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, config.Client.Contracts.ABIVersion, identityMain42Calldata, ttlnoncer)
callTx, err := transactions.NewContractCallTx(alice.Address, ctID, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, config.Client.Contracts.ABIVersion, identityMain42Calldata, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
callTxReceipt, err := aeternity.SignBroadcastWaitTransaction(callTx, alice, node, networkID, config.Client.WaitBlocks)
callTxReceipt, err := ctx.SignBroadcastWait(callTx, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 8 additions & 4 deletions integration_test/ga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func TestGeneralizedAccounts(t *testing.T) {
alice, bob := setupAccounts(t)
node := setupNetwork(t, privatenetURL, false)
compiler := naet.NewCompiler(config.Client.Contracts.CompilerURL, false)
ttlnoncer := transactions.NewTTLNoncer(node)
ctx, err := aeternity.NewContext(alice, node)
if err != nil {
t.Fatal(err)
}
ctx.SetCompiler(compiler)

// Take note of Bob's balance, and after this test, we expect it to have this much more AE
amount := utils.NewIntFromUint64(5000)
Expand Down Expand Up @@ -73,11 +77,11 @@ func TestGeneralizedAccounts(t *testing.T) {
if err != nil {
t.Fatal(err)
}
gaTx, err := transactions.NewGAAttachTx(testAccount.Address, authBytecode, auth.TypeInfo[0].FuncHash, config.Client.Contracts.VMVersion, config.Client.Contracts.ABIVersion, config.Client.Contracts.GasLimit, config.Client.GasPrice, authInitCalldata, ttlnoncer)
gaTx, err := transactions.NewGAAttachTx(testAccount.Address, authBytecode, auth.TypeInfo[0].FuncHash, config.Client.Contracts.VMVersion, config.Client.Contracts.ABIVersion, config.Client.Contracts.GasLimit, config.Client.GasPrice, authInitCalldata, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
_, err = aeternity.SignBroadcastWaitTransaction(gaTx, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(gaTx, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -103,7 +107,7 @@ func TestGeneralizedAccounts(t *testing.T) {
return 0, 0, 0, nil
}
spendTx, err := transactions.NewSpendTx(testAccount.Address, bob.Address, big.NewInt(5000), []byte{}, metaTxTTLNoncer)
gaMetaTx, err := transactions.NewGAMetaTx(testAccount.Address, authData, config.Client.Contracts.ABIVersion, config.Client.GasPrice, config.Client.GasPrice, spendTx, ttlnoncer)
gaMetaTx, err := transactions.NewGAMetaTx(testAccount.Address, authData, config.Client.Contracts.ABIVersion, config.Client.GasPrice, config.Client.GasPrice, spendTx, ctx.TTLNoncer())

gaMetaTxFinal, hash, _, err := transactions.SignHashTx(testAccount, gaMetaTx, config.Node.NetworkID)
if err != nil {
Expand Down
21 changes: 12 additions & 9 deletions integration_test/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func TestOracleHLL(t *testing.T) {
func TestOracleWorkflow(t *testing.T) {
alice, _ := setupAccounts(t)
node := setupNetwork(t, privatenetURL, false)
ttlnoncer := transactions.NewTTLNoncer(node)
ctx, err := aeternity.NewContext(alice, node)
if err != nil {
t.Fatal(err)
}

// Setup temporary test account and fund it
testAccount, err := account.New()
Expand All @@ -42,12 +45,12 @@ func TestOracleWorkflow(t *testing.T) {
fundAccount(t, node, alice, testAccount, big.NewInt(1000000000000000000))

// Register
register, err := transactions.NewOracleRegisterTx(testAccount.Address, "hello", "helloback", config.Client.Oracles.QueryFee, config.OracleTTLTypeDelta, config.Client.Oracles.OracleTTLValue, config.Client.Oracles.ABIVersion, ttlnoncer)
register, err := transactions.NewOracleRegisterTx(testAccount.Address, "hello", "helloback", config.Client.Oracles.QueryFee, config.OracleTTLTypeDelta, config.Client.Oracles.OracleTTLValue, config.Client.Oracles.ABIVersion, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Register %+v\n", register)
_, err = aeternity.SignBroadcastWaitTransaction(register, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(register, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -66,12 +69,12 @@ func TestOracleWorkflow(t *testing.T) {
// Extend
// save the oracle's initial TTL so we can compare it with after OracleExtendTx
oracleTTL := oracle.TTL
extend, err := transactions.NewOracleExtendTx(testAccount.Address, oraclePubKey, config.OracleTTLTypeDelta, config.Client.Oracles.OracleTTLValue, ttlnoncer)
extend, err := transactions.NewOracleExtendTx(testAccount.Address, oraclePubKey, config.OracleTTLTypeDelta, config.Client.Oracles.OracleTTLValue, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Extend %+v\n", extend)
_, err = aeternity.SignBroadcastWaitTransaction(extend, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(extend, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -86,12 +89,12 @@ func TestOracleWorkflow(t *testing.T) {
}

// Query
query, err := transactions.NewOracleQueryTx(testAccount.Address, oraclePubKey, "How was your day?", config.Client.Oracles.QueryFee, 0, 100, 0, 100, ttlnoncer)
query, err := transactions.NewOracleQueryTx(testAccount.Address, oraclePubKey, "How was your day?", config.Client.Oracles.QueryFee, 0, 100, 0, 100, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Query %+v\n", query)
_, err = aeternity.SignBroadcastWaitTransaction(query, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(query, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -103,12 +106,12 @@ func TestOracleWorkflow(t *testing.T) {
}

// Respond
respond, err := transactions.NewOracleRespondTx(testAccount.Address, oraclePubKey, oqID, "My day was fine thank you", config.OracleTTLTypeDelta, config.Client.Oracles.ResponseTTLValue, ttlnoncer)
respond, err := transactions.NewOracleRespondTx(testAccount.Address, oraclePubKey, oqID, "My day was fine thank you", config.OracleTTLTypeDelta, config.Client.Oracles.ResponseTTLValue, ctx.TTLNoncer())
if err != nil {
t.Fatal(err)
}
fmt.Printf("Respond %+v\n", respond)
_, err = aeternity.SignBroadcastWaitTransaction(respond, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(respond, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 5 additions & 1 deletion integration_test/spend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func TestSpendTx(t *testing.T) {
if err != nil {
t.Error(err)
}
_, err = aeternity.SignBroadcastWaitTransaction(tx, alice, node, networkID, config.Client.WaitBlocks)
receipt, err := aeternity.SignBroadcast(tx, alice, node, networkID)
if err != nil {
t.Fatal(err)
}
err = aeternity.WaitSynchronous(receipt, config.Client.WaitBlocks, node)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 5 additions & 1 deletion integration_test/testsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ func fundAccount(t *testing.T, node *naet.Node, source, destination *account.Acc
if err != nil {
t.Fatal(err)
}
_, err = aeternity.SignBroadcastWaitTransaction(tx, source, node, networkID, config.Client.WaitBlocks)
receipt, err := aeternity.SignBroadcast(tx, source, node, networkID)
if err != nil {
t.Fatal(err)
}
err = aeternity.WaitSynchronous(receipt, config.Client.WaitBlocks, node)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 74f46d9

Please sign in to comment.