Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added constructor for all types of transactions #153

Merged
merged 6 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 25 additions & 42 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,8 @@ func TestBlockchainWriteBody(t *testing.T) {
t.Run("should succeed if tx has from field", func(t *testing.T) {
t.Parallel()

tx := types.NewTx(&types.LegacyTx{
BaseTx: &types.BaseTx{
Value: big.NewInt(10),
V: big.NewInt(1),
From: addr,
},
})
tx := types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(10)),
types.WithSignatureValues(big.NewInt(1), nil, nil), types.WithFrom(addr)))

block := &types.Block{
Header: &types.Header{},
Expand Down Expand Up @@ -624,12 +619,8 @@ func TestBlockchainWriteBody(t *testing.T) {
t.Run("should return error if tx doesn't have from and recovering address fails", func(t *testing.T) {
t.Parallel()

tx := types.NewTx(&types.LegacyTx{
BaseTx: &types.BaseTx{
Value: big.NewInt(10),
V: big.NewInt(1),
},
})
tx := types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(10)),
types.WithSignatureValues(big.NewInt(1), nil, nil)))

block := &types.Block{
Header: &types.Header{},
Expand Down Expand Up @@ -658,12 +649,8 @@ func TestBlockchainWriteBody(t *testing.T) {
t.Run("should recover from address and store to storage", func(t *testing.T) {
t.Parallel()

tx := types.NewTx(&types.LegacyTx{
BaseTx: &types.BaseTx{
Value: big.NewInt(10),
V: big.NewInt(1),
},
})
tx := types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(10)),
types.WithSignatureValues(big.NewInt(1), nil, nil)))

block := &types.Block{
Header: &types.Header{},
Expand Down Expand Up @@ -721,8 +708,8 @@ func Test_recoverFromFieldsInBlock(t *testing.T) {
},
}

tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: addr1}})
tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: types.ZeroAddress}})
tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(addr1)))
tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(types.ZeroAddress)))

computeTxHashes(tx1, tx2)

Expand Down Expand Up @@ -751,9 +738,9 @@ func Test_recoverFromFieldsInBlock(t *testing.T) {
},
}

tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: types.ZeroAddress}})
tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: types.ZeroAddress}})
tx3 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 2, From: types.ZeroAddress}})
tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(types.ZeroAddress)))
tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(types.ZeroAddress)))
tx3 := types.NewTx(types.NewLegacyTx(types.WithNonce(2), types.WithFrom(types.ZeroAddress)))

computeTxHashes(tx1, tx2, tx3)

Expand Down Expand Up @@ -807,8 +794,8 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) {
},
}

tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: addr1}})
tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: types.ZeroAddress}})
tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(addr1)))
tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(types.ZeroAddress)))

computeTxHashes(tx1, tx2)

Expand Down Expand Up @@ -836,9 +823,9 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) {
},
}

tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: types.ZeroAddress}})
tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: types.ZeroAddress}})
tx3 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 2, From: types.ZeroAddress}})
tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(types.ZeroAddress)))
tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(types.ZeroAddress)))
tx3 := types.NewTx(types.NewLegacyTx(types.WithNonce(2), types.WithFrom(types.ZeroAddress)))

computeTxHashes(tx1, tx2, tx3)

Expand Down Expand Up @@ -870,8 +857,8 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) {
},
}

tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: addr1}})
tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: addr2}})
tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(addr1)))
tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(addr2)))

computeTxHashes(tx1, tx2)

