2018-03-21 16:11:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2018-03-25 10:45:54 +00:00
|
|
|
"fmt"
|
2018-03-21 16:11:04 +00:00
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
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
|
|
|
|
|
2019-09-26 15:14:00 +00:00
|
|
|
// getAndUpdate retrieves AccountState from temporary or persistent Store
|
|
|
|
// or creates a new one if it doesn't exist.
|
2019-10-16 13:41:50 +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
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:41:50 +00:00
|
|
|
account, err := getAccountStateFromStore(s, hash)
|
2019-09-26 15:14:00 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != storage.ErrKeyNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-16 13:41:50 +00:00
|
|
|
account = NewAccountState(hash)
|
2019-09-26 15:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a[hash] = account
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAccountStateFromStore returns AccountState from the given Store if it's
|
|
|
|
// present there. Returns nil otherwise.
|
|
|
|
func getAccountStateFromStore(s storage.Store, hash util.Uint160) (*AccountState, error) {
|
|
|
|
var account *AccountState
|
2019-11-27 09:20:31 +00:00
|
|
|
key := storage.AppendPrefix(storage.STAccount, hash.BytesBE())
|
2019-09-26 15:14:00 +00:00
|
|
|
b, err := s.Get(key)
|
|
|
|
if err == nil {
|
|
|
|
account = new(AccountState)
|
2019-09-16 16:31:49 +00:00
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
account.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode (AccountState): %s", r.Err)
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 15:14:00 +00:00
|
|
|
return account, err
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 14:29:21 +00:00
|
|
|
// putAccountStateIntoStore puts given AccountState into the given store.
|
|
|
|
func putAccountStateIntoStore(store storage.Store, as *AccountState) error {
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-10-16 14:29:21 +00:00
|
|
|
as.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
key := storage.AppendPrefix(storage.STAccount, as.ScriptHash.BytesBE())
|
2019-10-16 14:29:21 +00:00
|
|
|
return store.Put(key, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// commit writes all account states to the given Batch.
|
|
|
|
func (a Accounts) commit(store storage.Store) error {
|
|
|
|
for _, state := range a {
|
|
|
|
if err := putAccountStateIntoStore(store, state); err != nil {
|
|
|
|
return err
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:35:09 +00:00
|
|
|
// UnspentBalance contains input/output transactons that sum up into the
|
|
|
|
// account balance for the given asset.
|
|
|
|
type UnspentBalance struct {
|
2019-11-15 19:04:10 +00:00
|
|
|
Tx util.Uint256 `json:"txid"`
|
|
|
|
Index uint16 `json:"n"`
|
|
|
|
Value util.Fixed8 `json:"value"`
|
2019-11-15 17:35:09 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 14:35:04 +00:00
|
|
|
// UnspentBalances is a slice of UnspentBalance (mostly needed to sort them).
|
|
|
|
type UnspentBalances []UnspentBalance
|
|
|
|
|
2018-03-21 16:11:04 +00:00
|
|
|
// 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
|
2019-11-15 17:35:09 +00:00
|
|
|
Balances map[util.Uint256][]UnspentBalance
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{},
|
2019-11-15 17:35:09 +00:00
|
|
|
Balances: make(map[util.Uint256][]UnspentBalance),
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
// DecodeBinary decodes AccountState from the given BinReader.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (s *AccountState) DecodeBinary(br *io.BinReader) {
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&s.Version)
|
|
|
|
br.ReadLE(&s.ScriptHash)
|
|
|
|
br.ReadLE(&s.IsFrozen)
|
2019-11-14 07:50:03 +00:00
|
|
|
br.ReadArray(&s.Votes)
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2019-11-15 17:35:09 +00:00
|
|
|
s.Balances = make(map[util.Uint256][]UnspentBalance)
|
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)
|
2019-11-15 17:35:09 +00:00
|
|
|
ubs := make([]UnspentBalance, 0)
|
|
|
|
br.ReadArray(&ubs)
|
|
|
|
s.Balances[key] = ubs
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
// EncodeBinary encodes AccountState to the given BinWriter.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (s *AccountState) EncodeBinary(bw *io.BinWriter) {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(s.Version)
|
|
|
|
bw.WriteLE(s.ScriptHash)
|
|
|
|
bw.WriteLE(s.IsFrozen)
|
2019-11-13 07:36:29 +00:00
|
|
|
bw.WriteArray(s.Votes)
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2019-11-15 17:35:09 +00:00
|
|
|
bw.WriteVarUint(uint64(len(s.Balances)))
|
|
|
|
for k, v := range s.Balances {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(k)
|
2019-11-15 17:35:09 +00:00
|
|
|
bw.WriteArray(v)
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:35:09 +00:00
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (u *UnspentBalance) DecodeBinary(r *io.BinReader) {
|
|
|
|
u.Tx.DecodeBinary(r)
|
|
|
|
r.ReadLE(&u.Index)
|
|
|
|
r.ReadLE(&u.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (u UnspentBalance) EncodeBinary(w *io.BinWriter) {
|
|
|
|
u.Tx.EncodeBinary(w)
|
|
|
|
w.WriteLE(u.Index)
|
|
|
|
w.WriteLE(u.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBalanceValues sums all unspent outputs and returns a map of asset IDs to
|
|
|
|
// overall balances.
|
|
|
|
func (s *AccountState) GetBalanceValues() map[util.Uint256]util.Fixed8 {
|
|
|
|
res := make(map[util.Uint256]util.Fixed8)
|
2018-03-21 16:11:04 +00:00
|
|
|
for k, v := range s.Balances {
|
2019-11-15 17:35:09 +00:00
|
|
|
balance := util.Fixed8(0)
|
|
|
|
for _, b := range v {
|
|
|
|
balance += b.Value
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
2019-11-15 17:35:09 +00:00
|
|
|
res[k] = balance
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
2019-11-15 17:35:09 +00:00
|
|
|
return res
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
2019-11-19 14:35:04 +00:00
|
|
|
|
|
|
|
// Len returns the length of UnspentBalances (used to sort things).
|
|
|
|
func (us UnspentBalances) Len() int { return len(us) }
|
|
|
|
|
|
|
|
// Less compares two elements of UnspentBalances (used to sort things).
|
|
|
|
func (us UnspentBalances) Less(i, j int) bool { return us[i].Value < us[j].Value }
|
|
|
|
|
|
|
|
// Swap swaps two elements of UnspentBalances (used to sort things).
|
|
|
|
func (us UnspentBalances) Swap(i, j int) { us[i], us[j] = us[j], us[i] }
|