move account back to types

This commit is contained in:
Ethan Buchman
2018-01-09 19:11:00 -08:00
committed by Jae Kwon
parent f6a875d476
commit e908cfbb6f
12 changed files with 110 additions and 129 deletions
-65
View File
@@ -1,66 +1 @@
package main
import (
"encoding/json"
"path"
crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/types"
acm "github.com/cosmos/cosmos-sdk/x/account"
"github.com/cosmos/cosmos-sdk/x/sendtx"
"github.com/cosmos/cosmos-sdk/x/store"
)
func txParser(txBytes []byte) (types.Tx, error) {
var tx sendtx.SendTx
err := json.Unmarshal(txBytes, &tx)
return tx, err
}
//-----------------------------------------------------------------------------
type AccountStore struct {
kvStore types.KVStore
}
func newAccountStore(kvStore types.KVStore) store.AccountStore {
return AccountStore{kvStore}
}
func (accStore AccountStore) NewAccountWithAddress(addr crypto.Address) store.Account {
return acm.NewBaseAccountWithAddress(addr)
}
func (accStore AccountStore) GetAccount(addr crypto.Address) store.Account {
v := accStore.kvStore.Get(keyAccount(addr))
if len(v) == 0 {
return nil
}
acc := new(acm.BaseAccount)
if err := json.Unmarshal(v, acc); err != nil {
panic(err)
}
return acc
}
func (accStore AccountStore) SetAccount(acc store.Account) {
b, err := json.Marshal(acc)
if err != nil {
panic(err)
}
appAcc, ok := acc.(*acm.BaseAccount)
if !ok {
panic("acc is not *acm.BaseAccount") // XXX
}
accStore.kvStore.Set(keyAccount(appAcc.Address()), b)
}
func keyAccount(addr crypto.Address) []byte {
return []byte(path.Join("account", string(addr)))
}
+13 -3
View File
@@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"fmt"
"os"
@@ -11,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/app"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/types"
acm "github.com/cosmos/cosmos-sdk/x/account"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/sendtx"
)
@@ -38,14 +40,14 @@ func main() {
handler := types.ChainDecorators(
// recover.Decorator(),
// logger.Decorator(),
auth.DecoratorFn(newAccountStore),
auth.DecoratorFn(acm.NewAccountStore),
).WithHandler(
sendtx.TransferHandlerFn(newAccountStore),
sendtx.TransferHandlerFn(acm.NewAccountStore),
)
// TODO: load genesis
// TODO: InitChain with validators
// accounts := newAccountStore(multiStore.GetKVStore("main"))
// accounts := acm.NewAccountStore(multiStore.GetKVStore("main"))
// TODO: set the genesis accounts
// Set everything on the app and load latest
@@ -72,3 +74,11 @@ func main() {
})
return
}
func txParser(txBytes []byte) (types.Tx, error) {
var tx sendtx.SendTx
err := json.Unmarshal(txBytes, &tx)
return tx, err
}
//-----------------------------------------------------------------------------