Skip to content

Commit

Permalink
refactor: Transaction interface now includes the TransactionFeeCalcul…
Browse files Browse the repository at this point in the history
…able methods
  • Loading branch information
randomshinichi committed Nov 11, 2019
1 parent 5986f00 commit 4575526
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 60 deletions.
5 changes: 2 additions & 3 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/aeternity/aepp-sdk-go/v6/config"
"github.com/aeternity/aepp-sdk-go/v6/naet"
"github.com/aeternity/aepp-sdk-go/v6/transactions"
rlp "github.com/randomshinichi/rlpae"
)

// GetAnythingByNameFunc describes a function that returns lookup results for a
Expand Down Expand Up @@ -132,7 +131,7 @@ func WaitForTransactionForXBlocks(c getTransactionByHashHeighter, txHash string,
}

// SignBroadcastTransaction signs a transaction and broadcasts it to a node.
func SignBroadcastTransaction(tx rlp.Encoder, signingAccount *account.Account, n naet.PostTransactioner, networkID string) (signedTxStr, hash, signature string, err error) {
func SignBroadcastTransaction(tx transactions.Transaction, signingAccount *account.Account, n naet.PostTransactioner, networkID string) (signedTxStr, hash, signature string, err error) {
signedTx, hash, signature, err := transactions.SignHashTx(signingAccount, tx, networkID)
if err != nil {
return
Expand All @@ -157,7 +156,7 @@ type broadcastWaitTransactionNodeCapabilities interface {

// SignBroadcastWaitTransaction is a convenience function that combines
// SignBroadcastTransaction and WaitForTransactionForXBlocks.
func SignBroadcastWaitTransaction(tx rlp.Encoder, signingAccount *account.Account, n broadcastWaitTransactionNodeCapabilities, networkID string, x uint64) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
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)
if err != nil {
return
Expand Down
3 changes: 1 addition & 2 deletions integration_test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/aeternity/aepp-sdk-go/v6/naet"
"github.com/aeternity/aepp-sdk-go/v6/swagguard/node/models"
"github.com/aeternity/aepp-sdk-go/v6/transactions"
rlp "github.com/randomshinichi/rlpae"
)

/*
Expand Down Expand Up @@ -59,7 +58,7 @@ type txTypes struct {
var sentTxs txTypes
var useTestNet bool

func signBroadcastWaitKeepTrackOfTx(t *testing.T, tx rlp.Encoder, acc *account.Account, node *naet.Node) (height uint64, txHash string, mbHash string) {
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)
if err != nil {
t.Fatal(err)
Expand Down
32 changes: 21 additions & 11 deletions transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,11 @@ func buildContractID(sender string, senderNonce uint64) (ctID string, err error)
}

// Transaction is used to indicate a transaction of any type.
type Transaction interface {
rlp.Encoder
}

// TransactionFeeCalculable is a set of methods that simplify calculating the tx
// fee. In particular, SetFee and GetFee let the code increase the fee further
// In particular, SetFee and GetFee let the code increase the fee further
// in case the newer, calculated fee ends up increasing the size of the
// transaction (and thus necessitates an even larger fee)
type TransactionFeeCalculable interface {
Transaction
type Transaction interface {
rlp.Encoder
SetFee(*big.Int)
GetFee() *big.Int
CalcGas() (g *big.Int, err error)
Expand Down Expand Up @@ -285,7 +280,7 @@ func normalGasComponent(tx Transaction, gasLimit *big.Int) (gas *big.Int, err er

// CalculateFee calculates the required transaction fee, and increases the fee
// further in case the newer fee ends up increasing the transaction size.
func CalculateFee(tx TransactionFeeCalculable) (err error) {
func CalculateFee(tx Transaction) (err error) {
var fee, gas, newFee *big.Int
for {
fee = tx.GetFee()
Expand Down Expand Up @@ -344,7 +339,7 @@ func GetTransactionType(rawRLP []byte) (tx Transaction, err error) {
// SignedTx wraps around other Tx structs to hold the signature.
type SignedTx struct {
Signatures [][]byte
Tx rlp.Encoder
Tx Transaction
}

// EncodeRLP implements rlp.Encoder
Expand Down Expand Up @@ -436,8 +431,23 @@ func (tx *SignedTx) DecodeRLP(s *rlp.Stream) (err error) {
return
}

// SetFee implements Transaction
func (tx *SignedTx) SetFee(f *big.Int) {
tx.Tx.SetFee(f)
}

// GetFee implements Transaction
func (tx *SignedTx) GetFee() *big.Int {
return tx.Tx.GetFee()
}

// CalcGas implements Transaction
func (tx *SignedTx) CalcGas() (g *big.Int, err error) {
return tx.Tx.CalcGas()
}

// NewSignedTx ensures that all fields of SignedTx are filled out.
func NewSignedTx(Signatures [][]byte, tx rlp.Encoder) (s *SignedTx) {
func NewSignedTx(Signatures [][]byte, tx Transaction) (s *SignedTx) {
return &SignedTx{
Signatures: Signatures,
Tx: tx,
Expand Down
2 changes: 1 addition & 1 deletion transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func Test_readIDTag(t *testing.T) {
}

func Test_CalculateFee(t *testing.T) {
tests := []TransactionFeeCalculable{
tests := []Transaction{
&SpendTx{
SenderID: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
RecipientID: "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v",
Expand Down
30 changes: 15 additions & 15 deletions transactions/tx_aens.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,17 @@ func (tx *NamePreclaimTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *NamePreclaimTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *NamePreclaimTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *NamePreclaimTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down Expand Up @@ -334,17 +334,17 @@ func (tx *NameClaimTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *NameClaimTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *NameClaimTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *NameClaimTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down Expand Up @@ -577,17 +577,17 @@ func (tx *NameUpdateTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *NameUpdateTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *NameUpdateTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *NameUpdateTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down Expand Up @@ -726,17 +726,17 @@ func (tx *NameRevokeTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *NameRevokeTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *NameRevokeTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *NameRevokeTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down Expand Up @@ -881,17 +881,17 @@ func (tx *NameTransferTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *NameTransferTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *NameTransferTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *NameTransferTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down
12 changes: 6 additions & 6 deletions transactions/tx_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ func (tx *ContractCreateTx) ContractID() (string, error) {
return buildContractID(tx.OwnerID, tx.AccountNonce)
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *ContractCreateTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *ContractCreateTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *ContractCreateTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Mul(config.Client.BaseGas, big.NewInt(5))
Expand Down Expand Up @@ -353,17 +353,17 @@ func (tx *ContractCallTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *ContractCallTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *ContractCallTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *ContractCallTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Mul(config.Client.BaseGas, big.NewInt(30))
Expand Down
14 changes: 7 additions & 7 deletions transactions/tx_ga.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ func (tx *GAAttachTx) DecodeRLP(s *rlp.Stream) (err error) {
return
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *GAAttachTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *GAAttachTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *GAAttachTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Mul(config.Client.BaseGas, big.NewInt(5))
Expand Down Expand Up @@ -271,17 +271,17 @@ func (tx *GAMetaTx) DecodeRLP(s *rlp.Stream) (err error) {
return
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *GAMetaTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *GAMetaTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
// TODO Recursive tx gas calculation not implemented
func (tx *GAMetaTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
Expand All @@ -296,7 +296,7 @@ func (tx *GAMetaTx) CalcGas() (g *big.Int, err error) {
}

// NewGAMetaTx creates a GAMetaTx
func NewGAMetaTx(accountID string, authData string, abiVersion uint16, gasLimit *big.Int, gasPrice *big.Int, wrappedTx rlp.Encoder, ttler TTLer) (tx *GAMetaTx, err error) {
func NewGAMetaTx(accountID string, authData string, abiVersion uint16, gasLimit *big.Int, gasPrice *big.Int, wrappedTx Transaction, ttler TTLer) (tx *GAMetaTx, err error) {
ttl, err := ttler(config.Client.TTL)
if err != nil {
return
Expand Down
24 changes: 12 additions & 12 deletions transactions/tx_oracles.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ func (tx *OracleRegisterTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *OracleRegisterTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *OracleRegisterTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *OracleRegisterTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down Expand Up @@ -285,17 +285,17 @@ func (tx *OracleExtendTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *OracleExtendTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *OracleExtendTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *OracleExtendTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down Expand Up @@ -456,17 +456,17 @@ func (tx *OracleQueryTx) JSON() (string, error) {
return string(output), err
}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *OracleQueryTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *OracleQueryTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *OracleQueryTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down Expand Up @@ -608,17 +608,17 @@ func (tx *OracleRespondTx) JSON() (string, error) {

}

// SetFee implements TransactionFeeCalculable
// SetFee implements Transaction
func (tx *OracleRespondTx) SetFee(f *big.Int) {
tx.Fee = f
}

// GetFee implements TransactionFeeCalculable
// GetFee implements Transaction
func (tx *OracleRespondTx) GetFee() *big.Int {
return tx.Fee
}

// CalcGas implements TransactionFeeCalculable
// CalcGas implements Transaction
func (tx *OracleRespondTx) CalcGas() (g *big.Int, err error) {
baseGas := new(big.Int)
baseGas.Add(baseGas, config.Client.BaseGas)
Expand Down
Loading

0 comments on commit 4575526

Please sign in to comment.