Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions node/core/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"math/big"
"math/bits"

"github.com/morph-l2/node/types"
"github.com/scroll-tech/go-ethereum/common"
Expand Down Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions node/core/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion node/db/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var (
L1MessagePrefix = []byte("l1")

derivationL1HeightKey = []byte("LastDerivationL1Height")
latestBatchBlsKey = []byte("latestBatchBlsKey")
)

// encodeBlockNumber encodes an L1 enqueue index as big endian uint64
Expand Down
157 changes: 0 additions & 157 deletions node/sync/deposit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions node/types/types.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tx-submitter/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tx-submitter/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion tx-submitter/services/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const (
txSlotSize = 32 * 1024
txMaxSize = 4 * txSlotSize // 128KB
minFinalizeNum = 2 // min finalize num from contract
minGasLimit = 1000
)

type Rollup struct {
Expand Down
88 changes: 0 additions & 88 deletions tx-submitter/services/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand Down