Remove prefix space in modules, done with state space
This commit is contained in:
parent
5cc7406a00
commit
771c08483e
@ -14,17 +14,13 @@ import (
|
||||
const NameCoin = "coin"
|
||||
|
||||
// Handler includes an accountant
|
||||
type Handler struct {
|
||||
Accountant
|
||||
}
|
||||
type Handler struct{}
|
||||
|
||||
var _ basecoin.Handler = Handler{}
|
||||
|
||||
// NewHandler - new accountant handler for the coin module
|
||||
func NewHandler() Handler {
|
||||
return Handler{
|
||||
Accountant: NewAccountant(""),
|
||||
}
|
||||
return Handler{}
|
||||
}
|
||||
|
||||
// Name - return name space
|
||||
@ -41,7 +37,7 @@ func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.
|
||||
|
||||
// now make sure there is money
|
||||
for _, in := range send.Inputs {
|
||||
_, err = h.CheckCoins(store, in.Address, in.Coins.Negative(), in.Sequence)
|
||||
_, err = CheckCoins(store, in.Address, in.Coins.Negative(), in.Sequence)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
@ -60,7 +56,7 @@ func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoi
|
||||
|
||||
// deduct from all input accounts
|
||||
for _, in := range send.Inputs {
|
||||
_, err = h.ChangeCoins(store, in.Address, in.Coins.Negative(), in.Sequence)
|
||||
_, err = ChangeCoins(store, in.Address, in.Coins.Negative(), in.Sequence)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
@ -69,7 +65,7 @@ func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoi
|
||||
// add to all output accounts
|
||||
for _, out := range send.Outputs {
|
||||
// note: sequence number is ignored when adding coins, only checked for subtracting
|
||||
_, err = h.ChangeCoins(store, out.Address, out.Coins, 0)
|
||||
_, err = ChangeCoins(store, out.Address, out.Coins, 0)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
@ -97,7 +93,7 @@ func (h Handler) SetOption(l log.Logger, store state.KVStore, module, key, value
|
||||
}
|
||||
// this sets the permission for a public key signature, use that app
|
||||
actor := auth.SigPerm(addr)
|
||||
err = storeAccount(store, h.MakeKey(actor), acc.ToAccount())
|
||||
err = storeAccount(store, actor.Bytes(), acc.ToAccount())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ func TestDeliverTx(t *testing.T) {
|
||||
store := state.NewMemKVStore()
|
||||
for _, m := range tc.init {
|
||||
acct := Account{Coins: m.coins}
|
||||
err := storeAccount(store, h.MakeKey(m.addr), acct)
|
||||
err := storeAccount(store, m.addr.Bytes(), acct)
|
||||
require.Nil(err, "%d: %+v", i, err)
|
||||
}
|
||||
|
||||
@ -154,7 +154,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, f.addr.Bytes())
|
||||
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, f.addr.Bytes())
|
||||
assert.Nil(err, "%d: %+v", i, err)
|
||||
assert.Equal(f.coins, acct.Coins)
|
||||
}
|
||||
|
||||
@ -10,25 +10,9 @@ import (
|
||||
"github.com/tendermint/basecoin/state"
|
||||
)
|
||||
|
||||
// Accountant - custom object to manage coins for the coin module
|
||||
// TODO prefix should be post-fix if maintaining the same key space
|
||||
type Accountant struct {
|
||||
Prefix []byte
|
||||
}
|
||||
|
||||
// NewAccountant - create the new accountant with prefix information
|
||||
func NewAccountant(prefix string) Accountant {
|
||||
if prefix == "" {
|
||||
prefix = NameCoin
|
||||
}
|
||||
return Accountant{
|
||||
Prefix: []byte(prefix + "/"),
|
||||
}
|
||||
}
|
||||
|
||||
// GetAccount - Get account from store and address
|
||||
func (a Accountant) GetAccount(store state.KVStore, addr basecoin.Actor) (Account, error) {
|
||||
acct, err := loadAccount(store, a.MakeKey(addr))
|
||||
func GetAccount(store state.KVStore, addr basecoin.Actor) (Account, error) {
|
||||
acct, err := loadAccount(store, addr.Bytes())
|
||||
|
||||
// for empty accounts, don't return an error, but rather an empty account
|
||||
if IsNoAccountErr(err) {
|
||||
@ -38,27 +22,27 @@ func (a Accountant) GetAccount(store state.KVStore, addr basecoin.Actor) (Accoun
|
||||
}
|
||||
|
||||
// CheckCoins makes sure there are funds, but doesn't change anything
|
||||
func (a Accountant) CheckCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (Coins, error) {
|
||||
acct, err := a.updateCoins(store, addr, coins, seq)
|
||||
func CheckCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (Coins, error) {
|
||||
acct, err := updateCoins(store, addr, coins, seq)
|
||||
return acct.Coins, err
|
||||
}
|
||||
|
||||
// ChangeCoins changes the money, returns error if it would be negative
|
||||
func (a Accountant) ChangeCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (Coins, error) {
|
||||
acct, err := a.updateCoins(store, addr, coins, seq)
|
||||
func ChangeCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (Coins, error) {
|
||||
acct, err := updateCoins(store, addr, coins, seq)
|
||||
if err != nil {
|
||||
return acct.Coins, err
|
||||
}
|
||||
|
||||
err = storeAccount(store, a.MakeKey(addr), acct)
|
||||
err = storeAccount(store, addr.Bytes(), acct)
|
||||
return acct.Coins, err
|
||||
}
|
||||
|
||||
// updateCoins will load the account, make all checks, and return the updated account.
|
||||
//
|
||||
// it doesn't save anything, that is up to you to decide (Check/Change Coins)
|
||||
func (a Accountant) updateCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (acct Account, err error) {
|
||||
acct, err = loadAccount(store, a.MakeKey(addr))
|
||||
func updateCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (acct Account, err error) {
|
||||
acct, err = loadAccount(store, addr.Bytes())
|
||||
// we can increase an empty account...
|
||||
if IsNoAccountErr(err) && coins.IsPositive() {
|
||||
err = nil
|
||||
@ -85,16 +69,6 @@ func (a Accountant) updateCoins(store state.KVStore, addr basecoin.Actor, coins
|
||||
return acct, nil
|
||||
}
|
||||
|
||||
// MakeKey - generate key bytes from address using accountant prefix
|
||||
// TODO Prefix -> PostFix for consistent namespace
|
||||
func (a Accountant) MakeKey(addr basecoin.Actor) []byte {
|
||||
key := addr.Bytes()
|
||||
if len(a.Prefix) > 0 {
|
||||
key = append(a.Prefix, key...)
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
// Account - coin account structure
|
||||
type Account struct {
|
||||
Coins Coins `json:"coins"`
|
||||
|
||||
@ -43,7 +43,7 @@ func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoi
|
||||
|
||||
// lets try...
|
||||
role := NewRole(create.MinSigs, create.Signers)
|
||||
err = createRole(store, MakeKey(create.Role), role)
|
||||
err = createRole(store, create.Role, role)
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
||||
@ -68,7 +68,7 @@ func assumeRole(ctx basecoin.Context, store state.KVStore, assume AssumeRoleTx)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
role, err := loadRole(store, MakeKey(assume.Role))
|
||||
role, err := loadRole(store, assume.Role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -55,12 +55,6 @@ func (r Role) IsAuthorized(ctx basecoin.Context) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// MakeKey creates the lookup key for a role
|
||||
func MakeKey(role []byte) []byte {
|
||||
prefix := []byte(NameRole + "/")
|
||||
return append(prefix, role...)
|
||||
}
|
||||
|
||||
func loadRole(store state.KVStore, key []byte) (role Role, err error) {
|
||||
data := store.Get(key)
|
||||
if len(data) == 0 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user