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

[v2] Update blob header hasher #962

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions core/chainio.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ type Reader interface {
GetNumBlobVersions(ctx context.Context) (uint16, error)

// GetVersionedBlobParams returns the blob version parameters for the given block number and blob version.
GetVersionedBlobParams(ctx context.Context, blobVersion uint8) (*BlobVersionParameters, error)
GetVersionedBlobParams(ctx context.Context, blobVersion uint16) (*BlobVersionParameters, error)

// GetAllVersionedBlobParams returns the blob version parameters for all blob versions at the given block number.
GetAllVersionedBlobParams(ctx context.Context) (map[uint8]*BlobVersionParameters, error)
GetAllVersionedBlobParams(ctx context.Context) (map[uint16]*BlobVersionParameters, error)

// GetActiveReservations returns active reservations (end timestamp > current timestamp)
GetActiveReservations(ctx context.Context, accountIDs []gethcommon.Address) (map[gethcommon.Address]*ActiveReservation, error)
Expand Down
8 changes: 4 additions & 4 deletions core/eth/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func (t *Reader) GetNumBlobVersions(ctx context.Context) (uint16, error) {
})
}

func (t *Reader) GetVersionedBlobParams(ctx context.Context, blobVersion uint8) (*core.BlobVersionParameters, error) {
func (t *Reader) GetVersionedBlobParams(ctx context.Context, blobVersion uint16) (*core.BlobVersionParameters, error) {
params, err := t.bindings.EigenDAServiceManager.GetBlobParams(&bind.CallOpts{
Context: ctx,
}, uint16(blobVersion))
Expand All @@ -661,7 +661,7 @@ func (t *Reader) GetVersionedBlobParams(ctx context.Context, blobVersion uint8)
}, nil
}

func (t *Reader) GetAllVersionedBlobParams(ctx context.Context) (map[uint8]*core.BlobVersionParameters, error) {
func (t *Reader) GetAllVersionedBlobParams(ctx context.Context) (map[uint16]*core.BlobVersionParameters, error) {
if t.bindings.ThresholdRegistry == nil {
return nil, errors.New("threshold registry not deployed")
}
Expand All @@ -671,8 +671,8 @@ func (t *Reader) GetAllVersionedBlobParams(ctx context.Context) (map[uint8]*core
return nil, err
}

res := make(map[uint8]*core.BlobVersionParameters)
for version := uint8(0); version < uint8(numBlobVersions); version++ {
res := make(map[uint16]*core.BlobVersionParameters)
for version := uint16(0); version < uint16(numBlobVersions); version++ {
params, err := t.GetVersionedBlobParams(ctx, version)
if err != nil && strings.Contains(err.Error(), "execution reverted") {
break
Expand Down
6 changes: 3 additions & 3 deletions core/mock/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (t *MockWriter) GetNumBlobVersions(ctx context.Context) (uint16, error) {
return result.(uint16), args.Error(1)
}

func (t *MockWriter) GetVersionedBlobParams(ctx context.Context, blobVersion uint8) (*core.BlobVersionParameters, error) {
func (t *MockWriter) GetVersionedBlobParams(ctx context.Context, blobVersion uint16) (*core.BlobVersionParameters, error) {
args := t.Called()
if args.Get(0) == nil {
return nil, args.Error(1)
Expand All @@ -212,13 +212,13 @@ func (t *MockWriter) GetVersionedBlobParams(ctx context.Context, blobVersion uin
return result.(*core.BlobVersionParameters), args.Error(1)
}

func (t *MockWriter) GetAllVersionedBlobParams(ctx context.Context) (map[uint8]*core.BlobVersionParameters, error) {
func (t *MockWriter) GetAllVersionedBlobParams(ctx context.Context) (map[uint16]*core.BlobVersionParameters, error) {
args := t.Called()
result := args.Get(0)
if result == nil {
return nil, args.Error(1)
}
return result.(map[uint8]*core.BlobVersionParameters), args.Error(1)
return result.(map[uint16]*core.BlobVersionParameters), args.Error(1)
}

func (t *MockWriter) PubkeyHashToOperator(ctx context.Context, operatorId core.OperatorID) (gethcommon.Address, error) {
Expand Down
205 changes: 105 additions & 100 deletions core/v2/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,92 +24,85 @@ type abiBlobCommitments struct {
Commitment abiG1Commit
LengthCommitment abiG2Commit
LengthProof abiG2Commit
Length uint32
}
type abiBlobHeader struct {
BlobVersion uint8
BlobCommitments abiBlobCommitments
QuorumNumbers []byte
PaymentMetadataHash [32]byte
DataLength uint32
}

func blobHeaderArgMarshaling() []abi.ArgumentMarshaling {
return []abi.ArgumentMarshaling{
func (b *BlobHeader) BlobKey() (BlobKey, error) {
versionType, err := abi.NewType("uint16", "", nil)
if err != nil {
return [32]byte{}, err
}
quorumNumbersType, err := abi.NewType("bytes", "", nil)
if err != nil {
return [32]byte{}, err
}
commitmentType, err := abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{
Name: "blobVersion",
Type: "uint8",
Name: "commitment",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256",
},
{
Name: "Y",
Type: "uint256",
},
},
},
{
Name: "blobCommitments",
Name: "lengthCommitment",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "commitment",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256",
},
{
Name: "Y",
Type: "uint256",
},
},
Name: "X",
Type: "uint256[2]",
},
{
Name: "lengthCommitment",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256[2]",
},
{
Name: "Y",
Type: "uint256[2]",
},
},
Name: "Y",
Type: "uint256[2]",
},
},
},
{
Name: "lengthProof",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "lengthProof",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256[2]",
},
{
Name: "Y",
Type: "uint256[2]",
},
},
Name: "X",
Type: "uint256[2]",
},
{
Name: "length",
Type: "uint32",
Name: "Y",
Type: "uint256[2]",
},
},
},
{
Name: "quorumNumbers",
Type: "bytes",
Name: "dataLength",
Type: "uint32",
},
})
if err != nil {
return [32]byte{}, err
}
arguments := abi.Arguments{
{
Type: versionType,
},
{
Name: "paymentMetadataHash",
Type: "bytes32",
Type: quorumNumbersType,
},
{
Type: commitmentType,
},
}
}

