Skip to content

Commit

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

Backports #1047 to main

---

#### 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 authored Jul 28, 2023
1 parent 79162a7 commit d97ea41
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 @@ -294,12 +294,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 @@ -316,33 +315,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 @@ -381,7 +369,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 @@ -417,7 +404,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 @@ -558,9 +544,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 d97ea41

Please sign in to comment.