Skip to content

Commit

Permalink
feat: remove square size from qgb commitment creation (#1047)
Browse files Browse the repository at this point in the history
## Description

Closes #1040

After merging this one, I will cherry-pick this change for main. Then, I
will open a separate PR for main to remove the square size and the data
hash from the `Data` struct.

---


#### PR checklist

- [ ] Tests written/updated
- [ ] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
  • Loading branch information
rach-id committed Jul 26, 2023
1 parent 8c9a9af commit 9dd1ac1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
31 changes: 8 additions & 23 deletions rpc/core/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,11 @@ func To32PaddedHexBytes(number uint64) ([]byte, error) {
// The commitments will be signed by orchestrators and submitted to an EVM chain via a relayer.
// For more information: https://github.com/celestiaorg/quantum-gravity-bridge/blob/master/src/DataRootTuple.sol
type DataRootTuple struct {
height uint64
dataRoot [32]byte
squareSize uint64
height uint64
dataRoot [32]byte
}

// EncodeDataRootTuple takes a height, a data root and the square size, and returns the equivalent of
// EncodeDataRootTuple takes a height and a data root, and returns the equivalent of
// `abi.encode(...)` in Ethereum.
// The encoded type is a DataRootTuple, which has the following ABI:
//
Expand All @@ -283,33 +282,22 @@ type DataRootTuple struct {
// "type":"bytes32"
// },
// {
// "internalType":"uint256",
// "name":"squareSize",
// "type":"uint256"
// },
// {
// "internalType":"structDataRootTuple",
// "name":"_tuple",
// "type":"tuple"
// }
// ]
// }
//
// padding the hex representation of the height padded to 32 bytes concatenated to the data root concatenated
// to the hex representation of the square size padded to 32 bytes.
// padding the hex representation of the height padded to 32 bytes concatenated to the data root.
// For more information, refer to:
// https://github.com/celestiaorg/quantum-gravity-bridge/blob/master/src/DataRootTuple.sol
func EncodeDataRootTuple(height uint64, dataRoot [32]byte, squareSize uint64) ([]byte, error) {
func EncodeDataRootTuple(height uint64, dataRoot [32]byte) ([]byte, error) {
paddedHeight, err := To32PaddedHexBytes(height)
if err != nil {
return nil, err
}
dataSlice := dataRoot[:]
paddedSquareSize, err := To32PaddedHexBytes(squareSize)
if err != nil {
return nil, err
}
return append(paddedHeight, append(dataSlice, paddedSquareSize...)...), nil
return append(paddedHeight, dataRoot[:]...), nil
}

// validateDataCommitmentRange runs basic checks on the asc sorted list of
Expand Down Expand Up @@ -348,7 +336,6 @@ func hashDataRootTuples(tuples []DataRootTuple) ([]byte, error) {
encodedTuple, err := EncodeDataRootTuple(
tuple.height,
tuple.dataRoot,
tuple.squareSize,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -384,7 +371,6 @@ func proveDataRootTuples(tuples []DataRootTuple, height int64) (*merkle.Proof, e
encodedTuple, err := EncodeDataRootTuple(
tuple.height,
tuple.dataRoot,
tuple.squareSize,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -525,9 +511,8 @@ func fetchDataRootTuples(start, end uint64) ([]DataRootTuple, error) {
return nil, fmt.Errorf("couldn't load block %d", height)
}
tuples = append(tuples, DataRootTuple{
height: uint64(block.Height),
dataRoot: *(*[32]byte)(block.DataHash),
squareSize: block.SquareSize,
height: uint64(block.Height),
dataRoot: *(*[32]byte)(block.DataHash),
})
}
return tuples, nil
Expand Down
11 changes: 3 additions & 8 deletions rpc/core/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,23 @@ func TestBlockResults(t *testing.T) {
func TestEncodeDataRootTuple(t *testing.T) {
height := uint64(2)
dataRoot, err := hex.DecodeString("82dc1607d84557d3579ce602a45f5872e821c36dbda7ec926dfa17ebc8d5c013")
squareSize := uint64(64)
require.NoError(t, err)

expectedEncoding, err := hex.DecodeString(
// hex representation of height padded to 32 bytes
"0000000000000000000000000000000000000000000000000000000000000002" +
// data root
"82dc1607d84557d3579ce602a45f5872e821c36dbda7ec926dfa17ebc8d5c013" +
// original square size
"0000000000000000000000000000000000000000000000000000000000000040",
"82dc1607d84557d3579ce602a45f5872e821c36dbda7ec926dfa17ebc8d5c013",
)
require.NoError(t, err)
require.NotNil(t, expectedEncoding)

actualEncoding, err := EncodeDataRootTuple(height, *(*[32]byte)(dataRoot), squareSize)
actualEncoding, err := EncodeDataRootTuple(height, *(*[32]byte)(dataRoot))
require.NoError(t, err)
require.NotNil(t, actualEncoding)

// Check that the length of packed data is correct
assert.Equal(t, len(actualEncoding), 96)
assert.Equal(t, len(actualEncoding), 64)
assert.Equal(t, expectedEncoding, actualEncoding)
}

Expand Down Expand Up @@ -191,7 +188,6 @@ func TestDataCommitmentResults(t *testing.T) {
encodedTuple, err := EncodeDataRootTuple(
uint64(blocks[tc.beginQuery+i].Height),
*(*[32]byte)(blocks[tc.beginQuery+i].DataHash),
blocks[tc.beginQuery+i].SquareSize,
)
require.NoError(t, err)
dataRootEncodedTuples[i] = encodedTuple
Expand Down Expand Up @@ -266,7 +262,6 @@ func TestDataRootInclusionProofResults(t *testing.T) {
encodedTuple, err := EncodeDataRootTuple(
uint64(blocks[tc.firstQuery+i].Height),
*(*[32]byte)(blocks[tc.firstQuery+i].DataHash),
blocks[tc.firstQuery+i].SquareSize,
)
require.NoError(t, err)
dataRootEncodedTuples[i] = encodedTuple
Expand Down

0 comments on commit 9dd1ac1

Please sign in to comment.