func (b *BlobHeader) toABIStruct() (abiBlobHeader, error) {
paymentHash, err := b.PaymentMetadata.Hash()
if err != nil {
return abiBlobHeader{}, err
}
return abiBlobHeader{
BlobVersion: uint8(b.BlobVersion),
BlobCommitments: abiBlobCommitments{
packedBytes, err := arguments.Pack(
b.BlobVersion,
b.QuorumNumbers,
abiBlobCommitments{
Commitment: abiG1Commit{
X: b.BlobCommitments.Commitment.X.BigInt(new(big.Int)),
Y: b.BlobCommitments.Commitment.Y.BigInt(new(big.Int)),
Expand All @@ -134,82 +127,94 @@ func (b *BlobHeader) toABIStruct() (abiBlobHeader, error) {
b.BlobCommitments.LengthProof.Y.A1.BigInt(new(big.Int)),
},
},
Length: uint32(b.BlobCommitments.Length),
DataLength: uint32(b.BlobCommitments.Length),
},
QuorumNumbers: b.QuorumNumbers,
PaymentMetadataHash: paymentHash,
}, nil
}
)
if err != nil {
return [32]byte{}, err
}

func (b *BlobHeader) BlobKey() (BlobKey, error) {
blobHeaderType, err := abi.NewType("tuple", "", blobHeaderArgMarshaling())
var headerHash [32]byte
hasher := sha3.NewLegacyKeccak256()
hasher.Write(packedBytes)
copy(headerHash[:], hasher.Sum(nil)[:32])

blobKeyType, err := abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{
Name: "blobHeaderHash",
Type: "bytes32",
},
{
Name: "paymentMetadataHash",
Type: "bytes32",
},
})
if err != nil {
return [32]byte{}, err
}

