Skip to content

Commit

Permalink
test: add more dynFee txs to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paologalligit committed Jan 3, 2025
1 parent 59a8925 commit 2fb8578
Show file tree
Hide file tree
Showing 13 changed files with 436 additions and 209 deletions.
4 changes: 2 additions & 2 deletions api/subscriptions/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ func initChain(t *testing.T) *testchain.Chain {
Build()
tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey)

txDeploy := new(tx.LegacyBuilder).
txDeploy := new(tx.DynFeeBuilder).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
MaxFeePerGas(big.NewInt(1)).
Expiration(100).
Gas(1_000_000).
Nonce(3).
Expand Down
62 changes: 40 additions & 22 deletions api/subscriptions/pending_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestPendingTx_DispatchLoop(t *testing.T) {
p.Subscribe(txCh)

// Add a new tx to the mempool
transaction := createTx(repo, 0)
transaction := createLegacyTx(repo, 0)
txPool.AddLocal(transaction)

// Start the dispatch loop
Expand All @@ -113,7 +113,7 @@ func TestPendingTx_DispatchLoop(t *testing.T) {
p.Unsubscribe(txCh)

// Add another tx to the mempool
tx2 := createTx(repo, 1)
tx2 := createDynFeeTx(repo, 1)
txPool.AddLocal(tx2)

// Assert that the channel did not receive the second transaction
Expand Down Expand Up @@ -147,24 +147,6 @@ func addNewBlock(repo *chain.Repository, stater *state.Stater, b0 *block.Block,
}
}

func createTx(repo *chain.Repository, addressNumber uint) *tx.Transaction {
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))

return tx.MustSign(
new(tx.LegacyBuilder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(1000).
Gas(21000).
Nonce(uint64(datagen.RandInt())).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[addressNumber].PrivateKey,
)
}

