Skip to content

Commit

Permalink
Mixed transaction to different types of transactions (#102)
Browse files Browse the repository at this point in the history
* Mixed transaction to different types of transactions

* Ut fix

* Fix for e2e test and deleted mixed_tx.go

* legacy test fix

* better code

* lint fix

* lint

* comment fix

* test fix

* test and lint fix

* Simplify 1 ETH constant

* comment fix

* Fix tests

* Lint fix

* Instantiate correct tx data

* Rebase fix

* Fix failing tests

* Comment fix

---------

Co-authored-by: Stefan Negovanović <[email protected]>
Co-authored-by: Goran Rojovic <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2024
1 parent f2b212e commit ca993c5
Show file tree
Hide file tree
Showing 53 changed files with 1,857 additions and 1,334 deletions.
4 changes: 2 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ func (b *Blockchain) ReadTxLookup(hash types.Hash) (types.Hash, bool) {
// return error if the invalid signature found
func (b *Blockchain) recoverFromFieldsInBlock(block *types.Block) error {
for _, tx := range block.Transactions {
if tx.From() != types.ZeroAddress || tx.Type() == types.StateTx {
if tx.From() != types.ZeroAddress || tx.Type() == types.StateTxType {
continue
}

Expand All @@ -1041,7 +1041,7 @@ func (b *Blockchain) recoverFromFieldsInTransactions(transactions []*types.Trans
updated := false

for _, tx := range transactions {
if tx.From() != types.ZeroAddress || tx.Type() == types.StateTx {
if tx.From() != types.ZeroAddress || tx.Type() == types.StateTxType {
continue
}

Expand Down
53 changes: 26 additions & 27 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ func TestBlockchainWriteBody(t *testing.T) {
t.Run("should succeed if tx has from field", func(t *testing.T) {
t.Parallel()

tx := types.NewTx(&types.MixedTxn{
tx := types.NewTx(&types.LegacyTx{
Value: big.NewInt(10),
V: big.NewInt(1),
From: addr,
Expand Down Expand Up @@ -622,7 +622,7 @@ 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.MixedTxn{
tx := types.NewTx(&types.LegacyTx{
Value: big.NewInt(10),
V: big.NewInt(1),
})
Expand Down Expand Up @@ -654,7 +654,7 @@ 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.MixedTxn{
tx := types.NewTx(&types.LegacyTx{
Value: big.NewInt(10),
V: big.NewInt(1),
})
Expand Down Expand Up @@ -715,8 +715,8 @@ func Test_recoverFromFieldsInBlock(t *testing.T) {
},
}

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

computeTxHashes(tx1, tx2)

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

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

computeTxHashes(tx1, tx2, tx3)

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

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

computeTxHashes(tx1, tx2)

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

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

computeTxHashes(tx1, tx2, tx3)

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

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

computeTxHashes(tx1, tx2)

Expand Down Expand Up @@ -900,7 +900,7 @@ func TestBlockchainReadBody(t *testing.T) {

batchWriter := storage.NewBatchWriter(b.db)

tx := types.NewTx(&types.MixedTxn{
tx := types.NewTx(&types.LegacyTx{
Value: big.NewInt(10),
V: big.NewInt(1),
})
Expand Down Expand Up @@ -1602,7 +1602,7 @@ func TestBlockchain_WriteFullBlock(t *testing.T) {
{GasUsed: 100},
{GasUsed: 200},
}
tx := types.NewTx(&types.MixedTxn{
tx := types.NewTx(&types.LegacyTx{
Value: big.NewInt(1),
})

Expand Down Expand Up @@ -1868,7 +1868,15 @@ func customJSONBlockUnmarshall(tb testing.TB, jsonData []byte) (*types.FullBlock

for _, transactionJSON := range transactionsJSON {
tr := transactionJSON.(map[string]interface{})
transaction := types.NewTx(&types.MixedTxn{})

txType := tr["type"].(string)

txTypeNumber, err := common.ParseUint64orHex(&txType)
if err != nil {
return nil, err
}

transaction := types.NewTxWithType(types.TxType(txTypeNumber))
transaction.SetHash(types.StringToHash(tr["hash"].(string)))
nonce := tr["nonce"].(string)

Expand Down Expand Up @@ -1935,15 +1943,6 @@ func customJSONBlockUnmarshall(tb testing.TB, jsonData []byte) (*types.FullBlock

transaction.SetChainID(chainIDNumber)

txType := tr["type"].(string)

txTypeNumber, err := common.ParseUint64orHex(&txType)
if err != nil {
return nil, err
}

transaction.SetTransactionType(types.TxType(txTypeNumber))

gasFeeCapGeneric, ok := tr["maxFeePerGas"]
if ok {
gasFeeCap := gasFeeCapGeneric.(string)
Expand Down
6 changes: 2 additions & 4 deletions blockchain/storage/leveldb/leveldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ 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.MixedTxn{
tx := types.NewTx(&types.DynamicFeeTx{
Gas: types.StateTransactionGasLimit,
Nonce: uint64(startNonce + i),
From: from,
To: to,
Value: big.NewInt(2000),
Type: types.DynamicFeeTx,
GasFeeCap: big.NewInt(100),
GasTipCap: big.NewInt(10),
})
Expand Down Expand Up @@ -112,7 +110,7 @@ func generateBlock(t *testing.T, num uint64) *types.FullBlock {
b.Receipts[i] = &types.Receipt{
TxHash: b.Block.Transactions[i].Hash(),
Root: types.StringToHash("mockhashstring"),
TransactionType: types.LegacyTx,
TransactionType: types.LegacyTxType,
GasUsed: uint64(100000),
Status: &status,
Logs: logs,
Expand Down
6 changes: 3 additions & 3 deletions blockchain/storage/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func testBody(t *testing.T, m PlaceholderStorage) {
require.NoError(t, batch.WriteBatch())

addr1 := types.StringToAddress("11")
t0 := types.NewTx(&types.MixedTxn{
t0 := types.NewTx(&types.LegacyTx{
Nonce: 0,
To: &addr1,
Value: big.NewInt(1),
Expand All @@ -282,7 +282,7 @@ func testBody(t *testing.T, m PlaceholderStorage) {
t0.ComputeHash()

addr2 := types.StringToAddress("22")
t1 := types.NewTx(&types.MixedTxn{
t1 := types.NewTx(&types.LegacyTx{
Nonce: 0,
To: &addr2,
Value: big.NewInt(1),
Expand Down Expand Up @@ -338,7 +338,7 @@ func testReceipts(t *testing.T, m PlaceholderStorage) {

body := &types.Body{
Transactions: []*types.Transaction{
types.NewTx(&types.MixedTxn{
types.NewTx(&types.StateTx{
Nonce: 1000,
Gas: 50,
GasPrice: new(big.Int).SetUint64(100),
Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/block_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestBlockBuilder_BuildBlockTxOneFailedTxAndOneTakesTooMuchGas(t *testing.T)
gas = blockGasLimit - 1
}

tx := types.NewTx(&types.MixedTxn{
tx := types.NewTx(&types.LegacyTx{
Value: big.NewInt(amount),
GasPrice: big.NewInt(gasPrice),
Gas: gas,
Expand Down
5 changes: 2 additions & 3 deletions consensus/polybft/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (f *fsm) VerifyStateTransactions(transactions []*types.Transaction) error {
)

for _, tx := range transactions {
if tx.Type() != types.StateTx {
if tx.Type() != types.StateTxType {
continue
}

Expand Down Expand Up @@ -716,10 +716,9 @@ 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.MixedTxn{
tx := types.NewTx(&types.StateTx{
From: contracts.SystemCaller,
To: &target,
Type: types.StateTx,
Input: inputData,
Gas: types.StateTransactionGasLimit,
GasPrice: big.NewInt(0),
Expand Down
3 changes: 1 addition & 2 deletions consensus/polybft/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,10 @@ 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.MixedTxn{
alteredCommitEpochTx := types.NewTx(&types.StateTx{
To: &contracts.EpochManagerContract,
Input: []byte{},
Gas: 0,
Type: types.StateTx,
})
assert.ErrorContains(t, fsm.verifyCommitEpochTx(alteredCommitEpochTx), "invalid commit epoch transaction")

Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ func (p *Polybft) PreCommitState(block *types.Block, _ *state.Transition) error

// validate commitment state transactions
for _, tx := range block.Transactions {
if tx.Type() != types.StateTx {
if tx.Type() != types.StateTxType {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/state_sync_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func getCommitmentMessageSignedTx(txs []*types.Transaction) (*CommitmentMessageS
var commitFn contractsapi.CommitStateReceiverFn
for _, tx := range txs {
// skip non state CommitmentMessageSigned transactions
if tx.Type() != types.StateTx ||
if tx.Type() != types.StateTxType ||
len(tx.Input()) < abiMethodIDLength ||
!bytes.Equal(tx.Input()[:abiMethodIDLength], commitFn.Sig()) {
continue
Expand Down
6 changes: 3 additions & 3 deletions crypto/txsigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func calcTxHash(tx *types.Transaction, chainID uint64) types.Hash {
var hash []byte

switch tx.Type() {
case types.AccessListTx:
case types.AccessListTxType:
a := signerPool.Get()
v := a.NewArray()

Expand Down Expand Up @@ -141,9 +141,9 @@ func calcTxHash(tx *types.Transaction, chainID uint64) types.Hash {

return types.BytesToHash(hash)

case types.DynamicFeeTx, types.LegacyTx, types.StateTx:
case types.DynamicFeeTxType, types.LegacyTxType, types.StateTxType:
a := signerPool.Get()
isDynamicFeeTx := tx.Type() == types.DynamicFeeTx
isDynamicFeeTx := tx.Type() == types.DynamicFeeTxType

v := a.NewArray()

Expand Down
4 changes: 2 additions & 2 deletions crypto/txsigner_eip155_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestEIP155Signer_Sender(t *testing.T) {
t.Fatalf("Unable to generate key")
}

txn := types.NewTx(&types.MixedTxn{
txn := types.NewTx(&types.LegacyTx{
To: &toAddress,
Value: big.NewInt(1),
GasPrice: big.NewInt(0),
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestEIP155Signer_ChainIDMismatch(t *testing.T) {
t.Fatalf("Unable to generate key")
}

txn := types.NewTx(&types.MixedTxn{
txn := types.NewTx(&types.LegacyTx{
To: &toAddress,
Value: big.NewInt(1),
GasPrice: big.NewInt(0),
Expand Down
2 changes: 1 addition & 1 deletion crypto/txsigner_frontier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestFrontierSigner(t *testing.T) {
key, err := GenerateECDSAKey()
assert.NoError(t, err)

txn := types.NewTx(&types.MixedTxn{
txn := types.NewTx(&types.LegacyTx{
To: &toAddress,
Value: big.NewInt(10),
GasPrice: big.NewInt(0),
Expand Down
4 changes: 2 additions & 2 deletions crypto/txsigner_london_berlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (e *LondonOrBerlinSigner) Hash(tx *types.Transaction) types.Hash {

// Sender returns the transaction sender
func (e *LondonOrBerlinSigner) Sender(tx *types.Transaction) (types.Address, error) {
if tx.Type() != types.DynamicFeeTx && tx.Type() != types.AccessListTx {
if tx.Type() != types.DynamicFeeTxType && tx.Type() != types.AccessListTxType {
return e.fallbackSigner.Sender(tx)
}

Expand All @@ -45,7 +45,7 @@ func (e *LondonOrBerlinSigner) Sender(tx *types.Transaction) (types.Address, err

// SignTx signs the transaction using the passed in private key
func (e *LondonOrBerlinSigner) SignTx(tx *types.Transaction, pk *ecdsa.PrivateKey) (*types.Transaction, error) {
if tx.Type() != types.DynamicFeeTx && tx.Type() != types.AccessListTx {
if tx.Type() != types.DynamicFeeTxType && tx.Type() != types.AccessListTxType {
return e.fallbackSigner.SignTx(tx, pk)
}

Expand Down
Loading

0 comments on commit ca993c5

Please sign in to comment.