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

Add the dynamic fee tx and support for typed txs #903

Open
wants to merge 19 commits into
base: release/galactica
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
20 changes: 8 additions & 12 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func initAccountServer(t *testing.T, enabledDeprecated bool) {
genesisBlock = thorChain.GenesisBlock()
claTransfer := tx.NewClause(&addr).WithValue(value)
claDeploy := tx.NewClause(nil).WithData(bytecode)
transaction := buildTxWithClauses(thorChain.Repo().ChainTag(), claTransfer, claDeploy)
transaction := buildTxWithClauses(tx.LegacyTxType, thorChain.Repo().ChainTag(), claTransfer, claDeploy)
contractAddr = thor.CreateContractAddress(transaction.ID(), 1, 0)
method := "set"
abi, _ := ABI.New([]byte(abiJSON))
Expand All @@ -296,7 +296,7 @@ func initAccountServer(t *testing.T, enabledDeprecated bool) {
t.Fatal(err)
}
claCall := tx.NewClause(&contractAddr).WithData(input)
transactionCall := buildTxWithClauses(thorChain.Repo().ChainTag(), claCall)
transactionCall := buildTxWithClauses(tx.DynamicFeeTxType, thorChain.Repo().ChainTag(), claCall)
require.NoError(t,
thorChain.MintTransactions(
genesis.DevAccounts()[0],
Expand All @@ -312,17 +312,13 @@ func initAccountServer(t *testing.T, enabledDeprecated bool) {
ts = httptest.NewServer(router)
}

func buildTxWithClauses(chaiTag byte, clauses ...*tx.Clause) *tx.Transaction {
builder := new(tx.Builder).
ChainTag(chaiTag).
func buildTxWithClauses(txType int, chainTag byte, clauses ...*tx.Clause) *tx.Transaction {
trx, _ := tx.NewTxBuilder(txType).
ChainTag(chainTag).
Expiration(10).
Gas(1000000)
for _, c := range clauses {
builder.Clause(c)
}

trx := builder.Build()

Gas(1000000).
Clauses(clauses).
Build()
return tx.MustSign(trx, genesis.DevAccounts()[0].PrivateKey)
}

Expand Down
38 changes: 24 additions & 14 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,30 @@ func initBlockServer(t *testing.T) {

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
trx := tx.MustSign(
new(tx.Builder).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[0].PrivateKey,
)

require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], trx))
legacyTx, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
legacyTx = tx.MustSign(legacyTx, genesis.DevAccounts()[0].PrivateKey)

dynFeeTx, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
MaxFeePerGas(big.NewInt(100000)).
MaxPriorityFeePerGas(big.NewInt(100)).
Expiration(10).
Gas(21000).
Nonce(2).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
dynFeeTx = tx.MustSign(dynFeeTx, genesis.DevAccounts()[0].PrivateKey)

require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], legacyTx, dynFeeTx))

allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)
Expand Down
74 changes: 42 additions & 32 deletions api/blocks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package blocks

