Skip to content

Commit

Permalink
refactor: Replaced Helpers with closures for GetTTL, GetNextNonce, Ge…
Browse files Browse the repository at this point in the history
…tAnythingByName.

standardizing referring to *aeternity.Node as 'node'
better comments for helpers.go
  • Loading branch information
randomshinichi committed Sep 11, 2019
1 parent 7344548 commit 3c1ad87
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 250 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if err != nil {
fmt.Println(err)
return
}
aeNode := aeternity.NewNode("http://localhost:3013", false).WithAccount(acc)
node := aeternity.NewNode("http://localhost:3013", false).WithAccount(acc)
```

Most parameters are set by modifying the variables in `config.go` in this manner:
Expand Down
264 changes: 147 additions & 117 deletions aeternity/helpers.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ var balanceCmd = &cobra.Command{
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return balanceFunc(aeNode, args)
node := newAeNode()
return balanceFunc(node, args)
},
}

Expand Down
24 changes: 12 additions & 12 deletions cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var topCmd = &cobra.Command{
Short: "Query the top block of the chain",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return topFunc(aeNode, args)
node := newAeNode()
return topFunc(node, args)
},
}

Expand All @@ -53,8 +53,8 @@ var statusCmd = &cobra.Command{
Short: "Get the status and status of the node running the chain",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return statusFunc(aeNode, args)
node := newAeNode()
return statusFunc(node, args)
},
}

Expand All @@ -73,8 +73,8 @@ var playCmd = &cobra.Command{
Short: "Query the blocks of the chain one after the other",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return playFunc(aeNode, args)
node := newAeNode()
return playFunc(node, args)
},
}

Expand Down Expand Up @@ -121,8 +121,8 @@ var broadcastCmd = &cobra.Command{
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return broadcastFunc(aeNode, args)
node := newAeNode()
return broadcastFunc(node, args)
},
}

Expand Down Expand Up @@ -161,8 +161,8 @@ var ttlCmd = &cobra.Command{
Long: `Get the absolute TTL (node's height + recommended TTL offset) for a Transaction`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return ttlFunc(aeNode, args)
node := newAeNode()
return ttlFunc(node, args)
},
}

Expand All @@ -183,8 +183,8 @@ var networkIDCmd = &cobra.Command{
Long: ``,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return networkIDFunc(aeNode, args)
node := newAeNode()
return networkIDFunc(node, args)
},
}

Expand Down
38 changes: 0 additions & 38 deletions cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,6 @@ import (
models "github.com/aeternity/aepp-sdk-go/swagguard/node/models"
)

type mockHelpers struct{}

func (mh mockHelpers) GetTTL(uint64) (uint64, error) {
return 500, nil
}
func (mh mockHelpers) GetNextNonce(string) (uint64, error) {
return 1337, nil
}
func (mh mockHelpers) GetTTLNonce(string, uint64) (uint64, uint64, error) {
return 500, 1337, nil
}
func (mh mockHelpers) GetAccountsByName(string) ([]string, error) {
return []string{"ak_address"}, nil
}
func (mh mockHelpers) GetOraclesByName(string) ([]string, error) {
return []string{"ok_address"}, nil
}
func (mh mockHelpers) GetContractsByName(string) ([]string, error) {
return []string{"ct_address"}, nil
}
func (mh mockHelpers) GetChannelsByName(string) ([]string, error) {
return []string{"channel address?"}, nil
}

type mockGetHeighter struct {
h uint64
}
Expand All @@ -37,20 +13,6 @@ func (m *mockGetHeighter) GetHeight() (uint64, error) {
return m.h, nil
}

type mockgetHeightAccounter struct {
height uint64
account string
}

func (m *mockgetHeightAccounter) GetHeight() (uint64, error) {
return m.height, nil
}
func (m *mockgetHeightAccounter) GetAccount(accountID string) (acc *models.Account, err error) {
acc = &models.Account{}
err = acc.UnmarshalBinary([]byte(m.account))
return acc, err
}

type mockGetTopBlocker struct {
msg string
}
Expand Down
62 changes: 44 additions & 18 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ var txSpendCmd = &cobra.Command{
Long: ``,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
u := aeternity.Helpers{Node: aeNode}
return txSpendFunc(u, args)
node := newAeNode()
ttlFunc := aeternity.GenerateGetTTL(node)
nonceFunc := aeternity.GenerateGetNextNonce(node)
return txSpendFunc(ttlFunc, nonceFunc, args)
},
}

