auth and bank compile
This commit is contained in:
parent
ab438c3f45
commit
5f76febce8
|
@ -262,7 +262,7 @@ func (app *App) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) {
|
|||
}
|
||||
|
||||
// Run the ante handler.
|
||||
result, abort := app.defaultAnteHandler(ctx, tx)
|
||||
ctx, result, abort := app.defaultAnteHandler(ctx, tx)
|
||||
if isCheckTx || abort {
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,4 +2,4 @@ package types
|
|||
|
||||
type Handler func(ctx Context, tx Tx) Result
|
||||
|
||||
type AnteHandler func(ctx Context, tx Tx) (result Result, abort bool)
|
||||
type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
func NewAnteHandler(store types.AccountStore) types.AnteHandler {
|
||||
func NewAnteHandler(store sdk.AccountStore) sdk.AnteHandler {
|
||||
return func(
|
||||
ctx types.Context, tx types.Tx,
|
||||
) (newCtx types.Context, res types.Result, abort bool) {
|
||||
ctx sdk.Context, tx sdk.Tx,
|
||||
) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
|
||||
// Deduct the fee from the fee payer.
|
||||
// This is done first because it only
|
||||
|
@ -15,26 +15,26 @@ func NewAnteHandler(store types.AccountStore) types.AnteHandler {
|
|||
payerAddr := tx.GetFeePayer()
|
||||
payerAcc := store.GetAccount(ctx, payerAddr)
|
||||
if payerAcc == nil {
|
||||
return ctx, Result{
|
||||
return ctx, sdk.Result{
|
||||
Code: 1, // TODO
|
||||
}, true
|
||||
}
|
||||
|
||||
payerAcc.Subtract
|
||||
// payerAcc.Subtract ?
|
||||
|
||||
// Ensure that signatures are correct.
|
||||
var signerAddrs = tx.Signers()
|
||||
var signerAccs = make([]types.Account, len(signerAddrs))
|
||||
var signatures = tx.Signatures()
|
||||
var signerAddrs = tx.GetSigners()
|
||||
var signerAccs = make([]sdk.Account, len(signerAddrs))
|
||||
var signatures = tx.GetSignatures()
|
||||
|
||||
// Assert that there are signers.
|
||||
if len(signatures) == 0 {
|
||||
return ctx, types.Result{
|
||||
return ctx, sdk.Result{
|
||||
Code: 1, // TODO
|
||||
}, true
|
||||
}
|
||||
if len(signatures) != len(signers) {
|
||||
return ctx, types.Result{
|
||||
if len(signatures) != len(signerAddrs) {
|
||||
return ctx, sdk.Result{
|
||||
Code: 1, // TODO
|
||||
}, true
|
||||
}
|
||||
|
@ -42,40 +42,40 @@ func NewAnteHandler(store types.AccountStore) types.AnteHandler {
|
|||
// Check each nonce and sig.
|
||||
for i, sig := range signatures {
|
||||
|
||||
var signerAcc = store.GetAccount(signers[i])
|
||||
var signerAcc = store.GetAccount(ctx, signerAddrs[i])
|
||||
signerAccs[i] = signerAcc
|
||||
|
||||
// If no pubkey, set pubkey.
|
||||
if acc.GetPubKey().Empty() {
|
||||
err := acc.SetPubKey(sig.PubKey)
|
||||
if signerAcc.GetPubKey().Empty() {
|
||||
err := signerAcc.SetPubKey(sig.PubKey)
|
||||
if err != nil {
|
||||
return ctx, types.Result{
|
||||
return ctx, sdk.Result{
|
||||
Code: 1, // TODO
|
||||
}, true
|
||||
}
|
||||
}
|
||||
|
||||
// Check and incremenet sequence number.
|
||||
seq := acc.GetSequence()
|
||||
seq := signerAcc.GetSequence()
|
||||
if seq != sig.Sequence {
|
||||
return ctx, types.Result{
|
||||
return ctx, sdk.Result{
|
||||
Code: 1, // TODO
|
||||
}, true
|
||||
}
|
||||
acc.SetSequence(seq + 1)
|
||||
signerAcc.SetSequence(seq + 1)
|
||||
|
||||
// Check sig.
|
||||
if !sig.PubKey.VerifyBytes(tx.SignBytes(), sig.Signature) {
|
||||
return ctx, types.Result{
|
||||
if !sig.PubKey.VerifyBytes(tx.GetSignBytes(), sig.Signature) {
|
||||
return ctx, sdk.Result{
|
||||
Code: 1, // TODO
|
||||
}, true
|
||||
}
|
||||
|
||||
// Save the account.
|
||||
store.SetAccount(acc)
|
||||
store.SetAccount(ctx, signerAcc)
|
||||
}
|
||||
|
||||
ctx = WithSigners(ctx, signerAccs)
|
||||
return ctx, types.Result{}, false // continue...
|
||||
return ctx, sdk.Result{}, false // continue...
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ const (
|
|||
)
|
||||
|
||||
func WithSigners(ctx types.Context, accounts []types.Account) types.Context {
|
||||
return ctx.WithValueUnsafe(contextKeySigners, accounts)
|
||||
return ctx.WithValue(contextKeySigners, accounts)
|
||||
}
|
||||
|
||||
func GetSigners(ctx types.Context) []types.Account {
|
||||
|
|
|
@ -1,45 +1,47 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// Implements types.AccountStore
|
||||
// Implements sdk.AccountStore
|
||||
type accountStore struct {
|
||||
key *types.KVStoreKey
|
||||
codec types.Codec
|
||||
key sdk.SubstoreKey
|
||||
codec sdk.Codec
|
||||
}
|
||||
|
||||
func NewAccountStore(key *types.KVStoreKey, codec types.Codec) accountStore {
|
||||
func NewAccountStore(key sdk.SubstoreKey, codec sdk.Codec) accountStore {
|
||||
return accountStore{
|
||||
key: key,
|
||||
codec: codec,
|
||||
}
|
||||
}
|
||||
|
||||
// Implements types.AccountStore
|
||||
func (as accountStore) NewAccountWithAddress(ctx types.Context, addr crypto.Address) {
|
||||
acc := as.codec.Prototype().(types.Account)
|
||||
// Implements sdk.AccountStore
|
||||
func (as accountStore) NewAccountWithAddress(ctx sdk.Context, addr crypto.Address) sdk.Account {
|
||||
acc := as.codec.Prototype().(sdk.Account)
|
||||
acc.SetAddress(addr)
|
||||
return acc
|
||||
}
|
||||
|
||||
// Implements types.AccountStore
|
||||
func (as accountStore) GetAccount(ctx types.Context, addr crypto.Address) types.Account {
|
||||
// Implements sdk.AccountStore
|
||||
func (as accountStore) GetAccount(ctx sdk.Context, addr crypto.Address) sdk.Account {
|
||||
store := ctx.KVStore(as.key)
|
||||
bz := store.Get(addr)
|
||||
if bz == nil {
|
||||
return
|
||||
return nil // XXX
|
||||
}
|
||||
o, err := as.codec.Decode(bz)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return o.(types.Account)
|
||||
return o.(sdk.Account)
|
||||
}
|
||||
|
||||
// Implements types.AccountStore
|
||||
func (as accountStore) SetAccount(ctx types.Context, acc types.Account) {
|
||||
// Implements sdk.AccountStore
|
||||
func (as accountStore) SetAccount(ctx sdk.Context, acc sdk.Account) {
|
||||
addr := acc.GetAddress()
|
||||
store := ctx.KVStore(as.key)
|
||||
bz, err := as.codec.Encode(acc)
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
package bank
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
func TransferHandlerFn(newAccStore func(types.KVStore) types.AccountStore) types.Handler {
|
||||
return func(ctx types.Context, ms types.MultiStore, tx types.Tx) types.Result {
|
||||
func TransferHandlerFn(key sdk.SubstoreKey, newAccStore func(sdk.KVStore) sdk.AccountStore) sdk.Handler {
|
||||
return func(ctx sdk.Context, tx sdk.Tx) sdk.Result {
|
||||
|
||||
accStore := newAccStore(ms.GetKVStore("main"))
|
||||
accStore := newAccStore(ctx.KVStore(key))
|
||||
cs := CoinStore{accStore}
|
||||
|
||||
sendTx, ok := tx.(SendTx)
|
||||
sendTx, ok := tx.(sdk.Msg).(SendMsg)
|
||||
if !ok {
|
||||
panic("tx is not SendTx") // ?
|
||||
}
|
||||
|
@ -18,23 +18,23 @@ func TransferHandlerFn(newAccStore func(types.KVStore) types.AccountStore) types
|
|||
// NOTE: totalIn == totalOut should already have been checked
|
||||
|
||||
for _, in := range sendTx.Inputs {
|
||||
_, err := cs.SubtractCoins(in.Address, in.Coins)
|
||||
_, err := cs.SubtractCoins(ctx, in.Address, in.Coins)
|
||||
if err != nil {
|
||||
return types.Result{
|
||||
return sdk.Result{
|
||||
Code: 1, // TODO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, out := range sendTx.Outputs {
|
||||
_, err := cs.AddCoins(out.Address, out.Coins)
|
||||
_, err := cs.AddCoins(ctx, out.Address, out.Coins)
|
||||
if err != nil {
|
||||
return types.Result{
|
||||
return sdk.Result{
|
||||
Code: 1, // TODO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return types.Result{} // TODO
|
||||
return sdk.Result{} // TODO
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,10 +14,8 @@ type CoinStore struct {
|
|||
|
||||
// SubtractCoins subtracts amt from the coins at the addr.
|
||||
func (cs CoinStore) SubtractCoins(ctx sdk.Context, addr crypto.Address, amt sdk.Coins) (sdk.Coins, error) {
|
||||
acc, err := cs.store.GetAccount(ctx, addr)
|
||||
if err != nil {
|
||||
return amt, err
|
||||
} else if acc == nil {
|
||||
acc := cs.store.GetAccount(ctx, addr)
|
||||
if acc == nil {
|
||||
return amt, fmt.Errorf("Sending account (%s) does not exist", addr)
|
||||
}
|
||||
|
||||
|
@ -34,10 +32,8 @@ func (cs CoinStore) SubtractCoins(ctx sdk.Context, addr crypto.Address, amt sdk.
|
|||
|
||||
// AddCoins adds amt to the coins at the addr.
|
||||
func (cs CoinStore) AddCoins(ctx sdk.Context, addr crypto.Address, amt sdk.Coins) (sdk.Coins, error) {
|
||||
acc, err := cs.store.GetAccount(ctx, addr)
|
||||
if err != nil {
|
||||
return amt, err
|
||||
} else if acc == nil {
|
||||
acc := cs.store.GetAccount(ctx, addr)
|
||||
if acc == nil {
|
||||
acc = cs.store.NewAccountWithAddress(ctx, addr)
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ type SendMsg struct {
|
|||
}
|
||||
|
||||
// NewSendMsg - construct arbitrary multi-in, multi-out send msg.
|
||||
func NewSendMsg(in []Input, out []Output) types.Tx {
|
||||
func NewSendMsg(in []Input, out []Output) SendMsg {
|
||||
return SendMsg{Inputs: in, Outputs: out}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue