Skip to content

Commit

Permalink
#40 introduce in-memory store
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau authored and arturalbov committed Oct 3, 2018
1 parent 8d4629d commit 7eb2a52
Show file tree
Hide file tree
Showing 19 changed files with 459 additions and 208 deletions.
55 changes: 0 additions & 55 deletions cosmos/poc/app/account.go

This file was deleted.

29 changes: 18 additions & 11 deletions cosmos/poc/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/ibc"
. "github.com/cybercongress/cyberd/cosmos/poc/app/bank"
. "github.com/cybercongress/cyberd/cosmos/poc/app/storage"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
Expand All @@ -21,6 +23,7 @@ const (
type CyberdAppDbKeys struct {
main *sdk.KVStoreKey
acc *sdk.KVStoreKey
accIndex *sdk.KVStoreKey
cidIndex *sdk.KVStoreKey
cidIns *sdk.KVStoreKey
cidOuts *sdk.KVStoreKey
Expand All @@ -39,13 +42,15 @@ type CyberdApp struct {
dbKeys CyberdAppDbKeys

// manage getting and setting accounts
accountMapper auth.AccountMapper
accStorage auth.AccountMapper
cidIndexMapper CidIndexStorage
inCidsMapper LinksStorage
outCidsMapper LinksStorage
feeCollectionKeeper auth.FeeCollectionKeeper
coinKeeper bank.Keeper
ibcMapper ibc.Mapper

memStorage *InMemoryStorage
}

// NewBasecoinApp returns a reference to a new CyberdApp given a
Expand Down Expand Up @@ -75,32 +80,34 @@ func NewCyberdApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*baseapp.
}

// define and attach the mappers and keepers
app.accountMapper = auth.NewAccountMapper(cdc, dbKeys.acc, NewAccount)
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.cidIndexMapper = CidIndexStorage{mainStoreKey: dbKeys.main, indexKey: dbKeys.cidIndex, cdc: cdc}
app.inCidsMapper = LinksStorage{key: dbKeys.cidIns, cdc: cdc}
app.outCidsMapper = LinksStorage{key: dbKeys.cidOuts, cdc: cdc}
app.accStorage = auth.NewAccountMapper(app.cdc, dbKeys.acc, NewAccount)
app.coinKeeper = bank.NewKeeper(app.accStorage)
app.cidIndexMapper = NewCidIndexStorage(dbKeys.main, dbKeys.cidIndex)
app.inCidsMapper = NewLinksStorage(dbKeys.cidIns, app.cdc)
app.outCidsMapper = NewLinksStorage(dbKeys.cidOuts, app.cdc)

app.memStorage = NewInMemoryStorage(app.cidIndexMapper, app.inCidsMapper, app.outCidsMapper, app.accStorage)

// register message routes
app.Router().
AddRoute("bank", bank.NewHandler(app.coinKeeper)).
AddRoute("link", NewLinksHandler(app.cidIndexMapper, app.inCidsMapper, app.outCidsMapper))
AddRoute("bank", NewBankHandler(app.coinKeeper, app.memStorage)).
AddRoute("link", NewLinksHandler(app.cidIndexMapper, app.inCidsMapper, app.outCidsMapper, app.memStorage))

// perform initialization logic
app.SetInitChainer(app.initChainer)
app.SetInitChainer(NewGenesisApplier(app.memStorage, app.cdc, app.accStorage))
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
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.cidIns, dbKeys.cidOuts, dbKeys.rank)
err := app.LoadLatestVersion(dbKeys.main)
if err != nil {
cmn.Exit(err.Error())
}
app.memStorage.Load(app.BaseApp.NewContext(true, abci.Header{}))

app.Seal()

return app
}

Expand Down
41 changes: 41 additions & 0 deletions cosmos/poc/app/bank/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package bank

import (
sdk "github.com/cosmos/cosmos-sdk/types"
. "github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cybercongress/cyberd/cosmos/poc/app/coin"
"github.com/cybercongress/cyberd/cosmos/poc/app/storage"
"reflect"
)

// NewHandler returns a handler for "bank" type messages.
func NewBankHandler(k Keeper, imms *storage.InMemoryStorage) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgSend:
return handleMsgSend(ctx, k, msg, imms)
default:
errMsg := "Unrecognized bank Msg type: " + reflect.TypeOf(msg).Name()
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}

// NOTE: totalIn == totalOut should already have been checked
func handleMsgSend(ctx sdk.Context, k Keeper, msg MsgSend, imms *storage.InMemoryStorage) sdk.Result {

tags, err := k.InputOutputCoins(ctx, msg.Inputs, msg.Outputs)
if err != nil {
return err.Result()
}

for _, input := range msg.Inputs {
imms.UpdateStake(input.Address, -input.Coins.AmountOf(coin.CBD).Int64())
}

for _, output := range msg.Outputs {
imms.UpdateStake(output.Address, output.Coins.AmountOf(coin.CBD).Int64())
}

return sdk.Result{Tags: tags}
}
5 changes: 5 additions & 0 deletions cosmos/poc/app/coin/coins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package coin

