Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Add Weighted Median Price module. (#395)
Browse files Browse the repository at this point in the history
* partial work

* partial work of price

* add tests of model and fix set last validators

* finish price module and add parameters and genesis

* add testnet mode

* price module finised. wait validator module and integration into app

* time.Duration is not marshalable, replaced with seconds int64

* rebase and integrate price module into app

* app

* fix module dependency orders

* use punishment from validator

* fixes

* add version command
  • Loading branch information
Stumble authored Oct 3, 2019
1 parent f0b1ce4 commit 061d287
Show file tree
Hide file tree
Showing 65 changed files with 4,610 additions and 268 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GOPATH ?= $(shell $(GO) env GOPATH)
COMMIT := $(shell git log -1 --format='%H')
COMMIT := $(shell git --no-pager describe --tags --always --dirty)
PACKAGES=$(shell go list ./... | grep -v '/vendor/')
LD_FLAGS := "-X github.com/tendermint/tendermint/version.GitCommit=$(COMMIT) -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb"
LD_FLAGS := "-X github.com/lino-network/lino/app.Version=$(COMMIT) -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb"
GO_TAGS := "tendermint cgo cleveldb"
CGO_LDFLAGS := "-lsnappy"
GO111MODULE = on
Expand Down
49 changes: 38 additions & 11 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type LinoBlockchain struct {
CapKeyProposalStore *sdk.KVStoreKey
CapKeyReputationV2Store *sdk.KVStoreKey
CapKeyBandwidthStore *sdk.KVStoreKey
CapKeyPriceStore *sdk.KVStoreKey

// manager for different KVStore
accountManager acc.AccountKeeper
Expand All @@ -110,6 +111,9 @@ type LinoBlockchain struct {

// global param
paramHolder param.ParamHolder

// auth
auth sdk.AnteHandler
}

// NewLinoBlockchain - create a Lino Blockchain instance
Expand All @@ -134,24 +138,39 @@ func NewLinoBlockchain(
CapKeyProposalStore: sdk.NewKVStoreKey(types.ProposalKVStoreKey),
CapKeyReputationV2Store: sdk.NewKVStoreKey(types.ReputationV2KVStoreKey),
CapKeyBandwidthStore: sdk.NewKVStoreKey(types.BandwidthKVStoreKey),
CapKeyPriceStore: sdk.NewKVStoreKey(types.PriceKVStoreKey),
}
// layer-1: basics
lb.paramHolder = param.NewParamHolder(lb.CapKeyParamStore)
lb.priceManager = pricemn.TestnetPriceManager{}
lb.globalManager = global.NewGlobalManager(lb.CapKeyGlobalStore, lb.paramHolder)
registerEvent(lb.globalManager.WireCodec())

lb.accountManager = accmn.NewAccountManager(lb.CapKeyAccountStore, lb.paramHolder, &lb.globalManager)
lb.reputationManager = rep.NewReputationManager(lb.CapKeyReputationV2Store, lb.paramHolder)
voteManager := votemn.NewVoteManager(lb.CapKeyVoteStore, lb.paramHolder, lb.accountManager, &lb.globalManager)
lb.infraManager = infra.NewInfraManager(lb.CapKeyInfraStore, lb.paramHolder)
lb.proposalManager = proposal.NewProposalManager(lb.CapKeyProposalStore, lb.paramHolder)

lb.developerManager = devmn.NewDeveloperManager(
lb.CapKeyDeveloperStore, lb.paramHolder, &voteManager, lb.accountManager, lb.priceManager, &lb.globalManager)
lb.postManager = postmn.NewPostManager(lb.CapKeyPostStore, lb.accountManager, &lb.globalManager, lb.developerManager, lb.reputationManager, lb.priceManager)
lb.bandwidthManager = bandwidthmn.NewBandwidthManager(lb.CapKeyBandwidthStore, lb.paramHolder, &lb.globalManager, &voteManager, lb.developerManager, lb.accountManager)
// layer-2: middlewares
//// vote <--> validator
voteManager := votemn.NewVoteManager(lb.CapKeyVoteStore, lb.paramHolder, lb.accountManager, &lb.globalManager)
lb.valManager = valmn.NewValidatorManager(lb.CapKeyValStore, lb.paramHolder, &voteManager, &lb.globalManager, lb.accountManager)
lb.voteManager = *voteManager.SetHooks(votemn.NewMultiStakingHooks(lb.valManager.Hooks()))
//// price -> vote, validator
lb.priceManager = pricemn.NewWeightedMedianPriceManager(lb.CapKeyPriceStore, lb.valManager, lb.paramHolder)

// layer-3: applications
lb.developerManager = devmn.NewDeveloperManager(
lb.CapKeyDeveloperStore, lb.paramHolder,
&voteManager, lb.accountManager, lb.priceManager, &lb.globalManager)
//// post -> developer
lb.postManager = postmn.NewPostManager(
lb.CapKeyPostStore, lb.accountManager,
&lb.globalManager, lb.developerManager, lb.reputationManager, lb.priceManager)
// bandwidth -> developer
lb.bandwidthManager = bandwidthmn.NewBandwidthManager(
lb.CapKeyBandwidthStore, lb.paramHolder,
&lb.globalManager, &voteManager, lb.developerManager, lb.accountManager)
// bandwidth
lb.auth = auth.NewAnteHandler(lb.accountManager, lb.bandwidthManager)

lb.Router().
AddRoute(acctypes.RouterKey, acc.NewHandler(lb.accountManager)).
Expand Down Expand Up @@ -179,14 +198,14 @@ func NewLinoBlockchain(
lb.SetInitChainer(lb.initChainer)
lb.SetBeginBlocker(lb.beginBlocker)
lb.SetEndBlocker(lb.endBlocker)
lb.SetAnteHandler(auth.NewAnteHandler(lb.accountManager, lb.bandwidthManager))
lb.SetAnteHandler(lb.auth)
// TODO(Cosmos): mounting multiple stores is broken
// https://github.com/cosmos/cosmos-sdk/issues/532

lb.MountStores(
lb.CapKeyMainStore, lb.CapKeyAccountStore, lb.CapKeyPostStore, lb.CapKeyValStore,
lb.CapKeyVoteStore, lb.CapKeyInfraStore, lb.CapKeyDeveloperStore, lb.CapKeyGlobalStore,
lb.CapKeyParamStore, lb.CapKeyProposalStore, lb.CapKeyReputationV2Store, lb.CapKeyBandwidthStore)
lb.CapKeyParamStore, lb.CapKeyProposalStore, lb.CapKeyReputationV2Store, lb.CapKeyBandwidthStore, lb.CapKeyPriceStore)
if err := lb.LoadLatestVersion(lb.CapKeyMainStore); err != nil {
panic(err)
}
Expand Down Expand Up @@ -256,7 +275,9 @@ func (lb *LinoBlockchain) initChainer(ctx sdk.Context, req abci.RequestInitChain
genesisState.GenesisParam.CoinDayParam,
genesisState.GenesisParam.BandwidthParam,
genesisState.GenesisParam.AccountParam,
genesisState.GenesisParam.ReputationParam); err != nil {
genesisState.GenesisParam.ReputationParam,
genesisState.GenesisParam.PriceParam,
); err != nil {
panic(err)
}
} else {
Expand All @@ -281,16 +302,19 @@ func (lb *LinoBlockchain) initChainer(ctx sdk.Context, req abci.RequestInitChain
if err := lb.developerManager.InitGenesis(ctx, genesisState.ReservePool); err != nil {
panic(err)
}
if err := lb.priceManager.InitGenesis(ctx, genesisState.InitCoinPrice); err != nil {
panic(err)
}
if err := lb.infraManager.InitGenesis(ctx); err != nil {
panic(err)
}
if err := lb.proposalManager.InitGenesis(ctx); err != nil {
panic(err)
}
lb.valManager.InitGenesis(ctx)
if err := lb.bandwidthManager.InitGenesis(ctx); err != nil {
panic(err)
}
lb.valManager.InitGenesis(ctx)

// import from prev state, do not read from genesis.
if genesisState.LoadPrevStates {
Expand Down Expand Up @@ -536,6 +560,9 @@ func (lb *LinoBlockchain) executeHourlyEvent(ctx sdk.Context) {
if err := lb.bandwidthManager.ReCalculateAppBandwidthInfo(ctx); err != nil {
panic(err)
}
if err := lb.priceManager.UpdatePrice(ctx); err != nil {
panic(err)
}
}

// execute daily event, record consumption friction and lino power
Expand Down
32 changes: 27 additions & 5 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func newLinoBlockchain(t *testing.T, numOfValidators int) *LinoBlockchain {
lb := NewLinoBlockchain(logger, db, nil)

genesisState := GenesisState{
Accounts: []GenesisAccount{},
InitCoinPrice: types.NewMiniDollar(1200),
Accounts: []GenesisAccount{},
}

// Generate 21 validators
Expand Down Expand Up @@ -121,7 +122,8 @@ func TestGenesisAcc(t *testing.T) {
false, secp256k1.GenPrivKey().PubKey()},
}
genesisState := GenesisState{
Accounts: []GenesisAccount{},
InitCoinPrice: types.NewMiniDollar(1200),
Accounts: []GenesisAccount{},
}
for _, acc := range accs {
genesisAcc := GenesisAccount{
Expand Down Expand Up @@ -180,7 +182,8 @@ func TestGenesisFromConfig(t *testing.T) {
logger, db := loggerAndDB()
lb := NewLinoBlockchain(logger, db, nil)
genesisState := GenesisState{
Accounts: []GenesisAccount{},
InitCoinPrice: types.NewMiniDollar(1200),
Accounts: []GenesisAccount{},
}
genesisState.GenesisParam = GenesisParam{
true,
Expand Down Expand Up @@ -268,7 +271,15 @@ func TestGenesisFromConfig(t *testing.T) {
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
},
param.ReputationParam{
BestContentIndexN: 10,
BestContentIndexN: 200,
UserMaxN: 50,
},
param.PriceParam{
TestnetMode: true,
UpdateEverySec: int64(time.Hour.Seconds()),
FeedEverySec: int64((10 * time.Minute).Seconds()),
HistoryMaxLen: 71,
PenaltyMissFeed: types.NewCoinFromInt64(10000 * types.Decimals),
},
}
genesisState.InitGlobalMeta = globalModel.InitParamList{
Expand Down Expand Up @@ -687,7 +698,18 @@ func TestGlobalTime(t *testing.T) {
lb := NewLinoBlockchain(logger, db, nil)

genesisState := GenesisState{
Accounts: []GenesisAccount{},
InitCoinPrice: types.NewMiniDollar(1200),
Accounts: []GenesisAccount{
{
Name: user1,
Coin: coinPerValidator,
ResetKey: priv1.PubKey(),
TransactionKey: secp256k1.GenPrivKey().PubKey(),
AppKey: secp256k1.GenPrivKey().PubKey(),
IsValidator: true,
ValPubKey: priv2.PubKey(),
},
},
}

result, err := wire.MarshalJSONIndent(lb.cdc, genesisState)
Expand Down
15 changes: 13 additions & 2 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"strings"
"time"

wire "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -22,11 +23,11 @@ type GenesisState struct {
LoadPrevStates bool `json:"load_prev_states"`
Accounts []GenesisAccount `json:"accounts"`
ReservePool types.Coin `json:"reserve_pool"`
InitCoinPrice types.MiniDollar `json:"init_coin_price"`
Developers []GenesisAppDeveloper `json:"developers"`
Infra []GenesisInfraProvider `json:"infra"`
GenesisParam GenesisParam `json:"genesis_param"`
InitGlobalMeta globalModel.InitParamList `json:"init_global_meta"`
Reputation []byte `json:"reputation"`
}

// genesis account will get coin to the address and register user
Expand Down Expand Up @@ -68,6 +69,7 @@ type GenesisParam struct {
param.AccountParam
param.PostParam
param.ReputationParam
param.PriceParam
}

// LinoBlockchainGenTx - init genesis account
Expand Down Expand Up @@ -118,6 +120,7 @@ func LinoBlockchainGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appSt
LoadPrevStates: false,
Accounts: []GenesisAccount{},
ReservePool: types.NewCoinFromInt64(0),
InitCoinPrice: types.NewMiniDollar(1200),
Developers: []GenesisAppDeveloper{},
Infra: []GenesisInfraProvider{},
GenesisParam: GenesisParam{
Expand Down Expand Up @@ -206,7 +209,15 @@ func LinoBlockchainGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appSt
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
},
param.ReputationParam{
BestContentIndexN: 10,
BestContentIndexN: 200,
UserMaxN: 50,
},
param.PriceParam{
TestnetMode: true,
UpdateEverySec: int64(time.Hour.Seconds()),
FeedEverySec: int64((10 * time.Minute).Seconds()),
HistoryMaxLen: 71,
PenaltyMissFeed: types.NewCoinFromInt64(10000 * types.Decimals),
},
},
InitGlobalMeta: globalModel.InitParamList{
Expand Down
20 changes: 15 additions & 5 deletions app/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"strconv"
"testing"
"time"

"github.com/lino-network/lino/param"
"github.com/lino-network/lino/types"
Expand Down Expand Up @@ -39,10 +40,11 @@ func TestGetGenesisJson(t *testing.T) {
Name: "Lino",
}
genesisState := GenesisState{
Accounts: []GenesisAccount{genesisAcc},
ReservePool: types.NewCoinFromInt64(1000000),
Developers: []GenesisAppDeveloper{genesisAppDeveloper},
Infra: []GenesisInfraProvider{genesisInfraProvider},
Accounts: []GenesisAccount{genesisAcc},
ReservePool: types.NewCoinFromInt64(1000000),
InitCoinPrice: types.NewMiniDollar(1200),
Developers: []GenesisAppDeveloper{genesisAppDeveloper},
Infra: []GenesisInfraProvider{genesisInfraProvider},
GenesisParam: GenesisParam{
true,
param.GlobalAllocationParam{
Expand Down Expand Up @@ -129,7 +131,15 @@ func TestGetGenesisJson(t *testing.T) {
MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals),
},
param.ReputationParam{
BestContentIndexN: 10,
BestContentIndexN: 200,
UserMaxN: 50,
},
param.PriceParam{
TestnetMode: true,
UpdateEverySec: int64(time.Hour.Seconds()),
FeedEverySec: int64((10 * time.Minute).Seconds()),
HistoryMaxLen: 71,
PenaltyMissFeed: types.NewCoinFromInt64(10000 * types.Decimals),
},
},
InitGlobalMeta: globalModel.InitParamList{
Expand Down
25 changes: 25 additions & 0 deletions app/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app

import (
"fmt"

"github.com/spf13/cobra"
// "github.com/spf13/viper"
)

var Version = ""

// VersionCmd - print current version of binary.
func VersionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "version prints the version of this binary",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
fmt.Println(Version)
return nil
},
}

return cmd
}
2 changes: 2 additions & 0 deletions cmd/lino/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func main() {
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
}

rootCmd.AddCommand(app.VersionCmd())

rootCmd.AddCommand(app.InitCmd(ctx, cdc))

server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators)
Expand Down
1 change: 1 addition & 0 deletions cmd/linocli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func main() {

// Construct Root Command
rootCmd.AddCommand(
app.VersionCmd(),
rpc.StatusCommand(),
client.ConfigCmd(app.DefaultCLIHome),
queryCmd(cdc),
Expand Down
Loading

0 comments on commit 061d287

Please sign in to comment.