Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce in-house Merkle tree implementation #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions consensus/polybft/checkpoint_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/merkle-tree"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
merkle "github.com/Ethernal-Tech/merkle-tree"
hclog "github.com/hashicorp/go-hclog"
"github.com/umbracle/ethgo"
bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -310,7 +310,7 @@ func (c *checkpointManager) BuildEventRoot(epoch uint64) (types.Hash, error) {
return types.ZeroHash, err
}

return tree.Hash(), nil
return types.Hash(tree.Hash()), nil
}

// GenerateExitProof generates proof of exit event
Expand Down Expand Up @@ -404,7 +404,7 @@ func (c *checkpointManager) GenerateExitProof(exitID uint64) (types.Proof, error
exitEventHex := hex.EncodeToString(exitEventEncoded)

return types.Proof{
Data: proof,
Data: types.FromMerkleToTypesHash(proof),
Metadata: map[string]interface{}{
"LeafIndex": leafIndex,
"ExitEvent": exitEventHex,
Expand Down
8 changes: 4 additions & 4 deletions consensus/polybft/checkpoint_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/signer"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/merkle-tree"
merkle "github.com/Ethernal-Tech/merkle-tree"
hclog "github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestCheckpointManager_BuildEventRoot(t *testing.T) {

hash, err := checkpointManager.BuildEventRoot(1)
require.NoError(t, err)
require.Equal(t, tree.Hash(), hash)
require.Equal(t, tree.Hash(), merkle.Hash(hash))
})

t.Run("Get exit event root hash - no events", func(t *testing.T) {
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestCheckpointManager_GenerateExitProof(t *testing.T) {
t.Run("Generate and validate exit proof", func(t *testing.T) {
t.Parallel()
// verify generated proof on desired tree
require.NoError(t, merkle.VerifyProof(correctBlockToGetExit, encodedEvents[1], proof.Data, tree.Hash()))
require.NoError(t, merkle.VerifyProof(correctBlockToGetExit, encodedEvents[1], types.FromTypesToMerkleHash(proof.Data), tree.Hash()))
})

t.Run("Generate and validate exit proof - invalid proof", func(t *testing.T) {
Expand All @@ -422,7 +422,7 @@ func TestCheckpointManager_GenerateExitProof(t *testing.T) {

// verify generated proof on desired tree
require.ErrorContains(t, merkle.VerifyProof(correctBlockToGetExit,
encodedEvents[1], invalidProof, tree.Hash()), "not a member of merkle tree")
encodedEvents[1], types.FromTypesToMerkleHash(invalidProof), tree.Hash()), "not a member of merkle tree")
})

t.Run("Generate exit proof - no event", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/sc_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestIntegration_PerformExit(t *testing.T) {
EpochNumber: epochNumber,
CurrentValidatorsHash: accSetHash,
NextValidatorsHash: accSetHash,
EventRoot: eventRoot,
EventRoot: types.Hash(eventRoot),
}

checkpointHash, err := checkpointData.Hash(
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestIntegration_PerformExit(t *testing.T) {
BlockNumber: new(big.Int).SetUint64(blockNumber),
LeafIndex: new(big.Int).SetUint64(leafIndex),
UnhashedLeaf: proofExitEvent,
Proof: proof,
Proof: types.FromMerkleToTypesHash(proof),
}).EncodeAbi()
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/state_store_state_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/merkle-tree"
"github.com/0xPolygon/polygon-edge/types"
merkle "github.com/Ethernal-Tech/merkle-tree"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
Expand Down Expand Up @@ -253,7 +253,7 @@ func createTestCommitmentMessage(t *testing.T, fromIndex uint64) *CommitmentMess
require.NoError(t, err)

msg := &contractsapi.StateSyncCommitment{
Root: tree.Hash(),
Root: types.Hash(tree.Hash()),
StartID: big.NewInt(int64(fromIndex)),
EndID: big.NewInt(int64(fromIndex + maxCommitmentSize - 1)),
}
Expand Down
6 changes: 3 additions & 3 deletions consensus/polybft/state_sync_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/merkle-tree"
"github.com/0xPolygon/polygon-edge/state/runtime/precompiled"
"github.com/0xPolygon/polygon-edge/types"
merkle "github.com/Ethernal-Tech/merkle-tree"
)

const (
Expand Down Expand Up @@ -37,7 +37,7 @@ func NewPendingCommitment(epoch uint64, stateSyncEvents []*contractsapi.StateSyn
StateSyncCommitment: &contractsapi.StateSyncCommitment{
StartID: stateSyncEvents[0].ID,
EndID: stateSyncEvents[len(stateSyncEvents)-1].ID,
Root: tree.Hash(),
Root: types.Hash(tree.Hash()),
},
}, nil
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (cm *CommitmentMessageSigned) VerifyStateSyncProof(proof []types.Hash,
}

return merkle.VerifyProof(stateSync.ID.Uint64()-cm.Message.StartID.Uint64(),
hash, proof, cm.Message.Root)
hash, types.FromTypesToMerkleHash(proof), merkle.Hash(cm.Message.Root))
}

// ContainsStateSync checks if commitment contains given state sync event
Expand Down
16 changes: 8 additions & 8 deletions consensus/polybft/state_sync_commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestCommitmentMessage_Hash(t *testing.T) {
trie2, err := createMerkleTree(stateSyncEvents[0 : len(stateSyncEvents)-1])
require.NoError(t, err)

commitmentMessage1 := newTestCommitmentSigned(t, trie1.Hash(), 2, 8)
commitmentMessage2 := newTestCommitmentSigned(t, trie1.Hash(), 2, 8)
commitmentMessage3 := newTestCommitmentSigned(t, trie1.Hash(), 6, 10)
commitmentMessage4 := newTestCommitmentSigned(t, trie2.Hash(), 2, 8)
commitmentMessage1 := newTestCommitmentSigned(t, types.Hash(trie1.Hash()), 2, 8)
commitmentMessage2 := newTestCommitmentSigned(t, types.Hash(trie1.Hash()), 2, 8)
commitmentMessage3 := newTestCommitmentSigned(t, types.Hash(trie1.Hash()), 6, 10)
commitmentMessage4 := newTestCommitmentSigned(t, types.Hash(trie2.Hash()), 2, 8)

hash1, err := commitmentMessage1.Hash()
require.NoError(t, err)
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestCommitmentMessage_VerifyProof(t *testing.T) {
require.NoError(t, err)

execute := &contractsapi.ExecuteStateReceiverFn{
Proof: proof,
Proof: types.FromMerkleToTypesHash(proof),
Obj: (*contractsapi.StateSync)(stateSync),
}

Expand All @@ -98,7 +98,7 @@ func TestCommitmentMessage_VerifyProof(t *testing.T) {
require.Equal(t, stateSync.Sender, executionStateSync.Obj.Sender)
require.Equal(t, stateSync.Receiver, executionStateSync.Obj.Receiver)
require.Equal(t, stateSync.Data, executionStateSync.Obj.Data)
require.Equal(t, proof, executionStateSync.Proof)
require.Equal(t, proof, types.FromTypesToMerkleHash(executionStateSync.Proof))

err = commitmentSigned.VerifyStateSyncProof(executionStateSync.Proof,
(*contractsapi.StateSyncedEvent)(executionStateSync.Obj))
Expand Down Expand Up @@ -136,11 +136,11 @@ func TestCommitmentMessage_VerifyProof_StateSyncHashNotEqualToProof(t *testing.T
Message: &contractsapi.StateSyncCommitment{
StartID: big.NewInt(fromIndex),
EndID: big.NewInt(toIndex),
Root: tree.Hash(),
Root: types.Hash(tree.Hash()),
},
}

assert.ErrorContains(t, commitment.VerifyStateSyncProof(proof, stateSyncs[4]), "not a member of merkle tree")
assert.ErrorContains(t, commitment.VerifyStateSyncProof(types.FromMerkleToTypesHash(proof), stateSyncs[4]), "not a member of merkle tree")
}

func newTestCommitmentSigned(t *testing.T, root types.Hash, startID, endID int64) *CommitmentMessageSigned {
Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/state_sync_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (s *stateSyncManager) buildProofs(commitmentMsg *contractsapi.StateSyncComm
}

stateSyncProofs[i] = &StateSyncProof{
Proof: p,
Proof: types.FromMerkleToTypesHash(p),
StateSync: event,
}
}
Expand Down
6 changes: 3 additions & 3 deletions consensus/polybft/state_sync_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"testing"

merkle "github.com/Ethernal-Tech/merkle-tree"
"github.com/hashicorp/go-hclog"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
Expand All @@ -18,7 +19,6 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/merkle-tree"
"github.com/0xPolygon/polygon-edge/types"
)

Expand Down Expand Up @@ -237,7 +237,7 @@ func TestStateSyncManager_BuildCommitment(t *testing.T) {
{
MerkleTree: tree,
StateSyncCommitment: &contractsapi.StateSyncCommitment{
Root: tree.Hash(),
Root: types.Hash(tree.Hash()),
StartID: big.NewInt(0),
EndID: big.NewInt(1),
},
Expand Down Expand Up @@ -540,7 +540,7 @@ func TestStateSyncManager_GetProofs_NoProof_BuildProofs(t *testing.T) {
Message: &contractsapi.StateSyncCommitment{
StartID: big.NewInt(fromIndex),
EndID: big.NewInt(maxCommitmentSize),
Root: tree.Hash(),
Root: types.Hash(tree.Hash()),
},
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cloud.google.com/go/secretmanager v1.11.4
github.com/0xPolygon/go-ibft v0.4.1-0.20230717081138-628065cf23b6
github.com/Ethernal-Tech/blockchain-event-tracker v0.0.0-20231202204931-b886edca635a
github.com/Ethernal-Tech/merkle-tree v0.0.0-20231213143318-4db9da419e04
github.com/armon/go-metrics v0.4.1
github.com/aws/aws-sdk-go v1.46.1
github.com/btcsuite/btcd v0.22.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/DataDog/sketches-go v1.4.2 h1:gppNudE9d19cQ98RYABOetxIhpTCl4m7CnbRZjv
github.com/DataDog/sketches-go v1.4.2/go.mod h1:xJIXldczJyyjnbDop7ZZcLxJdV3+7Kra7H1KMgpgkLk=
github.com/Ethernal-Tech/blockchain-event-tracker v0.0.0-20231202204931-b886edca635a h1:IujnjiVu6UcVYhUIAaCN1ZI122VZzTO80epIbF6pDbk=
github.com/Ethernal-Tech/blockchain-event-tracker v0.0.0-20231202204931-b886edca635a/go.mod h1:IgWSrKhiPnqV6M68wQt8MyWNeWwA+3sYZQP6Y6STR2M=
github.com/Ethernal-Tech/merkle-tree v0.0.0-20231213143318-4db9da419e04 h1:DGIOHe3qAeU+mrRLNJ83mJWyH1t5SqN8XvTrwBUAXK4=
github.com/Ethernal-Tech/merkle-tree v0.0.0-20231213143318-4db9da419e04/go.mod h1:vwaNJ2xOVILZoOcD6CxswTg8XOCDvIhcRIp8xanmS3s=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
Expand Down
60 changes: 0 additions & 60 deletions merkle-tree/README.md

This file was deleted.

Loading
Loading