func txSpendFunc(helpers aeternity.HelpersInterface, args []string) (err error) {
func txSpendFunc(ttlFunc aeternity.GetTTLFunc, nonceFunc aeternity.GetNextNonceFunc, args []string) (err error) {
var (
sender string
recipient string
Expand Down Expand Up @@ -62,9 +63,19 @@ func txSpendFunc(helpers aeternity.HelpersInterface, args []string) (err error)
return errors.New("Error, missing or invalid fee")
}

// Connect to the node to find out sender nonce only
// If nonce was not specified as an argument, connect to the node to
// query it
if nonce == 0 {
nonce, err = helpers.GetNextNonce(sender)
nonce, err = nonceFunc(sender)
if err != nil {
return err
}
}

// If TTL was not specified as an argument, connect to the node to calculate
// it
if ttl == 0 {
ttl, err = ttlFunc(aeternity.Config.Client.TTL)
if err != nil {
return err
}
Expand All @@ -76,7 +87,7 @@ func txSpendFunc(helpers aeternity.HelpersInterface, args []string) (err error)
return err
}

// Sender, Recipient, Amount, Ttl, Fee, Nonce, Payload, Encoded
// Print the result
Pp(
"Sender acount", tx.SenderID,
"Recipient account", tx.RecipientID,
Expand All @@ -96,9 +107,10 @@ var txContractCreateCmd = &cobra.Command{
Long: ``,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
u := aeternity.Helpers{Node: aeNode}
return txContractCreateFunc(u, args)
node := newAeNode()
ttlFunc := aeternity.GenerateGetTTL(node)
nonceFunc := aeternity.GenerateGetNextNonce(node)
return txContractCreateFunc(ttlFunc, nonceFunc, args)
},
}

Expand All @@ -107,14 +119,14 @@ type getHeightAccounter interface {
aeternity.GetAccounter
}

func txContractCreateFunc(h aeternity.HelpersInterface, args []string) (err error) {
func txContractCreateFunc(ttlFunc aeternity.GetTTLFunc, nonceFunc aeternity.GetNextNonceFunc, args []string) (err error) {
var (
owner string
contract string
calldata string
)

// Load variables from arguments
// Load variables from arguments and validate them
owner = args[0]
if !IsAddress(owner) {
return errors.New("Error, missing or invalid owner address")
Expand All @@ -128,18 +140,32 @@ func txContractCreateFunc(h aeternity.HelpersInterface, args []string) (err erro
return errors.New("Error, missing or invalid init calldata bytecode")
}

c := aeternity.Context{Helpers: h, Address: owner}

tx, err := c.ContractCreateTx(contract, calldata, aeternity.Config.Client.Contracts.VMVersion, aeternity.Config.Client.Contracts.ABIVersion, aeternity.Config.Client.Contracts.Deposit, aeternity.Config.Client.Contracts.Amount, aeternity.Config.Client.Contracts.Gas, aeternity.Config.Client.Contracts.GasPrice, aeternity.Config.Client.Fee)
// If nonce was not specified as an argument, connect to the node to
// query it
if nonce == 0 {
nonce, err = nonceFunc(owner)
if err != nil {
return err
}
}

if err != nil {
return err
// If TTL was not specified as an argument, connect to the node to calculate
// it
if ttl == 0 {
ttl, err = ttlFunc(aeternity.Config.Client.TTL)
if err != nil {
return err
}
}

tx := aeternity.NewContractCreateTx(owner, nonce, contract, aeternity.Config.Client.Contracts.VMVersion, aeternity.Config.Client.Contracts.ABIVersion, aeternity.Config.Client.Contracts.Deposit, aeternity.Config.Client.Contracts.Amount, aeternity.Config.Client.Contracts.Gas, aeternity.Config.Client.Contracts.GasPrice, aeternity.Config.Client.Fee, ttl, calldata)

txStr, err := aeternity.SerializeTx(tx)
if err != nil {
return err
}

// Print the result
Pp(
"OwnerID", tx.OwnerID,
"AccountNonce", tx.AccountNonce,
Expand Down Expand Up @@ -229,7 +255,7 @@ func init() {

// tx spend command
txSpendCmd.Flags().StringVar(&fee, "fee", aeternity.Config.Client.Fee.String(), fmt.Sprintf("Set the transaction fee (default=%s)", aeternity.Config.Client.Fee.String()))
txSpendCmd.Flags().Uint64Var(&ttl, "ttl", aeternity.Config.Client.TTL, fmt.Sprintf("Set the TTL in keyblocks (default=%d)", aeternity.Config.Client.TTL))
txSpendCmd.Flags().Uint64Var(&ttl, "ttl", 0, fmt.Sprintf("Set the TTL in keyblocks (default=%d)", 0))
txSpendCmd.Flags().Uint64Var(&nonce, "nonce", 0, fmt.Sprint("Set the sender account nonce, if not the chain will be queried for its value"))
txSpendCmd.Flags().StringVar(&spendTxPayload, "payload", "", fmt.Sprint("Optional text payload for Spend Transactions, which will be turned into a bytearray"))
}
24 changes: 14 additions & 10 deletions cmd/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ var bob = "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v"

func Test_txSpendFunc(t *testing.T) {
type args struct {
helpers aeternity.HelpersInterface
args []string
ttlFunc aeternity.GetTTLFunc
nonceFunc aeternity.GetNextNonceFunc
args []string
}
tests := []struct {
name string
Expand All @@ -23,15 +24,16 @@ func Test_txSpendFunc(t *testing.T) {
{
name: "Alice sends 10 to Bob",
args: args{
helpers: &mockHelpers{},
args: []string{alice, bob, "10"},
ttlFunc: func(offset uint64) (ttl uint64, err error) { return 500, nil },
nonceFunc: func(address string) (nonce uint64, err error) { return 2, nil },
args: []string{alice, bob, "10"},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := txSpendFunc(tt.args.helpers, tt.args.args); (err != nil) != tt.wantErr {
if err := txSpendFunc(tt.args.ttlFunc, tt.args.nonceFunc, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("txSpendFunc() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down Expand Up @@ -63,8 +65,9 @@ func TestTxDumpRaw(t *testing.T) {

func Test_txContractCreateFunc(t *testing.T) {
type args struct {
helpers aeternity.HelpersInterface
args []string
ttlFunc aeternity.GetTTLFunc
nonceFunc aeternity.GetNextNonceFunc
args []string
}
tests := []struct {
name string
Expand All @@ -74,15 +77,16 @@ func Test_txContractCreateFunc(t *testing.T) {
{
name: "Deploy SimpleStorage with alice (unsigned)",
args: args{
helpers: &mockHelpers{},
args: []string{alice, contractSimpleStorageBytecode, contractSimpleStorageInit42},
ttlFunc: func(offset uint64) (ttl uint64, err error) { return 500, nil },
nonceFunc: func(address string) (nonce uint64, err error) { return 2, nil },
args: []string{alice, contractSimpleStorageBytecode, contractSimpleStorageInit42},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := txContractCreateFunc(tt.args.helpers, tt.args.args); (err != nil) != tt.wantErr {
if err := txContractCreateFunc(tt.args.ttlFunc, tt.args.nonceFunc, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("txContractCreateFunc() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
5 changes: 2 additions & 3 deletions integration_test/aens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func randomName(length int) string {

func TestAENSWorkflow(t *testing.T) {
node := setupNetwork(t, privatenetURL, false)
helpers := aeternity.Helpers{Node: node}
alice, bob := setupAccounts(t)
aensAlice := aeternity.Context{Helpers: helpers, Address: alice.Address}
aensAlice := aeternity.NewContextFromNode(node, alice.Address)

name := randomName(6)
// Preclaim the name
Expand Down Expand Up @@ -108,7 +107,7 @@ func TestAENSWorkflow(t *testing.T) {
_, _, _ = waitForTransaction(node, hash)

// Receiver updates the name, makes it point to himself
aensBob := aeternity.Context{Helpers: helpers, Address: bob.Address}
aensBob := aeternity.NewContextFromNode(node, bob.Address)

updateTx2, err := aensBob.NameUpdateTx(name, bob.Address)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions integration_test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ func TestAPI(t *testing.T) {
alice, bob := setupAccounts(t)

name := randomName(6)
helpers := aeternity.Helpers{Node: privateNet}
ctxAlice := aeternity.Context{Helpers: helpers, Address: alice.Address}
ctxBob := aeternity.Context{Helpers: helpers, Address: bob.Address}
ctxAlice := aeternity.NewContextFromNode(privateNet, alice.Address)
ctxBob := aeternity.NewContextFromNode(privateNet, bob.Address)
// SpendTx
fmt.Println("SpendTx")
spendTx, err := ctxAlice.SpendTx(sender, bob.Address, *big.NewInt(1000), aeternity.Config.Client.Fee, []byte(""))
Expand Down

0 comments on commit 3c1ad87

Please sign in to comment.