Skip to content

Commit

Permalink
test: move helper functions to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 7, 2024
1 parent 55cb0ff commit b308598
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 69 deletions.
71 changes: 71 additions & 0 deletions agglayer/types_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package agglayer

import (
"fmt"
"testing"

"github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/common"
)

// Helper function to create a dummy TokenInfo
func createDummyTokenInfo(t *testing.T) *TokenInfo {
t.Helper()

return &TokenInfo{
OriginNetwork: 1,
OriginTokenAddress: common.HexToAddress("0x2345"),
}
}

// Helper function to create a dummy GlobalIndex
func createDummyGlobalIndex(t *testing.T) *GlobalIndex {
t.Helper()

return &GlobalIndex{
MainnetFlag: false,
RollupIndex: 10,
LeafIndex: 1,
}
}

// Helper function to create a dummy Claim
func createDummyClaim(t *testing.T) *ClaimFromMainnnet {
t.Helper()

return &ClaimFromMainnnet{
ProofLeafMER: &MerkleProof{
Root: common.HexToHash("0x1234"),
Proof: [common.HashLength]common.Hash{
common.HexToHash("0x1234"),
common.HexToHash("0x5678"),
},
},
ProofGERToL1Root: &MerkleProof{
Root: common.HexToHash("0x5678"),
Proof: [common.HashLength]common.Hash{
common.HexToHash("0x5678"),
common.HexToHash("0x1234"),
},
},
L1Leaf: &L1InfoTreeLeaf{
L1InfoTreeIndex: 1,
RollupExitRoot: common.HexToHash("0x987654321"),
MainnetExitRoot: common.HexToHash("0x123456789"),
Inner: &L1InfoTreeLeafInner{},
},
}
}