const (
CBD = "CBD"
)
3 changes: 1 addition & 2 deletions cosmos/poc/app/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
const (
DefaultCodespace sdk.CodespaceType = 2

CodeInvalidCid sdk.CodeType = 4201
CodeInvalidCid sdk.CodeType = 4201
)

// NOTE: Don't stringer this, we'll put better messages in later.
Expand Down Expand Up @@ -41,4 +41,3 @@ func newError(codespace sdk.CodespaceType, code sdk.CodeType, msg string) sdk.Er
msg = msgOrDefaultMsg(msg, code)
return sdk.NewError(codespace, code, msg)
}

2 changes: 1 addition & 1 deletion cosmos/poc/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (app *CyberdApp) ExportAppStateAndValidators() (appState json.RawMessage, v
return false
}

app.accountMapper.IterateAccounts(ctx, appendAccountsFn)
app.accStorage.IterateAccounts(ctx, appendAccountsFn)

genState := GenesisState{Accounts: accounts}
appState, err = wire.MarshalJSONIndent(app.cdc, genState)
Expand Down
59 changes: 44 additions & 15 deletions cosmos/poc/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,63 @@ package app

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cybercongress/cyberd/cosmos/poc/app/coin"
"github.com/cybercongress/cyberd/cosmos/poc/app/storage"
abci "github.com/tendermint/tendermint/abci/types"
)

// GenesisState reflects the genesis state of the application.
type GenesisState struct {
Accounts []*GenesisAccount `json:"accounts"`
}

// GenesisAccount reflects a genesis account the application expects in it's
// genesis state.
type GenesisAccount struct {
Name string `json:"name"`
Address sdk.AccAddress `json:"address"`
Coins sdk.Coins `json:"coins"`
}

// initChainer implements the custom application logic that the BaseApp will
// invoke upon initialization. In this case, it will take the application's
// state provided by 'req' and attempt to deserialize said state. The state
// should contain all the genesis accounts. These accounts will be added to the
// application's account mapper.
func (app *CyberdApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
stateJSON := req.AppStateBytes

genesisState := new(GenesisState)
err := app.cdc.UnmarshalJSON(stateJSON, genesisState)
if err != nil {
// TODO: https://github.com/cosmos/cosmos-sdk/issues/468
panic(err)
}

for _, gacc := range genesisState.Accounts {
acc, err := gacc.ToBaseAccount()
func NewGenesisApplier(imms *storage.InMemoryStorage, cdc *wire.Codec, accStorage auth.AccountMapper) sdk.InitChainer {

return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {

stateJSON := req.AppStateBytes
genesisState := new(GenesisState)
err := cdc.UnmarshalJSON(stateJSON, genesisState)

if err != nil {
// TODO: https://github.com/cosmos/cosmos-sdk/issues/468
panic(err)
}

acc.AccountNumber = app.accountMapper.GetNextAccountNumber(ctx)
app.accountMapper.SetAccount(ctx, acc)
for _, gacc := range genesisState.Accounts {
acc, err := gacc.ToBaseAccount()
if err != nil {
panic(err)
}

acc.AccountNumber = accStorage.GetNextAccountNumber(ctx)
accStorage.SetAccount(ctx, acc)
imms.UpdateStake(acc.Address, acc.Coins.AmountOf(coin.CBD).Int64())
}

return abci.ResponseInitChain{}
}
}

return abci.ResponseInitChain{}
// ToAppAccount converts a GenesisAccount to an AppAccount.
func (ga *GenesisAccount) ToBaseAccount() (acc *auth.BaseAccount, err error) {
return &auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins.Sort(),
}, nil
}
37 changes: 24 additions & 13 deletions cosmos/poc/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@ package app

import (
sdk "github.com/cosmos/cosmos-sdk/types"
. "github.com/cybercongress/cyberd/cosmos/poc/app/storage"
)

type ContentIdLinks struct {
ContentID string `json:"cid"`
LinkedCIDS map[string]int `json:"linkedCids"`
}

// NewHandler returns a handler for "link" type messages.
// cis - cids index storage
// ils - incoming links storage
// ols - outgoing links storage
func NewLinksHandler(cis CidIndexStorage, ils LinksStorage, ols LinksStorage) sdk.Handler {
// cis - cids index storage
// ils - incoming links storage
// ols - outgoing links storage
// imms - in-memory storage
func NewLinksHandler(cis CidIndexStorage, ils LinksStorage, ols 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
}

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

link := msg.(MsgLink)

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

ils.AddLink(ctx, link.Address, link.ContentID2, link.ContentID1)
ols.AddLink(ctx, link.Address, link.ContentID1, link.ContentID2)
ils.AddLink(ctx, linkedCids)
imms.AddLink(linkedCids)
return sdk.Result{}
}

}
Loading

0 comments on commit 7eb2a52

Please sign in to comment.