From e2272eaf1edd942f40a886dca72a0bea904f5c1e Mon Sep 17 00:00:00 2001 From: seek Date: Thu, 31 Aug 2023 15:21:07 +0800 Subject: [PATCH 01/14] R4R: Hotfix mt batcher log (#1359) fix mt batcher log --- mt-batcher/services/sequencer/driver.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mt-batcher/services/sequencer/driver.go b/mt-batcher/services/sequencer/driver.go index 52b2997fd..76d5faac8 100644 --- a/mt-batcher/services/sequencer/driver.go +++ b/mt-batcher/services/sequencer/driver.go @@ -821,6 +821,7 @@ func (d *Driver) RollupMainWorker() { log.Error("MtBatcher disperse store data fail", "err", err) continue } + log.Info("MtBatcher disperse store data success", "txHash", receipt.TxHash.String()) d.Cfg.Metrics.L2StoredBlockNumber().Set(float64(start.Uint64())) time.Sleep(10 * time.Second) // sleep for data into graph node csdReceipt, err := d.ConfirmStoredData(receipt.TxHash.Bytes(), params, startL2BlockNumber, endL2BlockNumber, 0, big.NewInt(0), false) @@ -828,7 +829,7 @@ func (d *Driver) RollupMainWorker() { log.Error("MtBatcher confirm store data fail", "err", err) continue } - log.Debug("MtBatcher confirm store data success", "txHash", csdReceipt.TxHash.String()) + log.Info("MtBatcher confirm store data success", "txHash", csdReceipt.TxHash.String()) d.Cfg.Metrics.L2ConfirmedBlockNumber().Set(float64(start.Uint64())) if d.Cfg.FeeModelEnable { daFee, _ := d.CalcUserFeeByRules(big.NewInt(int64(len(aggregateTxData)))) @@ -862,7 +863,7 @@ func (d *Driver) RollUpFeeWorker() { continue } daFee := <-d.FeeCh - log.Debug("MtBatcher RollUpFeeWorker chainFee and daFee", "chainFee", chainFee, "daFee", *daFee) + log.Info("MtBatcher RollUpFeeWorker chainFee and daFee", "chainFee", chainFee, "daFee", *daFee) if chainFee.Cmp(daFee.RollUpFee) != 0 { txfRpt, err := d.UpdateUserDaFee(daFee.EndL2BlockNumber, daFee.RollUpFee) if err != nil { @@ -870,7 +871,7 @@ func (d *Driver) RollUpFeeWorker() { continue } d.Cfg.Metrics.EigenUserFee().Set(float64(daFee.RollUpFee.Uint64())) - log.Debug("MtBatcher RollUpFeeWorker update user fee success", "Hash", txfRpt.TxHash.String()) + log.Info("MtBatcher RollUpFeeWorker update user fee success", "Hash", txfRpt.TxHash.String()) } } case err := <-d.Ctx.Done(): @@ -924,7 +925,7 @@ func (d *Driver) CheckConfirmedWorker() { log.Info("Checker get l2 rollup block fail", "err", err) continue } - log.Debug("Checker DataStoreIdToL2RollUpBlock", "rollupBlock.StartL2BlockNumber", rollupBlock.StartL2BlockNumber, "rollupBlock.EndBL2BlockNumber", rollupBlock.EndBL2BlockNumber) + log.Info("Checker DataStoreIdToL2RollUpBlock", "rollupBlock.StartL2BlockNumber", rollupBlock.StartL2BlockNumber, "rollupBlock.EndBL2BlockNumber", rollupBlock.EndBL2BlockNumber) aggregateTxData, startL2BlockNumber, endL2BlockNumber := d.TxAggregator( d.Ctx, rollupBlock.StartL2BlockNumber, rollupBlock.EndBL2BlockNumber, From 727e1a22934251c7a3f06d4320e09c64136610be Mon Sep 17 00:00:00 2001 From: idyllsss <137248235+idyllsss@users.noreply.github.com> Date: Thu, 31 Aug 2023 15:21:59 +0800 Subject: [PATCH 02/14] R4R: update sdk estimate L1RollupFee (#1361) add interfaces to estimate L1RollupFee --- packages/sdk/src/interfaces/l2-provider.ts | 24 +++++++++++ packages/sdk/src/l2-provider.ts | 48 ++++++++++++++++++++++ packages/sdk/test/l2-provider.spec.ts | 12 ++++++ 3 files changed, 84 insertions(+) diff --git a/packages/sdk/src/interfaces/l2-provider.ts b/packages/sdk/src/interfaces/l2-provider.ts index bd988705e..ffcb6d685 100644 --- a/packages/sdk/src/interfaces/l2-provider.ts +++ b/packages/sdk/src/interfaces/l2-provider.ts @@ -48,6 +48,30 @@ export type L2Provider = TProvider & { */ getL1GasPrice(): Promise + /** + * Number of decimals of the scalar + * + * @returns decimals + */ + decimals(): Promise + + /** + * Value to scale the fee up by + * + * @returns scale + */ + scalar(): Promise + + /** + * Computes the amount of L1 gas used for a transaction + * The overhead represents the per batch gas overhead of + * posting both transaction and state roots to L1 given larger + * batch sizes. + * + * @returns Current L1 data gas price in wei. + */ + overhead(): Promise + /** * Estimates the L1 (data) gas required for a transaction. * diff --git a/packages/sdk/src/l2-provider.ts b/packages/sdk/src/l2-provider.ts index 7f03fdbdd..02bf7bb09 100644 --- a/packages/sdk/src/l2-provider.ts +++ b/packages/sdk/src/l2-provider.ts @@ -59,6 +59,45 @@ export const getL1GasPrice = async ( return gpo.l1BaseFee() } +/** + * Number of decimals of the scalar + * + * @param l2Provider L2 provider to query the decimals from. + * @returns decimals + */ +export const decimals = async ( + l2Provider: ProviderLike +): Promise => { + const gpo = connectGasPriceOracle(l2Provider) + return gpo.decimals() +} + +/** + * Value to scale the fee up by + * + * @param l2Provider L2 provider to query the L1 gas used from. + * @returns scalar + */ +export const scalar = async ( + l2Provider: ProviderLike +): Promise => { + const gpo = connectGasPriceOracle(l2Provider) + return gpo.scalar() +} + +/** + * Gets the current L1 gas limit as seen on L2. + * + * @param l2Provider L2 provider to query the L1 gas used from. + * @returns Current L1 gas limit as seen on L2. + */ +export const overhead = async ( + l2Provider: ProviderLike +): Promise => { + const gpo = connectGasPriceOracle(l2Provider) + return gpo.overhead() +} + /** * Estimates the amount of L1 gas required for a given L2 transaction. * @@ -245,6 +284,15 @@ export const asL2Provider = ( l2Provider.getL1GasPrice = async () => { return getL1GasPrice(l2Provider) } + l2Provider.decimals = async () => { + return decimals(l2Provider) + } + l2Provider.scalar = async () => { + return scalar(l2Provider) + } + l2Provider.overhead = async () => { + return overhead(l2Provider) + } l2Provider.estimateL1Gas = async (tx: TransactionRequest) => { return estimateL1Gas(l2Provider, tx) } diff --git a/packages/sdk/test/l2-provider.spec.ts b/packages/sdk/test/l2-provider.spec.ts index 51709d25a..6a9c35c3f 100644 --- a/packages/sdk/test/l2-provider.spec.ts +++ b/packages/sdk/test/l2-provider.spec.ts @@ -6,6 +6,18 @@ describe('L2Provider', () => { it('should query the GasPriceOracle contract', () => {}) }) + describe('decimals', () => { + it('should query the GasPriceOracle contract', () => {}) + }) + + describe('scalar', () => { + it('should query the GasPriceOracle contract', () => {}) + }) + + describe('overhead', () => { + it('should query the GasPriceOracle contract', () => {}) + }) + describe('estimateL1Gas', () => { it('should query the GasPriceOracle contract', () => {}) }) From e084e0f554e00dfb8c6a2b4d2d3bfe3a926400d2 Mon Sep 17 00:00:00 2001 From: ethan Date: Thu, 31 Aug 2023 03:01:40 +0800 Subject: [PATCH 03/14] finish rollup gas optimize --- batch-submitter/drivers/sequencer/batch.go | 117 +++--------------- batch-submitter/drivers/sequencer/driver.go | 27 ++-- batch-submitter/drivers/sequencer/encoding.go | 3 - 3 files changed, 35 insertions(+), 112 deletions(-) diff --git a/batch-submitter/drivers/sequencer/batch.go b/batch-submitter/drivers/sequencer/batch.go index ff8c9deb5..c85404de0 100644 --- a/batch-submitter/drivers/sequencer/batch.go +++ b/batch-submitter/drivers/sequencer/batch.go @@ -3,6 +3,7 @@ package sequencer import ( "errors" "fmt" + l2types "github.com/mantlenetworkio/mantle/l2geth/core/types" ) @@ -76,113 +77,27 @@ type groupedBlock struct { func GenSequencerBatchParams( shouldStartAtElement uint64, blockOffset uint64, - batch []BatchElement, + batchNumber uint64, + timestamp uint64, + blockNumber uint64, + + numSequencedTxs uint64, + numSubsequentQueueTxs uint64, + ) (*AppendSequencerBatchParams, error) { var ( - contexts []BatchContext - groupedBlocks []groupedBlock - txs []*CachedTx - lastBlockIsSequencerTx bool - lastTimestamp uint64 - lastBlockNumber uint64 + contexts []BatchContext ) - // Iterate over the batch elements, grouping the elements according to - // the following criteria: - // - All txs in the same group must have the same timestamp. - // - All sequencer txs in the same group must have the same block number. - // - If sequencer txs exist in a group, they must come before all - // queued txs. - // - // Assuming the block and timestamp criteria for sequencer txs are - // respected within each group, the following are examples of groupings: - // - [s] // sequencer can exist by itself - // - [q] // ququed tx can exist by itself - // - [s] [s] // differing sequencer tx timestamp/blocknumber - // - [s q] [s] // sequencer tx must precede queued tx in group - // - [q] [q s] // INVALID: consecutive queued txs are split - // - [q q] [s] // correct split for preceding case - // - [s q] [s q] // alternating sequencer tx interleaved with queued - for _, el := range batch { - // To enforce the above groupings, the following condition is - // used to determine when to create a new batch: - // - On the first pass, or - // - The preceding tx has a different timestamp, or - // - Whenever a sequencer tx is observed, and: - // - The preceding tx was a queued tx, or - // - The preceding sequencer tx has a different block number. - // Note that a sequencer tx is usually required to create a new group, - // so a queued tx may ONLY exist as the first element in a group if it - // is the very first element or it has a different timestamp from the - // preceding tx. - needsNewGroupOnSequencerTx := !lastBlockIsSequencerTx || - el.BlockNumber != lastBlockNumber - if len(groupedBlocks) == 0 || - el.Timestamp != lastTimestamp || - (el.IsSequencerTx() && needsNewGroupOnSequencerTx) { - - groupedBlocks = append(groupedBlocks, groupedBlock{}) - } - - // Append the tx to either the sequenced or queued txs, - // depending on its type. - cur := len(groupedBlocks) - 1 - if el.IsSequencerTx() { - groupedBlocks[cur].sequenced = - append(groupedBlocks[cur].sequenced, el) - - // Gather all sequencer txs, as these will be encoded in - // the calldata of the batch tx submitted to the L1 CTC - // contract. - txs = append(txs, el.Tx) - } else { - groupedBlocks[cur].queued = - append(groupedBlocks[cur].queued, el) - } - - lastBlockIsSequencerTx = el.IsSequencerTx() - lastTimestamp = el.Timestamp - lastBlockNumber = el.BlockNumber - } + contexts = append(contexts, BatchContext{ + NumSequencedTxs: numSequencedTxs, + NumSubsequentQueueTxs: numSubsequentQueueTxs, + Timestamp: timestamp, + BlockNumber: blockNumber, + }) - // For each group, construct the resulting BatchContext. - for _, block := range groupedBlocks { - numSequencedTxs := uint64(len(block.sequenced)) - numSubsequentQueueTxs := uint64(len(block.queued)) - - // Ensure at least one tx was included in this group. - if numSequencedTxs == 0 && numSubsequentQueueTxs == 0 { - return nil, ErrBlockWithInvalidContext - } - - // Compute the timestamp and block number from for the batch - // using either the earliest sequenced tx or the earliest queued - // tx. If a batch has a sequencer tx it is given preference, - // since it is guaranteed to be the earliest item in the group. - // Otherwise, we fallback to the earliest queued tx since it was - // the very first item. - var ( - timestamp uint64 - blockNumber uint64 - ) - if numSequencedTxs > 0 { - timestamp = block.sequenced[0].Timestamp - blockNumber = block.sequenced[0].BlockNumber - } else { - timestamp = block.queued[0].Timestamp - blockNumber = block.queued[0].BlockNumber - } - - contexts = append(contexts, BatchContext{ - NumSequencedTxs: numSequencedTxs, - NumSubsequentQueueTxs: numSubsequentQueueTxs, - Timestamp: timestamp, - BlockNumber: blockNumber, - }) - } return &AppendSequencerBatchParams{ ShouldStartAtElement: shouldStartAtElement - blockOffset, - TotalElementsToAppend: uint64(len(batch)), + TotalElementsToAppend: batchNumber, Contexts: contexts, - Txs: txs, }, nil } diff --git a/batch-submitter/drivers/sequencer/driver.go b/batch-submitter/drivers/sequencer/driver.go index a1697bce5..775054be2 100644 --- a/batch-submitter/drivers/sequencer/driver.go +++ b/batch-submitter/drivers/sequencer/driver.go @@ -231,7 +231,10 @@ func (d *Driver) CraftBatchTx( log.Info(name+" crafting batch tx", "start", start, "end", end, "nonce", nonce, "type", d.cfg.BatchType.String()) - var batchElements []BatchElement + var lastTimestamp uint64 + var lastBlockNumber uint64 + numSequencedTxs := 0 + numSubsequentQueueTxs := 0 for i := new(big.Int).Set(start); i.Cmp(end) < 0; i.Add(i, bigOne) { block, err := d.cfg.L2Client.BlockByNumber(ctx, i) @@ -242,20 +245,28 @@ func (d *Driver) CraftBatchTx( // For each sequencer transaction, update our running total with the // size of the transaction. batchElement := BatchElementFromBlock(block) - batchElements = append(batchElements, batchElement) + if batchElement.IsSequencerTx() { + numSequencedTxs += 1 + } else { + numSubsequentQueueTxs += 1 + } + if i.Cmp(big.NewInt(0).Sub(end, bigOne)) == 0 { + lastTimestamp = batchElement.Timestamp + lastBlockNumber = batchElement.BlockNumber + } } - + blocksLen := numSequencedTxs + numSubsequentQueueTxs shouldStartAt := start.Uint64() for { batchParams, err := GenSequencerBatchParams( - shouldStartAt, d.cfg.BlockOffset, batchElements, - ) + shouldStartAt, d.cfg.BlockOffset, uint64(blocksLen), lastTimestamp, + lastBlockNumber, uint64(numSequencedTxs), uint64(numSubsequentQueueTxs)) if err != nil { return nil, err } // Encode the batch arguments using the configured encoding type. - batchArguments, err := batchParams.Serialize(d.cfg.BatchType, start, big.NewInt(int64(d.cfg.DaUpgradeBlock))) + batchArguments, err := batchParams.Serialize(d.cfg.BatchType) if err != nil { return nil, err } @@ -266,10 +277,10 @@ func (d *Driver) CraftBatchTx( log.Info(name+" testing batch size", "calldata_size", len(calldata)) - d.metrics.NumElementsPerBatch().Observe(float64(len(batchElements))) + d.metrics.NumElementsPerBatch().Observe(float64(blocksLen)) log.Info(name+" batch constructed", - "num_txs", len(batchElements), + "num_txs", blocksLen, "final_size", len(calldata), "batch_type", d.cfg.BatchType) diff --git a/batch-submitter/drivers/sequencer/encoding.go b/batch-submitter/drivers/sequencer/encoding.go index af994e9b8..2dcbdce55 100644 --- a/batch-submitter/drivers/sequencer/encoding.go +++ b/batch-submitter/drivers/sequencer/encoding.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "math" - "math/big" l2types "github.com/mantlenetworkio/mantle/l2geth/core/types" l2rlp "github.com/mantlenetworkio/mantle/l2geth/rlp" @@ -239,8 +238,6 @@ func (p *AppendSequencerBatchParams) WriteNoTxn( // bytes slice. func (p *AppendSequencerBatchParams) Serialize( batchType BatchType, - l2BlockNumber *big.Int, - upgradeBlock *big.Int, ) ([]byte, error) { var buf bytes.Buffer if err := p.WriteNoTxn(&buf, batchType); err != nil { From f877f61efefc6e6c84e0f6870107e8dae2a5c490 Mon Sep 17 00:00:00 2001 From: "shijiang.guo" Date: Thu, 14 Sep 2023 11:35:47 +0800 Subject: [PATCH 04/14] code optimization and refactoring --- batch-submitter/drivers/sequencer/batch.go | 67 ++----------------- .../drivers/sequencer/batch_test.go | 9 ++- .../drivers/sequencer/cached_tx.go | 37 ---------- batch-submitter/drivers/sequencer/driver.go | 31 ++++++--- batch-submitter/drivers/sequencer/encoding.go | 14 ++-- .../drivers/sequencer/encoding_test.go | 43 +----------- datalayr | 2 +- 7 files changed, 37 insertions(+), 166 deletions(-) delete mode 100644 batch-submitter/drivers/sequencer/cached_tx.go diff --git a/batch-submitter/drivers/sequencer/batch.go b/batch-submitter/drivers/sequencer/batch.go index c85404de0..dd40dce36 100644 --- a/batch-submitter/drivers/sequencer/batch.go +++ b/batch-submitter/drivers/sequencer/batch.go @@ -1,19 +1,11 @@ package sequencer import ( - "errors" "fmt" l2types "github.com/mantlenetworkio/mantle/l2geth/core/types" ) -var ( - // ErrBlockWithInvalidContext signals an attempt to generate a - // BatchContext that specifies a total of zero txs. - ErrBlockWithInvalidContext = errors.New("attempted to generate batch " + - "context with 0 queued and 0 sequenced txs") -) - // BatchElement reflects the contents of an atomic update to the L2 state. // Currently, each BatchElement is constructed from a single block containing // exactly one tx. @@ -24,16 +16,8 @@ type BatchElement struct { // BlockNumber is the L1 BlockNumber of the batch. BlockNumber uint64 - // Tx is the optional transaction that was applied in this batch. - // - // NOTE: This field will only be populated for sequencer txs. - Tx *CachedTx -} - -// IsSequencerTx returns true if this batch contains a tx that needs to be -// posted to the L1 CTC contract. -func (b *BatchElement) IsSequencerTx() bool { - return b.Tx != nil + // whether it is a Sequencer transaction + IsSequencerTx bool } // BatchElementFromBlock constructs a BatchElement from a single L2 block. This @@ -54,50 +38,9 @@ func BatchElementFromBlock(block *l2types.Block) BatchElement { isSequencerTx := tx.QueueOrigin() == l2types.QueueOriginSequencer // Only include sequencer txs in the returned BatchElement. - var cachedTx *CachedTx - if isSequencerTx { - cachedTx = NewCachedTx(tx) - } - return BatchElement{ - Timestamp: block.Time(), - BlockNumber: l1BlockNumber, - Tx: cachedTx, + Timestamp: block.Time(), + BlockNumber: l1BlockNumber, + IsSequencerTx: isSequencerTx, } } - -type groupedBlock struct { - sequenced []BatchElement - queued []BatchElement -} - -// GenSequencerBatchParams generates a valid AppendSequencerBatchParams from a -// list of BatchElements. The BatchElements are assumed to be ordered in -// ascending order by L2 block height. -func GenSequencerBatchParams( - shouldStartAtElement uint64, - blockOffset uint64, - batchNumber uint64, - timestamp uint64, - blockNumber uint64, - - numSequencedTxs uint64, - numSubsequentQueueTxs uint64, - -) (*AppendSequencerBatchParams, error) { - var ( - contexts []BatchContext - ) - contexts = append(contexts, BatchContext{ - NumSequencedTxs: numSequencedTxs, - NumSubsequentQueueTxs: numSubsequentQueueTxs, - Timestamp: timestamp, - BlockNumber: blockNumber, - }) - - return &AppendSequencerBatchParams{ - ShouldStartAtElement: shouldStartAtElement - blockOffset, - TotalElementsToAppend: batchNumber, - Contexts: contexts, - }, nil -} diff --git a/batch-submitter/drivers/sequencer/batch_test.go b/batch-submitter/drivers/sequencer/batch_test.go index 9d66b82c5..64fa14a39 100644 --- a/batch-submitter/drivers/sequencer/batch_test.go +++ b/batch-submitter/drivers/sequencer/batch_test.go @@ -4,10 +4,11 @@ import ( "math/big" "testing" + "github.com/stretchr/testify/require" + "github.com/mantlenetworkio/mantle/batch-submitter/drivers/sequencer" l2common "github.com/mantlenetworkio/mantle/l2geth/common" l2types "github.com/mantlenetworkio/mantle/l2geth/core/types" - "github.com/stretchr/testify/require" ) func TestBatchElementFromBlock(t *testing.T) { @@ -30,8 +31,7 @@ func TestBatchElementFromBlock(t *testing.T) { require.Equal(t, element.Timestamp, expTime) require.Equal(t, element.BlockNumber, expBlockNumber) - require.True(t, element.IsSequencerTx()) - require.Equal(t, element.Tx.Tx(), expTx) + require.True(t, element.IsSequencerTx) queueMeta := l2types.NewTransactionMeta( new(big.Int).SetUint64(expBlockNumber), 0, nil, @@ -44,6 +44,5 @@ func TestBatchElementFromBlock(t *testing.T) { require.Equal(t, element.Timestamp, expTime) require.Equal(t, element.BlockNumber, expBlockNumber) - require.False(t, element.IsSequencerTx()) - require.Nil(t, element.Tx) + require.False(t, element.IsSequencerTx) } diff --git a/batch-submitter/drivers/sequencer/cached_tx.go b/batch-submitter/drivers/sequencer/cached_tx.go deleted file mode 100644 index d726e86b1..000000000 --- a/batch-submitter/drivers/sequencer/cached_tx.go +++ /dev/null @@ -1,37 +0,0 @@ -package sequencer - -import ( - "bytes" - "fmt" - - l2types "github.com/mantlenetworkio/mantle/l2geth/core/types" -) - -type CachedTx struct { - tx *l2types.Transaction - rawTx []byte -} - -func NewCachedTx(tx *l2types.Transaction) *CachedTx { - var txBuf bytes.Buffer - if err := tx.EncodeRLP(&txBuf); err != nil { - panic(fmt.Sprintf("Unable to encode tx: %v", err)) - } - - return &CachedTx{ - tx: tx, - rawTx: txBuf.Bytes(), - } -} - -func (t *CachedTx) Tx() *l2types.Transaction { - return t.tx -} - -func (t *CachedTx) Size() int { - return len(t.rawTx) -} - -func (t *CachedTx) RawTx() []byte { - return t.rawTx -} diff --git a/batch-submitter/drivers/sequencer/driver.go b/batch-submitter/drivers/sequencer/driver.go index 775054be2..8d8a83f6b 100644 --- a/batch-submitter/drivers/sequencer/driver.go +++ b/batch-submitter/drivers/sequencer/driver.go @@ -7,6 +7,9 @@ import ( "math/big" "strings" + kms "cloud.google.com/go/kms/apiv1" + "google.golang.org/api/option" + "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -23,9 +26,6 @@ import ( "github.com/mantlenetworkio/mantle/bss-core/metrics" "github.com/mantlenetworkio/mantle/bss-core/txmgr" l2ethclient "github.com/mantlenetworkio/mantle/l2geth/ethclient" - - kms "cloud.google.com/go/kms/apiv1" - "google.golang.org/api/option" ) const ( @@ -241,11 +241,10 @@ func (d *Driver) CraftBatchTx( if err != nil { return nil, err } - // For each sequencer transaction, update our running total with the // size of the transaction. batchElement := BatchElementFromBlock(block) - if batchElement.IsSequencerTx() { + if batchElement.IsSequencerTx { numSequencedTxs += 1 } else { numSubsequentQueueTxs += 1 @@ -257,12 +256,24 @@ func (d *Driver) CraftBatchTx( } blocksLen := numSequencedTxs + numSubsequentQueueTxs shouldStartAt := start.Uint64() + for { - batchParams, err := GenSequencerBatchParams( - shouldStartAt, d.cfg.BlockOffset, uint64(blocksLen), lastTimestamp, - lastBlockNumber, uint64(numSequencedTxs), uint64(numSubsequentQueueTxs)) - if err != nil { - return nil, err + var ( + contexts []BatchContext + ) + + batchContext := BatchContext{ + NumSequencedTxs: uint64(numSequencedTxs), + NumSubsequentQueueTxs: uint64(numSubsequentQueueTxs), + Timestamp: lastTimestamp, + BlockNumber: lastBlockNumber, + } + + contexts = append(contexts, batchContext) + batchParams := &AppendSequencerBatchParams{ + ShouldStartAtElement: shouldStartAt - d.cfg.BlockOffset, + TotalElementsToAppend: uint64(blocksLen), + Contexts: contexts, } // Encode the batch arguments using the configured encoding type. diff --git a/batch-submitter/drivers/sequencer/encoding.go b/batch-submitter/drivers/sequencer/encoding.go index 2dcbdce55..1d98d8dc2 100644 --- a/batch-submitter/drivers/sequencer/encoding.go +++ b/batch-submitter/drivers/sequencer/encoding.go @@ -181,10 +181,6 @@ type AppendSequencerBatchParams struct { // tx windows in Txs and implicitly allow one to compute how many // (omitted) queued txs are in a given window. Contexts []BatchContext - - // Txs contains all sequencer txs that will be recorded in the L1 CTC - // contract. - Txs []*CachedTx } // WriteNoTxn encodes the AppendSequencerBatchParams using the following format: @@ -209,7 +205,7 @@ type AppendSequencerBatchParams struct { // // Note that writing to a bytes.Buffer cannot // error, so errors are ignored here -func (p *AppendSequencerBatchParams) WriteNoTxn( +func (p *AppendSequencerBatchParams) Write( w *bytes.Buffer, batchType BatchType, ) error { @@ -240,7 +236,7 @@ func (p *AppendSequencerBatchParams) Serialize( batchType BatchType, ) ([]byte, error) { var buf bytes.Buffer - if err := p.WriteNoTxn(&buf, batchType); err != nil { + if err := p.Write(&buf, batchType); err != nil { return nil, err } return buf.Bytes(), nil @@ -320,10 +316,10 @@ func (p *AppendSequencerBatchParams) Read(r io.Reader) error { // encoded object. Silence the error and return success if // the batch is well formed. if err == io.EOF { - if len(p.Contexts) == 0 && len(p.Txs) != 0 { + if len(p.Contexts) == 0 { return ErrMalformedBatch } - if len(p.Txs) == 0 && len(p.Contexts) != 0 { + if len(p.Contexts) != 0 { return ErrMalformedBatch } return closeReader() @@ -335,8 +331,6 @@ func (p *AppendSequencerBatchParams) Read(r io.Reader) error { if err := tx.DecodeRLP(l2rlp.NewStream(r, txLen)); err != nil { return err } - - p.Txs = append(p.Txs, NewCachedTx(tx)) } } diff --git a/batch-submitter/drivers/sequencer/encoding_test.go b/batch-submitter/drivers/sequencer/encoding_test.go index bb0577e5d..b5acc2502 100644 --- a/batch-submitter/drivers/sequencer/encoding_test.go +++ b/batch-submitter/drivers/sequencer/encoding_test.go @@ -11,8 +11,6 @@ import ( "testing" "github.com/mantlenetworkio/mantle/batch-submitter/drivers/sequencer" - l2types "github.com/mantlenetworkio/mantle/l2geth/core/types" - l2rlp "github.com/mantlenetworkio/mantle/l2geth/rlp" "github.com/stretchr/testify/require" ) @@ -67,7 +65,6 @@ type AppendSequencerBatchParamsTest struct { ShouldStartAtElement uint64 `json:"should_start_at_element"` TotalElementsToAppend uint64 `json:"total_elements_to_append"` Contexts []sequencer.BatchContext `json:"contexts"` - Txs []string `json:"txs"` Error bool `json:"error"` } @@ -99,29 +96,12 @@ func TestAppendSequencerBatchParamsEncodeDecode(t *testing.T) { func testAppendSequencerBatchParamsEncodeDecode( t *testing.T, test AppendSequencerBatchParamsTest) { - - // Decode the expected transactions from their hex serialization. - var expTxs []*l2types.Transaction - for _, txHex := range test.Txs { - txBytes, err := hex.DecodeString(txHex) - require.Nil(t, err) - - rlpStream := l2rlp.NewStream(bytes.NewReader(txBytes), uint64(len(txBytes))) - - tx := new(l2types.Transaction) - err = tx.DecodeRLP(rlpStream) - require.Nil(t, err) - - expTxs = append(expTxs, tx) - } - // Construct the params we expect to decode, minus the txs. Those are // compared separately below. expParams := sequencer.AppendSequencerBatchParams{ ShouldStartAtElement: test.ShouldStartAtElement, TotalElementsToAppend: test.TotalElementsToAppend, Contexts: test.Contexts, - Txs: nil, } // Decode the batch from the test string. @@ -142,15 +122,11 @@ func testAppendSequencerBatchParamsEncodeDecode( // for spam prevention, so it is safe to ignore wrt. to serialization. // The decoded txs are reset on the the decoded params afterwards to // test the serialization. - decodedTxs := params.Txs - params.Txs = nil require.Equal(t, expParams, params) - compareTxs(t, expTxs, decodedTxs) - params.Txs = decodedTxs // Finally, encode the decoded object and assert it matches the original // hex string. - paramsBytes, err := params.Serialize(sequencer.BatchTypeLegacy, nil, nil) + paramsBytes, err := params.Serialize(sequencer.BatchTypeZlib) // Return early when testing error cases, no need to reserialize again if test.Error { @@ -162,7 +138,7 @@ func testAppendSequencerBatchParamsEncodeDecode( require.Equal(t, test.HexEncoding, hex.EncodeToString(paramsBytes)) // Serialize the batches in compressed form - compressedParamsBytes, err := params.Serialize(sequencer.BatchTypeZlib, nil, nil) + compressedParamsBytes, err := params.Serialize(sequencer.BatchTypeZlib) require.Nil(t, err) // Deserialize the compressed batch @@ -170,22 +146,7 @@ func testAppendSequencerBatchParamsEncodeDecode( err = paramsCompressed.Read(bytes.NewReader(compressedParamsBytes)) require.Nil(t, err) - decompressedTxs := paramsCompressed.Txs - paramsCompressed.Txs = nil - require.Equal(t, expParams, paramsCompressed) - compareTxs(t, expTxs, decompressedTxs) - paramsCompressed.Txs = decompressedTxs -} - -// compareTxs compares a list of two transactions, testing each pair by tx hash. -// This is used rather than require.Equal, since there `time` metadata on the -// decoded tx and the expected tx will differ, and can't be modified/ignored. -func compareTxs(t *testing.T, a []*l2types.Transaction, b []*sequencer.CachedTx) { - require.Equal(t, len(a), len(b)) - for i, txA := range a { - require.Equal(t, txA.Hash(), b[i].Tx().Hash()) - } } // TestMarkerContext asserts that each batch type returns the correct marker diff --git a/datalayr b/datalayr index fd219c089..b8238253d 160000 --- a/datalayr +++ b/datalayr @@ -1 +1 @@ -Subproject commit fd219c0894b90c600328805c744aa81dba6fca68 +Subproject commit b8238253db6903f12da0453806c3d86071fd7fe0 From 5d987cd69028196934fb9983f6caaee4f512edde Mon Sep 17 00:00:00 2001 From: HXHke Date: Thu, 14 Sep 2023 13:37:00 +0800 Subject: [PATCH 05/14] add some batch metric --- batch-submitter/drivers/sequencer/driver.go | 5 +++ bss-core/metrics/interface.go | 12 ++++++ bss-core/metrics/metrics.go | 48 +++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/batch-submitter/drivers/sequencer/driver.go b/batch-submitter/drivers/sequencer/driver.go index 8d8a83f6b..0c69a16d2 100644 --- a/batch-submitter/drivers/sequencer/driver.go +++ b/batch-submitter/drivers/sequencer/driver.go @@ -269,6 +269,11 @@ func (d *Driver) CraftBatchTx( BlockNumber: lastBlockNumber, } + d.metrics.BatchNumSequencedTxs().Set(float64(batchContext.NumSequencedTxs)) + d.metrics.BatchNumSubsequentQueueTxs().Set(float64(batchContext.NumSubsequentQueueTxs)) + d.metrics.BatchTimestamp().Set(float64(batchContext.Timestamp)) + d.metrics.BatchBlockNumber().Set(float64(batchContext.BlockNumber)) + contexts = append(contexts, batchContext) batchParams := &AppendSequencerBatchParams{ ShouldStartAtElement: shouldStartAt - d.cfg.BlockOffset, diff --git a/bss-core/metrics/interface.go b/bss-core/metrics/interface.go index 9e3ab8e93..8a893b707 100644 --- a/bss-core/metrics/interface.go +++ b/bss-core/metrics/interface.go @@ -41,4 +41,16 @@ type Metrics interface { // SccRollupTimeDuration state root rollup time duration SccRollupTimeDuration() prometheus.Gauge + + // BatchNumSequencedTxs Number of batch sequenced transactions + BatchNumSequencedTxs() prometheus.Gauge + + // BatchNumSubsequentQueueTxs Number of batch subsequent queue transactions + BatchNumSubsequentQueueTxs() prometheus.Gauge + + // BatchTimestamp Timestamp of batch + BatchTimestamp() prometheus.Gauge + + // BatchBlockNumber Block number of batch + BatchBlockNumber() prometheus.Gauge } diff --git a/bss-core/metrics/metrics.go b/bss-core/metrics/metrics.go index 1508249fa..04fe069f2 100644 --- a/bss-core/metrics/metrics.go +++ b/bss-core/metrics/metrics.go @@ -53,6 +53,14 @@ type Base struct { // sccRollupTimeDuration state root rollup time duration sccRollupTimeDuration prometheus.Gauge + + batchNumSequencedTxs prometheus.Gauge + + batchNumSubsequentQueueTxs prometheus.Gauge + + batchTimestamp prometheus.Gauge + + batchBlockNumber prometheus.Gauge } func NewBase(serviceName, subServiceName string) *Base { @@ -121,6 +129,26 @@ func NewBase(serviceName, subServiceName string) *Base { Help: "state root rollup time duration", Subsystem: subsystem, }), + batchNumSequencedTxs: promauto.NewGauge(prometheus.GaugeOpts{ + Name: "batch_num_sequenced_txs", + Help: "Number of batch sequenced transactions", + Subsystem: subsystem, + }), + batchNumSubsequentQueueTxs: promauto.NewGauge(prometheus.GaugeOpts{ + Name: "batch_num_subsequent_queue_txs", + Help: "Number of batch subsequent queue transactions", + Subsystem: subsystem, + }), + batchTimestamp: promauto.NewGauge(prometheus.GaugeOpts{ + Name: "batch_timestamp", + Help: "Timestamp of batch", + Subsystem: subsystem, + }), + batchBlockNumber: promauto.NewGauge(prometheus.GaugeOpts{ + Name: "batch_block_number", + Help: "Block number of batch", + Subsystem: subsystem, + }), } } @@ -187,6 +215,26 @@ func (b *Base) SccRollupTimeDuration() prometheus.Gauge { return b.sccRollupTimeDuration } +// BatchNumSequencedTxs Number of batch sequenced transactions +func (b *Base) BatchNumSequencedTxs() prometheus.Gauge { + return b.batchNumSequencedTxs +} + +// BatchNumSubsequentQueueTxs Number of batch subsequent queue transactions +func (b *Base) BatchNumSubsequentQueueTxs() prometheus.Gauge { + return b.batchNumSubsequentQueueTxs +} + +// BatchTimestamp Timestamp of batch +func (b *Base) BatchTimestamp() prometheus.Gauge { + return b.batchTimestamp +} + +// BatchBlockNumber Block number of batch +func (b *Base) BatchBlockNumber() prometheus.Gauge { + return b.batchBlockNumber +} + // MakeSubsystemName builds the subsystem name for a group of metrics, which // prometheus will use to prefix all metrics in the group. If two non-empty // strings are provided, they are joined with an underscore. If only one From ee48a8de973acfca03959f98fc7d8592e3e0a972 Mon Sep 17 00:00:00 2001 From: shellteo Date: Thu, 2 Nov 2023 15:59:08 +0800 Subject: [PATCH 06/14] add wstETH --- packages/sdk/src/utils/contracts.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/sdk/src/utils/contracts.ts b/packages/sdk/src/utils/contracts.ts index 5652a8b80..27e43e610 100644 --- a/packages/sdk/src/utils/contracts.ts +++ b/packages/sdk/src/utils/contracts.ts @@ -219,6 +219,13 @@ export const BRIDGE_ADAPTER_DATA: { l2Bridge: '0x467194771dAe2967Aef3ECbEDD3Bf9a310C76C65' as const, }, }, + [L2ChainID.MANTLE_TESTNET]: { + wstETH: { + Adapter: ERC20BridgeAdapter, + l1Bridge: '0x2fD573Ace456904709444d04AdCa189fB19e725a' as const, + l2Bridge: '0x08C2EE913D3cb544D182bCC7632cB0B382A2933e' as const, + }, + }, [L2ChainID.MANTLE_KOVAN]: { wstETH: { Adapter: ERC20BridgeAdapter, From da4444c931140f5b8379333cedbf9b352b92be3f Mon Sep 17 00:00:00 2001 From: LeoZhang Date: Tue, 31 Oct 2023 17:41:47 +0800 Subject: [PATCH 07/14] [R4R]-[l2geth]feat: add data in eth_calls response when revert --- .../accounts/abi/bind/backends/simulated.go | 8 +- l2geth/core/state_processor.go | 6 +- l2geth/core/state_transition.go | 52 ++++++-- l2geth/core/vm/evm.go | 10 +- l2geth/core/vm/instructions.go | 14 +-- l2geth/core/vm/interpreter.go | 4 +- l2geth/eth/api_tracer.go | 6 +- l2geth/graphql/graphql.go | 16 +-- l2geth/internal/ethapi/api.go | 114 ++++++++++++------ l2geth/rpc/client.go | 8 +- l2geth/rpc/handler.go | 21 ++-- l2geth/rpc/json.go | 8 ++ l2geth/rpc/types.go | 6 + 13 files changed, 197 insertions(+), 76 deletions(-) diff --git a/l2geth/accounts/abi/bind/backends/simulated.go b/l2geth/accounts/abi/bind/backends/simulated.go index 7b204cfe2..dd3c989e9 100644 --- a/l2geth/accounts/abi/bind/backends/simulated.go +++ b/l2geth/accounts/abi/bind/backends/simulated.go @@ -442,7 +442,13 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM log.Error("new state transition fail", "err", err) return nil, 0, false, err } - return stateTransition.TransitionDb() + ret, usedGas, vmerr, err := stateTransition.TransitionDb() + failed := false + if vmerr != nil { + failed = true + } + + return ret, usedGas, failed, err } // SendTransaction updates the pending block to include the given transaction. diff --git a/l2geth/core/state_processor.go b/l2geth/core/state_processor.go index 22cc6b3d1..8b90a7636 100644 --- a/l2geth/core/state_processor.go +++ b/l2geth/core/state_processor.go @@ -111,10 +111,14 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo } // Apply the transaction to the current state (included in the env) - _, gas, failed, err := ApplyMessage(vmenv, msg, gp) + _, gas, vmerr, err := ApplyMessage(vmenv, msg, gp) if err != nil { return nil, err } + failed := false + if vmerr != nil { + failed = true + } // Update the state with pending changes var root []byte diff --git a/l2geth/core/state_transition.go b/l2geth/core/state_transition.go index a4074755e..26ae69b9b 100644 --- a/l2geth/core/state_transition.go +++ b/l2geth/core/state_transition.go @@ -89,6 +89,42 @@ type Message interface { QueueOrigin() types.QueueOrigin } +// ExecutionResult includes all output after executing given evm +// message no matter the execution itself is successful or not. +type ExecutionResult struct { + UsedGas uint64 // Total used gas but include the refunded gas + Err error // Any error encountered during the execution(listed in core/vm/errors.go) + ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode) +} + +// Unwrap returns the internal evm error which allows us for further +// analysis outside. +func (result *ExecutionResult) Unwrap() error { + return result.Err +} + +// Failed returns the indicator whether the execution is successful or not +func (result *ExecutionResult) Failed() bool { return result.Err != nil } + +// Return is a helper function to help caller distinguish between revert reason +// and function return. Return returns the data after execution if no error occurs. +func (result *ExecutionResult) Return() []byte { + if result.Err != nil { + return nil + } + return common.CopyBytes(result.ReturnData) +} + +// Revert returns the concrete revert reason if the execution is aborted by `REVERT` +// opcode. Note the reason can be nil if no data supplied with revert opcode. +func (result *ExecutionResult) Revert() []byte { + log.Info("ExecutionResult", "result.Err", result.Err) + if result.Err != vm.ErrExecutionReverted { + return nil + } + return common.CopyBytes(result.ReturnData) +} + // IntrinsicGas computes the 'intrinsic gas' for a message with the given data. func IntrinsicGas(data []byte, contractCreation, isHomestead bool, isEIP2028 bool) (uint64, error) { // Set the starting gas for the raw transaction @@ -176,11 +212,11 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) (*StateTransition // the gas used (which includes gas refunds) and an error if it failed. An error always // indicates a core error meaning that the message would always fail for that particular // state and would never be accepted within a block. -func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool) ([]byte, uint64, bool, error) { +func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool) ([]byte, uint64, error, error) { stateTransition, err := NewStateTransition(evm, msg, gp) if err != nil { log.Error("apply message fall", "err", err) - return nil, 0, false, err + return nil, 0, err, err } return stateTransition.TransitionDb() } @@ -249,7 +285,7 @@ func (st *StateTransition) preCheck() error { // TransitionDb will transition the state by applying the current message and // returning the result including the used gas. It returns an error if failed. // An error indicates a consensus issue. -func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bool, err error) { +func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, vmerr error, err error) { if err = st.preCheck(); err != nil { return } @@ -262,10 +298,10 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo // Pay intrinsic gas gas, err := IntrinsicGas(st.data, contractCreation, homestead, istanbul) if err != nil { - return nil, 0, false, err + return nil, 0, err, err } if err = st.useGas(gas); err != nil { - return nil, 0, false, err + return nil, 0, err, err } var ( @@ -273,7 +309,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo // vm errors do not effect consensus and are therefore // not assigned to err, except for insufficient balance // error. - vmerr error + // vmerr error ) // The access list gets created here @@ -295,7 +331,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo // sufficient balance to make the transfer happen. The first // balance transfer may never fail. if vmerr == vm.ErrInsufficientBalance { - return nil, 0, false, vmerr + return nil, 0, vmerr, vmerr } } st.refundGas() @@ -311,7 +347,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo st.state.AddBalance(evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice)) } - return ret, st.gasUsed(), vmerr != nil, err + return ret, st.gasUsed(), vmerr, err } func (st *StateTransition) refundGas() { diff --git a/l2geth/core/vm/evm.go b/l2geth/core/vm/evm.go index 22cbef13e..afbf70828 100644 --- a/l2geth/core/vm/evm.go +++ b/l2geth/core/vm/evm.go @@ -281,7 +281,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas // when we're in homestead this also counts for code storage gas errors. if err != nil { evm.StateDB.RevertToSnapshot(snapshot) - if err != errExecutionReverted { + if err != ErrExecutionReverted { contract.UseGas(contract.Gas) } } @@ -321,7 +321,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, ret, err = run(evm, contract, input, false) if err != nil { evm.StateDB.RevertToSnapshot(snapshot) - if err != errExecutionReverted { + if err != ErrExecutionReverted { contract.UseGas(contract.Gas) } } @@ -354,7 +354,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by ret, err = run(evm, contract, input, false) if err != nil { evm.StateDB.RevertToSnapshot(snapshot) - if err != errExecutionReverted { + if err != ErrExecutionReverted { contract.UseGas(contract.Gas) } } @@ -395,7 +395,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte ret, err = run(evm, contract, input, true) if err != nil { evm.StateDB.RevertToSnapshot(snapshot) - if err != errExecutionReverted { + if err != ErrExecutionReverted { contract.UseGas(contract.Gas) } } @@ -483,7 +483,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, // when we're in homestead this also counts for code storage gas errors. if maxCodeSizeExceeded || (err != nil && (evm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas)) { evm.StateDB.RevertToSnapshot(snapshot) - if err != errExecutionReverted { + if err != ErrExecutionReverted { contract.UseGas(contract.Gas) } } diff --git a/l2geth/core/vm/instructions.go b/l2geth/core/vm/instructions.go index 1e68f58c0..923aa8f2f 100644 --- a/l2geth/core/vm/instructions.go +++ b/l2geth/core/vm/instructions.go @@ -34,7 +34,7 @@ var ( ErrWriteProtection = errors.New("evm: write protection") ErrNonceUintOverflow = errors.New("nonce uint64 overflow") errReturnDataOutOfBounds = errors.New("evm: return data out of bounds") - errExecutionReverted = errors.New("evm: execution reverted") + ErrExecutionReverted = errors.New("evm: execution reverted") errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded") errInvalidJump = errors.New("evm: invalid jump destination") ) @@ -721,7 +721,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memor contract.Gas += returnGas interpreter.intPool.put(value, offset, size) - if suberr == errExecutionReverted { + if suberr == ErrExecutionReverted { return res, nil } return nil, nil @@ -749,7 +749,7 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memo contract.Gas += returnGas interpreter.intPool.put(endowment, offset, size, salt) - if suberr == errExecutionReverted { + if suberr == ErrExecutionReverted { return res, nil } return nil, nil @@ -775,7 +775,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory } else { stack.push(interpreter.intPool.get().SetUint64(1)) } - if err == nil || err == errExecutionReverted { + if err == nil || err == ErrExecutionReverted { ret = common.CopyBytes(ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } @@ -805,7 +805,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, mem } else { stack.push(interpreter.intPool.get().SetUint64(1)) } - if err == nil || err == errExecutionReverted { + if err == nil || err == ErrExecutionReverted { ret = common.CopyBytes(ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } @@ -831,7 +831,7 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, } else { stack.push(interpreter.intPool.get().SetUint64(1)) } - if err == nil || err == errExecutionReverted { + if err == nil || err == ErrExecutionReverted { ret = common.CopyBytes(ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } @@ -857,7 +857,7 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, m } else { stack.push(interpreter.intPool.get().SetUint64(1)) } - if err == nil || err == errExecutionReverted { + if err == nil || err == ErrExecutionReverted { ret = common.CopyBytes(ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } diff --git a/l2geth/core/vm/interpreter.go b/l2geth/core/vm/interpreter.go index c5f913d9d..076707601 100644 --- a/l2geth/core/vm/interpreter.go +++ b/l2geth/core/vm/interpreter.go @@ -135,7 +135,7 @@ func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter { // // It's important to note that any errors returned by the interpreter should be // considered a revert-and-consume-all-gas operation except for -// errExecutionReverted which means revert-and-keep-gas-left. +// ErrExecutionReverted which means revert-and-keep-gas-left. func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (ret []byte, err error) { if in.intPool == nil { in.intPool = poolOfIntPools.get() @@ -289,7 +289,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) ( case err != nil: return nil, err case operation.reverts: - return res, errExecutionReverted + return res, ErrExecutionReverted case operation.halts: return res, nil case !operation.jumps: diff --git a/l2geth/eth/api_tracer.go b/l2geth/eth/api_tracer.go index 80c0f6619..d6c3fb5f5 100644 --- a/l2geth/eth/api_tracer.go +++ b/l2geth/eth/api_tracer.go @@ -931,10 +931,14 @@ func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, v // Run the transaction with tracing enabled. vmenv := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{Debug: true, Tracer: tracer}) - ret, gas, failed, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())) + ret, gas, vmerr, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())) if err != nil { return nil, fmt.Errorf("tracing failed: %v", err) } + failed := false + if vmerr != nil { + failed = true + } // Depending on the tracer type, format and return the output switch tracer := tracer.(type) { case *vm.StructLogger: diff --git a/l2geth/graphql/graphql.go b/l2geth/graphql/graphql.go index f9fc10d32..c72ef2fa5 100644 --- a/l2geth/graphql/graphql.go +++ b/l2geth/graphql/graphql.go @@ -776,14 +776,14 @@ func (b *Block) Call(ctx context.Context, args struct { return nil, err } } - result, gas, failed, err := ethapi.DoCall(ctx, b.backend, args.Data, *b.numberOrHash, nil, &vm.Config{}, 5*time.Second, b.backend.RPCGasCap()) + result, err := ethapi.DoCall(ctx, b.backend, args.Data, *b.numberOrHash, nil, &vm.Config{}, 5*time.Second, b.backend.RPCGasCap()) status := hexutil.Uint64(1) - if failed { + if result.Failed() { status = 0 } return &CallResult{ - data: hexutil.Bytes(result), - gasUsed: hexutil.Uint64(gas), + data: result.ReturnData, + gasUsed: hexutil.Uint64(result.UsedGas), status: status, }, err } @@ -842,14 +842,14 @@ func (p *Pending) Call(ctx context.Context, args struct { Data ethapi.CallArgs }) (*CallResult, error) { pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) - result, gas, failed, err := ethapi.DoCall(ctx, p.backend, args.Data, pendingBlockNr, nil, &vm.Config{}, 5*time.Second, p.backend.RPCGasCap()) + result, err := ethapi.DoCall(ctx, p.backend, args.Data, pendingBlockNr, nil, &vm.Config{}, 5*time.Second, p.backend.RPCGasCap()) status := hexutil.Uint64(1) - if failed { + if result.Failed() { status = 0 } return &CallResult{ - data: hexutil.Bytes(result), - gasUsed: hexutil.Uint64(gas), + data: result.ReturnData, + gasUsed: hexutil.Uint64(result.UsedGas), status: status, }, err } diff --git a/l2geth/internal/ethapi/api.go b/l2geth/internal/ethapi/api.go index 0995c2c0e..485aac594 100644 --- a/l2geth/internal/ethapi/api.go +++ b/l2geth/internal/ethapi/api.go @@ -830,12 +830,12 @@ type Account struct { StateDiff *map[common.Hash]common.Hash `json:"stateDiff"` } -func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides map[common.Address]Account, vmCfg *vm.Config, timeout time.Duration, globalGasCap *big.Int) ([]byte, uint64, bool, error) { +func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides map[common.Address]Account, vmCfg *vm.Config, timeout time.Duration, globalGasCap *big.Int) (*core.ExecutionResult, error) { defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now()) state, header, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash) if state == nil || err != nil { - return nil, 0, false, err + return nil, err } // Set sender address or use a default if none specified var addr common.Address @@ -865,7 +865,7 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo state.SetBalance(addr, (*big.Int)(*account.Balance)) } if account.State != nil && account.StateDiff != nil { - return nil, 0, false, fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex()) + return nil, fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex()) } // Replace entire state if caller requires. if account.State != nil { @@ -910,13 +910,13 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo if rcfg.UsingBVM { block, err := b.BlockByNumber(ctx, rpc.BlockNumber(header.Number.Uint64())) if err != nil { - return nil, 0, false, err + return nil, err } if block != nil { txs := block.Transactions() if header.Number.Uint64() != 0 { if len(txs) != 1 { - return nil, 0, false, fmt.Errorf("block %d has more than 1 transaction", header.Number.Uint64()) + return nil, fmt.Errorf("block %d has more than 1 transaction", header.Number.Uint64()) } tx := txs[0] blockNumber = tx.L1BlockNumber() @@ -943,7 +943,7 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo // Get a new instance of the EVM. evm, vmError, err := b.GetEVM(ctx, msg, state, header, vmCfg) if err != nil { - return nil, 0, false, err + return nil, err } // Wait for the context to be done and cancel the evm. Even if the // EVM has finished, cancelling may be done (repeatedly) @@ -955,15 +955,54 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo // Setup the gas pool (also for unmetered requests) // and apply the message. gp := new(core.GasPool).AddGas(math.MaxUint64) - res, gas, failed, err := core.ApplyMessage(evm, msg, gp) + res, gas, vmerr, err := core.ApplyMessage(evm, msg, gp) + result := &core.ExecutionResult{ + UsedGas: gas, + Err: vmerr, + ReturnData: res, + } if err := vmError(); err != nil { - return nil, 0, false, err + return nil, err } // If the timer caused an abort, return an appropriate error message if evm.Cancelled() { - return nil, 0, false, fmt.Errorf("execution aborted (timeout = %v)", timeout) + return nil, fmt.Errorf("execution aborted (timeout = %v)", timeout) + } + if err != nil { + return result, fmt.Errorf("err: %w (supplied gas %d)", err, msg.Gas()) } - return res, gas, failed, err + + return result, err +} + +func newRevertError(result *core.ExecutionResult) *revertError { + reason, errUnpack := abi.UnpackRevert(result.Revert()) + err := errors.New("execution reverted") + if errUnpack == nil { + err = fmt.Errorf("execution reverted: %v", reason) + } + return &revertError{ + error: err, + reason: hexutil.Encode(result.Revert()), + } +} + +// revertError is an API error that encompasses an EVM revertal with JSON error +// code and a binary data blob. +type revertError struct { + error + reason string // revert reason hex encoded +} + +// ErrorCode returns the JSON error code for a revertal. +// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal +func (e *revertError) ErrorCode() int { + return 3 +} + +// ErrorData returns the hex encoded revert reason. +func (e *revertError) ErrorData() interface{} { + return e.reason } // Call executes the given transaction on the state for the given block number. @@ -977,19 +1016,15 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNrOr if overrides != nil { accounts = *overrides } - result, _, failed, err := DoCall(ctx, s.b, args, blockNrOrHash, accounts, &vm.Config{}, 5*time.Second, s.b.RPCGasCap()) + result, err := DoCall(ctx, s.b, args, blockNrOrHash, accounts, &vm.Config{}, 5*time.Second, s.b.RPCGasCap()) if err != nil { return nil, err } - if failed { - reason, errUnpack := abi.UnpackRevert(result) - err := errors.New("execution reverted") - if errUnpack == nil { - err = fmt.Errorf("execution reverted: %v", reason) - } - return (hexutil.Bytes)(result), err + // If the result contains a revert reason, try to unpack and return it. + if len(result.Revert()) > 0 { + return nil, newRevertError(result) } - return (hexutil.Bytes)(result), err + return result.Return(), result.Err } // Mantle note: The gasPrice in Mantle is modified to always return 1 gwei. We @@ -1035,21 +1070,30 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash args.From = &common.Address{} } // Create a helper to check if a gas allowance results in an executable transaction - executable := func(gas uint64) (bool, []byte) { + executable := func(gas uint64) (bool, *core.ExecutionResult, error) { args.Gas = (*hexutil.Uint64)(&gas) - res, _, failed, err := DoCall(ctx, b, args, blockNrOrHash, nil, &vm.Config{}, 0, gasCap) - if err != nil || failed { - return false, res + result, err := DoCall(ctx, b, args, blockNrOrHash, nil, &vm.Config{}, 0, gasCap) + if err != nil { + if errors.Is(err, core.ErrIntrinsicGas) { + return true, nil, nil // Special case, raise gas limit + } + return true, nil, err } - return true, res + return result.Failed(), result, nil } // Execute the binary search and hone in on an executable gas limit for lo+1 < hi { mid := (hi + lo) / 2 - ok, _ := executable(mid) + failed, _, err := executable(mid) - if !ok { + // If the error is not nil(consensus error), it means the provided message + // call or transaction will never be accepted no matter how much gas it is + // assigned. Return the error directly, don't struggle any more. + if err != nil { + return 0, err + } + if failed { lo = mid } else { hi = mid @@ -1057,16 +1101,18 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash } // Reject the transaction as invalid if it still fails at the highest allowance if hi == cap { - ok, res := executable(hi) - if !ok { - if len(res) >= 4 && bytes.Equal(res[:4], abi.RevertSelector) { - reason, errUnpack := abi.UnpackRevert(res) - err := errors.New("execution reverted") - if errUnpack == nil { - err = fmt.Errorf("execution reverted: %v", reason) + failed, result, err := executable(hi) + if err != nil { + return 0, err + } + if failed { + if result != nil && result.Err != vm.ErrOutOfGas { + if len(result.Revert()) > 0 { + return 0, newRevertError(result) } - return 0, err + return 0, result.Err } + // Otherwise, the specified gas cap is too low return 0, fmt.Errorf("gas required exceeds allowance (%d)", cap) } } diff --git a/l2geth/rpc/client.go b/l2geth/rpc/client.go index 15e413e61..3411731d6 100644 --- a/l2geth/rpc/client.go +++ b/l2geth/rpc/client.go @@ -276,6 +276,9 @@ func (c *Client) Call(result interface{}, method string, args ...interface{}) er // The result must be a pointer so that package json can unmarshal into it. You // can also pass nil, in which case the result is ignored. func (c *Client) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error { + if result != nil && reflect.TypeOf(result).Kind() != reflect.Ptr { + return fmt.Errorf("call result parameter must be pointer or nil interface: %v", result) + } msg, err := c.newMessage(method, args...) if err != nil { return err @@ -300,7 +303,10 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str case len(resp.Result) == 0: return ErrNoResult default: - return json.Unmarshal(resp.Result, &result) + if result == nil { + return nil + } + return json.Unmarshal(resp.Result, result) } } diff --git a/l2geth/rpc/handler.go b/l2geth/rpc/handler.go index 4b87577d4..fae95c29e 100644 --- a/l2geth/rpc/handler.go +++ b/l2geth/rpc/handler.go @@ -34,21 +34,20 @@ import ( // // The entry points for incoming messages are: // -// h.handleMsg(message) -// h.handleBatch(message) +// h.handleMsg(message) +// h.handleBatch(message) // // Outgoing calls use the requestOp struct. Register the request before sending it // on the connection: // -// op := &requestOp{ids: ...} -// h.addRequestOp(op) +// op := &requestOp{ids: ...} +// h.addRequestOp(op) // // Now send the request, then wait for the reply to be delivered through handleMsg: // -// if err := op.wait(...); err != nil { -// h.removeRequestOp(op) // timeout, etc. -// } -// +// if err := op.wait(...); err != nil { +// h.removeRequestOp(op) // timeout, etc. +// } type handler struct { reg *serviceRegistry unsubscribeCb *callback @@ -296,7 +295,13 @@ func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMess return nil case msg.isCall(): resp := h.handleCall(ctx, msg) + var ctx []interface{} + ctx = append(ctx, "reqid", idForLog{msg.ID}, "duration", time.Since(start)) if resp.Error != nil { + ctx = append(ctx, "err", resp.Error.Message) + if resp.Error.Data != nil { + ctx = append(ctx, "errdata", resp.Error.Data) + } h.log.Warn("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start), "err", resp.Error.Message) } else { h.log.Debug("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start)) diff --git a/l2geth/rpc/json.go b/l2geth/rpc/json.go index 61631a3d7..3be5d55f4 100644 --- a/l2geth/rpc/json.go +++ b/l2geth/rpc/json.go @@ -115,6 +115,10 @@ func errorMessage(err error) *jsonrpcMessage { if ok { msg.Error.Code = ec.ErrorCode() } + de, ok := err.(DataError) + if ok { + msg.Error.Data = de.ErrorData() + } return msg } @@ -135,6 +139,10 @@ func (err *jsonError) ErrorCode() int { return err.Code } +func (err *jsonError) ErrorData() interface{} { + return err.Data +} + // Conn is a subset of the methods of net.Conn which are sufficient for ServerCodec. type Conn interface { io.ReadWriteCloser diff --git a/l2geth/rpc/types.go b/l2geth/rpc/types.go index b19f96e0b..d3f37c3c4 100644 --- a/l2geth/rpc/types.go +++ b/l2geth/rpc/types.go @@ -41,6 +41,12 @@ type Error interface { ErrorCode() int // returns the code } +// A DataError contains some data in addition to the error message. +type DataError interface { + Error() string // returns the message + ErrorData() interface{} // returns the error data +} + // ServerCodec implements reading, parsing and writing RPC messages for the server side of // a RPC session. Implementations must be go-routine safe since the codec can be called in // multiple go-routines concurrently. From d48929559984b24cae0c25035a8726d8d1ac8817 Mon Sep 17 00:00:00 2001 From: LeoZhang Date: Mon, 6 Nov 2023 15:02:08 +0800 Subject: [PATCH 08/14] [R4R]-[l2geth]feat: add data in eth_calls response when revert --- l2geth/core/state_transition.go | 1 - 1 file changed, 1 deletion(-) diff --git a/l2geth/core/state_transition.go b/l2geth/core/state_transition.go index 26ae69b9b..b0d51d764 100644 --- a/l2geth/core/state_transition.go +++ b/l2geth/core/state_transition.go @@ -118,7 +118,6 @@ func (result *ExecutionResult) Return() []byte { // Revert returns the concrete revert reason if the execution is aborted by `REVERT` // opcode. Note the reason can be nil if no data supplied with revert opcode. func (result *ExecutionResult) Revert() []byte { - log.Info("ExecutionResult", "result.Err", result.Err) if result.Err != vm.ErrExecutionReverted { return nil } From e3e224068eef8027ffa7262ebbef3b3f86d5457c Mon Sep 17 00:00:00 2001 From: idyllsss <137248235+idyllsss@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:34:06 +0800 Subject: [PATCH 09/14] Update api.go --- l2geth/internal/ethapi/api.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/l2geth/internal/ethapi/api.go b/l2geth/internal/ethapi/api.go index 485aac594..2564beea3 100644 --- a/l2geth/internal/ethapi/api.go +++ b/l2geth/internal/ethapi/api.go @@ -1122,9 +1122,12 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash // EstimateGas returns an estimate of the amount of gas needed to execute the // given transaction against the current pending block. This is modified to // encode the fee in wei as gas price is always 1 -func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (hexutil.Uint64, error) { - blockNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) - return DoEstimateGas(ctx, s.b, args, blockNrOrHash, s.b.RPCGasCap()) +func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs, blockNrOrHash *rpc.BlockNumberOrHash) (hexutil.Uint64, error) { + bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber) + if blockNrOrHash != nil { + bNrOrHash = *blockNrOrHash + } + return DoEstimateGas(ctx, s.b, args, bNrOrHash, s.b.RPCGasCap()) } // ExecutionResult groups all structured logs emitted by the EVM From e2426b156d5f244904896f5fd2a40239c1bf01a3 Mon Sep 17 00:00:00 2001 From: idyllsss <137248235+idyllsss@users.noreply.github.com> Date: Wed, 29 Nov 2023 10:54:22 +0800 Subject: [PATCH 10/14] bug fix about estimateGas rpc parameters (#1379) --- l2geth/core/state_transition.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/l2geth/core/state_transition.go b/l2geth/core/state_transition.go index b0d51d764..41af69993 100644 --- a/l2geth/core/state_transition.go +++ b/l2geth/core/state_transition.go @@ -18,6 +18,7 @@ package core import ( "errors" + "fmt" "math" "math/big" @@ -299,6 +300,10 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, vmerr err if err != nil { return nil, 0, err, err } + if st.gas < gas { + return nil, 0, nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gas, gas) + } + if err = st.useGas(gas); err != nil { return nil, 0, err, err } From 13f56e570caef2e6df931b192c0ff958ed8092bd Mon Sep 17 00:00:00 2001 From: idyllsss <137248235+idyllsss@users.noreply.github.com> Date: Tue, 19 Dec 2023 14:51:42 +0800 Subject: [PATCH 11/14] [l2geth]: RPC support query block with safe and finalized (#1385) --- l2geth/rpc/types.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/l2geth/rpc/types.go b/l2geth/rpc/types.go index d3f37c3c4..3aaaf7258 100644 --- a/l2geth/rpc/types.go +++ b/l2geth/rpc/types.go @@ -96,6 +96,12 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error { case "pending": *bn = PendingBlockNumber return nil + case "finalized": + *bn = LatestBlockNumber + return nil + case "safe": + *bn = LatestBlockNumber + return nil } blckNum, err := hexutil.DecodeUint64(input) From a4cea8fbf030a42b6554f764b6235560b75d580f Mon Sep 17 00:00:00 2001 From: shellteo Date: Fri, 22 Dec 2023 17:59:34 +0800 Subject: [PATCH 12/14] add mainnet wstETH --- packages/sdk/src/utils/contracts.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/sdk/src/utils/contracts.ts b/packages/sdk/src/utils/contracts.ts index 27e43e610..7ca81d50b 100644 --- a/packages/sdk/src/utils/contracts.ts +++ b/packages/sdk/src/utils/contracts.ts @@ -218,6 +218,11 @@ export const BRIDGE_ADAPTER_DATA: { l1Bridge: '0x10E6593CDda8c58a1d0f14C5164B376352a55f2F' as const, l2Bridge: '0x467194771dAe2967Aef3ECbEDD3Bf9a310C76C65' as const, }, + wstETH: { + Adapter: ERC20BridgeAdapter, + l1Bridge: '0x2D001d79E5aF5F65a939781FE228B267a8Ed468B' as const, + l2Bridge: '0x9c46560D6209743968cC24150893631A39AfDe4d' as const, + }, }, [L2ChainID.MANTLE_TESTNET]: { wstETH: { From 59fe2c63e7ef2ac6a7341ca85e45ea313a0a978a Mon Sep 17 00:00:00 2001 From: shellteo Date: Fri, 22 Dec 2023 18:04:59 +0800 Subject: [PATCH 13/14] upgrade sdk to 0.2.3 --- packages/fault-detector/package.json | 2 +- packages/message-relayer/package.json | 2 +- packages/sdk/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/fault-detector/package.json b/packages/fault-detector/package.json index 2cde9dd1c..06a1f06e8 100644 --- a/packages/fault-detector/package.json +++ b/packages/fault-detector/package.json @@ -50,7 +50,7 @@ "@mantleio/common-ts": "0.1.0", "@mantleio/contracts": "0.2.1", "@mantleio/core-utils": "0.1.0", - "@mantleio/sdk": "0.2.2", + "@mantleio/sdk": "0.2.3", "@ethersproject/abstract-provider": "^5.6.1" } } diff --git a/packages/message-relayer/package.json b/packages/message-relayer/package.json index 83e957d26..dfc8875b3 100644 --- a/packages/message-relayer/package.json +++ b/packages/message-relayer/package.json @@ -33,7 +33,7 @@ "dependencies": { "@mantleio/common-ts": "0.1.0", "@mantleio/core-utils": "0.1.0", - "@mantleio/sdk": "0.2.2", + "@mantleio/sdk": "0.2.3", "ethers": "^5.6.8" }, "devDependencies": { diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 9504d6f3d..e9466244d 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@mantleio/sdk", - "version": "0.2.2", + "version": "0.2.3", "description": "[Mantle] Tools for working with Mantle", "main": "dist/index", "types": "dist/index", From ed08415ada76cc85a4d26582c470a0c0d3b1cb42 Mon Sep 17 00:00:00 2001 From: shidaxi Date: Fri, 22 Dec 2023 21:49:10 +0800 Subject: [PATCH 14/14] DTL response null result with 404 http status --- .../src/services/server/service.ts | 41 +- .../src/utils/http-code.ts | 356 ++++++++++++++++++ .../data-transport-layer/src/utils/index.ts | 1 + 3 files changed, 381 insertions(+), 17 deletions(-) create mode 100644 packages/data-transport-layer/src/utils/http-code.ts diff --git a/packages/data-transport-layer/src/services/server/service.ts b/packages/data-transport-layer/src/services/server/service.ts index 8644f6261..43aa16755 100644 --- a/packages/data-transport-layer/src/services/server/service.ts +++ b/packages/data-transport-layer/src/services/server/service.ts @@ -8,6 +8,7 @@ import { JsonRpcProvider } from '@ethersproject/providers' import { LevelUp } from 'levelup' import * as Sentry from '@sentry/node' import * as Tracing from '@sentry/tracing' +import {HttpCodes} from "../../utils"; /* Imports: Internal */ @@ -237,7 +238,13 @@ export class L1TransportServer extends BaseService { this.state.app[method](route, async (req, res) => { const start = Date.now() try { - const json = await handler(req, res) + let httpCode = HttpCodes.OK, json; + const result = await handler(req, res) + if(Array.isArray(result)) { + [json, httpCode] = result; + } else { + json = result; + } const elapsed = Date.now() - start this.logger.info('Served HTTP Request', { method: req.method, @@ -249,7 +256,7 @@ export class L1TransportServer extends BaseService { url: req.url, body: json, }) - return res.json(json) + return res.status(httpCode).json(json) } catch (e) { const elapsed = Date.now() - start this.logger.error('Failed HTTP Request', { @@ -498,7 +505,7 @@ export class L1TransportServer extends BaseService { this._registerRoute( 'get', '/transaction/index/:index', - async (req): Promise => { + async (req): Promise<[TransactionResponse, HttpCodes]> => { const backend = req.query.backend || this.options.defaultBackend let transaction = null @@ -518,20 +525,20 @@ export class L1TransportServer extends BaseService { } if (transaction === null) { - return { + return [{ transaction: null, batch: null, - } + }, HttpCodes.NOT_FOUND] } const batch = await this.state.db.getTransactionBatchByIndex( transaction.batchIndex ) - return { + return [{ transaction, batch, - } + }, HttpCodes.OK] } ) @@ -565,16 +572,16 @@ export class L1TransportServer extends BaseService { this._registerRoute( 'get', '/batch/transaction/index/:index', - async (req): Promise => { + async (req): Promise<[TransactionBatchResponse, HttpCodes]> => { const batch = await this.state.db.getTransactionBatchByIndex( BigNumber.from(req.params.index).toNumber() ) if (batch === null) { - return { + return [{ batch: null, transactions: [], - } + }, HttpCodes.NOT_FOUND] } const transactions = @@ -584,10 +591,10 @@ export class L1TransportServer extends BaseService { BigNumber.from(batch.size).toNumber() ) - return { + return [{ batch, transactions, - } + }, HttpCodes.OK] } ) @@ -936,21 +943,21 @@ export class L1TransportServer extends BaseService { this._registerRoute( 'get', '/da/transaction/index/:index', - async (req): Promise => { + async (req): Promise<[TransactionResponse, HttpCodes]> => { const transactionEntry = await this.state.db.getDaTransactionByIndex( BigNumber.from(req.params.index).toNumber() ) if (transactionEntry === null) { - return { + return [{ transaction: null, batch: null, - } + }, HttpCodes.NOT_FOUND] } - return { + return [{ transaction: transactionEntry, batch: null, - } + }, HttpCodes.OK] } ) diff --git a/packages/data-transport-layer/src/utils/http-code.ts b/packages/data-transport-layer/src/utils/http-code.ts new file mode 100644 index 000000000..5019b3587 --- /dev/null +++ b/packages/data-transport-layer/src/utils/http-code.ts @@ -0,0 +1,356 @@ +export enum HttpCodes { + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.2.1 + * + * This interim response indicates that everything so far is OK and that the client should continue with the request or ignore it if it is already finished. + */ + CONTINUE = 100, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.2.2 + * + * This code is sent in response to an Upgrade request header by the client, and indicates the protocol the server is switching too. + */ + SWITCHING_PROTOCOLS = 101, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.1 + * + * This code indicates that the server has received and is processing the request, but no response is available yet. + */ + PROCESSING = 102, + /** + * Official Documentation @ https://www.rfc-editor.org/rfc/rfc8297#page-3 + * + * This code indicates to the client that the server is likely to send a final response with the header fields included in the informational response. + */ + EARLY_HINTS = 103, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.1 + * + * The request has succeeded. The meaning of a success varies depending on the HTTP method: + * GET: The resource has been fetched and is transmitted in the message body. + * HEAD: The entity headers are in the message body. + * POST: The resource describing the result of the action is transmitted in the message body. + * TRACE: The message body contains the request message as received by the server + */ + OK = 200, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.2 + * + * The request has succeeded and a new resource has been created as a result of it. This is typically the response sent after a PUT request. + */ + CREATED = 201, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.3 + * + * The request has been received but not yet acted upon. It is non-committal, meaning that there is no way in HTTP to later send an asynchronous response indicating the outcome of processing the request. It is intended for cases where another process or server handles the request, or for batch processing. + */ + ACCEPTED = 202, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.4 + * + * This response code means returned meta-information set is not exact set as available from the origin server, but collected from a local or a third party copy. Except this condition, 200 OK response should be preferred instead of this response. + */ + NON_AUTHORITATIVE_INFORMATION = 203, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.5 + * + * There is no content to send for this request, but the headers may be useful. The user-agent may update its cached headers for this resource with the new ones. + */ + NO_CONTENT = 204, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.6 + * + * This response code is sent after accomplishing request to tell user agent reset document view which sent this request. + */ + RESET_CONTENT = 205, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7233#section-4.1 + * + * This response code is used because of range header sent by the client to separate download into multiple streams. + */ + PARTIAL_CONTENT = 206, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.2 + * + * A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate. + */ + MULTI_STATUS = 207, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.1 + * + * The request has more than one possible responses. User-agent or user should choose one of them. There is no standardized way to choose one of the responses. + */ + MULTIPLE_CHOICES = 300, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.2 + * + * This response code means that URI of requested resource has been changed. Probably, new URI would be given in the response. + */ + MOVED_PERMANENTLY = 301, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.3 + * + * This response code means that URI of requested resource has been changed temporarily. New changes in the URI might be made in the future. Therefore, this same URI should be used by the client in future requests. + */ + MOVED_TEMPORARILY = 302, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.4 + * + * Server sent this response to directing client to get requested resource to another URI with an GET request. + */ + SEE_OTHER = 303, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7232#section-4.1 + * + * This is used for caching purposes. It is telling to client that response has not been modified. So, client can continue to use same cached version of response. + */ + NOT_MODIFIED = 304, + /** + * @deprecated + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.6 + * + * Was defined in a previous version of the HTTP specification to indicate that a requested response must be accessed by a proxy. It has been deprecated due to security concerns regarding in-band configuration of a proxy. + */ + USE_PROXY = 305, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.7 + * + * Server sent this response to directing client to get requested resource to another URI with same method that used prior request. This has the same semantic than the 302 Found HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request. + */ + TEMPORARY_REDIRECT = 307, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7538#section-3 + * + * This means that the resource is now permanently located at another URI, specified by the Location: HTTP Response header. This has the same semantics as the 301 Moved Permanently HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request. + */ + PERMANENT_REDIRECT = 308, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.1 + * + * This response means that server could not understand the request due to invalid syntax. + */ + BAD_REQUEST = 400, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7235#section-3.1 + * + * Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. + */ + UNAUTHORIZED = 401, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.2 + * + * This response code is reserved for future use. Initial aim for creating this code was using it for digital payment systems however this is not used currently. + */ + PAYMENT_REQUIRED = 402, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.3 + * + * The client does not have access rights to the content, i.e. they are unauthorized, so server is rejecting to give proper response. Unlike 401, the client's identity is known to the server. + */ + FORBIDDEN = 403, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.4 + * + * The server can not find requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 to hide the existence of a resource from an unauthorized client. This response code is probably the most famous one due to its frequent occurence on the web. + */ + NOT_FOUND = 404, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.5 + * + * The request method is known by the server but has been disabled and cannot be used. For example, an API may forbid DELETE-ing a resource. The two mandatory methods, GET and HEAD, must never be disabled and should not return this error code. + */ + METHOD_NOT_ALLOWED = 405, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.6 + * + * This response is sent when the web server, after performing server-driven content negotiation, doesn't find any content following the criteria given by the user agent. + */ + NOT_ACCEPTABLE = 406, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7235#section-3.2 + * + * This is similar to 401 but authentication is needed to be done by a proxy. + */ + PROXY_AUTHENTICATION_REQUIRED = 407, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.7 + * + * This response is sent on an idle connection by some servers, even without any previous request by the client. It means that the server would like to shut down this unused connection. This response is used much more since some browsers, like Chrome, Firefox 27+, or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also note that some servers merely shut down the connection without sending this message. + */ + REQUEST_TIMEOUT = 408, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.8 + * + * This response is sent when a request conflicts with the current state of the server. + */ + CONFLICT = 409, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.9 + * + * This response would be sent when the requested content has been permenantly deleted from server, with no forwarding address. Clients are expected to remove their caches and links to the resource. The HTTP specification intends this status code to be used for "limited-time, promotional services". APIs should not feel compelled to indicate resources that have been deleted with this status code. + */ + GONE = 410, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.10 + * + * The server rejected the request because the Content-Length header field is not defined and the server requires it. + */ + LENGTH_REQUIRED = 411, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7232#section-4.2 + * + * The client has indicated preconditions in its headers which the server does not meet. + */ + PRECONDITION_FAILED = 412, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.11 + * + * Request entity is larger than limits defined by server; the server might close the connection or return an Retry-After header field. + */ + REQUEST_TOO_LONG = 413, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.12 + * + * The URI requested by the client is longer than the server is willing to interpret. + */ + REQUEST_URI_TOO_LONG = 414, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.13 + * + * The media format of the requested data is not supported by the server, so the server is rejecting the request. + */ + UNSUPPORTED_MEDIA_TYPE = 415, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7233#section-4.4 + * + * The range specified by the Range header field in the request can't be fulfilled; it's possible that the range is outside the size of the target URI's data. + */ + REQUESTED_RANGE_NOT_SATISFIABLE = 416, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.14 + * + * This response code means the expectation indicated by the Expect request header field can't be met by the server. + */ + EXPECTATION_FAILED = 417, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc2324#section-2.3.2 + * + * Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout. + */ + IM_A_TEAPOT = 418, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.6 + * + * The 507 (Insufficient Storage) status code means the method could not be performed on the resource because the server is unable to store the representation needed to successfully complete the request. This condition is considered to be temporary. If the request which received this status code was the result of a user action, the request MUST NOT be repeated until it is requested by a separate user action. + */ + INSUFFICIENT_SPACE_ON_RESOURCE = 419, + /** + * @deprecated + * Official Documentation @ https://tools.ietf.org/rfcdiff?difftype=--hwdiff&url2=draft-ietf-webdav-protocol-06.txt + * + * A deprecated response used by the Spring Framework when a method has failed. + */ + METHOD_FAILURE = 420, + /** + * Official Documentation @ https://datatracker.ietf.org/doc/html/rfc7540#section-9.1.2 + * + * Defined in the specification of HTTP/2 to indicate that a server is not able to produce a response for the combination of scheme and authority that are included in the request URI. + */ + MISDIRECTED_REQUEST = 421, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.3 + * + * The request was well-formed but was unable to be followed due to semantic errors. + */ + UNPROCESSABLE_ENTITY = 422, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.4 + * + * The resource that is being accessed is locked. + */ + LOCKED = 423, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.5 + * + * The request failed due to failure of a previous request. + */ + FAILED_DEPENDENCY = 424, + /** + * Official Documentation @ https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.15 + * + * The server refuses to perform the request using the current protocol but might be willing to do so after the client upgrades to a different protocol. + */ + UPGRADE_REQUIRED = 426, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-3 + * + * The origin server requires the request to be conditional. Intended to prevent the 'lost update' problem, where a client GETs a resource's state, modifies it, and PUTs it back to the server, when meanwhile a third party has modified the state on the server, leading to a conflict. + */ + PRECONDITION_REQUIRED = 428, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-4 + * + * The user has sent too many requests in a given amount of time ("rate limiting"). + */ + TOO_MANY_REQUESTS = 429, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-5 + * + * The server is unwilling to process the request because its header fields are too large. The request MAY be resubmitted after reducing the size of the request header fields. + */ + REQUEST_HEADER_FIELDS_TOO_LARGE = 431, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7725 + * + * The user-agent requested a resource that cannot legally be provided, such as a web page censored by a government. + */ + UNAVAILABLE_FOR_LEGAL_REASONS = 451, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.1 + * + * The server encountered an unexpected condition that prevented it from fulfilling the request. + */ + INTERNAL_SERVER_ERROR = 500, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.2 + * + * The request method is not supported by the server and cannot be handled. The only methods that servers are required to support (and therefore that must not return this code) are GET and HEAD. + */ + NOT_IMPLEMENTED = 501, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.3 + * + * This error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response. + */ + BAD_GATEWAY = 502, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.4 + * + * The server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded. Note that together with this response, a user-friendly page explaining the problem should be sent. This responses should be used for temporary conditions and the Retry-After: HTTP header should, if possible, contain the estimated time before the recovery of the service. The webmaster must also take care about the caching-related headers that are sent along with this response, as these temporary condition responses should usually not be cached. + */ + SERVICE_UNAVAILABLE = 503, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.5 + * + * This error response is given when the server is acting as a gateway and cannot get a response in time. + */ + GATEWAY_TIMEOUT = 504, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.6 + * + * The HTTP version used in the request is not supported by the server. + */ + HTTP_VERSION_NOT_SUPPORTED = 505, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.6 + * + * The server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself, and is therefore not a proper end point in the negotiation process. + */ + INSUFFICIENT_STORAGE = 507, + /** + * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-6 + * + * The 511 status code indicates that the client needs to authenticate to gain network access. + */ + NETWORK_AUTHENTICATION_REQUIRED = 511 +} diff --git a/packages/data-transport-layer/src/utils/index.ts b/packages/data-transport-layer/src/utils/index.ts index 45d19020a..ac60a1b19 100644 --- a/packages/data-transport-layer/src/utils/index.ts +++ b/packages/data-transport-layer/src/utils/index.ts @@ -1,3 +1,4 @@ export * from './contracts' export * from './validation' export * from './eth-tx' +export * from './http-code'