Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: abstract codec versions into common interfaces #25

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ad42cd9
feat: support conditional encode
colinlyguo Aug 14, 2024
62758c8
move append conditionalEncode flag after validity check
colinlyguo Aug 14, 2024
6901956
update da-codec
colinlyguo Aug 18, 2024
e4bf12e
align naming
colinlyguo Aug 18, 2024
030349d
add ConvertBlobToBlobBytes utility functions
colinlyguo Aug 19, 2024
ed4de9e
kept blob bytes
colinlyguo Aug 19, 2024
c6af3bb
rename enableEncode to enableCompress
colinlyguo Aug 19, 2024
a5691d4
refactor: move some common functions to encoding (#24)
colinlyguo Aug 20, 2024
9532963
move symbol replace script to zstd folder
colinlyguo Aug 20, 2024
990bdb3
refactor: move some util functions to public package
colinlyguo Aug 20, 2024
6b86866
fix CI
colinlyguo Aug 20, 2024
3ad692a
add interfaces of codec
colinlyguo Aug 20, 2024
a5c6430
add SetCompression
colinlyguo Aug 20, 2024
43f56e6
move interface to encoding
colinlyguo Aug 20, 2024
cd280de
refactor
colinlyguo Aug 20, 2024
879bb98
add dablock.go
colinlyguo Aug 21, 2024
77aafd4
add dachunk.go
colinlyguo Aug 21, 2024
6ee5c19
add dabatch.go
colinlyguo Aug 21, 2024
79422a2
move computeBatchDataHash to codecv
colinlyguo Aug 21, 2024
296880e
fix
colinlyguo Aug 21, 2024
c038850
add DABatchBase
colinlyguo Aug 21, 2024
4499e2c
add GetCodecVersion
colinlyguo Aug 21, 2024
8e763dd
add BlobVersionedHashes
colinlyguo Aug 21, 2024
98d5635
rename encoding.go to interfaces.go
colinlyguo Aug 22, 2024
bdb98f8
add NewDABatchWithExpectedBlobVersionedHashes
colinlyguo Aug 22, 2024
f3f0fbd
Merge branch 'main' into refactor-move-some-util-functions-to-public-…
colinlyguo Aug 22, 2024
08d60a3
tweak
colinlyguo Aug 22, 2024
2d425d8
fix a bug
colinlyguo Aug 24, 2024
c1e4a0d
add more logs
colinlyguo Aug 24, 2024
e5df846
add DecodeDAChunks
colinlyguo Sep 5, 2024
ecaca71
add BlockRange interface
colinlyguo Sep 5, 2024
484fa59
fix
colinlyguo Sep 5, 2024
f1fe4c8
add version check
colinlyguo Sep 5, 2024
97711e2
add Version
colinlyguo Sep 5, 2024
2a63797
remove DABatchBase
colinlyguo Sep 5, 2024
87c4537
add DABlock
colinlyguo Sep 5, 2024
2ac7825
fixes
colinlyguo Sep 5, 2024
8a6c35f
fix
colinlyguo Sep 5, 2024
83f6b62
add CodecFromVersion and CodecFromConfig
colinlyguo Sep 5, 2024
c4a2495
remove GetCodecVersion
colinlyguo Sep 5, 2024
10af8e7
fix typos
colinlyguo Sep 18, 2024
1594e0f
make Block fields internal
colinlyguo Sep 22, 2024
1f9facd
make chunk fields internal
colinlyguo Sep 22, 2024
451eb68
make batch fields internal and add some tweaks
colinlyguo Sep 22, 2024
955f375
add JSONFromBytes
colinlyguo Sep 22, 2024
f73c63e
fix a typo
colinlyguo Sep 30, 2024
cf9f084
use register mode
colinlyguo Oct 1, 2024
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
426 changes: 167 additions & 259 deletions encoding/codecv0/codecv0.go

Large diffs are not rendered by default.

597 changes: 0 additions & 597 deletions encoding/codecv0/codecv0_test.go

This file was deleted.

490 changes: 246 additions & 244 deletions encoding/codecv1/codecv1.go

Large diffs are not rendered by default.

892 changes: 0 additions & 892 deletions encoding/codecv1/codecv1_test.go

This file was deleted.

434 changes: 301 additions & 133 deletions encoding/codecv2/codecv2.go

Large diffs are not rendered by default.

967 changes: 0 additions & 967 deletions encoding/codecv2/codecv2_test.go

This file was deleted.

630 changes: 467 additions & 163 deletions encoding/codecv3/codecv3.go

Large diffs are not rendered by default.

1,098 changes: 0 additions & 1,098 deletions encoding/codecv3/codecv3_test.go

This file was deleted.

529 changes: 348 additions & 181 deletions encoding/codecv4/codecv4.go

Large diffs are not rendered by default.

837 changes: 0 additions & 837 deletions encoding/codecv4/codecv4_test.go

This file was deleted.

50 changes: 30 additions & 20 deletions encoding/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,14 @@ import (
// BLSModulus is the BLS modulus defined in EIP-4844.
var BLSModulus = new(big.Int).SetBytes(common.FromHex("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"))

// CodecVersion defines the version of encoder and decoder.
type CodecVersion uint8
// CalldataNonZeroByteGas is the gas consumption per non zero byte in calldata.
const CalldataNonZeroByteGas = 16

const (
// CodecV0 represents the version 0 of the encoder and decoder.
CodecV0 CodecVersion = iota
// BlockContextByteSize is the size of the block context in bytes.
const BlockContextByteSize = 60

// CodecV1 represents the version 1 of the encoder and decoder.
CodecV1

// CodecV2 represents the version 2 of the encoder and decoder.
CodecV2

// CodecV3 represents the version 3 of the encoder and decoder.
CodecV3

// CodecV4 represents the version 4 of the encoder and decoder.
CodecV4
)
// TxLenByteSize is the size of the transaction length in bytes.
const TxLenByteSize = 4

// Block represents an L2 block.
type Block struct {
Expand Down Expand Up @@ -215,11 +204,11 @@ func (c *Chunk) NumL2Transactions() uint64 {

// L2GasUsed calculates the total gas of L2 transactions in a Chunk.
func (c *Chunk) L2GasUsed() uint64 {
var totalTxNum uint64
var totalGasUsed uint64
for _, block := range c.Blocks {
totalTxNum += block.Header.GasUsed
totalGasUsed += block.Header.GasUsed
}
return totalTxNum
return totalGasUsed
}

// StateRoot gets the state root after committing/finalizing the batch.
Expand Down Expand Up @@ -405,6 +394,27 @@ func ConstructBatchPayloadInBlob(chunks []*Chunk, MaxNumChunks uint64) ([]byte,
return batchBytes, nil
}

// GetKeccak256Gas calculates the gas cost for computing the keccak256 hash of a given size.
func GetKeccak256Gas(size uint64) uint64 {
return GetMemoryExpansionCost(size) + 30 + 6*((size+31)/32)
}

// GetMemoryExpansionCost calculates the cost of memory expansion for a given memoryByteSize.
func GetMemoryExpansionCost(memoryByteSize uint64) uint64 {
memorySizeWord := (memoryByteSize + 31) / 32
memoryCost := (memorySizeWord*memorySizeWord)/512 + (3 * memorySizeWord)
return memoryCost
}

// GetTxPayloadLength calculates the length of the transaction payload.
func GetTxPayloadLength(txData *types.TransactionData) (uint64, error) {
rlpTxData, err := ConvertTxDataToRLPEncoding(txData, false /* no mock */)
if err != nil {
return 0, err
}
return uint64(len(rlpTxData)), nil
}

// BlobDataProofFromValues creates the blob data proof from the given values.
// Memory layout of ``_blobDataProof``:
// | z | y | kzg_commitment | kzg_proof |
Expand Down
Loading