Skip to content

Commit

Permalink
chore: lint and add missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Sep 9, 2024
1 parent bea36fe commit dc43c87
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
51 changes: 51 additions & 0 deletions cmd/sedad/utils/merkle_proof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package utils

import (
"bytes"
"fmt"
"hash"

"golang.org/x/crypto/sha3"
)

// GenerateProof returns the proof of inclusion for the entry at
// index i in the entries list.
func GenerateProof(entries [][]byte, i int) ([][]byte, error) {
if i < 0 || i >= len(entries) {
return nil, fmt.Errorf("index %d is out of bounds", i)
}
if len(entries) == 1 {
return [][]byte{}, nil
}

k := largestPowerOfTwoSmallerThan(len(entries))
recurse := entries[:k]
aggregate := entries[k:]
if i >= k {
i -= k
recurse, aggregate = aggregate, recurse
}
res, err := GenerateProof(recurse, i)
if err != nil {
return nil, err
}
res = append(res, RootFromEntries(aggregate))
return res, nil
}

func VerifyProof(proof [][]byte, root, entry []byte) bool {
return bytes.Equal(processProof(sha3.New256(), proof, entry), root)
}

func processProof(sha hash.Hash, proof [][]byte, entry []byte) []byte {
currentHash := leafHash(sha, entry)

for i := 0; i < len(proof); i++ {
if bytes.Compare(currentHash, proof[i]) == -1 {
currentHash = nodeHash(sha, currentHash, proof[i])
} else {
currentHash = nodeHash(sha, proof[i], currentHash)
}
}
return currentHash
}
9 changes: 2 additions & 7 deletions x/batching/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"

"cosmossdk.io/collections"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sedaprotocol/seda-chain/cmd/sedad/utils"
Expand Down Expand Up @@ -115,11 +116,6 @@ func (k Keeper) ConstructDataResultTree(ctx sdk.Context) ([][]byte, string, erro
return leaves, hex.EncodeToString(root), nil
}

type validatorPower struct {
ValAddr sdk.ValAddress
Power int64
}

// ConstructValidatorTree constructs a validator tree based on the
// validators in the active set and their registered public keys.
// It returns the tree's entries (unhashed leaf contents) and hex-encoded
Expand All @@ -131,9 +127,8 @@ func (k Keeper) ConstructValidatorTree(ctx sdk.Context) ([][]byte, string, error
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return false
} else {
panic(err)
}
panic(err)
}

pkBytes := pubKey.Bytes()
Expand Down
9 changes: 7 additions & 2 deletions x/batching/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"github.com/stretchr/testify/require"

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkstakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
sdkstakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/sedaprotocol/seda-chain/cmd/sedad/utils"
)

Expand All @@ -36,6 +38,7 @@ func Test_ConstructValidatorTree(t *testing.T) {

// Generate proof for each entry and verify.
rootBytes, err := hex.DecodeString(root)
require.NoError(t, err)
for i := range entries {
pf, err := utils.GenerateProof(entries, i)
require.NoError(t, err)
Expand Down Expand Up @@ -75,9 +78,11 @@ func addBatchSigningValidators(t *testing.T, f *fixture, num int) ([]sdk.AccAddr
require.NoError(t, err)
require.NotNil(t, res)

f.stakingKeeper.Keeper.EndBlocker(ctx)
_, err = f.stakingKeeper.Keeper.EndBlocker(ctx)
require.NoError(t, err)

f.pubKeyKeeper.SetValidatorKeyAtIndex(ctx, valAddr, 0, pubKey)
err = f.pubKeyKeeper.SetValidatorKeyAtIndex(ctx, valAddr, 0, pubKey)
require.NoError(t, err)
}
return addrs, pubKeys, powers
}
2 changes: 1 addition & 1 deletion x/batching/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func DefaultGenesisState() *GenesisState {
}

// ValidateGenesis validates batching genesis data.
func ValidateGenesis(gs GenesisState) error {
func ValidateGenesis(_ GenesisState) error {
return nil
}

0 comments on commit dc43c87

Please sign in to comment.