2018-08-24 18:56:43 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-12-13 19:50:19 +00:00
|
|
|
"encoding/json"
|
2018-10-03 00:22:15 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-08-24 18:56:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2019-11-04 20:45:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
2019-11-13 17:00:21 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-12-13 19:50:19 +00:00
|
|
|
tmamino "github.com/tendermint/tendermint/crypto/encoding/amino"
|
|
|
|
"gopkg.in/yaml.v2"
|
2018-08-24 18:56:43 +00:00
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
var _ exported.Account = (*Account)(nil)
|
|
|
|
var _ exported.GenesisAccount = (*Account)(nil)
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-24 13:46:26 +00:00
|
|
|
const (
|
|
|
|
// DenomDefault defines the single coin type/denomination supported in
|
|
|
|
// Ethermint.
|
2019-07-04 19:46:54 +00:00
|
|
|
DenomDefault = "photon"
|
2018-10-24 13:46:26 +00:00
|
|
|
)
|
|
|
|
|
2018-10-03 14:57:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Main Ethermint account
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2019-11-13 17:00:21 +00:00
|
|
|
func init() {
|
|
|
|
authtypes.RegisterAccountTypeCodec(Account{}, EthermintAccountName)
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:36:22 +00:00
|
|
|
// Account implements the auth.Account interface and embeds an
|
2018-10-03 00:22:15 +00:00
|
|
|
// auth.BaseAccount type. It is compatible with the auth.AccountMapper.
|
|
|
|
type Account struct {
|
|
|
|
*auth.BaseAccount
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-04 16:06:19 +00:00
|
|
|
// merkle root of the storage trie
|
|
|
|
//
|
2019-09-18 13:51:18 +00:00
|
|
|
// TODO: add back root if needed (marshalling is broken if not initializing)
|
|
|
|
// Root ethcmn.Hash
|
2018-10-04 16:06:19 +00:00
|
|
|
|
2018-10-03 00:22:15 +00:00
|
|
|
CodeHash []byte
|
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-03 00:22:15 +00:00
|
|
|
// ProtoBaseAccount defines the prototype function for BaseAccount used for an
|
|
|
|
// account mapper.
|
2019-11-04 20:45:02 +00:00
|
|
|
func ProtoBaseAccount() exported.Account {
|
2018-10-03 00:22:15 +00:00
|
|
|
return &Account{BaseAccount: &auth.BaseAccount{}}
|
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-03 00:22:15 +00:00
|
|
|
// Balance returns the balance of an account.
|
|
|
|
func (acc Account) Balance() sdk.Int {
|
2018-10-24 13:46:26 +00:00
|
|
|
return acc.GetCoins().AmountOf(DenomDefault)
|
2018-10-03 00:22:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 21:07:34 +00:00
|
|
|
// SetBalance sets an account's balance of photons
|
2018-10-03 00:22:15 +00:00
|
|
|
func (acc Account) SetBalance(amt sdk.Int) {
|
2019-11-12 21:07:34 +00:00
|
|
|
coins := acc.GetCoins()
|
|
|
|
diff := amt.Sub(coins.AmountOf(DenomDefault))
|
2020-03-16 22:53:24 +00:00
|
|
|
switch {
|
|
|
|
case diff.IsPositive():
|
2019-11-12 21:07:34 +00:00
|
|
|
// Increase coins to amount
|
2020-03-16 22:53:24 +00:00
|
|
|
coins = coins.Add(sdk.NewCoins(sdk.NewCoin(DenomDefault, diff)))
|
|
|
|
case diff.IsNegative():
|
2019-11-12 21:07:34 +00:00
|
|
|
// Decrease coins to amount
|
2020-03-16 22:53:24 +00:00
|
|
|
coins = coins.Sub(sdk.NewCoins(sdk.NewCoin(DenomDefault, diff.Neg())))
|
|
|
|
default:
|
|
|
|
return
|
2019-11-12 21:07:34 +00:00
|
|
|
}
|
2020-03-16 22:53:24 +00:00
|
|
|
|
2019-11-12 21:07:34 +00:00
|
|
|
if err := acc.SetCoins(coins); err != nil {
|
|
|
|
panic(fmt.Sprintf("Could not set coins for address %s", acc.GetAddress()))
|
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 14:57:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Code & Storage
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-03 00:22:15 +00:00
|
|
|
type (
|
2019-07-02 19:36:22 +00:00
|
|
|
// Code is account Code type alias
|
|
|
|
Code []byte
|
|
|
|
// Storage is account storage type alias
|
2018-10-03 00:22:15 +00:00
|
|
|
Storage map[ethcmn.Hash]ethcmn.Hash
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c Code) String() string {
|
|
|
|
return string(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Storage) String() (str string) {
|
|
|
|
for key, value := range c {
|
|
|
|
str += fmt.Sprintf("%X : %X\n", key, value)
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
2018-10-03 00:22:15 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a copy of storage.
|
|
|
|
func (c Storage) Copy() Storage {
|
|
|
|
cpy := make(Storage)
|
|
|
|
for key, value := range c {
|
|
|
|
cpy[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return cpy
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
2019-12-13 19:50:19 +00:00
|
|
|
|
|
|
|
type ethermintAccountPretty struct {
|
|
|
|
Address sdk.AccAddress `json:"address" yaml:"address"`
|
|
|
|
Coins sdk.Coins `json:"coins" yaml:"coins"`
|
|
|
|
PubKey []byte `json:"public_key" yaml:"public_key"`
|
|
|
|
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
|
|
|
|
Sequence uint64 `json:"sequence" yaml:"sequence"`
|
|
|
|
CodeHash string `json:"code_hash" yaml:"code_hash"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML returns the YAML representation of an account.
|
|
|
|
func (acc Account) MarshalYAML() (interface{}, error) {
|
|
|
|
alias := ethermintAccountPretty{
|
|
|
|
Address: acc.Address,
|
|
|
|
Coins: acc.Coins,
|
|
|
|
AccountNumber: acc.AccountNumber,
|
|
|
|
Sequence: acc.Sequence,
|
|
|
|
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
|
|
|
|
}
|
|
|
|
|
|
|
|
if acc.PubKey != nil {
|
|
|
|
alias.PubKey = acc.PubKey.Bytes()
|
|
|
|
fmt.Println(len(alias.PubKey), alias.PubKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := yaml.Marshal(alias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bz), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON representation of an Account.
|
|
|
|
func (acc Account) MarshalJSON() ([]byte, error) {
|
|
|
|
alias := ethermintAccountPretty{
|
|
|
|
Address: acc.Address,
|
|
|
|
Coins: acc.Coins,
|
|
|
|
AccountNumber: acc.AccountNumber,
|
|
|
|
Sequence: acc.Sequence,
|
|
|
|
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
|
|
|
|
}
|
|
|
|
|
|
|
|
if acc.PubKey != nil {
|
|
|
|
alias.PubKey = acc.PubKey.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals raw JSON bytes into an Account.
|
|
|
|
func (acc *Account) UnmarshalJSON(bz []byte) error {
|
|
|
|
acc.BaseAccount = &authtypes.BaseAccount{}
|
|
|
|
var alias ethermintAccountPretty
|
|
|
|
if err := json.Unmarshal(bz, &alias); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if alias.PubKey != nil {
|
|
|
|
pubk, err := tmamino.PubKeyFromBytes(alias.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.BaseAccount.PubKey = pubk
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.BaseAccount.Address = alias.Address
|
|
|
|
acc.BaseAccount.Coins = alias.Coins
|
|
|
|
acc.BaseAccount.AccountNumber = alias.AccountNumber
|
|
|
|
acc.BaseAccount.Sequence = alias.Sequence
|
|
|
|
acc.CodeHash = ethcmn.Hex2Bytes(alias.CodeHash)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|