Skip to content

Commit

Permalink
[refactor] make auth and bank compilable
Browse files Browse the repository at this point in the history
  • Loading branch information
camelfred committed Sep 29, 2019
1 parent 7a351f2 commit 2020f70
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 104 deletions.
49 changes: 24 additions & 25 deletions app/app.go
Expand Up @@ -11,7 +11,7 @@ import (
cmn "github.com/tepleton/tmlibs/common"
"github.com/tepleton/tmlibs/log"

"github.com/tepleton/tepleton-sdk/types"
sdk "github.com/tepleton/tepleton-sdk/types"
)

var mainHeaderKey = []byte("header")
Expand All @@ -24,13 +24,13 @@ type App struct {
name string

// Main (uncached) state
ms types.CommitMultiStore
ms sdk.CommitMultiStore

// Unmarshal []byte into types.Tx
txDecoder types.TxDecoder
// Unmarshal []byte into sdk.Tx
txDecoder sdk.TxDecoder

// Ante handler for fee and auth.
defaultAnteHandler types.AnteHandler
defaultAnteHandler sdk.AnteHandler

// Handle any kind of message.
router Router
Expand All @@ -39,10 +39,10 @@ type App struct {
// Volatile

// CheckTx state, a cache-wrap of `.ms`.
msCheck types.CacheMultiStore
msCheck sdk.CacheMultiStore

// DeliverTx state, a cache-wrap of `.ms`.
msDeliver types.CacheMultiStore
msDeliver sdk.CacheMultiStore

// Current block header
header wrsp.Header
Expand All @@ -53,7 +53,7 @@ type App struct {

var _ wrsp.Application = &App{}

func NewApp(name string, ms CommitMultiStore) *App {
func NewApp(name string, ms sdk.CommitMultiStore) *App {
return &App{
logger: makeDefaultLogger(),
name: name,
Expand All @@ -66,11 +66,11 @@ func (app *App) Name() string {
return app.name
}

func (app *App) SetTxDecoder(txDecoder types.TxDecoder) {
func (app *App) SetTxDecoder(txDecoder sdk.TxDecoder) {
app.txDecoder = txDecoder
}

func (app *App) SetDefaultAnteHandler(ah types.AnteHandler) {
func (app *App) SetDefaultAnteHandler(ah sdk.AnteHandler) {
app.defaultAnteHandler = ah
}

Expand All @@ -84,18 +84,18 @@ func (app *App) SetEndBlocker(...) {}
func (app *App) SetInitStater(...) {}
*/

func (app *App) LoadLatestVersion() error {
func (app *App) LoadLatestVersion(mainKey sdk.SubstoreKey) error {
app.ms.LoadLatestVersion()
return app.initFromStore()
return app.initFromStore(mainKey)
}

func (app *App) LoadVersion(version int64) error {
func (app *App) LoadVersion(version int64, mainKey sdk.SubstoreKey) error {
app.ms.LoadVersion(version)
return app.initFromStore()
return app.initFromStore(mainKey)
}

// The last CommitID of the multistore.
func (app *App) LastCommitID() types.CommitID {
func (app *App) LastCommitID() sdk.CommitID {
return app.ms.LastCommitID()
}

Expand All @@ -105,13 +105,13 @@ func (app *App) LastBlockHeight() int64 {
}

// Initializes the remaining logic from app.ms.
func (app *App) initFromStore() error {
func (app *App) initFromStore(mainKey sdk.SubstoreKey) error {
lastCommitID := app.ms.LastCommitID()
main := app.ms.GetKVStore("main")
main := app.ms.GetKVStore(mainKey)
header := wrsp.Header{}

// Main store should exist.
if app.ms.GetKVStore("main") == nil {
if main == nil {
return errors.New("App expects MultiStore with 'main' KVStore")
}

Expand Down Expand Up @@ -226,38 +226,37 @@ func (app *App) DeliverTx(txBytes []byte) (res wrsp.ResponseDeliverTx) {
}
}

func (app *App) runTx(isCheckTx bool, txBytes []byte) (result types.Result) {
func (app *App) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) {

// Handle any panics.
defer func() {
if r := recover(); r != nil {
result = types.Result{
result = sdk.Result{
Code: 1, // TODO
Log: fmt.Sprintf("Recovered: %v\n", r),
}
}
}()

var store types.MultiStore
var store sdk.MultiStore
if isCheckTx {
store = app.msCheck
} else {
store = app.msDeliver
}

// Initialize arguments to Handler.
var ctx = types.NewContext(
var ctx = sdk.NewContext(
store,
app.header,
isCheckTx,
txBytes,
)

// Decode the Tx.
var err error
tx, err = app.txDecoder(txBytes)
tx, err := app.txDecoder(txBytes)
if err != nil {
return types.Result{
return sdk.Result{
Code: 1, // TODO
}
}
Expand Down
18 changes: 12 additions & 6 deletions app/router.go
@@ -1,13 +1,19 @@
package app

import (
"regexp"

sdk "github.com/tepleton/tepleton-sdk/types"
)

type Router interface {
AddRoute(r string, h Handler)
Route(path string) (h Handler)
AddRoute(r string, h sdk.Handler)
Route(path string) (h sdk.Handler)
}

type route struct {
r string
h Handler
h sdk.Handler
}

type router struct {
Expand All @@ -16,21 +22,21 @@ type router struct {

func NewRouter() router {
return router{
routes: make([]route),
routes: make([]route, 0),
}
}

var isAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString

func (rtr router) AddRoute(r string, h Handler) {
func (rtr router) AddRoute(r string, h sdk.Handler) {
if !isAlpha(r) {
panic("route expressions can only contain alphanumeric characters")
}
rtr.routes = append(rtr.routes, route{r, h})
}

// TODO handle expressive matches.
func (rtr router) Route(path string) (h Handler) {
func (rtr router) Route(path string) (h sdk.Handler) {
for _, route := range rtr.routes {
if route.r == path {
return route.h
Expand Down
2 changes: 1 addition & 1 deletion errors/errors.go
Expand Up @@ -31,7 +31,7 @@ func CodeToDefaultLog(code uint32) string {
return "Insufficent funds"
case CodeUnknownRequest:
return "Unknown request"
case CodeUnrecognizeAddress:
case CodeUnrecognizedAddress:
return "Unrecognized address"
default:
return fmt.Sprintf("Unknown code %d", code)
Expand Down
2 changes: 1 addition & 1 deletion examples/basecoin/main.go
Expand Up @@ -62,7 +62,7 @@ func main() {
// TODO: set the genesis accounts

// Load the stores.
if err := app.LoadLatestVersion(); err != nil {
if err := app.LoadLatestVersion(mainStoreKey); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions types/context.go
Expand Up @@ -62,7 +62,7 @@ func (c Context) Value(key interface{}) interface{} {
}

// KVStore fetches a KVStore from the MultiStore.
func (c Context) KVStore(key *KVStoreKey) KVStore {
func (c Context) KVStore(key SubstoreKey) KVStore {
return c.multiStore().GetKVStore(key)
}

Expand All @@ -85,7 +85,7 @@ func (c Context) WithProtoMsg(key interface{}, value proto.Message) Context {
return c.withValue(key, value)
}

func (c Context) WithMultiStore(key *KVStoreKey, ms MultiStore) Context {
func (c Context) WithMultiStore(key SubstoreKey, ms MultiStore) Context {
return c.withValue(key, ms)
}

Expand Down
2 changes: 1 addition & 1 deletion types/handler.go
Expand Up @@ -2,4 +2,4 @@ package types

type Handler func(ctx Context, tx Tx) Result

type AnteHandler func(ctx Context, tx Tx) (Result, abort bool)
type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool)
22 changes: 11 additions & 11 deletions x/auth/account.go
@@ -1,7 +1,7 @@
package auth

import (
"encoding/json"
"errors"

crypto "github.com/tepleton/go-crypto"

Expand Down Expand Up @@ -37,50 +37,50 @@ func (acc *BaseAccount) Set(key interface{}, value interface{}) error {

// Implements Account
func (acc BaseAccount) GetAddress() crypto.Address {
return acc.address
return acc.Address
}

// Implements Account
func (acc *BaseAccount) SetAddress(addr crypto.Address) error {
if acc.address != "" {
if len(acc.Address) != 0 {
return errors.New("cannot override BaseAccount address")
}
acc.address = addr
acc.Address = addr
return nil
}

// Implements Account
func (acc BaseAccount) GetPubKey() crypto.PubKey {
return acc.pubKey
return acc.PubKey
}

// Implements Account
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
if acc.pubKey != "" {
if !acc.PubKey.Empty() {
return errors.New("cannot override BaseAccount pubkey")
}
acc.pubKey = pubKey
acc.PubKey = pubKey
return nil
}

// Implements Account
func (acc *BaseAccount) GetCoins() sdk.Coins {
return acc.coins
return acc.Coins
}

// Implements Account
func (acc *BaseAccount) SetCoins(coins sdk.Coins) error {
acc.coins = coins
acc.Coins = coins
return nil
}

// Implements Account
func (acc *BaseAccount) GetSequence() int64 {
return acc.sequence
return acc.Sequence
}

// Implements Account
func (acc *BaseAccount) SetSequence(seq int64) error {
acc.sequence = seq
acc.Sequence = seq
return nil
}

0 comments on commit 2020f70

Please sign in to comment.