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

Mixed transaction to different types of transactions #102

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
better code
  • Loading branch information
dusannosovic-ethernal authored and goran-ethernal committed Feb 22, 2024
commit 7a5b364936bb548fad1f255e2dcf19bb0debf742
10 changes: 5 additions & 5 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.StateTx{
tx := types.NewTx(&types.LegacyTx{
Value: big.NewInt(10),
V: big.NewInt(1),
From: addr,
Expand Down Expand Up @@ -864,8 +864,8 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) {
},
}

tx1 := types.NewTx(&types.StateTx{Nonce: 0, From: addr1})
tx2 := types.NewTx(&types.StateTx{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 @@ -1602,7 +1602,7 @@ func TestBlockchain_WriteFullBlock(t *testing.T) {
{GasUsed: 100},
{GasUsed: 200},
}
tx := types.NewTx(&types.StateTx{
tx := types.NewTx(&types.LegacyTx{
Value: big.NewInt(1),
})

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

transaction := types.NewTx(&types.StateTx{})
transaction := types.NewTx(txData)
transaction.SetHash(types.StringToHash(tr["hash"].(string)))
nonce := tr["nonce"].(string)

Expand Down
4 changes: 2 additions & 2 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ func TestAddTx(t *testing.T) {
gasPrice,
slots uint64,
) *types.Transaction {
tx := newTx(addr, nonce, slots, types.StateTxType)
tx := newTx(addr, nonce, slots, types.LegacyTxType)
tx.GasPrice().SetUint64(gasPrice)

return tx
Expand Down Expand Up @@ -1385,7 +1385,7 @@ func TestPromoteHandler(t *testing.T) {
txs := make([]*types.Transaction, 20)

for i := 0; i < 20; i++ {
txs[i] = newTx(addr1, uint64(i), 1, types.StateTxType)
txs[i] = newTx(addr1, uint64(i), 1, types.LegacyTxType)
}

for _, tx := range txs {
Expand Down
83 changes: 48 additions & 35 deletions types/rlp_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,45 +149,67 @@ func TestRLPUnmarshal_Header_ComputeHash(t *testing.T) {
assert.Equal(t, h.Hash, h2.Hash)
}

/* func TestRLPMarshall_And_Unmarshall_TypedTransaction(t *testing.T) {
func TestRLPMarshall_And_Unmarshall_TypedTransaction(t *testing.T) {
addrTo := StringToAddress("11")
addrFrom := StringToAddress("22")
originalTx := NewTx(&MixedTxn{
Nonce: 0,
GasPrice: big.NewInt(11),
GasFeeCap: big.NewInt(12),
GasTipCap: big.NewInt(13),
Gas: 11,
To: &addrTo,
From: addrFrom,
Value: big.NewInt(1),
Input: []byte{1, 2},
V: big.NewInt(25),
S: big.NewInt(26),
R: big.NewInt(27),
})

txTypes := []TxType{
StateTxType,
LegacyTxType,
DynamicFeeTxType,
originalTxs := []*Transaction{
NewTx(&StateTx{
Nonce: 0,
GasPrice: big.NewInt(11),
Gas: 11,
To: &addrTo,
From: addrFrom,
Value: big.NewInt(1),
Input: []byte{1, 2},
V: big.NewInt(25),
S: big.NewInt(26),
R: big.NewInt(27),
}),
NewTx(&LegacyTx{
Nonce: 0,
GasPrice: big.NewInt(11),
Gas: 11,
To: &addrTo,
From: addrFrom,
Value: big.NewInt(1),
Input: []byte{1, 2},
V: big.NewInt(25),
S: big.NewInt(26),
R: big.NewInt(27),
}),
NewTx(&DynamicFeeTx{
Nonce: 0,
GasFeeCap: big.NewInt(12),
GasTipCap: big.NewInt(13),
Gas: 11,
To: &addrTo,
From: addrFrom,
Value: big.NewInt(1),
Input: []byte{1, 2},
V: big.NewInt(25),
S: big.NewInt(26),
R: big.NewInt(27),
}),
}

for _, v := range txTypes {
t.Run(v.String(), func(t *testing.T) {
for _, originalTx := range originalTxs {
t.Run(originalTx.Type().String(), func(t *testing.T) {
originalTx.ComputeHash()

unmarshalledTx := &Transaction{}
unmarshalledTx.InitInnerData(originalTx.Type())

txRLP := originalTx.MarshalRLP()

unmarshalledTx := NewTx(&MixedTxn{})
assert.NoError(t, unmarshalledTx.UnmarshalRLP(txRLP))

unmarshalledTx.ComputeHash()
assert.Equal(t, originalTx.Type(), unmarshalledTx.Type())
assert.Equal(t, originalTx.Hash(), unmarshalledTx.Hash())
})
}
} */
}

func TestRLPMarshall_Unmarshall_Missing_Data(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -254,18 +276,9 @@ func TestRLPMarshall_Unmarshall_Missing_Data(t *testing.T) {
v, err := parser.Parse(testData)
assert.Nil(t, err)

var unmarshalledTx *Transaction

switch txType {
case AccessListTxType:
unmarshalledTx = NewTx(&AccessListTxn{})
case StateTxType:
unmarshalledTx = NewTx(&StateTx{})
case LegacyTxType:
unmarshalledTx = NewTx(&LegacyTx{})
case DynamicFeeTxType:
unmarshalledTx = NewTx(&DynamicFeeTx{})
}
unmarshalledTx := &Transaction{}
dusannosovic-ethernal marked this conversation as resolved.
Show resolved Hide resolved

unmarshalledTx.InitInnerData(txType)

if tt.expectedErr {
assert.Error(t, unmarshalledTx.Inner.unmarshalRLPFrom(parser, v), tt.name)
Expand Down
14 changes: 3 additions & 11 deletions types/rlp_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,9 @@ func (b *Block) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {

// transactions
if err = unmarshalRLPFrom(p, elems[1], func(txType TxType, p *fastrlp.Parser, v *fastrlp.Value) error {
var bTxn *Transaction
switch txType {
case AccessListTxType:
bTxn = NewTx(&AccessListTxn{})
case LegacyTxType:
bTxn = NewTx(&LegacyTx{})
case StateTxType:
bTxn = NewTx(&StateTx{})
case DynamicFeeTxType:
bTxn = NewTx(&DynamicFeeTx{})
}
bTxn := &Transaction{}
dusannosovic-ethernal marked this conversation as resolved.
Show resolved Hide resolved

bTxn.InitInnerData(txType)

if err = bTxn.Inner.unmarshalRLPFrom(p, v); err != nil {
return err
Expand Down
35 changes: 4 additions & 31 deletions types/rlp_unmarshal_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,8 @@ func (b *Body) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {

// transactions
if err = unmarshalRLPFrom(p, tuple[0], func(txType TxType, p *fastrlp.Parser, v *fastrlp.Value) error {
var bTxn *Transaction
switch txType {
case AccessListTxType:
bTxn = NewTx(&AccessListTxn{})
case LegacyTxType:
bTxn = NewTx(&LegacyTx{})
case StateTxType:
bTxn = NewTx(&StateTx{})
case DynamicFeeTxType:
bTxn = NewTx(&DynamicFeeTx{})
}
bTxn := &Transaction{}
dusannosovic-ethernal marked this conversation as resolved.
Show resolved Hide resolved
bTxn.InitInnerData(txType)

if err = bTxn.unmarshalStoreRLPFrom(p, v); err != nil {
return err
Expand Down Expand Up @@ -80,16 +71,7 @@ func (t *Transaction) UnmarshalStoreRLP(input []byte) error {
return err
}

switch tType {
case LegacyTxType:
t.SetTxData(&LegacyTx{})
case StateTxType:
t.SetTxData(&StateTx{})
case AccessListTxType:
t.SetTxData(&AccessListTxn{})
default:
t.SetTxData(&DynamicFeeTx{})
}
t.InitInnerData(tType)

offset = 1
}
Expand All @@ -116,16 +98,7 @@ func (t *Transaction) unmarshalStoreRLPFrom(p *fastrlp.Parser, v *fastrlp.Value)

elems = elems[1:]

switch tType {
case LegacyTxType:
t.SetTxData(&LegacyTx{})
case StateTxType:
t.SetTxData(&StateTx{})
case AccessListTxType:
t.SetTxData(&AccessListTxn{})
default:
t.SetTxData(&DynamicFeeTx{})
}
t.InitInnerData(tType)
}

// consensus part
Expand Down