Skip to content

Commit 65b39d4

Browse files
authored
Merge branch 'main' into erc20_price
2 parents 01c611e + 3e02ce8 commit 65b39d4

File tree

8 files changed

+548
-46
lines changed

8 files changed

+548
-46
lines changed

node/types/blob.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func RetrieveBlobBytes(blob *kzg4844.Blob) ([]byte, error) {
5252
return data, nil
5353
}
5454

55-
func makeBCP(bz []byte) (b kzg4844.Blob, c kzg4844.Commitment, p kzg4844.Proof, err error) {
55+
func makeBlobCommitment(bz []byte) (b kzg4844.Blob, c kzg4844.Commitment, err error) {
5656
blob, err := MakeBlobCanonical(bz)
5757
if err != nil {
5858
return
@@ -62,10 +62,6 @@ func makeBCP(bz []byte) (b kzg4844.Blob, c kzg4844.Commitment, p kzg4844.Proof,
6262
if err != nil {
6363
return
6464
}
65-
p, err = kzg4844.ComputeBlobProof(&b, c)
66-
if err != nil {
67-
return
68-
}
6965
return
7066
}
7167

@@ -85,28 +81,26 @@ func MakeBlobTxSidecar(blobBytes []byte) (*eth.BlobTxSidecar, error) {
8581
err error
8682
blobs = make([]kzg4844.Blob, blobCount)
8783
commitments = make([]kzg4844.Commitment, blobCount)
88-
proofs = make([]kzg4844.Proof, blobCount)
8984
)
9085
switch blobCount {
9186
case 1:
92-
blobs[0], commitments[0], proofs[0], err = makeBCP(blobBytes)
87+
blobs[0], commitments[0], err = makeBlobCommitment(blobBytes)
9388
if err != nil {
9489
return nil, err
9590
}
9691
case 2:
97-
blobs[0], commitments[0], proofs[0], err = makeBCP(blobBytes[:MaxBlobBytesSize])
92+
blobs[0], commitments[0], err = makeBlobCommitment(blobBytes[:MaxBlobBytesSize])
9893
if err != nil {
9994
return nil, err
10095
}
101-
blobs[1], commitments[1], proofs[1], err = makeBCP(blobBytes[MaxBlobBytesSize:])
96+
blobs[1], commitments[1], err = makeBlobCommitment(blobBytes[MaxBlobBytesSize:])
10297
if err != nil {
10398
return nil, err
10499
}
105100
}
106101
return &eth.BlobTxSidecar{
107102
Blobs: blobs,
108103
Commitments: commitments,
109-
Proofs: proofs,
110104
}, nil
111105
}
112106

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package services
2+
3+
import (
4+
"crypto/sha256"
5+
"testing"
6+
7+
"github.com/morph-l2/go-ethereum/common"
8+
"github.com/morph-l2/go-ethereum/crypto/kzg4844"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// TestCalcBlobHashV1VsKZGToVersionedHash verifies that CalcBlobHashV1 and kZGToVersionedHash
13+
// produce the same results for the same commitment.
14+
func TestCalcBlobHashV1VsKZGToVersionedHash(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
commitment kzg4844.Commitment
18+
}{
19+
{
20+
name: "zero commitment",
21+
commitment: kzg4844.Commitment{},
22+
},
23+
{
24+
name: "all ones commitment",
25+
commitment: func() kzg4844.Commitment {
26+
var c kzg4844.Commitment
27+
for i := range c {
28+
c[i] = 0xFF
29+
}
30+
return c
31+
}(),
32+
},
33+
{
34+
name: "pattern commitment",
35+
commitment: func() kzg4844.Commitment {
36+
var c kzg4844.Commitment
37+
for i := range c {
38+
c[i] = byte(i % 256)
39+
}
40+
return c
41+
}(),
42+
},
43+
{
44+
name: "reversed pattern commitment",
45+
commitment: func() kzg4844.Commitment {
46+
var c kzg4844.Commitment
47+
for i := range c {
48+
c[i] = byte(255 - (i % 256))
49+
}
50+
return c
51+
}(),
52+
},
53+
}
54+
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
// Calculate using kZGToVersionedHash (v0 method)
58+
hashV0 := kZGToVersionedHash(tt.commitment)
59+
60+
// Calculate using CalcBlobHashV1 (v1 method)
61+
hasher := sha256.New()
62+
hashV1 := kzg4844.CalcBlobHashV1(hasher, &tt.commitment)
63+
64+
// Convert hashV1 to common.Hash for comparison
65+
hashV1CommonHash := common.Hash(hashV1)
66+
67+
// They should be equal
68+
assert.Equal(t, hashV0, hashV1CommonHash,
69+
"CalcBlobHashV1 and kZGToVersionedHash should produce the same result")
70+
71+
// Verify the first byte is 0x01 (version byte)
72+
assert.Equal(t, uint8(0x01), hashV0[0], "First byte should be version 0x01")
73+
assert.Equal(t, uint8(0x01), hashV1[0], "First byte should be version 0x01")
74+
})
75+
}
76+
}
77+
78+
// TestCalcBlobHashV1ReuseHasher verifies that CalcBlobHashV1 can correctly reuse a hasher
79+
// for multiple commitments.
80+
func TestCalcBlobHashV1ReuseHasher(t *testing.T) {
81+
// Create multiple commitments
82+
commitments := []kzg4844.Commitment{
83+
{0x01, 0x02, 0x03}, // Will be padded with zeros
84+
{0xFF, 0xFE, 0xFD}, // Will be padded with zeros
85+
{}, // Zero commitment
86+
}
87+
88+
// Calculate hashes using reused hasher
89+
hasher := sha256.New()
90+
hashesV1 := make([]common.Hash, len(commitments))
91+
for i, commit := range commitments {
92+
hashV1 := kzg4844.CalcBlobHashV1(hasher, &commit)
93+
hashesV1[i] = common.Hash(hashV1)
94+
}
95+
96+
// Calculate hashes using kZGToVersionedHash (should produce same results)
97+
hashesV0 := make([]common.Hash, len(commitments))
98+
for i, commit := range commitments {
99+
hashesV0[i] = kZGToVersionedHash(commit)
100+
}
101+
102+
// Compare results
103+
for i := range commitments {
104+
assert.Equal(t, hashesV0[i], hashesV1[i],
105+
"Hash %d should be equal regardless of hasher reuse", i)
106+
}
107+
}
108+
109+
// TestCalcBlobHashV1MultipleCalls verifies that CalcBlobHashV1 produces consistent
110+
// results when called multiple times with the same commitment.
111+
func TestCalcBlobHashV1MultipleCalls(t *testing.T) {
112+
commitment := kzg4844.Commitment{0x42}
113+
114+
hasher1 := sha256.New()
115+
hash1 := kzg4844.CalcBlobHashV1(hasher1, &commitment)
116+
117+
hasher2 := sha256.New()
118+
hash2 := kzg4844.CalcBlobHashV1(hasher2, &commitment)
119+
120+
// Same commitment should produce same hash
121+
assert.Equal(t, hash1, hash2, "Same commitment should produce same hash")
122+
123+
// Should also match kZGToVersionedHash
124+
hashV0 := kZGToVersionedHash(commitment)
125+
assert.Equal(t, common.Hash(hash1), hashV0, "Should match kZGToVersionedHash result")
126+
}

