Skip to content

Commit

Permalink
fix: use hex instead of byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Dec 6, 2024
1 parent 07d36a9 commit 1ed284c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
46 changes: 14 additions & 32 deletions agglayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ const (
type Certificate struct {
NetworkID uint32 `json:"network_id"`
Height uint64 `json:"height"`
PrevLocalExitRoot [32]byte `json:"prev_local_exit_root"`
NewLocalExitRoot [32]byte `json:"new_local_exit_root"`
PrevLocalExitRoot common.Hash `json:"prev_local_exit_root"`
NewLocalExitRoot common.Hash `json:"new_local_exit_root"`
BridgeExits []*BridgeExit `json:"bridge_exits"`
ImportedBridgeExits []*ImportedBridgeExit `json:"imported_bridge_exits"`
Metadata common.Hash `json:"metadata"`
Expand All @@ -118,7 +118,7 @@ func (c *Certificate) Brief() string {
return nilStr
}
res := fmt.Sprintf("agglayer.Cert {height: %d prevLER: %s newLER: %s exits: %d imported_exits: %d}", c.Height,
common.Bytes2Hex(c.PrevLocalExitRoot[:]), common.Bytes2Hex(c.NewLocalExitRoot[:]),
c.PrevLocalExitRoot.String(), c.NewLocalExitRoot.String(),
len(c.BridgeExits), len(c.ImportedBridgeExits))
return res
}
Expand All @@ -141,8 +141,8 @@ func (c *Certificate) Hash() common.Hash {
return crypto.Keccak256Hash(
cdkcommon.Uint32ToBytes(c.NetworkID),
cdkcommon.Uint64ToBytes(c.Height),
c.PrevLocalExitRoot[:],
c.NewLocalExitRoot[:],
c.PrevLocalExitRoot.Bytes(),
c.NewLocalExitRoot.Bytes(),
bridgeExitsPart,
importedBridgeExitsPart,
)
Expand All @@ -157,7 +157,7 @@ func (c *Certificate) HashToSign() common.Hash {
}

return crypto.Keccak256Hash(
c.NewLocalExitRoot[:],
c.NewLocalExitRoot.Bytes(),
crypto.Keccak256Hash(globalIndexHashes...).Bytes(),
)
}
Expand Down Expand Up @@ -344,18 +344,13 @@ type MerkleProof struct {

// MarshalJSON is the implementation of the json.Marshaler interface
func (m *MerkleProof) MarshalJSON() ([]byte, error) {
proofsAsBytes := [types.DefaultHeight][types.DefaultHeight]byte{}
for i, proof := range m.Proof {
proofsAsBytes[i] = proof
}

return json.Marshal(&struct {
Root [types.DefaultHeight]byte `json:"root"`
Proof map[string][types.DefaultHeight][types.DefaultHeight]byte `json:"proof"`
Root [types.DefaultHeight]byte `json:"root"`
Proof map[string][types.DefaultHeight]common.Hash `json:"proof"`
}{
Root: m.Root,
Proof: map[string][types.DefaultHeight][types.DefaultHeight]byte{
"siblings": proofsAsBytes,
Proof: map[string][types.DefaultHeight]common.Hash{
"siblings": m.Proof,
},
})
}
Expand Down Expand Up @@ -394,19 +389,6 @@ func (l *L1InfoTreeLeafInner) Hash() common.Hash {
)
}

// MarshalJSON is the implementation of the json.Marshaler interface
func (l *L1InfoTreeLeafInner) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
GlobalExitRoot [types.DefaultHeight]byte `json:"global_exit_root"`
BlockHash [types.DefaultHeight]byte `json:"block_hash"`
Timestamp uint64 `json:"timestamp"`
}{
GlobalExitRoot: l.GlobalExitRoot,
BlockHash: l.BlockHash,
Timestamp: l.Timestamp,
})
}