import (
"math/big"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/vechain/thor/v2/chain"
Expand Down Expand Up @@ -67,18 +69,20 @@ type JSONOutput struct {
}

type JSONEmbeddedTx struct {
ID thor.Bytes32 `json:"id"`
ChainTag byte `json:"chainTag"`
BlockRef string `json:"blockRef"`
Expiration uint32 `json:"expiration"`
Clauses []*JSONClause `json:"clauses"`
GasPriceCoef uint8 `json:"gasPriceCoef"`
Gas uint64 `json:"gas"`
Origin thor.Address `json:"origin"`
Delegator *thor.Address `json:"delegator"`
Nonce math.HexOrDecimal64 `json:"nonce"`
DependsOn *thor.Bytes32 `json:"dependsOn"`
Size uint32 `json:"size"`
ID thor.Bytes32 `json:"id"`
ChainTag byte `json:"chainTag"`
BlockRef string `json:"blockRef"`
Expiration uint32 `json:"expiration"`
Clauses []*JSONClause `json:"clauses"`
GasPriceCoef uint8 `json:"gasPriceCoef"`
MaxFeePerGas *big.Int `json:"maxFeePerGas,omitempty"`
MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas,omitempty"`
Gas uint64 `json:"gas"`
Origin thor.Address `json:"origin"`
Delegator *thor.Address `json:"delegator"`
Nonce math.HexOrDecimal64 `json:"nonce"`
DependsOn *thor.Bytes32 `json:"dependsOn"`
Size uint32 `json:"size"`

// receipt part
GasUsed uint64 `json:"gasUsed"`
Expand Down Expand Up @@ -148,13 +152,13 @@ func buildJSONOutput(txID thor.Bytes32, index uint32, c *tx.Clause, o *tx.Output

func buildJSONEmbeddedTxs(txs tx.Transactions, receipts tx.Receipts) []*JSONEmbeddedTx {
jTxs := make([]*JSONEmbeddedTx, 0, len(txs))
for itx, tx := range txs {
for itx, trx := range txs {
receipt := receipts[itx]

clauses := tx.Clauses()
blockRef := tx.BlockRef()
origin, _ := tx.Origin()
delegator, _ := tx.Delegator()
clauses := trx.Clauses()
blockRef := trx.BlockRef()
origin, _ := trx.Origin()
delegator, _ := trx.Delegator()

jcs := make([]*JSONClause, 0, len(clauses))
jos := make([]*JSONOutput, 0, len(receipt.Outputs))
Expand All @@ -166,31 +170,37 @@ func buildJSONEmbeddedTxs(txs tx.Transactions, receipts tx.Receipts) []*JSONEmbe
hexutil.Encode(c.Data()),
})
if !receipt.Reverted {
jos = append(jos, buildJSONOutput(tx.ID(), uint32(i), c, receipt.Outputs[i]))
jos = append(jos, buildJSONOutput(trx.ID(), uint32(i), c, receipt.Outputs[i]))
}
}

jTxs = append(jTxs, &JSONEmbeddedTx{
ID: tx.ID(),
ChainTag: tx.ChainTag(),
BlockRef: hexutil.Encode(blockRef[:]),
Expiration: tx.Expiration(),
Clauses: jcs,
GasPriceCoef: tx.GasPriceCoef(),
Gas: tx.Gas(),
Origin: origin,
Delegator: delegator,
Nonce: math.HexOrDecimal64(tx.Nonce()),
DependsOn: tx.DependsOn(),
Size: uint32(tx.Size()),
embedTx := &JSONEmbeddedTx{
ID: trx.ID(),
ChainTag: trx.ChainTag(),
BlockRef: hexutil.Encode(blockRef[:]),
Expiration: trx.Expiration(),
Clauses: jcs,
Gas: trx.Gas(),
Origin: origin,
Delegator: delegator,
Nonce: math.HexOrDecimal64(trx.Nonce()),
DependsOn: trx.DependsOn(),
Size: uint32(trx.Size()),

GasUsed: receipt.GasUsed,
GasPayer: receipt.GasPayer,
Paid: (*math.HexOrDecimal256)(receipt.Paid),
Reward: (*math.HexOrDecimal256)(receipt.Reward),
Reverted: receipt.Reverted,
Outputs: jos,
})
}
if trx.Type() == tx.LegacyTxType {
embedTx.GasPriceCoef = trx.GasPriceCoef()
} else {
embedTx.MaxFeePerGas = trx.MaxFeePerGas()
embedTx.MaxPriorityFeePerGas = trx.MaxPriorityFeePerGas()
}
jTxs = append(jTxs, embedTx)
}
return jTxs
}
21 changes: 18 additions & 3 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ 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, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
Expiration(10).
Gas(21000).
Expand All @@ -550,7 +550,7 @@ func initDebugServer(t *testing.T) {

cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000))
transaction = new(tx.Builder).
transaction, _ = tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -562,7 +562,22 @@ func initDebugServer(t *testing.T) {
Build()
transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey)

require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], transaction, noClausesTx))
dynFeeTx, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
Expiration(10).
Gas(21000).
MaxFeePerGas(big.NewInt(1000)).
MaxPriorityFeePerGas(big.NewInt(100000)).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
dynFeeTx = tx.MustSign(
dynFeeTx,
genesis.DevAccounts()[0].PrivateKey,
)

require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], transaction, noClausesTx, dynFeeTx))
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0]))

allBlocks, err := thorChain.GetAllBlocks()
Expand Down
6 changes: 3 additions & 3 deletions api/subscriptions/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func initChain(t *testing.T) *testchain.Chain {

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tr := new(tx.Builder).
tr, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -75,9 +75,9 @@ func initChain(t *testing.T) *testchain.Chain {
Build()
tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey)

txDeploy := new(tx.Builder).
txDeploy, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
MaxFeePerGas(big.NewInt(1)).
Expiration(100).
Gas(1_000_000).
Nonce(3).
Expand Down
45 changes: 23 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 := createTx(repo, 0, tx.LegacyTxType)
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 := createTx(repo, 1, tx.DynamicFeeTxType)
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.Builder).
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 := createTx(thorChain.Repo(), 0, tx.LegacyTxType)
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 := createTx(thorChain.Repo(), 0, tx.LegacyTxType)
txPool.AddLocal(trx)

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

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

trx, _ := tx.NewTxBuilder(txType).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(1000).
Gas(21000).
Nonce(uint64(datagen.RandInt())).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
return tx.MustSign(
trx,
genesis.DevAccounts()[addressNumber].PrivateKey,
)
}
10 changes: 5 additions & 5 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.Builder).
tr, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
MaxFeePerGas(big.NewInt(1)).
Expiration(10).
Gas(21000).
Nonce(1).
Expand All @@ -255,7 +255,7 @@ func initSubscriptionsServer(t *testing.T, enabledDeprecated bool) {
}
tr = tr.WithSignature(sig)

txDeploy := new(tx.Builder).
txDeploy, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(100).
Expand Down Expand Up @@ -291,7 +291,7 @@ func TestSubscriptionsBacktrace(t *testing.T) {

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tr := new(tx.Builder).
tr, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -307,7 +307,7 @@ func TestSubscriptionsBacktrace(t *testing.T) {
}
tr = tr.WithSignature(sig)

txDeploy := new(tx.Builder).
txDeploy, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(100).
Expand Down
Loading
Loading