WIP: refactor

Refactor

* No more decorators, but rather types.AntiHandler
* No more handlers, but rather types.MsgHandler
* Ability to pass "stores" in NewXYZHandler()
* Coins live in types, and Accounts have coins
* Coinstore -> bank
This commit is contained in:
Jae Kwon
2018-01-12 19:17:17 -08:00
parent 620bdf409f
commit ba2b4f0f21
28 changed files with 698 additions and 717 deletions
+35 -55
View File
@@ -5,75 +5,64 @@ import (
"fmt"
"os"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/tendermint/abci/server"
"github.com/tendermint/go-wire"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/cosmos/cosmos-sdk/app"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/types"
acm "github.com/cosmos/cosmos-sdk/x/account"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/coinstore"
bcm "github.com/cosmos/cosmos-sdk/examples/basecoin/types"
)
func main() {
app := app.NewApp("basecoin")
// First, create the Application.
app := sdk.NewApp("basecoin")
// Create the underlying leveldb datastore which will
// persist the Merkle tree inner & leaf nodes.
db, err := dbm.NewGoLevelDB("basecoin", "basecoin-data")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// create CommitStoreLoader
// Create CommitStoreLoader.
cacheSize := 10000
numHistory := int64(100)
mainLoader := store.NewIAVLStoreLoader(db, cacheSize, numHistory)
ibcLoader := store.NewIAVLStoreLoader(db, cacheSize, numHistory)
// The key to access the main KVStore.
var mainKey = storeKey("main")
var ibcKey = storeKey("ibc")
var mainStoreKey = new(KVStoreKey)
var ibcStoreKey = new(KVStoreKey)
// Create MultiStore
multiStore := store.NewCommitMultiStore(db)
multiStore.SetSubstoreLoader(mainKey, mainLoader)
multiStore.SetSubstoreLoader(ibcKey, ibcLoader)
multiStore.SetSubstoreLoader("main", mainStoreKey, mainLoader)
multiStore.SetSubstoreLoader("ibc", ibcStoreKey, ibcLoader)
app.SetCommitMultiStore(multiStore)
// XXX
var appAccountCodec AccountCodec = nil
// Set Tx decoder
app.SetTxDecoder(decodeTx)
// Create Handler
handler := types.ChainDecorators(
recover.Decorator(),
logger.Decorator(),
auth.Decorator(appAccountCodec),
fees.Decorator(mainKey),
rollbackDecorator(), // XXX define.
ibc.Decorator(ibcKey), // Handle IBC messages.
pos.Decorator(mainKey), // Handle staking messages.
gov.Decorator(mainKey), // Handle governance messages.
coins.Decorator(mainKey), // Handle coinstore messages.
).WithHandler(func(ctx types.context, tx Tx) Result {
/*
switch tx.(type) {
case CustomTx1: ...
case CustomTx2: ...
}
*/
})
var accStore = auth.NewAccountStore(mainStoreKey, bcm.AppAccountCodec{})
var authAnteHandler = auth.NewAnteHandler(accStore)
// Handle charging fees and checking signatures.
app.SetDefaultAnteHandler(authAnteHandler)
// Add routes to App.
app.Router().AddRoute("bank", bank.NewHandler(accStore))
// TODO: load genesis
// TODO: InitChain with validators
// accounts := acm.NewAccountStore(multiStore.GetKVStore("main"))
// accounts := auth.NewAccountStore(multiStore.GetKVStore("main"))
// TODO: set the genesis accounts
// Set everything on the app and load latest
app.SetCommitMultiStore(multiStore)
app.SetTxParser(txParser)
app.SetHandler(handler)
// Load the stores.
if err := app.LoadLatestVersion(); err != nil {
fmt.Println(err)
os.Exit(1)
@@ -98,22 +87,13 @@ func main() {
//----------------------------------------
// Misc.
func txParser(txBytes []byte) (types.Tx, error) {
var tx coinstore.SendTx
err := json.Unmarshal(txBytes, &tx)
func registerMsgs() {
wire.RegisterInterface((*types.Msg), nil)
wire.RegisterConcrete((*bank.SendMsg), nil)
}
func decodeTx(txBytes []byte) (types.Tx, error) {
var tx = sdk.StdTx{}
err := wire.UnmarshalBinary(txBytes, &tx)
return tx, err
}
// an unexported (private) key which no module could know of unless
// it was passed in from the app.
type storeKey struct {
writeable bool
name string
}
func newStoreKey(name string) storeKey {
return storeKey{true, name}
}
func (s storeKey) ReadOnly() storeKey {
return storeKey{false, s.name}
}
+36
View File
@@ -0,0 +1,36 @@
package types
import (
"github.com/cosmos/cosmos-sdk/x/auth"
)
type AppAccount struct {
auth.BaseAccount
// Custom extensions for this application.
Name string
}
func (acc AppAccount) GetName() string {
return acc.Name
}
func (acc *AppAccount) SetName(name string) {
acc.Name = name
}
//----------------------------------------
type AppAccountCodec struct{}
func (_ AppAccountCodec) Prototype() interface{} {
return AppAccount{}
}
func (_ AppAccountCodec) Encode(o interface{}) (bz []byte, err error) {
panic("not yet implemented")
}
func (_ AppAccountCodec) Decode(bz []byte) (o interface{}, err error) {
panic("not yet implemented")
}