func (l *L1InfoTreeLeafInner) String() string {
return fmt.Sprintf("GlobalExitRoot: %s, BlockHash: %s, Timestamp: %d",
l.GlobalExitRoot.String(), l.BlockHash.String(), l.Timestamp)
Expand All @@ -415,8 +397,8 @@ func (l *L1InfoTreeLeafInner) String() string {
// L1InfoTreeLeaf represents the leaf of the L1 info tree
type L1InfoTreeLeaf struct {
L1InfoTreeIndex uint32 `json:"l1_info_tree_index"`
RollupExitRoot [32]byte `json:"rer"`
MainnetExitRoot [32]byte `json:"mer"`
RollupExitRoot common.Hash `json:"rer"`
MainnetExitRoot common.Hash `json:"mer"`
Inner *L1InfoTreeLeafInner `json:"inner"`
}

Expand All @@ -428,8 +410,8 @@ func (l *L1InfoTreeLeaf) Hash() common.Hash {
func (l *L1InfoTreeLeaf) String() string {
return fmt.Sprintf("L1InfoTreeIndex: %d, RollupExitRoot: %s, MainnetExitRoot: %s, Inner: %s",
l.L1InfoTreeIndex,
common.Bytes2Hex(l.RollupExitRoot[:]),
common.Bytes2Hex(l.MainnetExitRoot[:]),
l.RollupExitRoot.String(),
l.MainnetExitRoot.String(),
l.Inner.String(),
)
}
Expand Down
4 changes: 2 additions & 2 deletions agglayer/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

const (
expectedSignedCertificateEmptyMetadataJSON = `{"network_id":1,"height":1,"prev_local_exit_root":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"new_local_exit_root":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"bridge_exits":[{"leaf_type":"Transfer","token_info":null,"dest_network":0,"dest_address":"0x0000000000000000000000000000000000000000","amount":"1","metadata":[]}],"imported_bridge_exits":[{"bridge_exit":{"leaf_type":"Transfer","token_info":null,"dest_network":0,"dest_address":"0x0000000000000000000000000000000000000000","amount":"1","metadata":[]},"claim_data":null,"global_index":{"mainnet_flag":false,"rollup_index":1,"leaf_index":1}}],"metadata":"0x0000000000000000000000000000000000000000000000000000000000000000","signature":{"r":"0x0000000000000000000000000000000000000000000000000000000000000000","s":"0x0000000000000000000000000000000000000000000000000000000000000000","odd_y_parity":false}}`
expectedSignedCertificateyMetadataJSON = `{"network_id":1,"height":1,"prev_local_exit_root":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"new_local_exit_root":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"bridge_exits":[{"leaf_type":"Transfer","token_info":null,"dest_network":0,"dest_address":"0x0000000000000000000000000000000000000000","amount":"1","metadata":[1,2,3]}],"imported_bridge_exits":[{"bridge_exit":{"leaf_type":"Transfer","token_info":null,"dest_network":0,"dest_address":"0x0000000000000000000000000000000000000000","amount":"1","metadata":[]},"claim_data":null,"global_index":{"mainnet_flag":false,"rollup_index":1,"leaf_index":1}}],"metadata":"0x0000000000000000000000000000000000000000000000000000000000000000","signature":{"r":"0x0000000000000000000000000000000000000000000000000000000000000000","s":"0x0000000000000000000000000000000000000000000000000000000000000000","odd_y_parity":false}}`
expectedSignedCertificateEmptyMetadataJSON = `{"network_id":1,"height":1,"prev_local_exit_root":"0x0000000000000000000000000000000000000000000000000000000000000000","new_local_exit_root":"0x0000000000000000000000000000000000000000000000000000000000000000","bridge_exits":[{"leaf_type":"Transfer","token_info":null,"dest_network":0,"dest_address":"0x0000000000000000000000000000000000000000","amount":"1","metadata":[]}],"imported_bridge_exits":[{"bridge_exit":{"leaf_type":"Transfer","token_info":null,"dest_network":0,"dest_address":"0x0000000000000000000000000000000000000000","amount":"1","metadata":[]},"claim_data":null,"global_index":{"mainnet_flag":false,"rollup_index":1,"leaf_index":1}}],"metadata":"0x0000000000000000000000000000000000000000000000000000000000000000","signature":{"r":"0x0000000000000000000000000000000000000000000000000000000000000000","s":"0x0000000000000000000000000000000000000000000000000000000000000000","odd_y_parity":false}}`
expectedSignedCertificateyMetadataJSON = `{"network_id":1,"height":1,"prev_local_exit_root":"0x0000000000000000000000000000000000000000000000000000000000000000","new_local_exit_root":"0x0000000000000000000000000000000000000000000000000000000000000000","bridge_exits":[{"leaf_type":"Transfer","token_info":null,"dest_network":0,"dest_address":"0x0000000000000000000000000000000000000000","amount":"1","metadata":[1,2,3]}],"imported_bridge_exits":[{"bridge_exit":{"leaf_type":"Transfer","token_info":null,"dest_network":0,"dest_address":"0x0000000000000000000000000000000000000000","amount":"1","metadata":[]},"claim_data":null,"global_index":{"mainnet_flag":false,"rollup_index":1,"leaf_index":1}}],"metadata":"0x0000000000000000000000000000000000000000000000000000000000000000","signature":{"r":"0x0000000000000000000000000000000000000000000000000000000000000000","s":"0x0000000000000000000000000000000000000000000000000000000000000000","odd_y_parity":false}}`
)

func TestBridgeExitHash(t *testing.T) {
Expand Down

0 comments on commit 1ed284c

Please sign in to comment.