Skip to content

Commit

Permalink
#40 Fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau committed Oct 3, 2018
1 parent fd7798a commit 36d3eb5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 98 deletions.
17 changes: 7 additions & 10 deletions cosmos/poc/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ type CyberdAppDbKeys struct {
acc *sdk.KVStoreKey
accIndex *sdk.KVStoreKey
cidIndex *sdk.KVStoreKey
inLinks *sdk.KVStoreKey
outLinks *sdk.KVStoreKey
links *sdk.KVStoreKey
rank *sdk.KVStoreKey
}

Expand Down Expand Up @@ -64,16 +63,14 @@ func NewCyberdApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*baseapp.
main: sdk.NewKVStoreKey("main"),
acc: sdk.NewKVStoreKey("acc"),
cidIndex: sdk.NewKVStoreKey("cid_index"),
inLinks: sdk.NewKVStoreKey("in_links"),
outLinks: sdk.NewKVStoreKey("out_links"),
links: sdk.NewKVStoreKey("links"),
rank: sdk.NewKVStoreKey("rank"),
}

cis := NewCidIndexStorage(dbKeys.main, dbKeys.cidIndex)
storages := CyberdPersistentStorages{
CidIndex: cis,
InLinks: NewLinksStorage(dbKeys.inLinks, cdc),
OutLinks: NewLinksStorage(dbKeys.outLinks, cdc),
Links: NewLinksStorage(dbKeys.links, cdc),
Rank: NewRankStorage(cis, dbKeys.rank),
}

Expand All @@ -88,12 +85,12 @@ func NewCyberdApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*baseapp.
// define and attach the mappers and keepers
app.accStorage = auth.NewAccountMapper(app.cdc, dbKeys.acc, NewAccount)
app.coinKeeper = bank.NewKeeper(app.accStorage)
app.memStorage = NewInMemoryStorage(storages, app.accStorage)
app.memStorage = &InMemoryStorage{}

// register message routes
app.Router().
AddRoute("bank", NewBankHandler(app.coinKeeper, app.memStorage)).
AddRoute("link", NewLinksHandler(storages.CidIndex, storages.InLinks, storages.OutLinks, app.memStorage))
AddRoute("link", NewLinksHandler(storages.CidIndex, storages.Links, app.memStorage))

// perform initialization logic
app.SetInitChainer(NewGenesisApplier(app.memStorage, app.cdc, app.accStorage))
Expand All @@ -102,12 +99,12 @@ func NewCyberdApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*baseapp.
app.SetAnteHandler(auth.NewAnteHandler(app.accStorage, app.feeCollectionKeeper))

// mount the multistore and load the latest state
app.MountStoresIAVL(dbKeys.main, dbKeys.acc, dbKeys.cidIndex, dbKeys.inLinks, dbKeys.outLinks, dbKeys.rank)
app.MountStoresIAVL(dbKeys.main, dbKeys.acc, dbKeys.cidIndex, dbKeys.links, dbKeys.rank)
err := app.LoadLatestVersion(dbKeys.main)
if err != nil {
cmn.Exit(err.Error())
}
app.memStorage.Load(app.BaseApp.NewContext(true, abci.Header{}))
app.memStorage.Load(app.BaseApp.NewContext(true, abci.Header{}), storages, app.accStorage)