// Helper function to create a dummy proof
func createDummyProof(t *testing.T) types.Proof {
t.Helper()

proof := types.Proof{}

for i := 0; i < int(types.DefaultHeight); i++ {
proof[i] = common.HexToHash(fmt.Sprintf("0x%x", i))
}

return proof
}
83 changes: 14 additions & 69 deletions agglayer/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ func TestMarshalJSON(t *testing.T) {
ClaimData: &ClaimFromMainnnet{
ProofLeafMER: &MerkleProof{
Root: common.HexToHash("0x333"),
Proof: generateTestProof(t),
Proof: createDummyProof(t),
},
ProofGERToL1Root: &MerkleProof{
Root: common.HexToHash("0x444"),
Proof: generateTestProof(t),
Proof: createDummyProof(t),
},
L1Leaf: &L1InfoTreeLeaf{
L1InfoTreeIndex: 1,
Expand Down Expand Up @@ -208,15 +208,15 @@ func TestMarshalJSON(t *testing.T) {
ClaimData: &ClaimFromRollup{
ProofLeafLER: &MerkleProof{
Root: common.HexToHash("0x333"),
Proof: generateTestProof(t),
Proof: createDummyProof(t),
},
ProofLERToRER: &MerkleProof{
Root: common.HexToHash("0x444"),
Proof: generateTestProof(t),
Proof: createDummyProof(t),
},
ProofGERToL1Root: &MerkleProof{
Root: common.HexToHash("0x555"),
Proof: generateTestProof(t),
Proof: createDummyProof(t),
},
L1Leaf: &L1InfoTreeLeaf{
L1InfoTreeIndex: 2,
Expand Down Expand Up @@ -474,18 +474,6 @@ func TestUnmarshalCertificateHeaderUnknownError(t *testing.T) {
require.Equal(t, expectedErr, result.Error)
}

func generateTestProof(t *testing.T) types.Proof {
t.Helper()

proof := types.Proof{}

for i := 0; i < int(types.DefaultHeight); i++ {
proof[i] = common.HexToHash(fmt.Sprintf("0x%x", i))
}

return proof
}

func TestConvertNumeric(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -529,49 +517,6 @@ func TestConvertNumeric(t *testing.T) {
}
}

// Helper function to create a dummy TokenInfo (mocking as needed).
func createDummyTokenInfo() *TokenInfo {
return &TokenInfo{
OriginNetwork: 1,
OriginTokenAddress: common.HexToAddress("0x2345"),
}
}

// Helper function to create a dummy GlobalIndex.
func createDummyGlobalIndex() *GlobalIndex {
return &GlobalIndex{
MainnetFlag: false,
RollupIndex: 1,
LeafIndex: 1,
}
}

// Helper function to create a dummy Claim (mock as needed).
func createDummyClaim() *ClaimFromMainnnet {
return &ClaimFromMainnnet{
ProofLeafMER: &MerkleProof{
Root: common.HexToHash("0x1234"),
Proof: [common.HashLength]common.Hash{
common.HexToHash("0x1234"),
common.HexToHash("0x5678"),
},
},
ProofGERToL1Root: &MerkleProof{
Root: common.HexToHash("0x5678"),
Proof: [common.HashLength]common.Hash{
common.HexToHash("0x5678"),
common.HexToHash("0x1234"),
},
},
L1Leaf: &L1InfoTreeLeaf{
L1InfoTreeIndex: 1,
RollupExitRoot: common.HexToHash("0x987654321"),
MainnetExitRoot: common.HexToHash("0x123456789"),
Inner: &L1InfoTreeLeafInner{},
},
}
}

func TestCertificateHash(t *testing.T) {
// Test inputs
prevLocalExitRoot := [common.HashLength]byte{}
Expand All @@ -583,15 +528,15 @@ func TestCertificateHash(t *testing.T) {
bridgeExits := []*BridgeExit{
{
LeafType: LeafTypeAsset,
TokenInfo: createDummyTokenInfo(),
TokenInfo: createDummyTokenInfo(t),
DestinationNetwork: 1,
DestinationAddress: common.HexToAddress("0x0000000000000000000000000000000000000001"),
Amount: big.NewInt(100),
Metadata: []byte("metadata1"),
},
{
LeafType: LeafTypeMessage,
TokenInfo: createDummyTokenInfo(),
TokenInfo: createDummyTokenInfo(t),
DestinationNetwork: 2,
DestinationAddress: common.HexToAddress("0x0000000000000000000000000000000000000002"),
Amount: big.NewInt(200),
Expand All @@ -604,26 +549,26 @@ func TestCertificateHash(t *testing.T) {
{
BridgeExit: &BridgeExit{
LeafType: LeafTypeAsset,
TokenInfo: createDummyTokenInfo(),
TokenInfo: createDummyTokenInfo(t),
DestinationNetwork: 3,
DestinationAddress: common.HexToAddress("0x0000000000000000000000000000000000000003"),
Amount: big.NewInt(300),
Metadata: []byte("metadata3"),
},
ClaimData: createDummyClaim(),
GlobalIndex: createDummyGlobalIndex(),
ClaimData: createDummyClaim(t),
GlobalIndex: createDummyGlobalIndex(t),
},
{
BridgeExit: &BridgeExit{
LeafType: LeafTypeAsset,
TokenInfo: createDummyTokenInfo(),
TokenInfo: createDummyTokenInfo(t),
DestinationNetwork: 4,
DestinationAddress: common.HexToAddress("0x0000000000000000000000000000000000000004"),
Amount: big.NewInt(400),
Metadata: []byte("metadata4"),
},
ClaimData: createDummyClaim(),
GlobalIndex: createDummyGlobalIndex(),
ClaimData: createDummyClaim(t),
GlobalIndex: createDummyGlobalIndex(t),
},
}

Expand Down Expand Up @@ -749,7 +694,7 @@ func TestBridgeExitString(t *testing.T) {
name: "With TokenInfo",
bridgeExit: &BridgeExit{
LeafType: LeafTypeAsset,
TokenInfo: createDummyTokenInfo(),
TokenInfo: createDummyTokenInfo(t),
DestinationNetwork: 100,
DestinationAddress: common.HexToAddress("0x2"),
Amount: big.NewInt(1000),
Expand Down

0 comments on commit b308598

Please sign in to comment.