Expand Down Expand Up @@ -906,12 +893,10 @@ func TestBlockchainReadBody(t *testing.T) {

batchWriter := storage.NewBatchWriter(b.db)

tx := types.NewTx(&types.LegacyTx{
BaseTx: &types.BaseTx{
Value: big.NewInt(10),
V: big.NewInt(1),
},
})
tx := types.NewTx(types.NewLegacyTx(
types.WithValue(big.NewInt(10)),
types.WithSignatureValues(big.NewInt(1), nil, nil),
))

tx.ComputeHash()

Expand Down Expand Up @@ -1610,11 +1595,9 @@ func TestBlockchain_WriteFullBlock(t *testing.T) {
{GasUsed: 100},
{GasUsed: 200},
}
tx := types.NewTx(&types.LegacyTx{
BaseTx: &types.BaseTx{
Value: big.NewInt(1),
},
})
tx := types.NewTx(types.NewLegacyTx(
types.WithValue(big.NewInt(1)),
))

tx.ComputeHash()
header.ComputeHash()
Expand Down
18 changes: 8 additions & 10 deletions blockchain/storage/leveldb/leveldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ func generateTxs(t *testing.T, startNonce, count int, from types.Address, to *ty
txs := make([]*types.Transaction, count)

for i := range txs {
tx := types.NewTx(&types.DynamicFeeTx{
GasFeeCap: big.NewInt(100),
GasTipCap: big.NewInt(10),
BaseTx: &types.BaseTx{
Gas: types.StateTransactionGasLimit,
Nonce: uint64(startNonce + i),
To: to,
Value: big.NewInt(2000),
},
})
tx := types.NewTx(types.NewDynamicFeeTx(
types.WithGasFeeCap(big.NewInt(100)),
types.WithGasTipCap(big.NewInt(10)),
types.WithGas(types.StateTransactionGasLimit),
types.WithNonce(uint64(startNonce+1)),
types.WithTo(to),
types.WithValue(big.NewInt(2000)),
))

input := make([]byte, 1000)
_, err := rand.Read(input)
Expand Down
54 changes: 24 additions & 30 deletions blockchain/storage/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,31 +270,27 @@ func testBody(t *testing.T, m PlaceholderStorage) {
require.NoError(t, batch.WriteBatch())

addr1 := types.StringToAddress("11")
t0 := types.NewTx(&types.LegacyTx{
GasPrice: big.NewInt(11),
BaseTx: &types.BaseTx{
Nonce: 0,
To: &addr1,
Value: big.NewInt(1),
Gas: 11,
Input: []byte{1, 2},
V: big.NewInt(1),
},
})
t0 := types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(11)),
types.WithNonce(0),
types.WithTo(&addr1),
types.WithValue(big.NewInt(1)),
types.WithGas(11),
types.WithInput([]byte{1, 2}),
types.WithSignatureValues(big.NewInt(1), nil, nil),
))
t0.ComputeHash()

addr2 := types.StringToAddress("22")
t1 := types.NewTx(&types.LegacyTx{
GasPrice: big.NewInt(11),
BaseTx: &types.BaseTx{
Nonce: 0,
To: &addr2,
Value: big.NewInt(1),
Gas: 22,
Input: []byte{4, 5},
V: big.NewInt(2),
},
})
t1 := types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(11)),
types.WithNonce(0),
types.WithTo(&addr2),
types.WithValue(big.NewInt(1)),
types.WithGas(22),
types.WithInput([]byte{4, 5}),
types.WithSignatureValues(big.NewInt(2), nil, nil),
))
t1.ComputeHash()

block := types.Block{
Expand Down Expand Up @@ -342,14 +338,12 @@ func testReceipts(t *testing.T, m PlaceholderStorage) {

body := &types.Body{
Transactions: []*types.Transaction{
types.NewTx(&types.StateTx{
GasPrice: new(big.Int).SetUint64(100),
BaseTx: &types.BaseTx{
Nonce: 1000,
Gas: 50,
V: big.NewInt(11),
},
}),
types.NewTx(types.NewStateTx(
types.WithGasPrice(new(big.Int).SetUint64(100)),
types.WithNonce(1000),
types.WithGas(50),
types.WithSignatureValues(big.NewInt(11), nil, nil),
)),
},
}
receipts := []*types.Receipt{
Expand Down
16 changes: 7 additions & 9 deletions consensus/polybft/block_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,13 @@ func TestBlockBuilder_BuildBlockTxOneFailedTxAndOneTakesTooMuchGas(t *testing.T)
gas = blockGasLimit - 1
}

tx := types.NewTx(&types.LegacyTx{
GasPrice: big.NewInt(gasPrice),
BaseTx: &types.BaseTx{
Value: big.NewInt(amount),
Gas: gas,
Nonce: 0,
To: &acc.address,
},
})
tx := types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(gasPrice)),
types.WithValue(big.NewInt(amount)),
types.WithGas(gas),
types.WithNonce(0),
types.WithTo(&acc.address),
))

