Skip to content

Commit

Permalink
refactor(tx): accept variable args for IntrinsicGas; add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Jun 22, 2018
1 parent 65544d3 commit 83579bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tx/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (t *Transaction) IntrinsicGas() (uint64, error) {
return cached.(uint64), nil
}

gas, err := IntrinsicGas(t.body.Clauses)
gas, err := IntrinsicGas(t.body.Clauses...)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -369,7 +369,7 @@ func (t *Transaction) String() string {
}

// IntrinsicGas calculate intrinsic gas cost for tx with such clauses.
func IntrinsicGas(clauses []*Clause) (uint64, error) {
func IntrinsicGas(clauses ...*Clause) (uint64, error) {
if len(clauses) == 0 {
return thor.TxGas + thor.ClauseGas, nil
}
Expand Down
18 changes: 18 additions & 0 deletions tx/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ func TestTx(t *testing.T) {
)
}

func TestIntrinsicGas(t *testing.T) {
gas, err := tx.IntrinsicGas()
assert.Nil(t, err)
assert.Equal(t, thor.TxGas+thor.ClauseGas, gas)

gas, err = tx.IntrinsicGas(tx.NewClause(&thor.Address{}))
assert.Nil(t, err)
assert.Equal(t, thor.TxGas+thor.ClauseGas, gas)

gas, err = tx.IntrinsicGas(tx.NewClause(nil))
assert.Nil(t, err)
assert.Equal(t, thor.TxGas+thor.ClauseGasContractCreation, gas)

gas, err = tx.IntrinsicGas(tx.NewClause(&thor.Address{}), tx.NewClause(&thor.Address{}))
assert.Nil(t, err)
assert.Equal(t, thor.TxGas+thor.ClauseGas*2, gas)
}

func BenchmarkTxMining(b *testing.B) {
tx := new(tx.Builder).Build()
signer := thor.BytesToAddress([]byte("acc1"))
Expand Down

0 comments on commit 83579bd

Please sign in to comment.