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

WIP #114 Add cbdbank module #120

Merged
merged 6 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
117 changes: 73 additions & 44 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
sdkbank "github.com/cosmos/cosmos-sdk/x/bank"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
. "github.com/cybercongress/cyberd/app/bank"
"github.com/cybercongress/cyberd/app/coin"
. "github.com/cybercongress/cyberd/app/genesis"
. "github.com/cybercongress/cyberd/app/storage"
cbd "github.com/cybercongress/cyberd/app/types"
"github.com/cybercongress/cyberd/app/types/coin"
"github.com/cybercongress/cyberd/x/bandwidth"
bw "github.com/cybercongress/cyberd/x/bandwidth/types"
cbdbank "github.com/cybercongress/cyberd/x/bank"
"github.com/cybercongress/cyberd/x/link"
"github.com/cybercongress/cyberd/x/mint"
"github.com/cybercongress/cyberd/x/rank"
Expand Down Expand Up @@ -72,26 +73,24 @@ type CyberdApp struct {
txDecoder sdk.TxDecoder

// bandwidth
bandwidthHandler bw.BandwidthHandler
msgBandwidthCost bw.MsgBandwidthCost
maxAccBandwidth bw.MaxAccBandwidth
bandwidthHandler bw.BandwidthMeter
curBlockSpentBandwidth uint64 //resets every block
lastTotalSpentBandwidth uint64 //resets every bandwidth price adjustment interval
currentCreditPrice float64

// keys to access the multistore
dbKeys CyberdAppDbKeys

// manage getting and setting accounts
// manage getting and setting app data
mainStorage MainStorage
accountKeeper auth.AccountKeeper
feeCollectionKeeper auth.FeeCollectionKeeper
bankKeeper bank.Keeper
bankKeeper cbdbank.Keeper
stakeKeeper stake.Keeper
slashingKeeper slashing.Keeper
distrKeeper distr.Keeper
paramsKeeper params.Keeper
accBandwidthKeeper bandwidth.AccountBandwidthKeeper
accBandwidthKeeper bw.Keeper

//inflation
minter mint.Minter
Expand Down Expand Up @@ -145,41 +144,41 @@ func NewCyberdApp(

// create your application type
var app = &CyberdApp{
cdc: cdc,
txDecoder: txDecoder,
BaseApp: baseapp.NewBaseApp(appName, logger, db, txDecoder, baseAppOptions...),
dbKeys: dbKeys,
persistStorages: storages,
mainStorage: ms,
computeUnit: computeUnit,
msgBandwidthCost: bandwidth.MsgBandwidthCost,
cdc: cdc,
txDecoder: txDecoder,
BaseApp: baseapp.NewBaseApp(appName, logger, db, txDecoder, baseAppOptions...),
dbKeys: dbKeys,
persistStorages: storages,
mainStorage: ms,
computeUnit: computeUnit,
}

// define and attach the mappers and keepers
app.accountKeeper = auth.NewAccountKeeper(app.cdc, dbKeys.acc, NewAccount)
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.accountKeeper = auth.NewAccountKeeper(app.cdc, dbKeys.acc, cbd.NewCyberdAccount)
app.paramsKeeper = params.NewKeeper(app.cdc, dbKeys.params, dbKeys.tParams)
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, dbKeys.fees)
app.accBandwidthKeeper = bandwidth.NewAccountBandwidthKeeper(dbKeys.accBandwidth)

stakeKeeper := stake.NewKeeper(
var stakeKeeper *stake.Keeper
app.bankKeeper = cbdbank.NewBankKeeper(app.accountKeeper, stakeKeeper, nil)
app.accBandwidthKeeper = bandwidth.NewAccBandwidthKeeper(dbKeys.accBandwidth)
*stakeKeeper = stake.NewKeeper(
app.cdc, dbKeys.stake,
dbKeys.tStake, app.bankKeeper,
app.paramsKeeper.Subspace(stake.DefaultParamspace),
stake.DefaultCodespace,
)
app.slashingKeeper = slashing.NewKeeper(
app.cdc, dbKeys.slashing,
&stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace),
stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace),
slashing.DefaultCodespace,
)
app.distrKeeper = distr.NewKeeper(
app.cdc, dbKeys.keyDistr,
app.paramsKeeper.Subspace(distr.DefaultParamspace),
app.bankKeeper, &stakeKeeper, app.feeCollectionKeeper,
app.bankKeeper, stakeKeeper, app.feeCollectionKeeper,
distr.DefaultCodespace,
)
app.minter = mint.NewMinter(app.feeCollectionKeeper, stakeKeeper)
app.minter = mint.NewMinter(app.feeCollectionKeeper, *stakeKeeper)

app.memStorage = &InMemoryStorage{}

Expand All @@ -188,12 +187,13 @@ func NewCyberdApp(
// so that it can be modified like below:
app.stakeKeeper = *stakeKeeper.SetHooks(NewHooks(app.slashingKeeper.Hooks()))

app.maxAccBandwidth = bw.NewMaxAccBandwidth(app.stakeKeeper, bandwidth.MaxNetworkBandwidth)
app.bandwidthHandler = bandwidth.NewBandwidthHandler(app.accountKeeper, app.accBandwidthKeeper, app.msgBandwidthCost, app.maxAccBandwidth)
app.bandwidthHandler = bandwidth.NewBaseMeter(
app.accountKeeper, app.bankKeeper, app.accBandwidthKeeper, bandwidth.MsgBandwidthCosts,
)

// register message routes
app.Router().
AddRoute("bank", NewBankHandler(app.bankKeeper, app.memStorage, app.accountKeeper, app.maxAccBandwidth, app.accBandwidthKeeper)).
AddRoute("bank", sdkbank.NewHandler(app.bankKeeper)).
AddRoute("link", link.NewLinksHandler(storages.CidIndex, storages.Links, app.memStorage, app.accountKeeper)).
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
AddRoute("slashing", slashing.NewHandler(app.slashingKeeper))
Expand Down Expand Up @@ -307,8 +307,7 @@ func (app *CyberdApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) ab
}
}

