Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanIliev545 committed Jan 17, 2025
1 parent 03f271c commit 6c62ac0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
54 changes: 54 additions & 0 deletions go/common/merkle/merkle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package merkle

import (
"encoding/json"

smt "github.com/FantasyJony/openzeppelin-merkle-tree-go/standard_merkle_tree"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ten-protocol/go-ten/go/common"
"github.com/ten-protocol/go-ten/go/enclave/crosschain"
)

func UnmarshalCrossChainTree(serializedTree common.SerializedCrossChainTree) ([][]interface{}, error) {
xchainTree := make([][]interface{}, 0) // ["v", "0xblablablabla"]
err := json.Unmarshal(serializedTree, &xchainTree)
if err != nil {
return nil, err
}

for k, value := range xchainTree {
xchainTree[k][1] = gethcommon.HexToHash(value[1].(string))
}
return xchainTree, nil
}

func ComputeCrossChainRootFromBatches(batches []*common.BatchHeader) (gethcommon.Hash, common.SerializedCrossChainTree, error) {
xchainTrees := make([][]interface{}, 0)
for _, batch := range batches {
if len(batch.CrossChainTree) == 0 {
// Batch with no outbound messages; nothing to do.
continue
}
xchainTree, err := UnmarshalCrossChainTree(batch.CrossChainTree)
if err != nil {
return gethcommon.MaxHash, nil, err
}
xchainTrees = append(xchainTrees, xchainTree...)
}

if len(xchainTrees) == 0 {
return gethcommon.MaxHash, nil, nil
}

tree, err := smt.Of(xchainTrees, crosschain.CrossChainEncodings)
if err != nil {
panic(err)
}

serializedTree, err := json.Marshal(xchainTrees)
if err != nil {
return gethcommon.MaxHash, nil, err
}

return gethcommon.Hash(tree.GetRoot()), serializedTree, nil
}
53 changes: 3 additions & 50 deletions go/enclave/components/rollup_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package components

import (
"context"
"encoding/json"
"fmt"

smt "github.com/FantasyJony/openzeppelin-merkle-tree-go/standard_merkle_tree"
"github.com/ethereum/go-ethereum/crypto/kzg4844"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ten-protocol/go-ten/go/common/measure"
"github.com/ten-protocol/go-ten/go/common/merkle"
"github.com/ten-protocol/go-ten/go/enclave/core"
"github.com/ten-protocol/go-ten/go/enclave/crosschain"
"github.com/ten-protocol/go-ten/go/enclave/storage"
"github.com/ten-protocol/go-ten/go/ethadapter"

Expand Down Expand Up @@ -87,7 +85,7 @@ func (rc *rollupConsumerImpl) ProcessRollups(ctx context.Context, rollups []*com
return nil, err
}

crossChainRoot, serializedTree, err := ComputeCrossChainRootFromBatches(batches)
crossChainRoot, serializedTree, err := merkle.ComputeCrossChainRootFromBatches(batches)
if err != nil {
return nil, err
}
Expand All @@ -103,57 +101,12 @@ func (rc *rollupConsumerImpl) ProcessRollups(ctx context.Context, rollups []*com
}

if len(rollupMetadata) < len(rollups) {
panic("some rollups were not processed")
return nil, fmt.Errorf("missing metadata for some rollups")
}

return rollupMetadata, nil
}

func UnmarshalCrossChainTree(serializedTree common.SerializedCrossChainTree) ([][]interface{}, error) {
xchainTree := make([][]interface{}, 0) // ["v", "0xblablablabla"]
err := json.Unmarshal(serializedTree, &xchainTree)
if err != nil {
return nil, err
}

for k, value := range xchainTree {
xchainTree[k][1] = gethcommon.HexToHash(value[1].(string))
}
return xchainTree, nil
}

// todo - move this to a more appropriate place
func ComputeCrossChainRootFromBatches(batches []*common.BatchHeader) (gethcommon.Hash, common.SerializedCrossChainTree, error) {
xchainTrees := make([][]interface{}, 0)
for _, batch := range batches {
if len(batch.CrossChainTree) == 0 {
// Batch with no outbound messages; nothing to do.
continue
}
xchainTree, err := UnmarshalCrossChainTree(batch.CrossChainTree)
if err != nil {
return gethcommon.MaxHash, nil, err
}
xchainTrees = append(xchainTrees, xchainTree...)
}

if len(xchainTrees) == 0 {
return gethcommon.MaxHash, nil, nil
}

tree, err := smt.Of(xchainTrees, crosschain.CrossChainEncodings)
if err != nil {
panic(err)
}

serializedTree, err := json.Marshal(xchainTrees)
if err != nil {
return gethcommon.MaxHash, nil, err
}

return gethcommon.Hash(tree.GetRoot()), serializedTree, nil
}

// GetRollupsFromL1Data - extracts the rollups from the processed L1 data and checks sequencer signature on them
func (rc *rollupConsumerImpl) GetRollupsFromL1Data(processed *common.ProcessedL1Data) ([]*common.ExtRollup, error) {
defer core.LogMethodDuration(rc.logger, measure.NewStopwatch(), "Rollup consumer get rollups from L1 data", log.BlockHashKey, processed.BlockHeader.Hash())
Expand Down
3 changes: 2 additions & 1 deletion go/enclave/components/rollup_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ethereum/go-ethereum/core/types"

"github.com/ten-protocol/go-ten/go/common/merkle"
"github.com/ten-protocol/go-ten/go/enclave/storage"

gethlog "github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -87,7 +88,7 @@ func exportCrossChainData(ctx context.Context, storage storage.Storage, fromSeqN
return nil, err
}

root, _, err := ComputeCrossChainRootFromBatches(canonicalBatches)
root, _, err := merkle.ComputeCrossChainRootFromBatches(canonicalBatches)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6c62ac0

Please sign in to comment.