Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

feat(sender): splite adjustGasFee #637

Merged
merged 7 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e985d593c2b2b59490150c2d5a76e3ccb5fc0fe2
c2d7d6e16ca85e46943af9b520f986921f7b897d
24 changes: 0 additions & 24 deletions bindings/encoding/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ var (
Name: "l1Height",
Type: "uint64",
},
{
Name: "txListByteOffset",
Type: "uint24",
},
{
Name: "txListByteSize",
Type: "uint24",
},
{
Name: "minTier",
Type: "uint16",
Expand Down Expand Up @@ -120,22 +112,6 @@ var (
Name: "extraData",
Type: "bytes32",
},
{
Name: "blobHash",
Type: "bytes32",
},
{
Name: "txListByteOffset",
Type: "uint24",
},
{
Name: "txListByteSize",
Type: "uint24",
},
{
Name: "cacheBlobForReuse",
Type: "bool",
},
{
Name: "parentMetaHash",
Type: "bytes32",
Expand Down
14 changes: 5 additions & 9 deletions bindings/encoding/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ type HookCall struct {

// BlockParams should be same with TaikoData.BlockParams.
type BlockParams struct {
AssignedProver common.Address
Coinbase common.Address
ExtraData [32]byte
BlobHash [32]byte
TxListByteOffset *big.Int
TxListByteSize *big.Int
CacheBlobForReuse bool
ParentMetaHash [32]byte
HookCalls []HookCall
AssignedProver common.Address
Coinbase common.Address
ExtraData [32]byte
ParentMetaHash [32]byte
HookCalls []HookCall
}

// TierFee should be same with TaikoData.TierFee.
Expand Down
26 changes: 13 additions & 13 deletions bindings/gen_assignment_hook.go

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions bindings/gen_guardian_prover.go

Large diffs are not rendered by default.

179 changes: 7 additions & 172 deletions bindings/gen_lib_proposing.go

Large diffs are not rendered by default.

371 changes: 33 additions & 338 deletions bindings/gen_taiko_l1.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions bindings/gen_taiko_token.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions driver/chain_syncer/calldata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,30 @@ type Syncer struct {
// NewSyncer creates a new syncer instance.
func NewSyncer(
ctx context.Context,
rpc *rpc.Client,
client *rpc.Client,
state *state.State,
progressTracker *beaconsync.SyncProgressTracker,
) (*Syncer, error) {
configs, err := rpc.TaikoL1.GetConfig(&bind.CallOpts{Context: ctx})
configs, err := client.TaikoL1.GetConfig(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, fmt.Errorf("failed to get protocol configs: %w", err)
}

constructor, err := anchorTxConstructor.New(rpc)
constructor, err := anchorTxConstructor.New(client)
if err != nil {
return nil, fmt.Errorf("failed to initialize anchor constructor: %w", err)
}

return &Syncer{
ctx: ctx,
rpc: rpc,
rpc: client,
state: state,
progressTracker: progressTracker,
anchorConstructor: constructor,
txListValidator: txListValidator.NewTxListValidator(
uint64(configs.BlockMaxGasLimit),
configs.BlockMaxTxListBytes.Uint64(),
rpc.L2.ChainID,
rpc.BlockMaxTxListBytes,
client.L2.ChainID,
),
}, nil
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/rpc/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,14 @@ func TestGetPoolContentValid(t *testing.T) {
goldenTouchAddress, err := client.TaikoL2.GOLDENTOUCHADDRESS(nil)
require.Nil(t, err)
gasLimit := configs.BlockMaxGasLimit
maxBytes := configs.BlockMaxTxListBytes

txPools := []common.Address{goldenTouchAddress}

_, err2 := client.GetPoolContent(
context.Background(),
goldenTouchAddress,
gasLimit,
maxBytes.Uint64(),
BlockMaxTxListBytes,
txPools,
4,
)
Expand Down
7 changes: 4 additions & 3 deletions pkg/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (

var (
ZeroAddress common.Address
waitReceiptPollingInterval = 3 * time.Second
defaultWaitReceiptTimeout = 1 * time.Minute
BlobBytes = params.BlobTxBytesPerFieldElement * params.BlobTxFieldElementsPerBlob
waitReceiptPollingInterval = 3 * time.Second
defaultWaitReceiptTimeout = 1 * time.Minute
BlobBytes = params.BlobTxBytesPerFieldElement * params.BlobTxFieldElementsPerBlob
BlockMaxTxListBytes uint64 = (params.BlobTxBytesPerFieldElement - 1) * params.BlobTxFieldElementsPerBlob
)

// GetProtocolStateVariables gets the protocol states from TaikoL1 contract.
Expand Down
34 changes: 21 additions & 13 deletions pkg/sender/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,46 @@ import (
"github.com/taikoxyz/taiko-client/internal/utils"
)

// adjustGas adjusts the gas fee cap and gas tip cap of the given transaction with the configured
// AdjustGasFee adjusts the gas fee cap and gas tip cap of the given transaction with the configured
// growth rate.
func (s *Sender) adjustGas(txData types.TxData) {
rate := s.GasGrowthRate + 100
func (s *Sender) AdjustGasFee(txData types.TxData) {
rate := s.GasGrowthRate
switch baseTx := txData.(type) {
case *types.DynamicFeeTx:
gasFeeCap := baseTx.GasFeeCap.Int64()
gasFeeCap = gasFeeCap / 100 * int64(rate)
gasFeeCap = utils.Min(gasFeeCap, int64(s.MaxGasFee))
baseTx.GasFeeCap = big.NewInt(gasFeeCap)
gasFeeCap += gasFeeCap * int64(rate) / 100
baseTx.GasFeeCap = big.NewInt(utils.Min(gasFeeCap, int64(s.MaxGasFee)))

gasTipCap := baseTx.GasTipCap.Int64()
gasTipCap = gasTipCap / 100 * int64(rate)
gasTipCap += gasTipCap * int64(rate) / 100
gasTipCap = utils.Min(gasFeeCap, utils.Min(gasTipCap, int64(s.MaxGasFee)))
baseTx.GasTipCap = big.NewInt(gasTipCap)
case *types.BlobTx:
gasFeeCap := baseTx.GasFeeCap.Uint64()
gasFeeCap = gasFeeCap / 100 * rate
gasFeeCap = utils.Min(gasFeeCap, s.MaxGasFee)
baseTx.GasFeeCap = uint256.NewInt(gasFeeCap)
gasFeeCap += gasFeeCap * rate / 100
baseTx.GasFeeCap = uint256.NewInt(utils.Min(gasFeeCap, s.MaxGasFee))

gasTipCap := baseTx.GasTipCap.Uint64()
gasTipCap = gasTipCap / 100 * rate
gasTipCap += gasTipCap * rate / 100
gasTipCap = utils.Min(gasFeeCap, utils.Min(gasTipCap, s.MaxGasFee))
baseTx.GasTipCap = uint256.NewInt(gasTipCap)
default:
log.Warn("Unsupported transaction type when adjust gasFeeCap and gasTipCap", "from", s.opts.From)
}
}

// AdjustBlobGasFee adjusts the gas fee cap and gas tip cap of the given transaction with the configured.
func (s *Sender) AdjustBlobGasFee(txData types.TxData) {
rate := s.GasGrowthRate + 100
switch baseTx := txData.(type) {
case *types.BlobTx:
blobFeeCap := baseTx.BlobFeeCap.Uint64()
blobFeeCap = blobFeeCap / 100 * rate
// Use Max check is to catch is situation: +1 is necessary, if blobFeeCap can't increase.
blobFeeCap = utils.Max(blobFeeCap*rate/100, blobFeeCap+1)
blobFeeCap = utils.Min(blobFeeCap, s.MaxBlobFee)
baseTx.BlobFeeCap = uint256.NewInt(blobFeeCap)
default:
log.Warn("Unsupported transaction type when adjust gas fee", "from", s.opts.From)
log.Warn("Unsupported transaction type when adjust blobGasFeeCap", "from", s.opts.From)
}
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,17 @@ func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
}
continue
}
if strings.Contains(err.Error(), "replacement transaction underpriced") {
s.adjustGas(originalTx)
// handle the list:
// ErrUnderpriced: "transaction underpriced"
// ErrReplaceUnderpriced: "replacement transaction underpriced"
// blob tx err at https://github.com/ethereum/go-ethereum/blob/
// 20d3e0ac06ef2ad2f5f6500402edc5b6f0bf5b7c/core/txpool/blobpool/blobpool.go#L1157`
if strings.Contains(err.Error(), "transaction underpriced") {
if strings.Contains(err.Error(), "new tx blob gas fee cap") {
s.AdjustBlobGasFee(originalTx)
} else {
s.AdjustGasFee(originalTx)
}
log.Warn(
"Replacement transaction underpriced",
"txId", tx.ID,
Expand Down
31 changes: 31 additions & 0 deletions pkg/sender/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
"github.com/stretchr/testify/suite"
"golang.org/x/sync/errgroup"

"github.com/taikoxyz/taiko-client/internal/testutils"
"github.com/taikoxyz/taiko-client/internal/utils"
"github.com/taikoxyz/taiko-client/pkg/sender"
)

Expand Down Expand Up @@ -172,6 +174,35 @@ func (s *SenderTestSuite) TestNonceTooLow() {
s.Equal(nonce, confirm.CurrentTx.Nonce())
}

func (s *SenderTestSuite) TestAdjustGas() {
send := s.sender
dynamicTx := &types.DynamicFeeTx{}
blobTx := &types.BlobTx{}

for _, val := range []uint64{1, 20, 50, 100, 200, 1000, 10000, 20000} {
expectGasFeeCap := val + val*(send.GasGrowthRate)/100
expectGasTipCap := val + val*(send.GasGrowthRate)/100
expectGasTipCap = utils.Min(expectGasFeeCap, utils.Min(expectGasTipCap, send.MaxGasFee))

dynamicTx.GasFeeCap = new(big.Int).SetUint64(val)
dynamicTx.GasTipCap = new(big.Int).SetUint64(val)
send.AdjustGasFee(dynamicTx)
s.Equal(expectGasFeeCap, dynamicTx.GasFeeCap.Uint64(), "val: %d", val)
s.Equal(expectGasTipCap, dynamicTx.GasTipCap.Uint64(), "val: %d", val)

blobTx.GasFeeCap = uint256.NewInt(val)
blobTx.GasTipCap = uint256.NewInt(val)
send.AdjustGasFee(blobTx)
s.Equal(expectGasFeeCap, blobTx.GasFeeCap.Uint64(), "val: %d", val)
s.Equal(expectGasTipCap, blobTx.GasTipCap.Uint64(), "val: %d", val)

expectGasTipCap = utils.Max(val*(send.GasGrowthRate+100)/100, val+1)
blobTx.BlobFeeCap = uint256.NewInt(val)
send.AdjustBlobGasFee(blobTx)
s.Equal(expectGasTipCap, blobTx.BlobFeeCap.Uint64(), "val: %d", val)
}
}

func (s *SenderTestSuite) SetupTest() {
s.ClientTestSuite.SetupTest()
s.SetL1Automine(true)
Expand Down
2 changes: 1 addition & 1 deletion proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (p *Proposer) ProposeOp(ctx context.Context) error {
ctx,
p.proposerAddress,
p.protocolConfigs.BlockMaxGasLimit,
p.protocolConfigs.BlockMaxTxListBytes.Uint64(),
rpc.BlockMaxTxListBytes,
p.LocalAddresses,
p.MaxProposedTxListsPerEpoch,
)
Expand Down
14 changes: 5 additions & 9 deletions proposer/transaction_builder/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,11 @@ func (b *BlobTransactionBuilder) Build(

// ABI encode the TaikoL1.proposeBlock parameters.
encodedParams, err := encoding.EncodeBlockParams(&encoding.BlockParams{
AssignedProver: assignedProver,
ExtraData: rpc.StringToBytes32(b.extraData),
TxListByteOffset: common.Big0,
TxListByteSize: big.NewInt(int64(len(txListBytes))),
BlobHash: [32]byte{},
CacheBlobForReuse: false,
Coinbase: b.l2SuggestedFeeRecipient,
ParentMetaHash: parentMetaHash,
HookCalls: []encoding.HookCall{{Hook: b.assignmentHookAddress, Data: hookInputData}},
AssignedProver: assignedProver,
ExtraData: rpc.StringToBytes32(b.extraData),
Coinbase: b.l2SuggestedFeeRecipient,
ParentMetaHash: parentMetaHash,
HookCalls: []encoding.HookCall{{Hook: b.assignmentHookAddress, Data: hookInputData}},
})
if err != nil {
return nil, err
Expand Down
14 changes: 5 additions & 9 deletions proposer/transaction_builder/calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,11 @@ func (b *CalldataTransactionBuilder) Build(

// ABI encode the TaikoL1.proposeBlock parameters.
encodedParams, err := encoding.EncodeBlockParams(&encoding.BlockParams{
AssignedProver: assignedProver,
Coinbase: b.l2SuggestedFeeRecipient,
ExtraData: rpc.StringToBytes32(b.extraData),
TxListByteOffset: common.Big0,
TxListByteSize: common.Big0,
BlobHash: [32]byte{},
CacheBlobForReuse: false,
ParentMetaHash: parentMetaHash,
HookCalls: []encoding.HookCall{{Hook: b.assignmentHookAddress, Data: hookInputData}},
AssignedProver: assignedProver,
Coinbase: b.l2SuggestedFeeRecipient,
ExtraData: rpc.StringToBytes32(b.extraData),
ParentMetaHash: parentMetaHash,
HookCalls: []encoding.HookCall{{Hook: b.assignmentHookAddress, Data: hookInputData}},
})
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions prover/proof_submitter/transaction/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func (s *TransactionTestSuite) TestBuildTxs() {
_, err := s.builder.Build(
common.Big256,
&bindings.TaikoDataBlockMetadata{TxListByteOffset: common.Big1, TxListByteSize: common.Big256},
&bindings.TaikoDataBlockMetadata{},
&bindings.TaikoDataTransition{},
&bindings.TaikoDataTierProof{},
false,
Expand All @@ -20,7 +20,7 @@ func (s *TransactionTestSuite) TestBuildTxs() {

_, err = s.builder.Build(
common.Big256,
&bindings.TaikoDataBlockMetadata{TxListByteOffset: common.Big1, TxListByteSize: common.Big256},
&bindings.TaikoDataBlockMetadata{},
&bindings.TaikoDataTransition{},
&bindings.TaikoDataTierProof{},
true,
Expand Down
Loading