WIP commit

This commit is contained in:
Jae Kwon
2018-04-06 17:25:08 -07:00
parent a44e871dc7
commit 49fdf80d9d
25 changed files with 126 additions and 149 deletions
+2 -2
View File
@@ -118,9 +118,9 @@ func processSig(
// If pubkey is not known for account,
// set it from the StdSignature.
pubKey := acc.GetPubKey()
if pubKey.Empty() {
if pubKey == nil {
pubKey = sig.PubKey
if pubKey.Empty() {
if pubKey == nil {
return nil, sdk.ErrInvalidPubKey("PubKey not found").Result()
}
if !bytes.Equal(pubKey.Address(), addr) {
+4 -4
View File
@@ -31,7 +31,7 @@ func newCoins() sdk.Coins {
func privAndAddr() (crypto.PrivKey, sdk.Address) {
priv := crypto.GenPrivKeyEd25519()
addr := priv.PubKey().Address()
return priv.Wrap(), addr
return priv, addr
}
// run the tx through the anteHandler and ensure its valid
@@ -310,16 +310,16 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
msg = newTestMsg(addr2)
tx = newTestTx(ctx, msg, privs, seqs, fee)
sigs := tx.GetSignatures()
sigs[0].PubKey = crypto.PubKey{}
sigs[0].PubKey = nil
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidPubKey)
acc2 = mapper.GetAccount(ctx, addr2)
assert.True(t, acc2.GetPubKey().Empty())
assert.Nil(t, acc2.GetPubKey())
// test invalid signature and public key
tx = newTestTx(ctx, msg, privs, seqs, fee)
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidPubKey)
acc2 = mapper.GetAccount(ctx, addr2)
assert.True(t, acc2.GetPubKey().Empty())
assert.Nil(t, acc2.GetPubKey())
}
+1 -2
View File
@@ -61,7 +61,7 @@ func (acc BaseAccount) GetPubKey() crypto.PubKey {
// Implements sdk.Account.
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
if !acc.PubKey.Empty() {
if acc.PubKey != nil {
return errors.New("cannot override BaseAccount pubkey")
}
acc.PubKey = pubKey
@@ -94,6 +94,5 @@ func (acc *BaseAccount) SetSequence(seq int64) error {
// Wire
func RegisterWireBaseAccount(cdc *wire.Codec) {
// Register crypto.[PubKey,PrivKey,Signature] types.
wire.RegisterCrypto(cdc)
}
+2 -2
View File
@@ -15,7 +15,7 @@ func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.Address) {
key := crypto.GenPrivKeyEd25519()
pub := key.PubKey()
addr := pub.Address()
return key.Wrap(), pub, addr
return key, pub, addr
}
func TestBaseAccountAddressPubKey(t *testing.T) {
@@ -25,7 +25,7 @@ func TestBaseAccountAddressPubKey(t *testing.T) {
// check the address (set) and pubkey (not set)
assert.EqualValues(t, addr1, acc.GetAddress())
assert.EqualValues(t, crypto.PubKey{}, acc.GetPubKey())
assert.EqualValues(t, nil, acc.GetPubKey())
// can't override address
err := acc.SetAddress(addr2)
+10 -30
View File
@@ -1,12 +1,9 @@
package auth
import (
"bytes"
"fmt"
"reflect"
oldwire "github.com/tendermint/go-wire"
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
)
@@ -16,7 +13,7 @@ var _ sdk.AccountMapper = (*sealedAccountMapper)(nil)
// Implements sdk.AccountMapper.
// This AccountMapper encodes/decodes accounts using the
// go-wire (binary) encoding/decoding library.
// go-amino (binary) encoding/decoding library.
type accountMapper struct {
// The (unexposed) key used to access the store from the Context.
@@ -30,7 +27,7 @@ type accountMapper struct {
}
// NewAccountMapper returns a new sdk.AccountMapper that
// uses go-wire to (binary) encode and decode concrete sdk.Accounts.
// uses go-amino to (binary) encode and decode concrete sdk.Accounts.
func NewAccountMapper(key sdk.StoreKey, proto sdk.Account) accountMapper {
cdc := wire.NewCodec()
return accountMapper{
@@ -54,7 +51,7 @@ func NewAccountMapperSealed(key sdk.StoreKey, proto sdk.Account) sealedAccountMa
return am.Seal()
}
// Returns the go-wire codec. You may need to register interfaces
// Returns the go-amino codec. You may need to register interfaces
// and concrete types here, if your app's sdk.Account
// implementation includes interface fields.
// NOTE: It is not secure to expose the codec, so check out
@@ -103,7 +100,7 @@ type sealedAccountMapper struct {
}
// There's no way for external modules to mutate the
// sam.accountMapper.ctx from here, even with reflection.
// sam.accountMapper.cdc from here, even with reflection.
func (sam sealedAccountMapper) WireCodec() *wire.Codec {
panic("accountMapper is sealed")
}
@@ -152,34 +149,17 @@ func (am accountMapper) clonePrototype() sdk.Account {
}
func (am accountMapper) encodeAccount(acc sdk.Account) []byte {
bz, err := am.cdc.MarshalBinary(acc)
bz, err := am.cdc.MarshalBinaryBare(acc)
if err != nil {
panic(err)
}
return bz
}
func (am accountMapper) decodeAccount(bz []byte) sdk.Account {
// ... old go-wire ...
r, n, err := bytes.NewBuffer(bz), new(int), new(error)
accI := oldwire.ReadBinary(struct{ sdk.Account }{}, r, len(bz), n, err)
if *err != nil {
panic(*err)
func (am accountMapper) decodeAccount(bz []byte) (acc sdk.Account) {
err := am.cdc.UnmarshalBinaryBare(bz, &acc)
if err != nil {
panic(err)
}
acc := accI.(struct{ sdk.Account }).Account
return acc
/*
accPtr := am.clonePrototypePtr()
err := am.cdc.UnmarshalBinary(bz, accPtr)
if err != nil {
panic(err)
}
if reflect.ValueOf(am.proto).Kind() == reflect.Ptr {
return reflect.ValueOf(accPtr).Interface().(sdk.Account)
} else {
return reflect.ValueOf(accPtr).Elem().Interface().(sdk.Account)
}
*/
return
}
+3 -3
View File
@@ -6,8 +6,6 @@ import (
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
oldwire "github.com/tendermint/go-wire"
dbm "github.com/tendermint/tmlibs/db"
"github.com/cosmos/cosmos-sdk/store"
@@ -21,11 +19,13 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey) {
ms.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db)
ms.LoadLatestVersion()
/* XXX
// wire registration while we're at it ... TODO
var _ = oldwire.RegisterInterface(
struct{ sdk.Account }{},
oldwire.ConcreteType{&BaseAccount{}, 0x1},
)
*/
return ms, capKey
}
@@ -47,7 +47,7 @@ func TestAccountMapperGetSet(t *testing.T) {
acc = mapper.NewAccountWithAddress(ctx, addr)
assert.NotNil(t, acc)
assert.Equal(t, addr, acc.GetAddress())
assert.EqualValues(t, crypto.PubKey{}, acc.GetPubKey())
assert.EqualValues(t, nil, acc.GetPubKey())
assert.EqualValues(t, 0, acc.GetSequence())
// NewAccount doesn't call Set, so it's still nil
+10 -26
View File
@@ -7,7 +7,6 @@ import (
abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-crypto"
oldwire "github.com/tendermint/go-wire"
dbm "github.com/tendermint/tmlibs/db"
"github.com/cosmos/cosmos-sdk/store"
@@ -37,35 +36,20 @@ func getCoins(ck bank.CoinKeeper, ctx sdk.Context, addr crypto.Address) (sdk.Coi
return ck.AddCoins(ctx, addr, zero)
}
// custom tx codec
// TODO: use new go-wire
func makeCodec() *wire.Codec {
var cdc = wire.NewCodec()
const msgTypeSend = 0x1
const msgTypeIssue = 0x2
const msgTypeQuiz = 0x3
const msgTypeSetTrend = 0x4
const msgTypeIBCTransferMsg = 0x5
const msgTypeIBCReceiveMsg = 0x6
var _ = oldwire.RegisterInterface(
struct{ sdk.Msg }{},
oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend},
oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue},
oldwire.ConcreteType{IBCTransferMsg{}, msgTypeIBCTransferMsg},
oldwire.ConcreteType{IBCReceiveMsg{}, msgTypeIBCReceiveMsg},
)
// Register Msgs
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
cdc.RegisterConcrete(bank.SendMsg{}, "test/ibc/Send", nil)
cdc.RegisterConcrete(bank.IssueMsg{}, "test/ibc/Issue", nil)
cdc.RegisterConcrete(IBCTransferMsg{}, "test/ibc/IBCTransferMsg", nil)
cdc.RegisterConcrete(IBCReceiveMsg{}, "test/ibc/IBCReceiveMsg", nil)
const accTypeApp = 0x1
var _ = oldwire.RegisterInterface(
struct{ sdk.Account }{},
oldwire.ConcreteType{&auth.BaseAccount{}, accTypeApp},
)
cdc := wire.NewCodec()
// Register AppAccount
cdc.RegisterInterface((*sdk.Account)(nil), nil)
cdc.RegisterConcrete(&auth.BaseAccount{}, "test/ibc/Account", nil)
// cdc.RegisterInterface((*sdk.Msg)(nil), nil)
// bank.RegisterWire(cdc) // Register bank.[SendMsg,IssueMsg] types.
// crypto.RegisterWire(cdc) // Register crypto.[PubKey,PrivKey,Signature] types.
// ibc.RegisterWire(cdc) // Register ibc.[IBCTransferMsg, IBCReceiveMsg] types.
return cdc
}
+1 -1
View File
@@ -77,7 +77,7 @@ func (co commander) bondTxCmd(cmd *cobra.Command, args []string) error {
var pubKeyEd crypto.PubKeyEd25519
copy(pubKeyEd[:], rawPubKey)
msg := simplestake.NewBondMsg(from, stake, pubKeyEd.Wrap())
msg := simplestake.NewBondMsg(from, stake, pubKeyEd)
return co.sendMsg(msg)
}
+2 -2
View File
@@ -83,7 +83,7 @@ func (k Keeper) Bond(ctx sdk.Context, addr sdk.Address, pubKey crypto.PubKey, st
func (k Keeper) Unbond(ctx sdk.Context, addr sdk.Address) (crypto.PubKey, int64, sdk.Error) {
bi := k.getBondInfo(ctx, addr)
if bi.isEmpty() {
return crypto.PubKey{}, 0, ErrInvalidUnbond()
return nil, 0, ErrInvalidUnbond()
}
k.deleteBondInfo(ctx, addr)
@@ -121,7 +121,7 @@ func (k Keeper) bondWithoutCoins(ctx sdk.Context, addr sdk.Address, pubKey crypt
func (k Keeper) unbondWithoutCoins(ctx sdk.Context, addr sdk.Address) (crypto.PubKey, int64, sdk.Error) {
bi := k.getBondInfo(ctx, addr)
if bi.isEmpty() {
return crypto.PubKey{}, 0, ErrInvalidUnbond()
return nil, 0, ErrInvalidUnbond()
}
k.deleteBondInfo(ctx, addr)
+1 -1
View File
@@ -34,7 +34,7 @@ func (msg BondMsg) ValidateBasic() sdk.Error {
return ErrEmptyStake()
}
if msg.PubKey.Empty() {
if msg.PubKey == nil {
return sdk.ErrInvalidPubKey("BondMsg.PubKey must not be empty")
}
+1 -1
View File
@@ -250,6 +250,6 @@ func GetPubKey(pubKeyStr string) (pk crypto.PubKey, err error) {
}
var pkEd crypto.PubKeyEd25519
copy(pkEd[:], pkBytes[:])
pk = pkEd.Wrap()
pk = pkEd
return
}
+2 -2
View File
@@ -83,7 +83,7 @@ func subspace(prefix []byte) (start, end []byte) {
return prefix, end
}
func MakeCodec() *wire.Codec {
func makeTestCodec() *wire.Codec {
var cdc = wire.NewCodec()
// Register Msgs
@@ -152,7 +152,7 @@ func newPubKey(pk string) (res crypto.PubKey) {
//res, err = crypto.PubKeyFromBytes(pkBytes)
var pkEd crypto.PubKeyEd25519
copy(pkEd[:], pkBytes[:])
return pkEd.Wrap()
return pkEd
}
// for incode address generation