Skip to content

Commit

Permalink
refactor: SignBroadcastWaitTransaction returns a *TxReceipt
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Jan 21, 2020
1 parent b02675c commit 479607f
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 46 deletions.
10 changes: 5 additions & 5 deletions aeternity/aens.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ type nodeStatusHeightAccounterBroadcaster interface {

// RegisterName allows one to easily register a name on AENS. It does the
// preclaim, transaction sending, confirmation and claim for you.
func (ctx *Context) RegisterName(name string, nameFee *big.Int) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
preclaimTx, nameSalt, err := transactions.NewNamePreclaimTx(ctx.Account.Address, name, ctx.ttlnoncer)
func (ctx *Context) RegisterName(name string, nameFee *big.Int) (claimTxReceipt *TxReceipt, err error) {
preclaimTx, nameSalt, err := transactions.NewNamePreclaimTx(ctx.Account.Address, name, ctx.TTLNoncer)
if err != nil {
return
}
_, _, _, _, _, err = ctx.SignBroadcastWait(preclaimTx, config.Client.WaitBlocks)
_, err = ctx.SignBroadcastWait(preclaimTx, config.Client.WaitBlocks)
if err != nil {
return
}

claimTx, err := transactions.NewNameClaimTx(ctx.Account.Address, name, nameSalt, nameFee, ctx.ttlnoncer)
claimTx, err := transactions.NewNameClaimTx(ctx.Account.Address, name, nameSalt, nameFee, ctx.TTLNoncer)
if err != nil {
return
}

signedTxStr, hash, signature, blockHeight, blockHash, err = ctx.SignBroadcastWait(claimTx, config.Client.WaitBlocks)
claimTxReceipt, err = ctx.SignBroadcastWait(claimTx, config.Client.WaitBlocks)
if err != nil {
return
}
Expand Down
18 changes: 9 additions & 9 deletions aeternity/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func findVMABIVersion(nodeVersion, compilerBackend string) (VMVersion, ABIVersio
}

// CreateContract lets one deploy a contract with minimum fuss.
func (ctx *Context) CreateContract(source, function string, args []string, backend string) (ctID, signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
bytecode, err := ctx.compiler.CompileContract(source, backend)
func (ctx *Context) CreateContract(source, function string, args []string, backend string) (ctID string, createTxReceipt *TxReceipt, err error) {
bytecode, err := ctx.Compiler.CompileContract(source, backend)
if err != nil {
return
}
calldata, err := ctx.compiler.EncodeCalldata(source, function, args, backend)
calldata, err := ctx.Compiler.EncodeCalldata(source, function, args, backend)
if err != nil {
return
}

status, err := ctx.node.GetStatus()
status, err := ctx.TxSender.GetStatus()
if err != nil {
return
}
Expand All @@ -40,15 +40,15 @@ func (ctx *Context) CreateContract(source, function string, args []string, backe
return
}

createTx, err := transactions.NewContractCreateTx(ctx.Account.Address, bytecode, VMVersion, ABIVersion, config.Client.Contracts.Deposit, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, calldata, ctx.ttlnoncer)
createTx, err := transactions.NewContractCreateTx(ctx.Account.Address, bytecode, VMVersion, ABIVersion, config.Client.Contracts.Deposit, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, calldata, ctx.TTLNoncer)
if err != nil {
return
}

createTxStr, _ := transactions.SerializeTx(createTx)
fmt.Printf("%+v\n", createTx)
fmt.Println(createTxStr)
signedTxStr, hash, signature, blockHeight, blockHash, err = ctx.SignBroadcastWait(createTx, config.Client.WaitBlocks)
createTxReceipt, err = ctx.SignBroadcastWait(createTx, config.Client.WaitBlocks)
if err != nil {
return
}
Expand All @@ -58,13 +58,13 @@ func (ctx *Context) CreateContract(source, function string, args []string, backe

// CallContract calls a smart contract's function, automatically calling the
// compiler to transform the arguments into bytecode.
func (ctx *Context) CallContract(ctID, source, function string, args []string, backend string) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
callData, err := ctx.compiler.EncodeCalldata(source, function, args, backend)
func (ctx *Context) CallContract(ctID, source, function string, args []string, backend string) (txReceipt *TxReceipt, err error) {
callData, err := ctx.Compiler.EncodeCalldata(source, function, args, backend)
if err != nil {
return
}

callTx, err := transactions.NewContractCallTx(ctx.Account.Address, ctID, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, config.Client.Contracts.ABIVersion, callData, ctx.ttlnoncer)
callTx, err := transactions.NewContractCallTx(ctx.Account.Address, ctID, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, config.Client.Contracts.ABIVersion, callData, ctx.TTLNoncer)
if err != nil {
return
}
Expand Down
19 changes: 15 additions & 4 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,19 @@ type broadcastWaitTransactionNodeCapabilities interface {

// SignBroadcastWaitTransaction is a convenience function that combines
// SignBroadcastTransaction and WaitForTransactionForXBlocks.
func SignBroadcastWaitTransaction(tx transactions.Transaction, signingAccount *account.Account, n broadcastWaitTransactionNodeCapabilities, networkID string, x uint64) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
signedTxStr, hash, signature, err = SignBroadcastTransaction(tx, signingAccount, n, networkID)
func SignBroadcastWaitTransaction(tx transactions.Transaction, signingAccount *account.Account, n broadcastWaitTransactionNodeCapabilities, networkID string, x uint64) (txReceipt *TxReceipt, err error) {
signedTxStr, hash, signature, err := SignBroadcastTransaction(tx, signingAccount, n, networkID)
if err != nil {
return
}
blockHeight, blockHash, err = WaitForTransactionForXBlocks(n, hash, x)
blockHeight, blockHash, err := WaitForTransactionForXBlocks(n, hash, x)
txReceipt = &TxReceipt{
SignedTx: signedTxStr,
Hash: hash,
Signature: signature,
BlockHeight: blockHeight,
BlockHash: blockHash,
}
return
}

Expand Down Expand Up @@ -192,6 +199,10 @@ type TxReceipt struct {
BlockHash string
}

func (t *TxReceipt) String() string {
return fmt.Sprintf("Signed Tx: %s\nHash: %s\nSignature: %s\nBlockHeight: %d\nBlockHash: %s", t.SignedTx, t.Hash, t.Signature, t.BlockHeight, t.BlockHash)
}

type Context struct {
Account *account.Account
NetworkID string
Expand All @@ -213,6 +224,6 @@ func NewContext(signingAccount *account.Account, node transactionSender) (b *Con
}, nil
}

func (c *Context) SignBroadcastWait(tx transactions.Transaction, blocks uint64) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
func (c *Context) SignBroadcastWait(tx transactions.Transaction, blocks uint64) (txReceipt *TxReceipt, err error) {
return SignBroadcastWaitTransaction(tx, c.Account, c.TxSender, c.NetworkID, blocks)
}
4 changes: 2 additions & 2 deletions aeternity/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ func Example() {
fmt.Println("Could not create the SpendTx:", err)
}

signedTxStr, hash, signature, foundAtBlockHeight, foundAtBlockHash, err := SignBroadcastWaitTransaction(tx, alice, node, config.Node.NetworkID, 10)
spendTxReceipt, err := SignBroadcastWaitTransaction(tx, alice, node, config.Node.NetworkID, 10)
if err != nil {
fmt.Println("SignBroadcastTransaction failed with:", err)
}
fmt.Println(signedTxStr, hash, signature, foundAtBlockHeight, foundAtBlockHash)
fmt.Println(spendTxReceipt)

// check the recipient's balance
time.Sleep(2 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion aeternity/oracles.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type generateTTLNoncerNodeInterface interface {

// CreateOracle registers a new oracle with the given queryspec and responsespec
func (ctx *Context) CreateOracle(queryspec, responsespec string, queryFee *big.Int, queryTTLType uint64, oracleTTL uint64) (oracleID string, err error) {
registerTx, err := transactions.NewOracleRegisterTx(ctx.Account.Address, queryspec, responsespec, queryFee, queryTTLType, oracleTTL, config.Client.Oracles.ABIVersion, ctx.ttlnoncer)
registerTx, err := transactions.NewOracleRegisterTx(ctx.Account.Address, queryspec, responsespec, queryFee, queryTTLType, oracleTTL, config.Client.Oracles.ABIVersion, ctx.TTLNoncer)
if err != nil {
return
}
Expand Down
14 changes: 7 additions & 7 deletions integration_test/aens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAENSWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Preclaim %+v with name %s \n", preclaimTx, name)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(preclaimTx, alice, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(preclaimTx, alice, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -60,7 +60,7 @@ func TestAENSWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Claim %+v\n", claimTx)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(claimTx, alice, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(claimTx, alice, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -79,7 +79,7 @@ func TestAENSWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Update %+v\n", updateTx)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(updateTx, alice, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(updateTx, alice, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -96,7 +96,7 @@ func TestAENSWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Transfer %+v\n", transferTx)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(transferTx, alice, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(transferTx, alice, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -107,7 +107,7 @@ func TestAENSWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Update Signed By Recipient %+v\n", updateTx2)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(updateTx2, bob, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(updateTx2, bob, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -118,7 +118,7 @@ func TestAENSWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Revoke %+v\n", revokeTx)
_, _, _, _, _, revokeTxShouldHaveFailed := aeternity.SignBroadcastWaitTransaction(revokeTx, alice, node, networkID, config.Client.WaitBlocks)
_, revokeTxShouldHaveFailed := aeternity.SignBroadcastWaitTransaction(revokeTx, alice, node, networkID, 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 @@ -133,7 +133,7 @@ func TestAENSWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Revoke Signed By Recipient %+v\n", revokeTx2)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(revokeTx2, bob, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(revokeTx2, bob, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions integration_test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ 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) {
_, txHash, _, height, mbHash, err := aeternity.SignBroadcastWaitTransaction(tx, acc, node, networkID, config.Client.WaitBlocks)
receipt, err := aeternity.SignBroadcastWaitTransaction(tx, acc, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
info := txInfo{
height: height,
txHash: txHash,
mbHash: mbHash,
height: receipt.BlockHeight,
txHash: receipt.Hash,
mbHash: receipt.BlockHash,
}
rts := strings.Split(reflect.TypeOf(tx).String(), ".") // ['*aeternity', 'SpendTx']
switch txType := rts[1]; txType {
Expand Down
6 changes: 3 additions & 3 deletions integration_test/contract_higherlevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract SimpleStorage =
function get() : int = state.data
stateful function set(value : int) = put(state{data = value})`

_, _, _, _, _, _, err := aeternity.CreateContract(n, c, alice, simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
_, err := aeternity.CreateContract(n, c, alice, simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
if err != nil {
t.Error(err)
}
Expand All @@ -38,12 +38,12 @@ contract SimpleStorage =
function get() : int = state.data
stateful function set(value : int) = put(state{data = value})`

ctID, _, _, _, _, _, err := aeternity.CreateContract(n, c, alice, simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
ctID, _, err := aeternity.CreateContract(n, c, alice, simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
if err != nil {
t.Fatal(err)
}

_, _, _, _, _, err = aeternity.CallContract(n, c, alice, ctID, simplestorage, "get", []string{}, config.CompilerBackendFATE)
err = aeternity.CallContract(n, c, alice, ctID, simplestorage, "get", []string{}, config.CompilerBackendFATE)
if err != nil {
t.Error(err)
}
Expand Down
8 changes: 4 additions & 4 deletions integration_test/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func TestContracts(t *testing.T) {
t.Fatal(err)
}
ctID, _ = create.ContractID()
_, txHash, _, _, _, err := aeternity.SignBroadcastWaitTransaction(create, alice, node, networkID, config.Client.WaitBlocks)
createTxReceipt, err := aeternity.SignBroadcastWaitTransaction(create, alice, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Create %s, %+v %s\n", ctID, create, txHash)
fmt.Printf("Create Tx\n%s, %+v %s\n", ctID, create, createTxReceipt.Hash)

// Confirm that contract was created
getContract := func() {
Expand All @@ -44,9 +44,9 @@ func TestContracts(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, txHash, _, _, _, err = aeternity.SignBroadcastWaitTransaction(callTx, alice, node, networkID, config.Client.WaitBlocks)
callTxReceipt, err := aeternity.SignBroadcastWaitTransaction(callTx, alice, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Call %+v %s\n", callTx, txHash)
fmt.Printf("Call %+v %s\n", callTx, callTxReceipt.Hash)
}
2 changes: 1 addition & 1 deletion integration_test/ga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestGeneralizedAccounts(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(gaTx, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(gaTx, testAccount, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions integration_test/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestOracleWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Register %+v\n", register)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(register, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(register, testAccount, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -54,7 +54,7 @@ func TestOracleWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Extend %+v\n", extend)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(extend, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(extend, testAccount, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -74,7 +74,7 @@ func TestOracleWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Query %+v\n", query)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(query, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(query, testAccount, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand All @@ -91,7 +91,7 @@ func TestOracleWorkflow(t *testing.T) {
t.Fatal(err)
}
fmt.Printf("Respond %+v\n", respond)
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(respond, testAccount, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(respond, testAccount, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion integration_test/spend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSpendTx(t *testing.T) {
if err != nil {
t.Error(err)
}
_, _, _, _, _, err = aeternity.SignBroadcastWaitTransaction(tx, alice, node, networkID, config.Client.WaitBlocks)
_, err = aeternity.SignBroadcastWaitTransaction(tx, alice, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion integration_test/testsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ 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)
_, err = aeternity.SignBroadcastWaitTransaction(tx, source, node, networkID, config.Client.WaitBlocks)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 479607f

Please sign in to comment.