Skip to content

Commit

Permalink
Enhance Tx Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Sep 16, 2024
1 parent 5bc66a8 commit 7473cba
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 125 deletions.
11 changes: 4 additions & 7 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"io"
"math/big"
"net/http"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
ABI "github.com/vechain/thor/v2/abi"
Expand Down Expand Up @@ -293,12 +293,9 @@ func buildTxWithClauses(t *testing.T, chaiTag byte, clauses ...*tx.Clause) *tx.T
builder.Clause(c)
}

transaction := builder.Build()
sig, err := crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
return transaction.WithSignature(sig)
transaction, err := builder.BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
return transaction
}

func packTx(repo *chain.Repository, stater *state.Stater, transaction *tx.Transaction, t *testing.T) {
Expand Down
11 changes: 3 additions & 8 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -171,21 +170,17 @@ func initBlockServer(t *testing.T) {
repo, _ := chain.NewRepository(db, b)
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tx := new(tx.Builder).
tx, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

sig, err := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
tx = tx.WithSignature(sig)
packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
flow, err := packer.Schedule(sum, uint64(time.Now().Unix()))
Expand Down
22 changes: 7 additions & 15 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"io"
"math/big"
"net/http"
Expand All @@ -20,7 +21,6 @@ import (

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/block"
Expand Down Expand Up @@ -527,20 +527,16 @@ func initDebugServer(t *testing.T) {

// Adding an empty clause transaction to the block to cover the case of
// scanning multiple txs before getting the right one
noClausesTx := new(tx.Builder).
noClausesTx, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
Expiration(10).
Gas(21000).
Build()
sig, err := crypto.Sign(noClausesTx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
noClausesTx = noClausesTx.WithSignature(sig)
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000))
transaction = new(tx.Builder).
transaction, err = new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -549,13 +545,9 @@ func initDebugServer(t *testing.T) {
Clause(cla).
Clause(cla2).
BlockRef(tx.NewBlockRef(0)).
Build()
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

sig, err = crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
transaction = transaction.WithSignature(sig)
packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
flow, err := packer.Schedule(sum, uint64(time.Now().Unix()))
Expand Down
12 changes: 4 additions & 8 deletions api/subscriptions/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/genesis"
Expand Down Expand Up @@ -77,21 +77,17 @@ func initChain(t *testing.T) (*chain.Repository, []*block.Block, *txpool.TxPool)

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tr := new(tx.Builder).
tr, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

sig, err := crypto.Sign(tr.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
tr = tr.WithSignature(sig)
packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
flow, err := packer.Schedule(sum, uint64(time.Now().Unix()))
Expand Down
12 changes: 4 additions & 8 deletions api/subscriptions/pending_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/genesis"
Expand Down Expand Up @@ -132,20 +132,16 @@ func addNewBlock(repo *chain.Repository, stater *state.Stater, b0 *block.Block,
func createTx(t *testing.T, repo *chain.Repository, addressNumber uint) *tx.Transaction {
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tx := new(tx.Builder).
tx, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
sig, err := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[addressNumber].PrivateKey)
if err != nil {
t.Fatal(err)
}
tx = tx.WithSignature(sig)
BuildAndSign(genesis.DevAccounts()[addressNumber].PrivateKey)
require.NoError(t, err)

return tx
}
23 changes: 7 additions & 16 deletions api/subscriptions/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package subscriptions

import (
"bytes"
"github.com/stretchr/testify/require"
"math/big"
"testing"

Expand Down Expand Up @@ -95,20 +96,15 @@ func TestConvertTransfer(t *testing.T) {
repo, _ := chain.NewRepository(db, b)

// New tx
transaction := new(tx.Builder).
transaction, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build()

sig, err := crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
transaction = transaction.WithSignature(sig)
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

// New block
blk := new(block.Builder).
Expand Down Expand Up @@ -187,20 +183,15 @@ func TestConvertEvent(t *testing.T) {
repo, _ := chain.NewRepository(db, b)

// New tx
transaction := new(tx.Builder).
transaction, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build()

sig, err := crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
transaction = transaction.WithSignature(sig)
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

// New block
blk := new(block.Builder).
Expand Down
36 changes: 11 additions & 25 deletions api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"io"
"math/big"
"net/http"
Expand All @@ -18,7 +19,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -112,17 +112,14 @@ func sendTx(t *testing.T) {
var expiration = uint32(10)
var gas = uint64(21000)

tx := new(tx.Builder).
tx, err := new(tx.Builder).
BlockRef(blockRef).
ChainTag(chainTag).
Expiration(expiration).
Gas(gas).
Build()
sig, err := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
tx = tx.WithSignature(sig)
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

rlpTx, err := rlp.EncodeToBytes(tx)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -287,35 +284,24 @@ func initTransactionServer(t *testing.T) {
repo, _ = chain.NewRepository(db, b)
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
transaction = new(tx.Builder).
transaction, err = new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

mempoolTx = new(tx.Builder).
mempoolTx, err = new(tx.Builder).
ChainTag(repo.ChainTag()).
Expiration(10).
Gas(21000).
Nonce(1).
Build()

sig, err := crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}

sig2, err := crypto.Sign(mempoolTx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}

transaction = transaction.WithSignature(sig)
mempoolTx = mempoolTx.WithSignature(sig2)
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
Expand Down
6 changes: 2 additions & 4 deletions cmd/thor/node/tx_stash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sort"
"testing"

"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
Expand All @@ -20,9 +19,8 @@ import (
)

func newTx() *tx.Transaction {
tx := new(tx.Builder).Nonce(rand.Uint64()).Build() // nolint:gosec
sig, _ := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
return tx.WithSignature(sig)
tx, _ := new(tx.Builder).Nonce(rand.Uint64()).BuildAndSign(genesis.DevAccounts()[0].PrivateKey) // nolint:gosec
return tx
}

func TestTxStash(t *testing.T) {
Expand Down
9 changes: 2 additions & 7 deletions cmd/thor/solo/solo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/builtin"
Expand Down Expand Up @@ -261,14 +260,10 @@ func (s *Solo) newTx(clauses []*tx.Clause, from genesis.DevAccount) (*tx.Transac
builder.Clause(c)
}

newTx := builder.BlockRef(tx.NewBlockRef(0)).
return builder.BlockRef(tx.NewBlockRef(0)).
Expiration(math.MaxUint32).
Nonce(rand.Uint64()). // nolint:gosec
DependsOn(nil).
Gas(1_000_000).
Build()

sig, err := crypto.Sign(newTx.SigningHash().Bytes(), from.PrivateKey)

return newTx.WithSignature(sig), err
BuildAndSign(from.PrivateKey)
}
6 changes: 3 additions & 3 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/ecdsa"
"crypto/rand"
"fmt"
"github.com/stretchr/testify/require"
"math/big"
"testing"

Expand Down Expand Up @@ -577,9 +578,8 @@ func TestValidateBlockBody(t *testing.T) {
{
"TxOriginBlocked", func(t *testing.T) {
thor.MockBlocklist([]string{genesis.DevAccounts()[9].Address.String()})
tx := txBuilder(tc.tag).Build()
sig, _ := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[9].PrivateKey)
tx = tx.WithSignature(sig)
tx, err := txBuilder(tc.tag).BuildAndSign(genesis.DevAccounts()[9].PrivateKey)
require.NoError(t, err)

blk, err := tc.sign(
tc.builder(tc.original.Header()).Transaction(tx),
Expand Down
Loading

0 comments on commit 7473cba

Please sign in to comment.