From 7a19ecf5d283c5ca81812ef122d45fee5d2b7c20 Mon Sep 17 00:00:00 2001 From: "fletcher.fan" Date: Mon, 18 Mar 2024 11:42:42 +0800 Subject: [PATCH] Declaration redundancy --- node/core/batch.go | 8 -- node/core/executor.go | 8 -- node/db/keys.go | 1 - node/sync/deposit_log.go | 157 -------------------------------- node/types/types.go | 22 ----- tx-submitter/cmd/main.go | 2 +- tx-submitter/entry.go | 2 +- tx-submitter/services/rollup.go | 1 - tx-submitter/services/utils.go | 88 ------------------ 9 files changed, 2 insertions(+), 287 deletions(-) diff --git a/node/core/batch.go b/node/core/batch.go index 39f900f2c..f05060d18 100644 --- a/node/core/batch.go +++ b/node/core/batch.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "math/big" - "math/bits" "github.com/morph-l2/node/types" "github.com/scroll-tech/go-ethereum/common" @@ -551,10 +550,3 @@ func heightFromBCBytes(blockBytes []byte) (uint64, error) { } return curBlock.Number, nil } - -func skippedNums(skippedBitMap []*big.Int) (count int) { - for _, s := range skippedBitMap { - count += bits.OnesCount64(s.Uint64()) - } - return -} diff --git a/node/core/executor.go b/node/core/executor.go index 7ff8d3627..ee9769d02 100644 --- a/node/core/executor.go +++ b/node/core/executor.go @@ -421,11 +421,3 @@ func (e *Executor) getParamsAndValsAtHeight(height int64) (*tmproto.BatchParams, func (e *Executor) L2Client() *types.RetryableClient { return e.l2Client } - -func L1MessagesToTxs(l1Messages []types.L1Message) []eth.L1MessageTx { - txs := make([]eth.L1MessageTx, len(l1Messages)) - for i, l1Message := range l1Messages { - txs[i] = l1Message.L1MessageTx - } - return txs -} diff --git a/node/db/keys.go b/node/db/keys.go index e5abb6c6f..334009223 100644 --- a/node/db/keys.go +++ b/node/db/keys.go @@ -7,7 +7,6 @@ var ( L1MessagePrefix = []byte("l1") derivationL1HeightKey = []byte("LastDerivationL1Height") - latestBatchBlsKey = []byte("latestBatchBlsKey") ) // encodeBlockNumber encodes an L1 enqueue index as big endian uint64 diff --git a/node/sync/deposit_log.go b/node/sync/deposit_log.go index 390e0f1f9..43cd53cba 100644 --- a/node/sync/deposit_log.go +++ b/node/sync/deposit_log.go @@ -6,13 +6,11 @@ import ( "math/big" "github.com/hashicorp/go-multierror" - "github.com/holiman/uint256" "github.com/morph-l2/bindings/bindings" "github.com/morph-l2/node/types" "github.com/scroll-tech/go-ethereum/common" eth "github.com/scroll-tech/go-ethereum/core/types" "github.com/scroll-tech/go-ethereum/crypto" - "github.com/scroll-tech/go-ethereum/log" ) var ( @@ -60,161 +58,6 @@ func (c *BridgeClient) deriveFromReceipt(receipts []*eth.Receipt) ([]types.L1Mes return out, result } -func deriveFromReceipt(receipts []*eth.Receipt, depositContractAddr common.Address) ([]types.L1Message, error) { - var out []types.L1Message - var result error - for i, rec := range receipts { - if rec.Status != eth.ReceiptStatusSuccessful { - continue - } - for j, lg := range rec.Logs { - if lg.Address == depositContractAddr && len(lg.Topics) > 0 && lg.Topics[0] == DepositEventABIHash { - msg, err := UnmarshalDepositLogEvent(lg) - if err != nil { - result = multierror.Append(result, fmt.Errorf("malformatted L1 deposit log in receipt %d, log %d: %w", i, j, err)) - } else { - if msg == nil { - continue - } - out = append(out, types.L1Message{ - L1MessageTx: *msg, - L1TxHash: lg.TxHash, - }) - } - } - } - } - return out, result -} - -// UnmarshalDepositLogEvent decodes an EVM log entry emitted by the deposit contract into typed deposit data. -// -// parse log data for: -// -// event TransactionDeposited( -// address indexed from, -// address indexed to, -// uint256 indexed version, -// bytes opaqueData -// ); -// -// Additionally, the event log-index and -func UnmarshalDepositLogEvent(ev *eth.Log) (*eth.L1MessageTx, error) { - if len(ev.Topics) != 4 { - return nil, fmt.Errorf("expected 4 event topics (event identity, indexed from, indexed to, indexed version), got %d", len(ev.Topics)) - } - if ev.Topics[0] != DepositEventABIHash { - return nil, fmt.Errorf("invalid deposit event selector: %s, expected %s", ev.Topics[0], DepositEventABIHash) - } - if len(ev.Data) < 64 { - return nil, fmt.Errorf("incomplate opaqueData slice header (%d bytes): %x", len(ev.Data), ev.Data) - } - if len(ev.Data)%32 != 0 { - return nil, fmt.Errorf("expected log data to be multiple of 32 bytes: got %d bytes", len(ev.Data)) - } - - // indexed 0 - from := common.BytesToAddress(ev.Topics[1][12:]) - log.Trace("Unmarshalling deposit log", "from", from.String()) - // indexed 1 - to := common.BytesToAddress(ev.Topics[2][12:]) - // indexed 2 - version := ev.Topics[3] - // unindexed data - // Solidity serializes the event's Data field as follows: - // abi.encode(abi.encodPacked(uint256 mint, uint256 value, uint64 gasLimit, uint8 isCreation, bytes data)) - // Thus the first 32 bytes of the Data will give us the offset of the opaqueData, - // which should always be 0x20. - var opaqueContentOffset uint256.Int - opaqueContentOffset.SetBytes(ev.Data[0:32]) - if !opaqueContentOffset.IsUint64() || opaqueContentOffset.Uint64() != 32 { - return nil, fmt.Errorf("invalid opaqueData slice header offset: %d", opaqueContentOffset.Uint64()) - } - // The next 32 bytes indicate the length of the opaqueData content. - var opaqueContentLength uint256.Int - opaqueContentLength.SetBytes(ev.Data[32:64]) - // Make sure the length is an uint64, it's not larger than the remaining data, and the log is using minimal padding (i.e. can't add 32 bytes without exceeding data) - if !opaqueContentLength.IsUint64() || opaqueContentLength.Uint64() > uint64(len(ev.Data)-64) || opaqueContentLength.Uint64()+32 <= uint64(len(ev.Data)-64) { - return nil, fmt.Errorf("invalid opaqueData slice header length: %d", opaqueContentLength.Uint64()) - } - // The remaining data is the opaqueData which is tightly packed - // and then padded to 32 bytes by the EVM. - opaqueData := ev.Data[64 : 64+opaqueContentLength.Uint64()] - - var tx *eth.L1MessageTx - var err error - switch version { - case DepositEventVersion0: - tx, err = unmarshalDepositVersion0(to, opaqueData) - default: - return nil, fmt.Errorf("invalid deposit version, got %s", version) - } - if err != nil { - if err == types.ErrNotFromCrossDomainMessenger { - log.Warn("found the message not sent by L1CrossDomainMessenger, ignore it for now") - return nil, nil - } - return nil, fmt.Errorf("failed to decode deposit (version %s): %w", version, err) - } - tx.Sender = from - return tx, nil -} - -func unmarshalDepositVersion0(to common.Address, opaqueData []byte) (*eth.L1MessageTx, error) { - var message eth.L1MessageTx - if len(opaqueData) < 32+32+8+1 { - return nil, fmt.Errorf("unexpected opaqueData length: %d", len(opaqueData)) - } - offset := uint64(0) - // uint256 mint - mint := new(big.Int).SetBytes(opaqueData[offset : offset+32]) - offset += 32 - log.Trace("Unmarshalling deposit log", "mint", mint) - - // uint256 value - value := new(big.Int).SetBytes(opaqueData[offset : offset+32]) - offset += 32 - message.Value = value - log.Trace("Unmarshalling deposit log", "value", value) - - // uint64 gas - gas := new(big.Int).SetBytes(opaqueData[offset : offset+8]) - if !gas.IsUint64() { - return nil, fmt.Errorf("bad gas value: %x", opaqueData[offset:offset+8]) - } - message.Gas = gas.Uint64() - offset += 8 - - // uint8 isCreation - // isCreation: If the boolean byte is 1 then dep.To will stay nil, - // and it will create a contract using L2 account nonce to determine the created address. - if opaqueData[offset] == 0 { - message.To = &to - } - offset += 1 - - // The remainder of the opaqueData is the transaction data (without length prefix). - // The data may be padded to a multiple of 32 bytes - txDataLen := uint64(len(opaqueData)) - offset - - // remaining bytes fill the data - message.Data = opaqueData[offset : offset+txDataLen] - - // NOTE: currently we acquire the nonce from relayMessage input parsed from event data, - // which means we only allow the cross message data which are formed as relayMessage for now. - // in the future, the `nonce` is supposed to be exposed as one of the event fields - relayMessage, err := unpackRelayMessage(message.Data) - if err != nil { - return nil, types.ErrNotFromCrossDomainMessenger - } - message.QueueIndex, err = types.DecodeNonce(relayMessage.nonce) - if err != nil { - return nil, err - } - - return &message, nil -} - type relayMessageData struct { nonce *big.Int sender common.Address diff --git a/node/types/types.go b/node/types/types.go index 1b5bb298c..c04b00947 100644 --- a/node/types/types.go +++ b/node/types/types.go @@ -1,27 +1,5 @@ package types -import ( - "errors" - "fmt" - "math/big" - - "github.com/scroll-tech/go-ethereum/common/hexutil" -) - -var Version1StartedNonce = new(big.Int).SetBytes(hexutil.MustDecode("0x1000000000000000000000000000000000000000000000000000000000000")) - -func EncodeNonce(nonce uint64) *big.Int { - return new(big.Int).Add(Version1StartedNonce, big.NewInt(int64(nonce))) -} - -func DecodeNonce(encodedNonce *big.Int) (uint64, error) { - decoded := new(big.Int).Sub(encodedNonce, Version1StartedNonce) - if decoded.Sign() < 0 { - return 0, errors.New(fmt.Sprintf("wrong encoded nonce: %s", encodedNonce.String())) - } - return decoded.Uint64(), nil -} - func IsAllZero(s []byte) bool { for _, v := range s { if v != 0 { diff --git a/tx-submitter/cmd/main.go b/tx-submitter/cmd/main.go index e25e9a20a..6a15105af 100644 --- a/tx-submitter/cmd/main.go +++ b/tx-submitter/cmd/main.go @@ -34,7 +34,7 @@ func main() { app.Usage = "Batch Submitter Service" app.Description = "Service for generating and submitting batched transactions " + "that synchronize L2 state to L1 contracts" - app.Action = submitter.Main(GitCommit) + app.Action = submitter.Main() err := app.Run(os.Args) if err != nil { log.Crit("Application failed", "message", err) diff --git a/tx-submitter/entry.go b/tx-submitter/entry.go index 8cd746425..be1e3bc79 100644 --- a/tx-submitter/entry.go +++ b/tx-submitter/entry.go @@ -21,7 +21,7 @@ import ( // a closure that executes the service and blocks until the service exits. The // use of a closure allows the parameters bound to the top-level main package, // e.g. GitVersion, to be captured and used once the function is executed. -func Main(gitCommit string) func(ctx *cli.Context) error { +func Main() func(ctx *cli.Context) error { return func(cliCtx *cli.Context) error { cfg, err := utils.NewConfig(cliCtx) if err != nil { diff --git a/tx-submitter/services/rollup.go b/tx-submitter/services/rollup.go index 4bab7bbe8..774833f27 100644 --- a/tx-submitter/services/rollup.go +++ b/tx-submitter/services/rollup.go @@ -34,7 +34,6 @@ const ( txSlotSize = 32 * 1024 txMaxSize = 4 * txSlotSize // 128KB minFinalizeNum = 2 // min finalize num from contract - minGasLimit = 1000 ) type Rollup struct { diff --git a/tx-submitter/services/utils.go b/tx-submitter/services/utils.go index 51b2c5a4e..a0afe7623 100644 --- a/tx-submitter/services/utils.go +++ b/tx-submitter/services/utils.go @@ -2,14 +2,10 @@ package services import ( "crypto/sha256" - "fmt" "math/big" - "strings" - "github.com/holiman/uint256" "github.com/scroll-tech/go-ethereum/common" "github.com/scroll-tech/go-ethereum/crypto/kzg4844" - "github.com/scroll-tech/go-ethereum/params" ) const ( @@ -24,51 +20,6 @@ var blobPriceBumpPercent = big.NewInt(100 + blobPriceBump) var oneHundred = big.NewInt(100) var two = big.NewInt(2) -func encodeBlobs(data []byte) []kzg4844.Blob { - blobs := []kzg4844.Blob{{}} - blobIndex := 0 - fieldIndex := -1 - for i := 0; i < len(data); i += 31 { - fieldIndex++ - if fieldIndex == params.BlobTxFieldElementsPerBlob { - blobs = append(blobs, kzg4844.Blob{}) - blobIndex++ - fieldIndex = 0 - } - max := i + 31 - if max > len(data) { - max = len(data) - } - copy(blobs[blobIndex][fieldIndex*32:], data[i:max]) - } - return blobs -} - -func EncodeBlobs(data []byte) ([]kzg4844.Blob, []kzg4844.Commitment, []kzg4844.Proof, []common.Hash, error) { - var ( - blobs = encodeBlobs(data) - commits []kzg4844.Commitment - proofs []kzg4844.Proof - versionedHashes []common.Hash - ) - for _, blob := range blobs { - commit, err := kzg4844.BlobToCommitment(blob) - if err != nil { - return nil, nil, nil, nil, err - } - commits = append(commits, commit) - - proof, err := kzg4844.ComputeBlobProof(blob, commit) - if err != nil { - return nil, nil, nil, nil, err - } - proofs = append(proofs, proof) - - versionedHashes = append(versionedHashes, kZGToVersionedHash(commit)) - } - return blobs, commits, proofs, versionedHashes, nil -} - var blobCommitmentVersionKZG uint8 = 0x01 // kZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844 @@ -79,45 +30,6 @@ func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash { return h } -func DecodeBlob(blob []byte) []byte { - if len(blob) != params.BlobTxFieldElementsPerBlob*32 { - panic("invalid blob encoding") - } - var data []byte - - // XXX: the following removes trailing 0s in each field element (see EncodeBlobs), which could be unexpected for certain blobs - j := 0 - for i := 0; i < params.BlobTxFieldElementsPerBlob; i++ { - data = append(data, blob[j:j+31]...) - j += 32 - } - - i := len(data) - 1 - for ; i >= 0; i-- { - if data[i] != 0x00 { - break - } - } - data = data[:i+1] - return data -} - -func DecodeUint256String(hexOrDecimal string) (*uint256.Int, error) { - var base = 10 - if strings.HasPrefix(hexOrDecimal, "0x") { - base = 16 - } - b, ok := new(big.Int).SetString(hexOrDecimal, base) - if !ok { - return nil, fmt.Errorf("invalid value") - } - val256, nok := uint256.FromBig(b) - if nok { - return nil, fmt.Errorf("value is too big") - } - return val256, nil -} - // calcThresholdValue returns x * priceBumpPercent / 100 func calcThresholdValue(x *big.Int, isBlobTx bool) *big.Int { var percent *big.Int