tx-submitter/services/rollup.go

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ func (r *Rollup) finalize() error {
842842
if err != nil {
843843
return fmt.Errorf("pack finalizeBatch error:%v", err)
844844
}
845-
tip, feecap, _, err := r.GetGasTipAndCap()
845+
tip, feecap, _, _, err := r.GetGasTipAndCap()
846846
if err != nil {
847847
log.Error("get gas tip and cap error", "business", "finalize")
848848
return fmt.Errorf("get gas tip and cap error:%v", err)
@@ -1068,7 +1068,7 @@ func (r *Rollup) rollup() error {
10681068
}
10691069

10701070
// tip and cap
1071-
tip, gasFeeCap, blobFee, err := r.GetGasTipAndCap()
1071+
tip, gasFeeCap, blobFee, head, err := r.GetGasTipAndCap()
10721072
if err != nil {
10731073
return fmt.Errorf("get gas tip and cap error:%v", err)
10741074
}
@@ -1105,7 +1105,7 @@ func (r *Rollup) rollup() error {
11051105
}
11061106

11071107
// Create and sign transaction
1108-
tx, err := r.createRollupTx(batch, nonce, gas, tip, gasFeeCap, blobFee, calldata)
1108+
tx, err := r.createRollupTx(batch, nonce, gas, tip, gasFeeCap, blobFee, calldata, head)
11091109
if err != nil {
11101110
return fmt.Errorf("failed to create rollup tx: %w", err)
11111111
}
@@ -1119,14 +1119,14 @@ func (r *Rollup) rollup() error {
11191119
r.logTxInfo(signedTx, batchIndex)
11201120

11211121
// Send transaction
1122-
if err := r.SendTx(signedTx); err != nil {
1122+
if err = r.SendTx(signedTx); err != nil {
11231123
return fmt.Errorf("failed to send tx: %w", err)
11241124
}
11251125

11261126
// Update pending state
11271127
r.pendingTxs.SetPindex(batchIndex)
11281128
r.pendingTxs.SetNonce(tx.Nonce())
1129-
if err := r.pendingTxs.Add(signedTx); err != nil {
1129+
if err = r.pendingTxs.Add(signedTx); err != nil {
11301130
log.Error("Failed to track transaction", "error", err)
11311131
}
11321132

@@ -1146,17 +1146,36 @@ func (r *Rollup) getNextNonce() uint64 {
11461146
return nonce
11471147
}
11481148

1149-
func (r *Rollup) createRollupTx(batch *eth.RPCRollupBatch, nonce, gas uint64, tip, gasFeeCap, blobFee *big.Int, calldata []byte) (*ethtypes.Transaction, error) {
1149+
func (r *Rollup) createRollupTx(batch *eth.RPCRollupBatch, nonce, gas uint64, tip, gasFeeCap, blobFee *big.Int, calldata []byte, head *ethtypes.Header) (*ethtypes.Transaction, error) {
11501150
if len(batch.Sidecar.Blobs) > 0 {
1151-
return r.createBlobTx(batch, nonce, gas, tip, gasFeeCap, blobFee, calldata)
1151+
return r.createBlobTx(batch, nonce, gas, tip, gasFeeCap, blobFee, calldata, head)
11521152
}
11531153
return r.createDynamicFeeTx(nonce, gas, tip, gasFeeCap, calldata)
11541154
}
11551155

1156-
func (r *Rollup) createBlobTx(batch *eth.RPCRollupBatch, nonce, gas uint64, tip, gasFeeCap, blobFee *big.Int, calldata []byte) (*ethtypes.Transaction, error) {
1157-
versionedHashes := make([]common.Hash, 0, len(batch.Sidecar.Commitments))
1158-
for _, commit := range batch.Sidecar.Commitments {
1159-
versionedHashes = append(versionedHashes, kZGToVersionedHash(commit))
1156+
func (r *Rollup) createBlobTx(batch *eth.RPCRollupBatch, nonce, gas uint64, tip, gasFeeCap, blobFee *big.Int, calldata []byte, head *ethtypes.Header) (*ethtypes.Transaction, error) {
1157+
versionedHashes := types.BlobHashes(batch.Sidecar.Blobs, batch.Sidecar.Commitments)
1158+
sidecar := &ethtypes.BlobTxSidecar{
1159+
Blobs: batch.Sidecar.Blobs,
1160+
Commitments: batch.Sidecar.Commitments,
1161+
}
1162+
switch types.DetermineBlobVersion(head, r.chainId.Uint64()) {
1163+
case ethtypes.BlobSidecarVersion0:
1164+
sidecar.Version = ethtypes.BlobSidecarVersion0
1165+
proof, err := types.MakeBlobProof(sidecar.Blobs, sidecar.Commitments)
1166+
if err != nil {
1167+
return nil, fmt.Errorf("gen blob proof failed %v", err)
1168+
}
1169+
sidecar.Proofs = proof
1170+
case ethtypes.BlobSidecarVersion1:
1171+
sidecar.Version = ethtypes.BlobSidecarVersion1
1172+
proof, err := types.MakeCellProof(sidecar.Blobs)
1173+
if err != nil {
1174+
return nil, fmt.Errorf("gen cell proof failed %v", err)
1175+
}
1176+
sidecar.Proofs = proof
1177+
default:
1178+
return nil, fmt.Errorf("unsupported blob version")
11601179
}
11611180

11621181
return ethtypes.NewTx(&ethtypes.BlobTx{
@@ -1169,11 +1188,7 @@ func (r *Rollup) createBlobTx(batch *eth.RPCRollupBatch, nonce, gas uint64, tip,
11691188
Data: calldata,
11701189
BlobFeeCap: uint256.MustFromBig(blobFee),
11711190
BlobHashes: versionedHashes,
1172-
Sidecar: &ethtypes.BlobTxSidecar{
1173-
Blobs: batch.Sidecar.Blobs,
1174-
Commitments: batch.Sidecar.Commitments,
1175-
Proofs: batch.Sidecar.Proofs,
1176-
},
1191+
Sidecar: sidecar,
11771192
}), nil
11781193
}
11791194

@@ -1242,21 +1257,21 @@ func (r *Rollup) buildSignatureInput(batch *eth.RPCRollupBatch) (*bindings.IRoll
12421257
return &sigData, nil
12431258
}
12441259

1245-
func (r *Rollup) GetGasTipAndCap() (*big.Int, *big.Int, *big.Int, error) {
1260+
func (r *Rollup) GetGasTipAndCap() (*big.Int, *big.Int, *big.Int, *ethtypes.Header, error) {
12461261
head, err := r.L1Client.HeaderByNumber(context.Background(), nil)
12471262
if err != nil {
1248-
return nil, nil, nil, err
1263+
return nil, nil, nil, nil, err
12491264
}
12501265
if head.BaseFee != nil {
12511266
log.Info("market fee info", "feecap", head.BaseFee)
12521267
if r.cfg.MaxBaseFee > 0 && head.BaseFee.Cmp(big.NewInt(int64(r.cfg.MaxBaseFee))) > 0 {
1253-
return nil, nil, nil, fmt.Errorf("base fee is too high, base fee %v exceeds max %v", head.BaseFee, r.cfg.MaxBaseFee)
1268+
return nil, nil, nil, nil, fmt.Errorf("base fee is too high, base fee %v exceeds max %v", head.BaseFee, r.cfg.MaxBaseFee)
12541269
}
12551270
}
12561271

12571272
tip, err := r.L1Client.SuggestGasTipCap(context.Background())
12581273
if err != nil {
1259-
return nil, nil, nil, err
1274+
return nil, nil, nil, nil, err
12601275
}
12611276
log.Info("market fee info", "tip", tip)
12621277

@@ -1265,7 +1280,7 @@ func (r *Rollup) GetGasTipAndCap() (*big.Int, *big.Int, *big.Int, error) {
12651280
tip = new(big.Int).Div(tip, big.NewInt(100))
12661281
}
12671282
if r.cfg.MaxTip > 0 && tip.Cmp(big.NewInt(int64(r.cfg.MaxTip))) > 0 {
1268-
return nil, nil, nil, fmt.Errorf("tip is too high, tip %v exceeds max %v", tip, r.cfg.MaxTip)
1283+
return nil, nil, nil, nil, fmt.Errorf("tip is too high, tip %v exceeds max %v", tip, r.cfg.MaxTip)
12691284
}
12701285

12711286
var gasFeeCap *big.Int
@@ -1282,7 +1297,7 @@ func (r *Rollup) GetGasTipAndCap() (*big.Int, *big.Int, *big.Int, error) {
12821297
var blobFee *big.Int
12831298
if head.ExcessBlobGas != nil {
12841299
log.Info("market blob fee info", "excess blob gas", *head.ExcessBlobGas)
1285-
blobConfig, exist := r.ChainConfigMap[r.chainId.Uint64()]
1300+
blobConfig, exist := types.ChainConfigMap[r.chainId.Uint64()]
12861301
if !exist {
12871302
blobConfig = types.DefaultBlobConfig
12881303
}
@@ -1298,7 +1313,7 @@ func (r *Rollup) GetGasTipAndCap() (*big.Int, *big.Int, *big.Int, error) {
12981313
"blobfee", blobFee,
12991314
)
13001315

1301-
return tip, gasFeeCap, blobFee, nil
1316+
return tip, gasFeeCap, blobFee, head, nil
13021317
}
13031318

13041319
// PreCheck is run before the submitter to check whether the submitter can be started
@@ -1578,7 +1593,7 @@ func (r *Rollup) ReSubmitTx(resend bool, tx *ethtypes.Transaction) (*ethtypes.Tr
15781593
"nonce", tx.Nonce(),
15791594
)
15801595

1581-
tip, gasFeeCap, blobFeeCap, err := r.GetGasTipAndCap()
1596+
tip, gasFeeCap, blobFeeCap, head, err := r.GetGasTipAndCap()
15821597
if err != nil {
15831598
return nil, fmt.Errorf("get gas tip and cap error:%w", err)
15841599
}
@@ -1606,11 +1621,6 @@ func (r *Rollup) ReSubmitTx(resend bool, tx *ethtypes.Transaction) (*ethtypes.Tr
16061621
if r.cfg.MinTip > 0 && tip.Cmp(big.NewInt(int64(r.cfg.MinTip))) < 0 {
16071622
log.Info("replace tip is too low, update tip to min tip ", "tip", tip, "min_tip", r.cfg.MinTip)
16081623
tip = big.NewInt(int64(r.cfg.MinTip))
1609-
// recalc feecap
1610-
head, err := r.L1Client.HeaderByNumber(context.Background(), nil)
1611-
if err != nil {
1612-
return nil, fmt.Errorf("get l1 head error:%w", err)
1613-
}
16141624
var recalculatedFeecap *big.Int
16151625
if head.BaseFee != nil {
16161626
recalculatedFeecap = new(big.Int).Add(
@@ -1640,7 +1650,14 @@ func (r *Rollup) ReSubmitTx(resend bool, tx *ethtypes.Transaction) (*ethtypes.Tr
16401650
Data: tx.Data(),
16411651
})
16421652
case ethtypes.BlobTxType:
1643-
1653+
sidecar := tx.BlobTxSidecar()
1654+
version := types.DetermineBlobVersion(head, r.chainId.Uint64())
1655+
if sidecar.Version == ethtypes.BlobSidecarVersion0 && version == ethtypes.BlobSidecarVersion1 {
1656+
err = types.BlobSidecarVersionToV1(sidecar)
1657+
if err != nil {
1658+
return nil, err
1659+
}
1660+
}
16441661
newTx = ethtypes.NewTx(&ethtypes.BlobTx{
16451662
ChainID: uint256.MustFromBig(tx.ChainId()),
16461663
Nonce: tx.Nonce(),
@@ -1652,7 +1669,7 @@ func (r *Rollup) ReSubmitTx(resend bool, tx *ethtypes.Transaction) (*ethtypes.Tr
16521669
Data: tx.Data(),
16531670
BlobFeeCap: uint256.MustFromBig(blobFeeCap),
16541671
BlobHashes: tx.BlobHashes(),
1655-
Sidecar: tx.BlobTxSidecar(),
1672+
Sidecar: sidecar,
16561673
})
16571674

16581675
default:
@@ -1818,7 +1835,7 @@ func (r *Rollup) CancelTx(tx *ethtypes.Transaction) (*ethtypes.Transaction, erro
18181835
"nonce", tx.Nonce(),
18191836
)
18201837

1821-
tip, gasFeeCap, blobFeeCap, err := r.GetGasTipAndCap()
1838+
tip, gasFeeCap, blobFeeCap, _, err := r.GetGasTipAndCap()
18221839
if err != nil {
18231840
return nil, fmt.Errorf("get gas tip and cap error:%w", err)
18241841
}

0 commit comments

Comments
 (0)