Fix up BasecoinApp and tests

This commit is contained in:
Ethan Frey
2017-07-03 21:34:08 +02:00
parent 7c4f408934
commit fc44de2141
6 changed files with 94 additions and 75 deletions
+6 -4
View File
@@ -8,6 +8,7 @@ import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/types"
)
@@ -24,7 +25,7 @@ var _ basecoin.Handler = Handler{}
func NewHandler() Handler {
return Handler{
Accountant: Accountant{Prefix: []byte(NameCoin + "/")},
Accountant: NewAccountant(""),
}
}
@@ -41,7 +42,7 @@ func (h Handler) CheckTx(ctx basecoin.Context, store types.KVStore, tx basecoin.
// now make sure there is money
for _, in := range send.Inputs {
_, err = h.CheckCoins(store, in.Address, in.Coins, in.Sequence)
_, err = h.CheckCoins(store, in.Address, in.Coins.Negative(), in.Sequence)
if err != nil {
return res, err
}
@@ -91,8 +92,9 @@ func (h Handler) SetOption(l log.Logger, store types.KVStore, key, value string)
if err != nil {
return "", ErrInvalidAddress()
}
actor := basecoin.Actor{App: NameCoin, Address: addr}
err = storeAccount(store, h.makeKey(actor), acc.ToAccount())
// this sets the permission for a public key signature, use that app
actor := stack.SigPerm(addr)
err = storeAccount(store, h.MakeKey(actor), acc.ToAccount())
if err != nil {
return "", err
}
+3 -3
View File
@@ -143,7 +143,7 @@ func TestDeliverTx(t *testing.T) {
store := types.NewMemKVStore()
for _, m := range tc.init {
acct := Account{Coins: m.coins}
err := storeAccount(store, h.makeKey(m.addr), acct)
err := storeAccount(store, h.MakeKey(m.addr), acct)
require.Nil(err, "%d: %+v", i, err)
}
@@ -153,7 +153,7 @@ func TestDeliverTx(t *testing.T) {
assert.Nil(err, "%d: %+v", i, err)
// make sure the final balances are correct
for _, f := range tc.final {
acct, err := loadAccount(store, h.makeKey(f.addr))
acct, err := loadAccount(store, h.MakeKey(f.addr))
assert.Nil(err, "%d: %+v", i, err)
assert.Equal(f.coins, acct.Coins)
}
@@ -210,7 +210,7 @@ func TestSetOption(t *testing.T) {
// check state is proper
for _, f := range tc.expected {
acct, err := loadAccount(store, h.makeKey(f.addr))
acct, err := loadAccount(store, h.MakeKey(f.addr))
assert.Nil(err, "%d: %+v", i, err)
assert.Equal(f.coins, acct.Coins)
}
+13 -4
View File
@@ -14,8 +14,17 @@ type Accountant struct {
Prefix []byte
}
func NewAccountant(prefix string) Accountant {
if prefix == "" {
prefix = NameCoin
}
return Accountant{
Prefix: []byte(prefix + "/"),
}
}
func (a Accountant) GetAccount(store types.KVStore, addr basecoin.Actor) (Account, error) {
acct, err := loadAccount(store, a.makeKey(addr))
acct, err := loadAccount(store, a.MakeKey(addr))
// for empty accounts, don't return an error, but rather an empty account
if IsNoAccountErr(err) {
err = nil
@@ -36,7 +45,7 @@ func (a Accountant) ChangeCoins(store types.KVStore, addr basecoin.Actor, coins
return acct.Coins, err
}
err = storeAccount(store, a.makeKey(addr), acct)
err = storeAccount(store, a.MakeKey(addr), acct)
return acct.Coins, err
}
@@ -44,7 +53,7 @@ func (a Accountant) ChangeCoins(store types.KVStore, addr basecoin.Actor, coins
//
// it doesn't save anything, that is up to you to decide (Check/Change Coins)
func (a Accountant) updateCoins(store types.KVStore, addr basecoin.Actor, coins types.Coins, seq int) (acct Account, err error) {
acct, err = loadAccount(store, a.makeKey(addr))
acct, err = loadAccount(store, a.MakeKey(addr))
// we can increase an empty account...
if IsNoAccountErr(err) && coins.IsPositive() {
err = nil
@@ -71,7 +80,7 @@ func (a Accountant) updateCoins(store types.KVStore, addr basecoin.Actor, coins
return acct, nil
}
func (a Accountant) makeKey(addr basecoin.Actor) []byte {
func (a Accountant) MakeKey(addr basecoin.Actor) []byte {
key := addr.Bytes()
if len(a.Prefix) > 0 {
key = append(a.Prefix, key...)