state: drop State from NEOBalance and NEP17Balance

We're in the `state` package already.
This commit is contained in:
Roman Khimov 2021-07-18 12:39:31 +03:00
parent f4a9139a05
commit 588f3fbbd3
4 changed files with 30 additions and 30 deletions

View file

@ -252,7 +252,7 @@ func getAccountState(ctx *cli.Context) error {
if len(res.Stack) == 0 { if len(res.Stack) == 0 {
return cli.NewExitError("result stack is empty", 1) return cli.NewExitError("result stack is empty", 1)
} }
st := new(state.NEOBalanceState) st := new(state.NEOBalance)
err = st.FromStackItem(res.Stack[0]) err = st.FromStackItem(res.Stack[0])
if err != nil { if err != nil {
return cli.NewExitError(fmt.Errorf("failed to convert account state from stackitem: %w", err), 1) return cli.NewExitError(fmt.Errorf("failed to convert account state from stackitem: %w", err), 1)

View file

@ -53,7 +53,7 @@ func newGAS() *GAS {
} }
func (g *GAS) increaseBalance(_ *interop.Context, _ util.Uint160, si *state.StorageItem, amount *big.Int) error { func (g *GAS) increaseBalance(_ *interop.Context, _ util.Uint160, si *state.StorageItem, amount *big.Int) error {
acc, err := state.NEP17BalanceStateFromBytes(*si) acc, err := state.NEP17BalanceFromBytes(*si)
if err != nil { if err != nil {
return err return err
} }
@ -72,7 +72,7 @@ func (g *GAS) increaseBalance(_ *interop.Context, _ util.Uint160, si *state.Stor
} }
func (g *GAS) balanceFromBytes(si *state.StorageItem) (*big.Int, error) { func (g *GAS) balanceFromBytes(si *state.StorageItem) (*big.Int, error) {
acc, err := state.NEP17BalanceStateFromBytes(*si) acc, err := state.NEP17BalanceFromBytes(*si)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -392,7 +392,7 @@ func (n *NEO) getGASPerVote(d dao.DAO, key []byte, index ...uint32) []big.Int {
} }
func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.StorageItem, amount *big.Int) error { func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.StorageItem, amount *big.Int) error {
acc, err := state.NEOBalanceStateFromBytes(*si) acc, err := state.NEOBalanceFromBytes(*si)
if err != nil { if err != nil {
return err return err
} }
@ -424,14 +424,14 @@ func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.Sto
} }
func (n *NEO) balanceFromBytes(si *state.StorageItem) (*big.Int, error) { func (n *NEO) balanceFromBytes(si *state.StorageItem) (*big.Int, error) {
acc, err := state.NEOBalanceStateFromBytes(*si) acc, err := state.NEOBalanceFromBytes(*si)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &acc.Balance, err return &acc.Balance, err
} }
func (n *NEO) distributeGas(ic *interop.Context, h util.Uint160, acc *state.NEOBalanceState) error { func (n *NEO) distributeGas(ic *interop.Context, h util.Uint160, acc *state.NEOBalance) error {
if ic.Block == nil || ic.Block.Index == 0 { if ic.Block == nil || ic.Block.Index == 0 {
return nil return nil
} }
@ -609,7 +609,7 @@ func (n *NEO) CalculateBonus(d dao.DAO, acc util.Uint160, end uint32) (*big.Int,
if si == nil { if si == nil {
return nil, storage.ErrKeyNotFound return nil, storage.ErrKeyNotFound
} }
st, err := state.NEOBalanceStateFromBytes(si) st, err := state.NEOBalanceFromBytes(si)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -752,7 +752,7 @@ func (n *NEO) VoteInternal(ic *interop.Context, h util.Uint160, pub *keys.Public
if si == nil { if si == nil {
return errors.New("invalid account") return errors.New("invalid account")
} }
acc, err := state.NEOBalanceStateFromBytes(si) acc, err := state.NEOBalanceFromBytes(si)
if err != nil { if err != nil {
return err return err
} }
@ -802,7 +802,7 @@ func (n *NEO) VoteInternal(ic *interop.Context, h util.Uint160, pub *keys.Public
// ModifyAccountVotes modifies votes of the specified account by value (can be negative). // ModifyAccountVotes modifies votes of the specified account by value (can be negative).
// typ specifies if this modify is occurring during transfer or vote (with old or new validator). // typ specifies if this modify is occurring during transfer or vote (with old or new validator).
func (n *NEO) ModifyAccountVotes(acc *state.NEOBalanceState, d dao.DAO, value *big.Int, isNewVote bool) error { func (n *NEO) ModifyAccountVotes(acc *state.NEOBalance, d dao.DAO, value *big.Int, isNewVote bool) error {
n.votesChanged.Store(true) n.votesChanged.Store(true)
if acc.VoteTo != nil { if acc.VoteTo != nil {
key := makeValidatorKey(acc.VoteTo) key := makeValidatorKey(acc.VoteTo)

View file

@ -10,21 +10,21 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
) )
// NEP17BalanceState represents balance state of a NEP17-token. // NEP17Balance represents balance state of a NEP17-token.
type NEP17BalanceState struct { type NEP17Balance struct {
Balance big.Int Balance big.Int
} }
// NEOBalanceState represents balance state of a NEO-token. // NEOBalance represents balance state of a NEO-token.
type NEOBalanceState struct { type NEOBalance struct {
NEP17BalanceState NEP17Balance
BalanceHeight uint32 BalanceHeight uint32
VoteTo *keys.PublicKey VoteTo *keys.PublicKey
} }
// NEP17BalanceStateFromBytes converts serialized NEP17BalanceState to structure. // NEP17BalanceFromBytes converts serialized NEP17Balance to structure.
func NEP17BalanceStateFromBytes(b []byte) (*NEP17BalanceState, error) { func NEP17BalanceFromBytes(b []byte) (*NEP17Balance, error) {
balance := new(NEP17BalanceState) balance := new(NEP17Balance)
err := balanceFromBytes(b, balance) err := balanceFromBytes(b, balance)
if err != nil { if err != nil {
return nil, err return nil, err
@ -32,8 +32,8 @@ func NEP17BalanceStateFromBytes(b []byte) (*NEP17BalanceState, error) {
return balance, nil return balance, nil
} }
// Bytes returns serialized NEP17BalanceState. // Bytes returns serialized NEP17Balance.
func (s *NEP17BalanceState) Bytes() []byte { func (s *NEP17Balance) Bytes() []byte {
return balanceToBytes(s) return balanceToBytes(s)
} }
@ -53,12 +53,12 @@ func balanceToBytes(item stackitem.Convertible) []byte {
} }
// ToStackItem implements stackitem.Convertible. It never returns an error. // ToStackItem implements stackitem.Convertible. It never returns an error.
func (s *NEP17BalanceState) ToStackItem() (stackitem.Item, error) { func (s *NEP17Balance) ToStackItem() (stackitem.Item, error) {
return stackitem.NewStruct([]stackitem.Item{stackitem.NewBigInteger(&s.Balance)}), nil return stackitem.NewStruct([]stackitem.Item{stackitem.NewBigInteger(&s.Balance)}), nil
} }
// FromStackItem implements stackitem.Convertible. // FromStackItem implements stackitem.Convertible.
func (s *NEP17BalanceState) FromStackItem(item stackitem.Item) error { func (s *NEP17Balance) FromStackItem(item stackitem.Item) error {
items, ok := item.Value().([]stackitem.Item) items, ok := item.Value().([]stackitem.Item)
if !ok { if !ok {
return errors.New("not a struct") return errors.New("not a struct")
@ -74,9 +74,9 @@ func (s *NEP17BalanceState) FromStackItem(item stackitem.Item) error {
return nil return nil
} }
// NEOBalanceStateFromBytes converts serialized NEOBalanceState to structure. // NEOBalanceFromBytes converts serialized NEOBalance to structure.
func NEOBalanceStateFromBytes(b []byte) (*NEOBalanceState, error) { func NEOBalanceFromBytes(b []byte) (*NEOBalance, error) {
balance := new(NEOBalanceState) balance := new(NEOBalance)
err := balanceFromBytes(b, balance) err := balanceFromBytes(b, balance)
if err != nil { if err != nil {
return nil, err return nil, err
@ -84,14 +84,14 @@ func NEOBalanceStateFromBytes(b []byte) (*NEOBalanceState, error) {
return balance, nil return balance, nil
} }
// Bytes returns serialized NEOBalanceState. // Bytes returns serialized NEOBalance.
func (s *NEOBalanceState) Bytes() []byte { func (s *NEOBalance) Bytes() []byte {
return balanceToBytes(s) return balanceToBytes(s)
} }
// ToStackItem implements stackitem.Convertible interface. It never returns an error. // ToStackItem implements stackitem.Convertible interface. It never returns an error.
func (s *NEOBalanceState) ToStackItem() (stackitem.Item, error) { func (s *NEOBalance) ToStackItem() (stackitem.Item, error) {
resItem, _ := s.NEP17BalanceState.ToStackItem() resItem, _ := s.NEP17Balance.ToStackItem()
result := resItem.(*stackitem.Struct) result := resItem.(*stackitem.Struct)
result.Append(stackitem.NewBigInteger(big.NewInt(int64(s.BalanceHeight)))) result.Append(stackitem.NewBigInteger(big.NewInt(int64(s.BalanceHeight))))
if s.VoteTo != nil { if s.VoteTo != nil {
@ -102,8 +102,8 @@ func (s *NEOBalanceState) ToStackItem() (stackitem.Item, error) {
return result, nil return result, nil
} }
// FromStackItem converts stackitem.Item to NEOBalanceState. // FromStackItem converts stackitem.Item to NEOBalance.
func (s *NEOBalanceState) FromStackItem(item stackitem.Item) error { func (s *NEOBalance) FromStackItem(item stackitem.Item) error {
structItem, ok := item.Value().([]stackitem.Item) structItem, ok := item.Value().([]stackitem.Item)
if !ok || len(structItem) < 3 { if !ok || len(structItem) < 3 {
return errors.New("invalid stackitem length") return errors.New("invalid stackitem length")