func TestPendingTx_NoWriteAfterUnsubscribe(t *testing.T) {
// Arrange
thorChain := initChain(t)
Expand All @@ -183,7 +165,7 @@ func TestPendingTx_NoWriteAfterUnsubscribe(t *testing.T) {

done := make(chan struct{})
// Attempt to write a new transaction
trx := createTx(thorChain.Repo(), 0)
trx := createLegacyTx(thorChain.Repo(), 0)
assert.NotPanics(t, func() {
p.dispatch(trx, done) // dispatch should not panic after unsubscribe
}, "Dispatching after unsubscribe should not panic")
Expand Down Expand Up @@ -221,7 +203,7 @@ func TestPendingTx_UnsubscribeOnWebSocketClose(t *testing.T) {
defer ws.Close()

// Add a transaction
trx := createTx(thorChain.Repo(), 0)
trx := createLegacyTx(thorChain.Repo(), 0)
txPool.AddLocal(trx)

// Wait to receive transaction
Expand All @@ -242,3 +224,39 @@ func TestPendingTx_UnsubscribeOnWebSocketClose(t *testing.T) {
require.Equal(t, len(sub.pendingTx.listeners), 0)
sub.pendingTx.mu.Unlock()
}

func createLegacyTx(repo *chain.Repository, addressNumber uint) *tx.Transaction {
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))

return tx.MustSign(
new(tx.LegacyBuilder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(1000).
Gas(21000).
Nonce(uint64(datagen.RandInt())).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[addressNumber].PrivateKey,
)
}

func createDynFeeTx(repo *chain.Repository, addressNumber uint) *tx.Transaction {
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))

return tx.MustSign(
new(tx.DynFeeBuilder).
ChainTag(repo.ChainTag()).
MaxFeePerGas(big.NewInt(1)).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[addressNumber].PrivateKey,
)
}
4 changes: 2 additions & 2 deletions api/subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ func initSubscriptionsServer(t *testing.T, enabledDeprecated bool) {

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tr := new(tx.LegacyBuilder).
tr := new(tx.DynFeeBuilder).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
MaxFeePerGas(big.NewInt(1)).
Expiration(10).
Gas(21000).
Nonce(1).
Expand Down
70 changes: 51 additions & 19 deletions api/subscriptions/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,30 @@ func TestConvertTransfer(t *testing.T) {
repo, _ := chain.NewRepository(db, b)

// New tx
transaction := new(tx.LegacyBuilder).
legacyTx := new(tx.LegacyBuilder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build()
transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey)
legacyTx = tx.MustSign(legacyTx, genesis.DevAccounts()[0].PrivateKey)

dynFeeTx := new(tx.DynFeeBuilder).
ChainTag(repo.ChainTag()).
MaxFeePerGas(big.NewInt(1)).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build()
dynFeeTx = tx.MustSign(dynFeeTx, genesis.DevAccounts()[0].PrivateKey)

// New block
blk := new(block.Builder).
Transaction(transaction).
Transaction(legacyTx).
Transaction(dynFeeTx).
Build()

transfer := &tx.Transfer{
Expand All @@ -117,25 +128,46 @@ func TestConvertTransfer(t *testing.T) {
}

// Act
transferMessage, err := convertTransfer(blk.Header(), transaction, 0, transfer, false)
transferLegacyMessage, errL := convertTransfer(blk.Header(), legacyTx, 0, transfer, false)
transferDynFeeMessage, errD := convertTransfer(blk.Header(), dynFeeTx, 1, transfer, false)

// Assert
assert.NoError(t, err)
assert.Equal(t, transfer.Sender, transferMessage.Sender)
assert.Equal(t, transfer.Recipient, transferMessage.Recipient)
assert.NoError(t, errL)
assert.NoError(t, errD)

assert.Equal(t, transfer.Sender, transferLegacyMessage.Sender)
assert.Equal(t, transfer.Sender, transferDynFeeMessage.Sender)

assert.Equal(t, transfer.Recipient, transferLegacyMessage.Recipient)
assert.Equal(t, transfer.Recipient, transferDynFeeMessage.Recipient)

amount := (*math.HexOrDecimal256)(transfer.Amount)
assert.Equal(t, amount, transferMessage.Amount)
assert.Equal(t, blk.Header().ID(), transferMessage.Meta.BlockID)
assert.Equal(t, blk.Header().Number(), transferMessage.Meta.BlockNumber)
assert.Equal(t, blk.Header().Timestamp(), transferMessage.Meta.BlockTimestamp)
assert.Equal(t, transaction.ID(), transferMessage.Meta.TxID)
origin, err := transaction.Origin()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, origin, transferMessage.Meta.TxOrigin)
assert.Equal(t, uint32(0), transferMessage.Meta.ClauseIndex)
assert.Equal(t, false, transferMessage.Obsolete)
assert.Equal(t, amount, transferLegacyMessage.Amount)
assert.Equal(t, amount, transferDynFeeMessage.Amount)

assert.Equal(t, blk.Header().ID(), transferLegacyMessage.Meta.BlockID)
assert.Equal(t, blk.Header().ID(), transferDynFeeMessage.Meta.BlockID)

assert.Equal(t, blk.Header().Number(), transferLegacyMessage.Meta.BlockNumber)
assert.Equal(t, blk.Header().Number(), transferDynFeeMessage.Meta.BlockNumber)

assert.Equal(t, blk.Header().Timestamp(), transferLegacyMessage.Meta.BlockTimestamp)
assert.Equal(t, blk.Header().Timestamp(), transferDynFeeMessage.Meta.BlockTimestamp)

assert.Equal(t, legacyTx.ID(), transferLegacyMessage.Meta.TxID)
assert.Equal(t, dynFeeTx.ID(), transferDynFeeMessage.Meta.TxID)

origin, err := legacyTx.Origin()
assert.NoError(t, err)
assert.Equal(t, origin, transferLegacyMessage.Meta.TxOrigin)
assert.Equal(t, uint32(0), transferLegacyMessage.Meta.ClauseIndex)
assert.Equal(t, false, transferLegacyMessage.Obsolete)

origin, err = dynFeeTx.Origin()
assert.NoError(t, err)
assert.Equal(t, origin, transferDynFeeMessage.Meta.TxOrigin)
assert.Equal(t, uint32(1), transferDynFeeMessage.Meta.ClauseIndex)
assert.Equal(t, false, transferDynFeeMessage.Obsolete)
}

func TestConvertEventWithBadSignature(t *testing.T) {
Expand Down
98 changes: 62 additions & 36 deletions api/transactions/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,72 @@ import (
)

func TestErrorWhileRetrievingTxOriginInConvertReceipt(t *testing.T) {
tr := new(tx.LegacyBuilder).Build()
header := &block.Header{}
receipt := &tx.Receipt{
Reward: big.NewInt(100),
Paid: big.NewInt(10),
txs := []*tx.Transaction{
new(tx.LegacyBuilder).Build(),
new(tx.DynFeeBuilder).Build(),
}

convRec, err := convertReceipt(receipt, header, tr)
for _, tr := range txs {
header := &block.Header{}
receipt := &tx.Receipt{
Reward: big.NewInt(100),
Paid: big.NewInt(10),
}

assert.Error(t, err)
assert.Equal(t, err, secp256k1.ErrInvalidSignatureLen)
assert.Nil(t, convRec)
convRec, err := convertReceipt(receipt, header, tr)

assert.Error(t, err)
assert.Equal(t, err, secp256k1.ErrInvalidSignatureLen)
assert.Nil(t, convRec)
}
}

func TestConvertReceiptWhenTxHasNoClauseTo(t *testing.T) {
value := big.NewInt(100)
tr := newTx(tx.NewClause(nil).WithValue(value))
b := new(block.Builder).Build()
header := b.Header()
receipt := newReceipt()
expectedOutputAddress := thor.CreateContractAddress(tr.ID(), uint32(0), 0)
txs := []*tx.Transaction{
newLegacyTx(tx.NewClause(nil).WithValue(value)),
newDynFeeTx(tx.NewClause(nil).WithValue(value)),
}
for _, tr := range txs {
b := new(block.Builder).Build()
header := b.Header()
receipt := newReceipt()
expectedOutputAddress := thor.CreateContractAddress(tr.ID(), uint32(0), 0)

convRec, err := convertReceipt(receipt, header, tr)
convRec, err := convertReceipt(receipt, header, tr)

assert.NoError(t, err)
assert.Equal(t, 1, len(convRec.Outputs))
assert.Equal(t, &expectedOutputAddress, convRec.Outputs[0].ContractAddress)
assert.NoError(t, err)
assert.Equal(t, 1, len(convRec.Outputs))
assert.Equal(t, &expectedOutputAddress, convRec.Outputs[0].ContractAddress)
}
}

func TestConvertReceipt(t *testing.T) {
value := big.NewInt(100)
addr := randAddress()
tr := newTx(tx.NewClause(&addr).WithValue(value))
b := new(block.Builder).Build()
header := b.Header()
receipt := newReceipt()

convRec, err := convertReceipt(receipt, header, tr)

assert.NoError(t, err)
assert.Equal(t, 1, len(convRec.Outputs))
assert.Equal(t, 1, len(convRec.Outputs[0].Events))
assert.Equal(t, 1, len(convRec.Outputs[0].Transfers))
assert.Nil(t, convRec.Outputs[0].ContractAddress)
assert.Equal(t, receipt.Outputs[0].Events[0].Address, convRec.Outputs[0].Events[0].Address)
assert.Equal(t, hexutil.Encode(receipt.Outputs[0].Events[0].Data), convRec.Outputs[0].Events[0].Data)
assert.Equal(t, receipt.Outputs[0].Transfers[0].Sender, convRec.Outputs[0].Transfers[0].Sender)
assert.Equal(t, receipt.Outputs[0].Transfers[0].Recipient, convRec.Outputs[0].Transfers[0].Recipient)
assert.Equal(t, (*math.HexOrDecimal256)(receipt.Outputs[0].Transfers[0].Amount), convRec.Outputs[0].Transfers[0].Amount)

txs := []*tx.Transaction{
newLegacyTx(tx.NewClause(&addr).WithValue(value)),
newDynFeeTx(tx.NewClause(&addr).WithValue(value)),
}
for _, tr := range txs {
b := new(block.Builder).Build()
header := b.Header()
receipt := newReceipt()

convRec, err := convertReceipt(receipt, header, tr)

assert.NoError(t, err)
assert.Equal(t, 1, len(convRec.Outputs))
assert.Equal(t, 1, len(convRec.Outputs[0].Events))
assert.Equal(t, 1, len(convRec.Outputs[0].Transfers))
assert.Nil(t, convRec.Outputs[0].ContractAddress)
assert.Equal(t, receipt.Outputs[0].Events[0].Address, convRec.Outputs[0].Events[0].Address)
assert.Equal(t, hexutil.Encode(receipt.Outputs[0].Events[0].Data), convRec.Outputs[0].Events[0].Data)
assert.Equal(t, receipt.Outputs[0].Transfers[0].Sender, convRec.Outputs[0].Transfers[0].Sender)
assert.Equal(t, receipt.Outputs[0].Transfers[0].Recipient, convRec.Outputs[0].Transfers[0].Recipient)
assert.Equal(t, (*math.HexOrDecimal256)(receipt.Outputs[0].Transfers[0].Amount), convRec.Outputs[0].Transfers[0].Amount)
}
}

// Utilities functions
Expand Down Expand Up @@ -99,7 +116,7 @@ func newReceipt() *tx.Receipt {
}
}

func newTx(clause *tx.Clause) *tx.Transaction {
func newLegacyTx(clause *tx.Clause) *tx.Transaction {
tx := new(tx.LegacyBuilder).
Clause(clause).
Build()
Expand All @@ -108,6 +125,15 @@ func newTx(clause *tx.Clause) *tx.Transaction {
return tx.WithSignature(sig)
}

func newDynFeeTx(clause *tx.Clause) *tx.Transaction {
tx := new(tx.DynFeeBuilder).
Clause(clause).
Build()
pk, _ := crypto.GenerateKey()
sig, _ := crypto.Sign(tx.SigningHash().Bytes(), pk)
return tx.WithSignature(sig)
}

func randomBytes32() thor.Bytes32 {
var b32 thor.Bytes32

Expand Down
2 changes: 1 addition & 1 deletion block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func TestBlock(t *testing.T) {
tx1 := new(tx.LegacyBuilder).Clause(tx.NewClause(&thor.Address{})).Clause(tx.NewClause(&thor.Address{})).Build()
tx2 := new(tx.LegacyBuilder).Clause(tx.NewClause(nil)).Build()
tx2 := new(tx.DynFeeBuilder).Clause(tx.NewClause(nil)).Build()

privKey := string("dce1443bd2ef0c2631adc1c67e5c93f13dc23a41c18b536effbbdcbcdb96fb65")

Expand Down
Loading

0 comments on commit 2fb8578

Please sign in to comment.