app.Seal()
return app
Expand Down
29 changes: 17 additions & 12 deletions cosmos/poc/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,35 @@ import (
// ils - incoming links storage
// ols - outgoing links storage
// imms - in-memory storage
func NewLinksHandler(cis CidIndexStorage, ils LinksStorage, ols LinksStorage, imms *InMemoryStorage) sdk.Handler {
func NewLinksHandler(cis CidIndexStorage, ls LinksStorage, imms *InMemoryStorage) sdk.Handler {

getCidIndex := func(ctx sdk.Context, cid Cid) CidNumber {

index, exist := imms.GetCidIndex(cid)
if !exist { // new cid
index = cis.GetOrPutCidIndex(ctx, cid)
}
return index
}
getCidNumber := GetCidNumberFunc(cis, imms)

return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {

link := msg.(MsgLink)

linkedCids := LinkedCids{
FromCid: getCidIndex(ctx, link.CidFrom),
ToCid: getCidIndex(ctx, link.CidTo),
FromCid: getCidNumber(ctx, link.CidFrom),
ToCid: getCidNumber(ctx, link.CidTo),
Creator: AccountNumber(link.Address.String()),
}

ils.AddLink(ctx, linkedCids)
ls.AddLink(ctx, linkedCids)
imms.AddLink(linkedCids)
return sdk.Result{}
}

}

func GetCidNumberFunc(cis CidIndexStorage, imms *InMemoryStorage) func(sdk.Context, Cid) CidNumber {

return func(ctx sdk.Context, cid Cid) CidNumber {

index, exist := imms.GetCidIndex(cid)
if !exist { // new cid
index = cis.GetOrPutCidIndex(ctx, cid)
}
return index
}
}
6 changes: 3 additions & 3 deletions cosmos/poc/app/storage/cids.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

var cidsCount = []byte("cids_count")
var lastCidNumber = []byte("cids_count")

type CidIndexStorage struct {
mainStoreKey *sdk.KVStoreKey
Expand Down Expand Up @@ -38,7 +38,7 @@ func (cis CidIndexStorage) GetOrPutCidIndex(ctx sdk.Context, cid Cid) CidNumber

mainStore := ctx.KVStore(cis.mainStoreKey)
cidsIndex.Set(cidAsBytes, lastIndexAsBytes)
mainStore.Set(cidsCount, lastIndexAsBytes)
mainStore.Set(lastCidNumber, lastIndexAsBytes)
return CidNumber(lastIndex)
}

Expand All @@ -49,7 +49,7 @@ func (cis CidIndexStorage) GetOrPutCidIndex(ctx sdk.Context, cid Cid) CidNumber
func (cis CidIndexStorage) GetCidsCount(ctx sdk.Context) uint64 {

mainStore := ctx.KVStore(cis.mainStoreKey)
lastIndexAsBytes := mainStore.Get(cidsCount)
lastIndexAsBytes := mainStore.Get(lastCidNumber)

if lastIndexAsBytes == nil {
return 0
Expand Down
50 changes: 7 additions & 43 deletions cosmos/poc/app/storage/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,25 @@ type InMemoryStorage struct {
cidRank []float64

userStake map[AccountNumber]int64

// persistent storages
persistentStorage CyberdPersistentStorages
am auth.AccountMapper
}

func NewInMemoryStorage(persistentStorage CyberdPersistentStorages, am auth.AccountMapper) *InMemoryStorage {

return &InMemoryStorage{
persistentStorage: persistentStorage,
am: am,
}
}

// Load from underlying persistent storage
// Heavy operation
func (s *InMemoryStorage) Load(ctx sdk.Context) {
func (s *InMemoryStorage) Load(ctx sdk.Context, ps CyberdPersistentStorages, am auth.AccountMapper) {

inLinks, outLinks, err := s.persistentStorage.InLinks.GetAllLinks(ctx)
inLinks, outLinks, err := ps.Links.GetAllLinks(ctx)
if err != nil {
cmn.Exit(err.Error())
}

cidsIndexes := s.persistentStorage.CidIndex.GetFullCidsIndex(ctx)
cidsIndexes := ps.CidIndex.GetFullCidsIndex(ctx)

s.inLinks = inLinks
s.outLinks = outLinks
s.cidsIndexes = cidsIndexes
s.cidsCount = uint64(len(cidsIndexes))
s.userStake = GetAllAccountsStakes(ctx, s.am)
s.cidRank = s.persistentStorage.Rank.GetFullRank(ctx)
s.userStake = GetAllAccountsStakes(ctx, am)
s.cidRank = ps.Rank.GetFullRank(ctx)
}

// Also returns bool flag, whether index exists
Expand All @@ -62,31 +50,7 @@ func (s *InMemoryStorage) UpdateStake(acc sdk.AccAddress, stake int64) {
}

func (s *InMemoryStorage) AddLink(link LinkedCids) {
//
// out links
cidLinks := s.outLinks[link.FromCid]
if cidLinks == nil {
cidLinks = make(CidLinks)
}
users := cidLinks[link.ToCid]
if users == nil {
users = make(map[AccountNumber]struct{})
}
users[link.Creator] = struct{}{}
cidLinks[link.ToCid] = users
s.outLinks[link.FromCid] = cidLinks

//
// in links
cidLinks = s.inLinks[link.ToCid]
if cidLinks == nil {
cidLinks = make(CidLinks)
}
users = cidLinks[link.FromCid]
if users == nil {
users = make(map[AccountNumber]struct{})
}
users[link.Creator] = struct{}{}
cidLinks[link.FromCid] = users
s.inLinks[link.ToCid] = cidLinks
CidsLinks(s.outLinks).Put(link.FromCid, link.ToCid, link.Creator)
CidsLinks(s.inLinks).Put(link.ToCid, link.FromCid, link.Creator)
}
30 changes: 2 additions & 28 deletions cosmos/poc/app/storage/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,8 @@ func (ls LinksStorage) GetAllLinks(ctx sdk.Context) (map[CidNumber]CidLinks, map
return nil, nil, err
}

//
// out links
cidLinks := outLinks[link.FromCid]
if cidLinks == nil {
cidLinks = make(CidLinks)
}
users := cidLinks[link.ToCid]
if users == nil {
users = make(map[AccountNumber]struct{})
}
users[link.Creator] = struct{}{}
cidLinks[link.ToCid] = users
outLinks[link.FromCid] = cidLinks

//
// in links
cidLinks = inLinks[link.ToCid]
if cidLinks == nil {
cidLinks = make(CidLinks)
}
users = cidLinks[link.FromCid]
if users == nil {
users = make(map[AccountNumber]struct{})
}
users[link.Creator] = struct{}{}
cidLinks[link.FromCid] = users
inLinks[link.ToCid] = cidLinks

CidsLinks(outLinks).Put(link.FromCid, link.ToCid, link.Creator)
CidsLinks(inLinks).Put(link.ToCid, link.FromCid, link.Creator)
iterator.Next()
}
iterator.Close()
Expand Down
19 changes: 17 additions & 2 deletions cosmos/poc/app/storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ type LinkedCids struct {
// All addition storages introduced by cyberd
type CyberdPersistentStorages struct {
CidIndex CidIndexStorage
InLinks LinksStorage //incoming links storage
OutLinks LinksStorage //outgoing links storage
Links LinksStorage
Rank RankStorage
}

type CidsLinks map[CidNumber]CidLinks

func (mp CidsLinks) Put(c1 CidNumber, c2 CidNumber, acc AccountNumber) {
cidLinks := mp[c1]
if cidLinks == nil {
cidLinks = make(CidLinks)
}
users := cidLinks[c2]
if users == nil {
users = make(map[AccountNumber]struct{})
}
users[acc] = struct{}{}
cidLinks[c2] = users
mp[c1] = cidLinks
}

0 comments on commit 36d3eb5

Please sign in to comment.