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

Remove merkling of graph #501

Merged
merged 1 commit into from
Mar 19, 2020
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
11 changes: 1 addition & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,16 +573,7 @@ func (app *CyberdApp) appHash() []byte {
return make([]byte, 0)
}

linkHash := app.linkIndexedKeeper.GetNetworkLinkHash()
rankHash := app.rankStateKeeper.GetNetworkRankHash()

result := make([]byte, len(linkHash))

for i := 0; i < len(linkHash); i++ {
result[i] = linkHash[i] ^ rankHash[i]
}

return result
return app.rankStateKeeper.GetNetworkRankHash()
}

func (app *CyberdApp) LoadHeight(height int64) error {
Expand Down
5 changes: 2 additions & 3 deletions x/link/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type KeeperI interface {
GetAllLinksFiltered(sdk.Context, types.LinkFilter) (types.Links, types.Links, error)

GetLinksCount(sdk.Context) uint64
Iterate(sdk.Context, func(types.CompactLink))
IterateLinks(sdk.Context, func(types.CompactLink))
IterateBinaryLinks(sdk.Context, func([]byte))

PutLink(sdk.Context, types.CompactLink)
WriteLinks(sdk.Context, io.Writer) error
Expand All @@ -38,8 +39,6 @@ type IndexedKeeperI interface {
GetCurrentBlockLinks() []types.CompactLink
GetCurrentBlockNewLinks() []types.CompactLink

GetNetworkLinkHash() []byte

IsAnyLinkExist(from types.CidNumber, to types.CidNumber) bool
IsLinkExist(types.CompactLink) bool

Expand Down
19 changes: 1 addition & 18 deletions x/link/internal/keeper/index.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package keeper

import (
"github.com/cybercongress/go-cyber/merkle"
"github.com/cybercongress/go-cyber/util"
"github.com/cybercongress/go-cyber/x/link/internal/types"

sdk "github.com/cosmos/cosmos-sdk/types"
tmos "github.com/tendermint/tendermint/libs/os"

"crypto/sha256"
"encoding/binary"
"io"
)
Expand All @@ -25,13 +23,10 @@ type IndexedKeeper struct {
nextRankOutLinks types.Links

currentBlockLinks []types.CompactLink
MerkleTree *merkle.Tree
}

func NewIndexedKeeper(keeper *Keeper) *IndexedKeeper {
merkleTree := merkle.NewTree(sha256.New(), true)

return &IndexedKeeper{Keeper: keeper, MerkleTree: merkleTree}
return &IndexedKeeper{Keeper: keeper}
}

func (i *IndexedKeeper) Load(rankCtx sdk.Context, freshCtx sdk.Context) {
Expand All @@ -53,11 +48,6 @@ func (i *IndexedKeeper) Load(rankCtx sdk.Context, freshCtx sdk.Context) {

i.nextRankInLinks = newInLinks
i.nextRankOutLinks = newOutLinks

i.Iterate(freshCtx, func(link types.CompactLink) {
linkAsBytes := link.MarshalBinary()
i.MerkleTree.Push(linkAsBytes)
})
}

func (i *IndexedKeeper) FixLinks() {
Expand Down Expand Up @@ -87,9 +77,6 @@ func (i *IndexedKeeper) PutLink(ctx sdk.Context, link types.CompactLink) {
i.currentBlockLinks = append(i.currentBlockLinks, link)
}

linkAsBytes := link.MarshalBinary()
i.MerkleTree.Push(linkAsBytes)

i.Keeper.PutLink(ctx, link)
}

Expand Down Expand Up @@ -119,10 +106,6 @@ func (i *IndexedKeeper) GetCurrentBlockNewLinks() []types.CompactLink {
return result
}

func (i *IndexedKeeper) GetNetworkLinkHash() []byte {
return i.MerkleTree.RootHash()
}

func (i *IndexedKeeper) IsAnyLinkExist(from types.CidNumber, to types.CidNumber) bool {
return i.currentRankOutLinks.IsAnyLinkExist(from, to) || i.nextRankOutLinks.IsAnyLinkExist(from, to)
}
Expand Down
22 changes: 8 additions & 14 deletions x/link/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (lk Keeper) GetAllLinksFiltered(ctx sdk.Context, filter types.LinkFilter) (
inLinks := make(map[types.CidNumber]types.CidLinks)
outLinks := make(map[types.CidNumber]types.CidLinks)

lk.Iterate(ctx, func(link types.CompactLink) {
lk.IterateLinks(ctx, func(link types.CompactLink) {
if filter(link) {
types.Links(outLinks).Put(link.From(), link.To(), link.Acc())
types.Links(inLinks).Put(link.To(), link.From(), link.Acc())
Expand All @@ -73,22 +73,16 @@ func (lk Keeper) GetLinksCount(ctx sdk.Context) uint64 {
return lk.ms.GetLinksCount(ctx)
}

func (lk Keeper) Iterate(ctx sdk.Context, process func(link types.CompactLink)) {
lk.IterateTillVersion(ctx, func(bytes []byte) {
func (lk Keeper) IterateLinks(ctx sdk.Context, process func(link types.CompactLink)) {
lk.IterateBinaryLinks(ctx, func(bytes []byte) {
process(types.UnmarshalBinaryLink(bytes))
}, ctx.BlockHeight())
})
}

func (lk Keeper) IterateTillVersion(ctx sdk.Context, process func(bytes []byte), ver int64) {
func (lk Keeper) IterateBinaryLinks(ctx sdk.Context, process func(bytes []byte)) {
store := ctx.KVStore(lk.storeKey)

startAsBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(startAsBytes, uint64(1))

endAsBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(endAsBytes, uint64(ver+1)) // Iterator end is exclusive.

iterator := store.Iterator(startAsBytes, endAsBytes)
iterator := store.Iterator(nil, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand All @@ -115,12 +109,12 @@ func (lk Keeper) WriteLinks(ctx sdk.Context, writer io.Writer) (err error) {
return
}

lk.IterateTillVersion(ctx, func(bytes []byte) {
lk.IterateBinaryLinks(ctx, func(bytes []byte) {
_, err = writer.Write(bytes)
if err != nil {
return
}
}, ctx.BlockHeight())
})

return nil
}
Expand Down