From 771c08483ee91f42ee922090e4152672eb5f1f91 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 11 Jul 2017 15:11:10 +0200 Subject: [PATCH] Remove prefix space in modules, done with state space --- modules/coin/handler.go | 16 +++++-------- modules/coin/handler_test.go | 6 ++--- modules/coin/store.go | 44 ++++++++---------------------------- modules/roles/handler.go | 2 +- modules/roles/middleware.go | 2 +- modules/roles/store.go | 6 ----- 6 files changed, 20 insertions(+), 56 deletions(-) diff --git a/modules/coin/handler.go b/modules/coin/handler.go index e0b51b89ff..bd047f8eb4 100644 --- a/modules/coin/handler.go +++ b/modules/coin/handler.go @@ -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 } diff --git a/modules/coin/handler_test.go b/modules/coin/handler_test.go index 50a4bbed75..716ad7ed7f 100644 --- a/modules/coin/handler_test.go +++ b/modules/coin/handler_test.go @@ -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) } diff --git a/modules/coin/store.go b/modules/coin/store.go index 01032cf1d3..94c086f1de 100644 --- a/modules/coin/store.go +++ b/modules/coin/store.go @@ -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"` diff --git a/modules/roles/handler.go b/modules/roles/handler.go index 99621ceccb..ce1ab137d2 100644 --- a/modules/roles/handler.go +++ b/modules/roles/handler.go @@ -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 } diff --git a/modules/roles/middleware.go b/modules/roles/middleware.go index c2d5352525..78bc4b1b30 100644 --- a/modules/roles/middleware.go +++ b/modules/roles/middleware.go @@ -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 } diff --git a/modules/roles/store.go b/modules/roles/store.go index bfde718339..4b3d4f3d14 100644 --- a/modules/roles/store.go +++ b/modules/roles/store.go @@ -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 {