2018-03-21 16:11:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-03-25 10:45:54 +00:00
|
|
|
"fmt"
|
2018-03-21 16:11:04 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2018-03-21 16:11:04 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Accounts is mapping between a account address and AccountState.
|
|
|
|
type Accounts map[util.Uint160]*AccountState
|
|
|
|
|
2018-04-16 20:15:30 +00:00
|
|
|
func (a Accounts) getAndUpdate(s storage.Store, hash util.Uint160) (*AccountState, error) {
|
2018-03-21 16:11:04 +00:00
|
|
|
if account, ok := a[hash]; ok {
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
account := &AccountState{}
|
2018-03-21 16:11:04 +00:00
|
|
|
key := storage.AppendPrefix(storage.STAccount, hash.Bytes())
|
|
|
|
if b, err := s.Get(key); err == nil {
|
|
|
|
if err := account.DecodeBinary(bytes.NewReader(b)); err != nil {
|
2018-03-25 10:45:54 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode (AccountState): %s", err)
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
account = NewAccountState(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
a[hash] = account
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// commit writes all account states to the given Batch.
|
|
|
|
func (a Accounts) commit(b storage.Batch) error {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for hash, state := range a {
|
|
|
|
if err := state.EncodeBinary(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := storage.AppendPrefix(storage.STAccount, hash.Bytes())
|
|
|
|
b.Put(key, buf.Bytes())
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountState represents the state of a NEO account.
|
|
|
|
type AccountState struct {
|
2018-03-25 10:45:54 +00:00
|
|
|
Version uint8
|
2018-03-21 16:11:04 +00:00
|
|
|
ScriptHash util.Uint160
|
|
|
|
IsFrozen bool
|
2019-08-27 13:29:42 +00:00
|
|
|
Votes []*keys.PublicKey
|
2018-03-21 16:11:04 +00:00
|
|
|
Balances map[util.Uint256]util.Fixed8
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccountState returns a new AccountState object.
|
|
|
|
func NewAccountState(scriptHash util.Uint160) *AccountState {
|
|
|
|
return &AccountState{
|
2018-03-25 10:45:54 +00:00
|
|
|
Version: 0,
|
2018-03-21 16:11:04 +00:00
|
|
|
ScriptHash: scriptHash,
|
|
|
|
IsFrozen: false,
|
2019-08-27 13:29:42 +00:00
|
|
|
Votes: []*keys.PublicKey{},
|
2018-03-21 16:11:04 +00:00
|
|
|
Balances: make(map[util.Uint256]util.Fixed8),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary decodes AccountState from the given io.Reader.
|
|
|
|
func (s *AccountState) DecodeBinary(r io.Reader) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
br.ReadLE(&s.Version)
|
|
|
|
br.ReadLE(&s.ScriptHash)
|
|
|
|
br.ReadLE(&s.IsFrozen)
|
|
|
|
lenVotes := br.ReadVarUint()
|
2019-08-27 13:29:42 +00:00
|
|
|
s.Votes = make([]*keys.PublicKey, lenVotes)
|
2018-03-21 16:11:04 +00:00
|
|
|
for i := 0; i < int(lenVotes); i++ {
|
2019-08-27 13:29:42 +00:00
|
|
|
s.Votes[i] = &keys.PublicKey{}
|
2018-03-21 16:11:04 +00:00
|
|
|
if err := s.Votes[i].DecodeBinary(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Balances = make(map[util.Uint256]util.Fixed8)
|
2019-08-28 16:27:06 +00:00
|
|
|
lenBalances := br.ReadVarUint()
|
2018-03-21 16:11:04 +00:00
|
|
|
for i := 0; i < int(lenBalances); i++ {
|
|
|
|
key := util.Uint256{}
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&key)
|
2018-03-21 16:11:04 +00:00
|
|
|
var val util.Fixed8
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&val)
|
2018-03-21 16:11:04 +00:00
|
|
|
s.Balances[key] = val
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
return br.Err
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary encode AccountState to the given io.Writer.
|
|
|
|
func (s *AccountState) EncodeBinary(w io.Writer) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw := util.BinWriter{W: w}
|
|
|
|
bw.WriteLE(s.Version)
|
|
|
|
bw.WriteLE(s.ScriptHash)
|
|
|
|
bw.WriteLE(s.IsFrozen)
|
|
|
|
bw.WriteVarUint(uint64(len(s.Votes)))
|
2018-03-21 16:11:04 +00:00
|
|
|
for _, point := range s.Votes {
|
|
|
|
if err := point.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
balances := s.nonZeroBalances()
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteVarUint(uint64(len(balances)))
|
2018-03-25 10:45:54 +00:00
|
|
|
for k, v := range balances {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(k)
|
|
|
|
bw.WriteLE(v)
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
return bw.Err
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns only the non-zero balances for the account.
|
|
|
|
func (s *AccountState) nonZeroBalances() map[util.Uint256]util.Fixed8 {
|
|
|
|
b := make(map[util.Uint256]util.Fixed8)
|
2018-03-21 16:11:04 +00:00
|
|
|
for k, v := range s.Balances {
|
|
|
|
if v > 0 {
|
2018-03-25 10:45:54 +00:00
|
|
|
b[k] = v
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-25 10:45:54 +00:00
|
|
|
return b
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|