Skip to content

Commit

Permalink
feat: experimental test of different trees and proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Oct 29, 2024
1 parent 8cd9de3 commit dac23e4
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions l1infotreesync/processor_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package l1infotreesync

import (
"fmt"
"testing"

"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/l1infotree"
"github.com/0xPolygon/cdk/l1infotreesync/migrations"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/sync"
"github.com/0xPolygon/cdk/tree"
"github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -263,3 +268,83 @@ func Test_processor_Reorg(t *testing.T) {
})
}
}

func TestProofsFromDifferentTrees(t *testing.T) {
t.Skip("This is an experiment")

l1Tree, err := l1infotree.NewL1InfoTree(log.WithFields("test"), types.DefaultHeight, [][32]byte{})
require.NoError(t, err)

leaves := createTestLeaves(1)

aLeaves := make([][32]byte, len(leaves))
for i, leaf := range leaves {
aLeaves[i] = l1infotree.HashLeafData(
leaf.GlobalExitRoot,
leaf.PreviousBlockHash,
leaf.Timestamp)
}

proof, root, err := l1Tree.ComputeMerkleProof(leaves[0].L1InfoTreeIndex, aLeaves)
require.NoError(t, err)

hashProof := make([]common.Hash, len(proof))
for i, p := range proof {
hashProof[i] = common.BytesToHash(p[:])
}

fmt.Println(root)
fmt.Println(hashProof)
fmt.Println("===========================================================================================================")

dbPath := "file:l1InfoTreeTest?mode=memory&cache=shared"
require.NoError(t, migrations.RunMigrations(dbPath))

dbe, err := db.NewSQLiteDB(dbPath)
require.NoError(t, err)

l1InfoTree := tree.NewAppendOnlyTree(dbe, migrations.L1InfoTreePrefix)

tx, err := db.NewTx(context.Background(), dbe)
require.NoError(t, err)

for _, leaf := range leaves {
err = l1InfoTree.AddLeaf(tx, leaf.BlockNumber, leaf.BlockPosition, types.Leaf{
Index: leaf.L1InfoTreeIndex,
Hash: leaf.Hash,
})

require.NoError(t, err)
}

require.NoError(t, tx.Commit())

pro, err := l1InfoTree.GetProof(context.Background(), leaves[0].L1InfoTreeIndex, leaves[0].GlobalExitRoot)
require.NoError(t, err)

fmt.Println(leaves[0].GlobalExitRoot)
fmt.Println(pro)
}

func createTestLeaves(numOfLeaves int) []*L1InfoTreeLeaf {
leaves := make([]*L1InfoTreeLeaf, 0, numOfLeaves)

for i := 0; i < numOfLeaves; i++ {
leaf := &L1InfoTreeLeaf{
L1InfoTreeIndex: uint32(i),
Timestamp: uint64(i),
BlockNumber: uint64(i),
BlockPosition: uint64(i),
PreviousBlockHash: common.HexToHash(fmt.Sprintf("0x%x", i)),
MainnetExitRoot: common.HexToHash(fmt.Sprintf("0x%x", i)),
RollupExitRoot: common.HexToHash(fmt.Sprintf("0x%x", i)),
}

leaf.GlobalExitRoot = leaf.globalExitRoot()
leaf.Hash = leaf.hash()

leaves = append(leaves, leaf)
}

return leaves
}

0 comments on commit dac23e4

Please sign in to comment.