arguments := abi.Arguments{
arguments = abi.Arguments{
{
Type: blobHeaderType,
Type: blobKeyType,
},
}

s, err := b.toABIStruct()
paymentMetadataHash, err := b.PaymentMetadata.Hash()
if err != nil {
return [32]byte{}, err
}

bytes, err := arguments.Pack(s)
s2 := struct {
BlobHeaderHash [32]byte
PaymentMetadataHash [32]byte
}{
BlobHeaderHash: headerHash,
PaymentMetadataHash: paymentMetadataHash,
}

packedBytes, err = arguments.Pack(s2)
if err != nil {
return [32]byte{}, err
}

var headerHash [32]byte
hasher := sha3.NewLegacyKeccak256()
hasher.Write(bytes)
copy(headerHash[:], hasher.Sum(nil)[:32])
var blobKey [32]byte
hasher = sha3.NewLegacyKeccak256()
hasher.Write(packedBytes)
copy(blobKey[:], hasher.Sum(nil)[:32])

return headerHash, nil
return blobKey, nil
}

func (c *BlobCertificate) Hash() ([32]byte, error) {
if c.BlobHeader == nil {
return [32]byte{}, fmt.Errorf("blob header is nil")
}

blobCertType, err := abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{
Name: "blobHeader",
Type: "tuple",
Components: blobHeaderArgMarshaling(),
},
{
Name: "relayKeys",
Type: "uint32[]",
},
})
blobKeyType, err := abi.NewType("bytes32", "", nil)
if err != nil {
return [32]byte{}, err
}

relayKeysType, err := abi.NewType("uint32[]", "", nil)
if err != nil {
return [32]byte{}, err
}

arguments := abi.Arguments{
{
Type: blobCertType,
Type: blobKeyType,
},
{
Type: relayKeysType,
},
}

bh, err := c.BlobHeader.toABIStruct()
blobKey, err := c.BlobHeader.BlobKey()
if err != nil {
return [32]byte{}, err
}
s := struct {
BlobHeader abiBlobHeader
RelayKeys []RelayKey
}{
BlobHeader: bh,
RelayKeys: c.RelayKeys,
}

bytes, err := arguments.Pack(s)
bytes, err := arguments.Pack(blobKey, c.RelayKeys)
if err != nil {
return [32]byte{}, err
}
Expand Down
11 changes: 7 additions & 4 deletions core/v2/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ func TestBlobKeyFromHeader(t *testing.T) {
AccountID: "0x123",
ReservationPeriod: 5,
CumulativePayment: big.NewInt(100),
Salt: 42,
},
Signature: []byte{1, 2, 3},
}
blobKey, err := bh.BlobKey()
assert.NoError(t, err)
// 0x1354b29d9dd9a332959795d17f456c219566417fdbf1a7b4f5d118f5c2a36bbd verified in solidity
assert.Equal(t, "1354b29d9dd9a332959795d17f456c219566417fdbf1a7b4f5d118f5c2a36bbd", blobKey.Hex())
// 0x22c9e31c3d79c7c4085b564113f488019cbae18198c9a4fc4ecd70a5742e8638 verified in solidity
assert.Equal(t, "22c9e31c3d79c7c4085b564113f488019cbae18198c9a4fc4ecd70a5742e8638", blobKey.Hex())
}

func TestBatchHeaderHash(t *testing.T) {
Expand Down Expand Up @@ -102,6 +103,7 @@ func TestBlobCertHash(t *testing.T) {
AccountID: "0x123",
ReservationPeriod: 5,
CumulativePayment: big.NewInt(100),
Salt: 42,
},
Signature: []byte{1, 2, 3},
},
Expand All @@ -110,8 +112,8 @@ func TestBlobCertHash(t *testing.T) {

hash, err := blobCert.Hash()
assert.NoError(t, err)
// 0xad938e477d0bc1f9f4e8de7c5cd837560bdbb2dc7094207a7ad53e7442611a43 verified in solidity
assert.Equal(t, "ad938e477d0bc1f9f4e8de7c5cd837560bdbb2dc7094207a7ad53e7442611a43", hex.EncodeToString(hash[:]))
// 0x182087a394c8aab23e8da107c820679333c1efee66fd4380ba283c0e4c09efd6 verified in solidity
assert.Equal(t, "182087a394c8aab23e8da107c820679333c1efee66fd4380ba283c0e4c09efd6", hex.EncodeToString(hash[:]))
}

func TestBlobCertSerialization(t *testing.T) {
Expand All @@ -130,6 +132,7 @@ func TestBlobCertSerialization(t *testing.T) {
AccountID: "0x123",
ReservationPeriod: 5,
CumulativePayment: big.NewInt(100),
Salt: 42,
},
Signature: []byte{1, 2, 3},
},
Expand Down
Loading
Loading