bandwidth.InitGenesis(ctx, app.accBandwidthKeeper, app.accountKeeper, app.maxAccBandwidth)

bandwidth.InitGenesis(ctx, app.bandwidthHandler, app.accBandwidthKeeper, genesisState.Accounts)
return abci.ResponseInitChain{
Validators: validators,
}
Expand All @@ -327,25 +326,27 @@ func (app *CyberdApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock)

func (app *CyberdApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {

cachedCtx, writeCache := app.NewContext(true, abci.Header{Height: app.latestBlockHeight}).CacheContext()
ctx := app.NewContext(true, abci.Header{Height: app.latestBlockHeight})
tx, acc, err := app.decodeTxAndAcc(ctx, txBytes)

var tx, err = app.txDecoder(txBytes)
if err == nil {

_, err = app.bandwidthHandler(cachedCtx, app.currentCreditPrice, tx)
if err == nil {
txCost := app.bandwidthHandler.GetTxCost(ctx, app.currentCreditPrice, tx)
accBw := app.bandwidthHandler.GetCurrentAccBandwidth(ctx, acc)

if !accBw.HasEnoughRemained(txCost) {
err = cbd.ErrNotEnoughBandwidth()
} else {

resp := app.BaseApp.CheckTx(txBytes)
if resp.Code == 0 {
writeCache()
app.bandwidthHandler.ConsumeAccBandwidth(ctx, accBw, txCost)
}

return resp
}
}

result := err.Result()

return abci.ResponseCheckTx{
Code: uint32(result.Code),
Data: result.Data,
Expand All @@ -354,23 +355,26 @@ func (app *CyberdApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {
GasUsed: int64(result.GasUsed),
Tags: result.Tags,
}

}

func (app *CyberdApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {

cachedCtx, writeCache := app.NewContext(false, abci.Header{Height: app.latestBlockHeight}).CacheContext()
ctx := app.NewContext(false, abci.Header{Height: app.latestBlockHeight})
tx, acc, err := app.decodeTxAndAcc(ctx, txBytes)

var tx, err = app.txDecoder(txBytes)
if err == nil {

spent, err := app.bandwidthHandler(cachedCtx, app.currentCreditPrice, tx)
if err == nil {
txCost := app.bandwidthHandler.GetTxCost(ctx, app.currentCreditPrice, tx)
accBw := app.bandwidthHandler.GetCurrentAccBandwidth(ctx, acc)

if !accBw.HasEnoughRemained(txCost) {
err = cbd.ErrNotEnoughBandwidth()
} else {

resp := app.BaseApp.DeliverTx(txBytes)
if resp.Code == 0 {
writeCache()
app.curBlockSpentBandwidth = app.curBlockSpentBandwidth + uint64(spent)
app.bandwidthHandler.ConsumeAccBandwidth(ctx, accBw, txCost)
app.curBlockSpentBandwidth = app.curBlockSpentBandwidth + uint64(txCost)
}

return abci.ResponseDeliverTx{
Expand All @@ -386,7 +390,6 @@ func (app *CyberdApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
}

result := err.Result()

return abci.ResponseDeliverTx{
Code: uint32(result.Code),
Codespace: string(result.Codespace),
Expand All @@ -398,6 +401,32 @@ func (app *CyberdApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
}
}

func (app *CyberdApp) decodeTxAndAcc(ctx sdk.Context, txBytes []byte) (auth.StdTx, sdk.AccAddress, sdk.Error) {

decoded, err := app.txDecoder(txBytes)
if err != nil {
return auth.StdTx{}, nil, err
}

tx := decoded.(auth.StdTx)
if tx.GetMsgs() == nil || len(tx.GetMsgs()) == 0 {
return tx, nil, sdk.ErrInternal("Tx.GetMsgs() must return at least one message in list")
}

if err := tx.ValidateBasic(); err != nil {
return tx, nil, err
}

// signers acc [0] bandwidth will be consumed
account := tx.GetSigners()[0]
acc := app.accountKeeper.GetAccount(ctx, account)
if acc == nil {
return tx, nil, sdk.ErrUnknownAddress(account.String())
}

return tx, account, nil
}

func getSignersTags(tx sdk.Tx) sdk.Tags {

signers := make(map[string]struct{})
Expand Down
66 changes: 0 additions & 66 deletions app/bank/handler.go

This file was deleted.

2 changes: 1 addition & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
. "github.com/cybercongress/cyberd/app/genesis"
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
)

// todo what is purpose of this functions?
// ExportAppStateAndValidators implements custom application logic that exposes
// various parts of the application's state and set of validators. An error is
// returned if any step getting the state or set of validators fails.
Expand Down
4 changes: 2 additions & 2 deletions app/genesis.go → app/genesis/genesis.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package genesis

import (
"encoding/json"
Expand All @@ -9,7 +9,7 @@ import (
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cybercongress/cyberd/app/coin"
"github.com/cybercongress/cyberd/app/types/coin"
"github.com/pkg/errors"
tmtypes "github.com/tendermint/tendermint/types"

Expand Down
4 changes: 2 additions & 2 deletions app/params.go → app/genesis/params.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app
package genesis

import (
"github.com/cosmos/cosmos-sdk/x/stake/types"
"github.com/cybercongress/cyberd/app/coin"
"github.com/cybercongress/cyberd/app/types/coin"
"time"
)

Expand Down
Loading