Skip to content

Commit

Permalink
use early return
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed May 6, 2024
1 parent c2a0852 commit 67cc822
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 47 deletions.
1 change: 1 addition & 0 deletions encoding/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func ConstructSkippedBitmap(batchIndex uint64, chunks []*Chunk, totalL1MessagePo
if tx.Type != types.L1MessageTxType {
continue
}

currentIndex := tx.Nonce

if currentIndex < nextIndex {
Expand Down
2 changes: 2 additions & 0 deletions encoding/codecv0/codecv0.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (c *DAChunk) Encode() ([]byte, error) {
if txData.Type == types.L1MessageTxType {
continue
}

var txLen [4]byte
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(txData)
if err != nil {
Expand Down Expand Up @@ -320,6 +321,7 @@ func EstimateBlockL1CommitCalldataSize(b *encoding.Block) (uint64, error) {
if txData.Type == types.L1MessageTxType {
continue
}

size += 4 // 4 bytes payload length
txPayloadLength, err := getTxPayloadLength(txData)
if err != nil {
Expand Down
52 changes: 29 additions & 23 deletions encoding/codecv1/codecv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,19 @@ func (c *DAChunk) Hash() (common.Hash, error) {
// concatenate l1 tx hashes
for _, blockTxs := range c.Transactions {
for _, txData := range blockTxs {
if txData.Type == types.L1MessageTxType {
txHash := strings.TrimPrefix(txData.TxHash, "0x")
hashBytes, err := hex.DecodeString(txHash)
if err != nil {
return common.Hash{}, err
}
if len(hashBytes) != 32 {
return common.Hash{}, fmt.Errorf("unexpected hash: %s", txData.TxHash)
}
dataBytes = append(dataBytes, hashBytes...)
if txData.Type != types.L1MessageTxType {
continue
}

txHash := strings.TrimPrefix(txData.TxHash, "0x")
hashBytes, err := hex.DecodeString(txHash)
if err != nil {
return common.Hash{}, err
}
if len(hashBytes) != 32 {
return common.Hash{}, fmt.Errorf("unexpected hash: %s", txData.TxHash)
}
dataBytes = append(dataBytes, hashBytes...)
}
}

Expand Down Expand Up @@ -316,14 +318,16 @@ func constructBlobPayload(chunks []*encoding.Chunk) (*kzg4844.Blob, common.Hash,

for _, block := range chunk.Blocks {
for _, tx := range block.Transactions {
if tx.Type != types.L1MessageTxType {
// encode L2 txs into blob payload
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
if err != nil {
return nil, common.Hash{}, nil, err
}
blobBytes = append(blobBytes, rlpTxData...)
if tx.Type == types.L1MessageTxType {
continue
}

// encode L2 txs into blob payload
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
if err != nil {
return nil, common.Hash{}, nil, err
}
blobBytes = append(blobBytes, rlpTxData...)
}
}

Expand Down Expand Up @@ -505,13 +509,15 @@ func chunkL1CommitBlobDataSize(c *encoding.Chunk) (uint64, error) {
var dataSize uint64
for _, block := range c.Blocks {
for _, tx := range block.Transactions {
if tx.Type != types.L1MessageTxType {
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
if err != nil {
return 0, err
}
dataSize += uint64(len(rlpTxData))
if tx.Type == types.L1MessageTxType {
continue
}

rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
if err != nil {
return 0, err
}
dataSize += uint64(len(rlpTxData))
}
}
return dataSize, nil
Expand Down
54 changes: 30 additions & 24 deletions encoding/codecv2/codecv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,19 @@ func (c *DAChunk) Hash() (common.Hash, error) {
// concatenate l1 tx hashes
for _, blockTxs := range c.Transactions {
for _, txData := range blockTxs {
if txData.Type == types.L1MessageTxType {
txHash := strings.TrimPrefix(txData.TxHash, "0x")
hashBytes, err := hex.DecodeString(txHash)
if err != nil {
return common.Hash{}, err
}
if len(hashBytes) != 32 {
return common.Hash{}, fmt.Errorf("unexpected hash: %s", txData.TxHash)
}
dataBytes = append(dataBytes, hashBytes...)
if txData.Type != types.L1MessageTxType {
continue
}

txHash := strings.TrimPrefix(txData.TxHash, "0x")
hashBytes, err := hex.DecodeString(txHash)
if err != nil {
return common.Hash{}, err
}
if len(hashBytes) != 32 {
return common.Hash{}, fmt.Errorf("unexpected hash: %s", txData.TxHash)
}
dataBytes = append(dataBytes, hashBytes...)
}
}

Expand Down Expand Up @@ -276,14 +278,16 @@ func constructBlobPayload(chunks []*encoding.Chunk) (*kzg4844.Blob, common.Hash,

for _, block := range chunk.Blocks {
for _, tx := range block.Transactions {
if tx.Type != types.L1MessageTxType {
// encode L2 txs into blob payload
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
if err != nil {
return nil, common.Hash{}, nil, err
}
blobBytes = append(blobBytes, rlpTxData...)
if tx.Type == types.L1MessageTxType {
continue
}

// encode L2 txs into blob payload
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
if err != nil {
return nil, common.Hash{}, nil, err
}
blobBytes = append(blobBytes, rlpTxData...)
}
}

Expand Down Expand Up @@ -472,14 +476,16 @@ func constructBatchPayload(chunks []*encoding.Chunk) ([]byte, error) {

for _, block := range chunk.Blocks {
for _, tx := range block.Transactions {
if tx.Type != types.L1MessageTxType {
// encode L2 txs into batch payload
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
if err != nil {
return nil, err
}
batchBytes = append(batchBytes, rlpTxData...)
if tx.Type == types.L1MessageTxType {
continue
}

// encode L2 txs into batch payload
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
if err != nil {
return nil, err
}
batchBytes = append(batchBytes, rlpTxData...)
}
}

Expand Down

0 comments on commit 67cc822

Please sign in to comment.