tx, err = signer.SignTx(tx, acc.privKey)
require.NoError(t, err)
Expand Down
16 changes: 7 additions & 9 deletions consensus/polybft/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,15 +716,13 @@ func validateHeaderFields(parent *types.Header, header *types.Header, blockTimeD
// createStateTransactionWithData creates a state transaction
// with provided target address and inputData parameter which is ABI encoded byte array.
func createStateTransactionWithData(target types.Address, inputData []byte) *types.Transaction {
tx := types.NewTx(&types.StateTx{
GasPrice: big.NewInt(0),
BaseTx: &types.BaseTx{
From: contracts.SystemCaller,
To: &target,
Input: inputData,
Gas: types.StateTransactionGasLimit,
},
})
tx := types.NewTx(types.NewStateTx(
types.WithGasPrice(big.NewInt(0)),
types.WithFrom(contracts.SystemCaller),
types.WithTo(&target),
types.WithInput(inputData),
types.WithGas(types.StateTransactionGasLimit),
))

return tx.ComputeHash()
}
13 changes: 6 additions & 7 deletions consensus/polybft/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ func TestFSM_verifyCommitEpochTx(t *testing.T) {
assert.NoError(t, fsm.verifyCommitEpochTx(commitEpochTx))

// submit tampered commit epoch transaction to the epoch ending block
alteredCommitEpochTx := types.NewTx(&types.StateTx{
BaseTx: &types.BaseTx{
To: &contracts.EpochManagerContract,
Input: []byte{},
Gas: 0,
},
})
alteredCommitEpochTx := types.NewTx(types.NewStateTx(
types.WithTo(&contracts.EpochManagerContract),
types.WithInput([]byte{}),
types.WithGas(0),
))

assert.ErrorContains(t, fsm.verifyCommitEpochTx(alteredCommitEpochTx), "invalid commit epoch transaction")

// submit validators commit epoch transaction to the non-epoch ending block
Expand Down
24 changes: 10 additions & 14 deletions crypto/txsigner_eip155_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ func TestEIP155Signer_Sender(t *testing.T) {
t.Fatalf("Unable to generate key")
}

txn := types.NewTx(&types.LegacyTx{
GasPrice: big.NewInt(0),
BaseTx: &types.BaseTx{
To: &toAddress,
Value: big.NewInt(1),
},
})
txn := types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(0)),
types.WithTo(&toAddress),
types.WithValue(big.NewInt(1)),
))

signer := NewEIP155Signer(
testCase.chainID.Uint64(),
Expand Down Expand Up @@ -107,13 +105,11 @@ func TestEIP155Signer_ChainIDMismatch(t *testing.T) {
t.Fatalf("Unable to generate key")
}

txn := types.NewTx(&types.LegacyTx{
GasPrice: big.NewInt(0),
BaseTx: &types.BaseTx{
To: &toAddress,
Value: big.NewInt(1),
},
})
txn := types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(0)),
types.WithTo(&toAddress),
types.WithValue(big.NewInt(1)),
))

signer := NewEIP155Signer(chainIDTop)

Expand Down
13 changes: 6 additions & 7 deletions crypto/txsigner_frontier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ func TestFrontierSigner(t *testing.T) {
key, err := GenerateECDSAKey()
assert.NoError(t, err)

txn := types.NewTx(&types.LegacyTx{
GasPrice: big.NewInt(0),
BaseTx: &types.BaseTx{
To: &toAddress,
Value: big.NewInt(10),
},
})
txn := types.NewTx(types.NewLegacyTx(
types.WithGasPrice(big.NewInt(0)),
types.WithTo(&toAddress),
types.WithValue(big.NewInt(10)),
))

signedTx, err := signer.SignTx(txn, key)
assert.NoError(t, err)

Expand Down
Loading
Loading