core: rename state.NEP17Balances to state.NEP17TransferInfo

Balances are to be removed from state.NEP17TransferInfo, so the remnant
fields are NextTransferBatch, NewBatch and a map of LastUpdatedBlocks.
These fields are more staff-related.

Also rename dao.[Get, Put, put]NEP17Balances and STNEP17Balances
preffix.

Also rename NEP17TransferInfo.Trackers to LastUpdatedBlockTrackers
because NEP17TransferInfo.Balances are to be removed.
This commit is contained in:
Anna Shaleva 2021-07-25 13:48:50 +03:00
parent c0a2c74e0c
commit e46d76d7aa
10 changed files with 69 additions and 69 deletions

View file

@ -260,7 +260,7 @@ func (chain *FakeChain) ForEachNEP17Transfer(util.Uint160, func(*state.NEP17Tran
}
// GetNEP17Balances implements Blockchainer interface.
func (chain *FakeChain) GetNEP17Balances(util.Uint160) *state.NEP17Balances {
func (chain *FakeChain) GetNEP17Balances(util.Uint160) *state.NEP17TransferInfo {
panic("TODO")
}

View file

@ -989,14 +989,14 @@ func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b
Tx: h,
}
if !fromAddr.Equals(util.Uint160{}) {
balances, err := cache.GetNEP17Balances(fromAddr)
balances, err := cache.GetNEP17TransferInfo(fromAddr)
if err != nil {
return
}
bs := balances.Trackers[id]
bs := balances.LastUpdated[id]
bs.Balance = *new(big.Int).Sub(&bs.Balance, amount)
bs.LastUpdatedBlock = b.Index
balances.Trackers[id] = bs
balances.LastUpdated[id] = bs
transfer.Amount = *new(big.Int).Sub(&transfer.Amount, amount)
balances.NewBatch, err = cache.AppendNEP17Transfer(fromAddr,
balances.NextTransferBatch, balances.NewBatch, transfer)
@ -1006,19 +1006,19 @@ func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b
if balances.NewBatch {
balances.NextTransferBatch++
}
if err := cache.PutNEP17Balances(fromAddr, balances); err != nil {
if err := cache.PutNEP17TransferInfo(fromAddr, balances); err != nil {
return
}
}
if !toAddr.Equals(util.Uint160{}) {
balances, err := cache.GetNEP17Balances(toAddr)
balances, err := cache.GetNEP17TransferInfo(toAddr)
if err != nil {
return
}
bs := balances.Trackers[id]
bs := balances.LastUpdated[id]
bs.Balance = *new(big.Int).Add(&bs.Balance, amount)
bs.LastUpdatedBlock = b.Index
balances.Trackers[id] = bs
balances.LastUpdated[id] = bs
transfer.Amount = *amount
balances.NewBatch, err = cache.AppendNEP17Transfer(toAddr,
@ -1029,7 +1029,7 @@ func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b
if balances.NewBatch {
balances.NextTransferBatch++
}
if err := cache.PutNEP17Balances(toAddr, balances); err != nil {
if err := cache.PutNEP17TransferInfo(toAddr, balances); err != nil {
return
}
}
@ -1037,7 +1037,7 @@ func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b
// ForEachNEP17Transfer executes f for each nep17 transfer in log.
func (bc *Blockchain) ForEachNEP17Transfer(acc util.Uint160, f func(*state.NEP17Transfer) (bool, error)) error {
balances, err := bc.dao.GetNEP17Balances(acc)
balances, err := bc.dao.GetNEP17TransferInfo(acc)
if err != nil {
return nil
}
@ -1058,8 +1058,8 @@ func (bc *Blockchain) ForEachNEP17Transfer(acc util.Uint160, f func(*state.NEP17
}
// GetNEP17Balances returns NEP17 balances for the acc.
func (bc *Blockchain) GetNEP17Balances(acc util.Uint160) *state.NEP17Balances {
bs, err := bc.dao.GetNEP17Balances(acc)
func (bc *Blockchain) GetNEP17Balances(acc util.Uint160) *state.NEP17TransferInfo {
bs, err := bc.dao.GetNEP17TransferInfo(acc)
if err != nil {
return nil
}
@ -1068,22 +1068,22 @@ func (bc *Blockchain) GetNEP17Balances(acc util.Uint160) *state.NEP17Balances {
// GetUtilityTokenBalance returns utility token (GAS) balance for the acc.
func (bc *Blockchain) GetUtilityTokenBalance(acc util.Uint160) *big.Int {
bs, err := bc.dao.GetNEP17Balances(acc)
bs, err := bc.dao.GetNEP17TransferInfo(acc)
if err != nil {
return big.NewInt(0)
}
balance := bs.Trackers[bc.contracts.GAS.ID].Balance
balance := bs.LastUpdated[bc.contracts.GAS.ID].Balance
return &balance
}
// GetGoverningTokenBalance returns governing token (NEO) balance and the height
// of the last balance change for the account.
func (bc *Blockchain) GetGoverningTokenBalance(acc util.Uint160) (*big.Int, uint32) {
bs, err := bc.dao.GetNEP17Balances(acc)
bs, err := bc.dao.GetNEP17TransferInfo(acc)
if err != nil {
return big.NewInt(0), 0
}
neo := bs.Trackers[bc.contracts.NEO.ID]
neo := bs.LastUpdated[bc.contracts.NEO.ID]
return &neo.Balance, neo.LastUpdatedBlock
}

View file

@ -47,7 +47,7 @@ type Blockchainer interface {
GetNativeContractScriptHash(string) (util.Uint160, error)
GetNatives() []state.NativeContract
GetNextBlockValidators() ([]*keys.PublicKey, error)
GetNEP17Balances(util.Uint160) *state.NEP17Balances
GetNEP17Balances(util.Uint160) *state.NEP17TransferInfo
GetNotaryContractScriptHash() util.Uint160
GetNotaryBalance(acc util.Uint160) *big.Int
GetPolicer() Policer

View file

@ -13,7 +13,7 @@ import (
// objects in the storeBlock().
type Cached struct {
DAO
balances map[util.Uint160]*state.NEP17Balances
balances map[util.Uint160]*state.NEP17TransferInfo
transfers map[util.Uint160]map[uint32]*state.NEP17TransferLog
dropNEP17Cache bool
@ -21,21 +21,21 @@ type Cached struct {
// NewCached returns new Cached wrapping around given backing store.
func NewCached(d DAO) *Cached {
balances := make(map[util.Uint160]*state.NEP17Balances)
balances := make(map[util.Uint160]*state.NEP17TransferInfo)
transfers := make(map[util.Uint160]map[uint32]*state.NEP17TransferLog)
return &Cached{d.GetWrapped(), balances, transfers, false}
}
// GetNEP17Balances retrieves NEP17Balances for the acc.
func (cd *Cached) GetNEP17Balances(acc util.Uint160) (*state.NEP17Balances, error) {
// GetNEP17TransferInfo retrieves NEP17TransferInfo for the acc.
func (cd *Cached) GetNEP17TransferInfo(acc util.Uint160) (*state.NEP17TransferInfo, error) {
if bs := cd.balances[acc]; bs != nil {
return bs, nil
}
return cd.DAO.GetNEP17Balances(acc)
return cd.DAO.GetNEP17TransferInfo(acc)
}
// PutNEP17Balances saves NEP17Balances for the acc.
func (cd *Cached) PutNEP17Balances(acc util.Uint160, bs *state.NEP17Balances) error {
// PutNEP17TransferInfo saves NEP17TransferInfo for the acc.
func (cd *Cached) PutNEP17TransferInfo(acc util.Uint160, bs *state.NEP17TransferInfo) error {
cd.balances[acc] = bs
return nil
}
@ -88,7 +88,7 @@ func (cd *Cached) Persist() (int, error) {
// caches (accounts/transfer data) in any way.
if ok {
if cd.dropNEP17Cache {
lowerCache.balances = make(map[util.Uint160]*state.NEP17Balances)
lowerCache.balances = make(map[util.Uint160]*state.NEP17TransferInfo)
}
var simpleCache *Simple
for simpleCache == nil {
@ -105,7 +105,7 @@ func (cd *Cached) Persist() (int, error) {
buf := io.NewBufBinWriter()
for acc, bs := range cd.balances {
err := cd.DAO.putNEP17Balances(acc, bs, buf)
err := cd.DAO.putNEP17TransferInfo(acc, bs, buf)
if err != nil {
return 0, err
}

View file

@ -42,7 +42,7 @@ type DAO interface {
GetCurrentBlockHeight() (uint32, error)
GetCurrentHeaderHeight() (i uint32, h util.Uint256, err error)
GetHeaderHashes() ([]util.Uint256, error)
GetNEP17Balances(acc util.Uint160) (*state.NEP17Balances, error)
GetNEP17TransferInfo(acc util.Uint160) (*state.NEP17TransferInfo, error)
GetNEP17TransferLog(acc util.Uint160, index uint32) (*state.NEP17TransferLog, error)
GetStorageItem(id int32, key []byte) state.StorageItem
GetStorageItems(id int32) (map[string]state.StorageItem, error)
@ -55,7 +55,7 @@ type DAO interface {
PutAppExecResult(aer *state.AppExecResult, buf *io.BufBinWriter) error
PutContractID(id int32, hash util.Uint160) error
PutCurrentHeader(hashAndIndex []byte) error
PutNEP17Balances(acc util.Uint160, bs *state.NEP17Balances) error
PutNEP17TransferInfo(acc util.Uint160, bs *state.NEP17TransferInfo) error
PutNEP17TransferLog(acc util.Uint160, index uint32, lg *state.NEP17TransferLog) error
PutStorageItem(id int32, key []byte, si state.StorageItem) error
PutVersion(v string) error
@ -63,7 +63,7 @@ type DAO interface {
StoreAsBlock(block *block.Block, buf *io.BufBinWriter) error
StoreAsCurrentBlock(block *block.Block, buf *io.BufBinWriter) error
StoreAsTransaction(tx *transaction.Transaction, index uint32, buf *io.BufBinWriter) error
putNEP17Balances(acc util.Uint160, bs *state.NEP17Balances, buf *io.BufBinWriter) error
putNEP17TransferInfo(acc util.Uint160, bs *state.NEP17TransferInfo, buf *io.BufBinWriter) error
}
// Simple is memCached wrapper around DB, simple DAO implementation.
@ -142,12 +142,12 @@ func (dao *Simple) GetContractScriptHash(id int32) (util.Uint160, error) {
return *data, nil
}
// -- start nep17 balances.
// -- start nep17 transfer info.
// GetNEP17Balances retrieves nep17 balances from the cache.
func (dao *Simple) GetNEP17Balances(acc util.Uint160) (*state.NEP17Balances, error) {
key := storage.AppendPrefix(storage.STNEP17Balances, acc.BytesBE())
bs := state.NewNEP17Balances()
// GetNEP17TransferInfo retrieves nep17 transfer info from the cache.
func (dao *Simple) GetNEP17TransferInfo(acc util.Uint160) (*state.NEP17TransferInfo, error) {
key := storage.AppendPrefix(storage.STNEP17TransferInfo, acc.BytesBE())
bs := state.NewNEP17TransferInfo()
err := dao.GetAndDecode(bs, key)
if err != nil && err != storage.ErrKeyNotFound {
return nil, err
@ -155,17 +155,17 @@ func (dao *Simple) GetNEP17Balances(acc util.Uint160) (*state.NEP17Balances, err
return bs, nil
}
// PutNEP17Balances saves nep17 balances from the cache.
func (dao *Simple) PutNEP17Balances(acc util.Uint160, bs *state.NEP17Balances) error {
return dao.putNEP17Balances(acc, bs, io.NewBufBinWriter())
// PutNEP17TransferInfo saves nep17 transfer info in the cache.
func (dao *Simple) PutNEP17TransferInfo(acc util.Uint160, bs *state.NEP17TransferInfo) error {
return dao.putNEP17TransferInfo(acc, bs, io.NewBufBinWriter())
}
func (dao *Simple) putNEP17Balances(acc util.Uint160, bs *state.NEP17Balances, buf *io.BufBinWriter) error {
key := storage.AppendPrefix(storage.STNEP17Balances, acc.BytesBE())
func (dao *Simple) putNEP17TransferInfo(acc util.Uint160, bs *state.NEP17TransferInfo, buf *io.BufBinWriter) error {
key := storage.AppendPrefix(storage.STNEP17TransferInfo, acc.BytesBE())
return dao.putWithBuffer(bs, key, buf)
}
// -- end nep17 balances.
// -- end nep17 transfer info.
// -- start transfer log.

View file

@ -713,7 +713,7 @@ func checkFAULTState(t *testing.T, result *state.AppExecResult) {
}
func checkBalanceOf(t *testing.T, chain *Blockchain, addr util.Uint160, expected int) {
balance := chain.GetNEP17Balances(addr).Trackers[chain.contracts.GAS.ID]
balance := chain.GetNEP17Balances(addr).LastUpdated[chain.contracts.GAS.ID]
require.Equal(t, int64(expected), balance.Balance.Int64())
}

View file

@ -76,11 +76,11 @@ func TestGAS_Roundtrip(t *testing.T) {
bc := newTestChain(t)
getUtilityTokenBalance := func(bc *Blockchain, acc util.Uint160) (*big.Int, uint32) {
bs, err := bc.dao.GetNEP17Balances(acc)
bs, err := bc.dao.GetNEP17TransferInfo(acc)
if err != nil {
return big.NewInt(0), 0
}
balance := bs.Trackers[bc.contracts.GAS.ID]
balance := bs.LastUpdated[bc.contracts.GAS.ID]
return &balance.Balance, balance.LastUpdatedBlock
}

View file

@ -44,25 +44,25 @@ type NEP17Transfer struct {
Tx util.Uint256
}
// NEP17Balances is a map of the NEP17 contract IDs
// NEP17TransferInfo is a map of the NEP17 contract IDs
// to the corresponding structures.
type NEP17Balances struct {
Trackers map[int32]NEP17Tracker
type NEP17TransferInfo struct {
LastUpdated map[int32]NEP17Tracker
// NextTransferBatch stores an index of the next transfer batch.
NextTransferBatch uint32
// NewBatch is true if batch with the `NextTransferBatch` index should be created.
NewBatch bool
}
// NewNEP17Balances returns new NEP17Balances.
func NewNEP17Balances() *NEP17Balances {
return &NEP17Balances{
Trackers: make(map[int32]NEP17Tracker),
// NewNEP17TransferInfo returns new NEP17TransferInfo.
func NewNEP17TransferInfo() *NEP17TransferInfo {
return &NEP17TransferInfo{
LastUpdated: make(map[int32]NEP17Tracker),
}
}
// DecodeBinary implements io.Serializable interface.
func (bs *NEP17Balances) DecodeBinary(r *io.BinReader) {
func (bs *NEP17TransferInfo) DecodeBinary(r *io.BinReader) {
bs.NextTransferBatch = r.ReadU32LE()
bs.NewBatch = r.ReadBool()
lenBalances := r.ReadVarUint()
@ -73,15 +73,15 @@ func (bs *NEP17Balances) DecodeBinary(r *io.BinReader) {
tr.DecodeBinary(r)
m[key] = tr
}
bs.Trackers = m
bs.LastUpdated = m
}
// EncodeBinary implements io.Serializable interface.
func (bs *NEP17Balances) EncodeBinary(w *io.BinWriter) {
func (bs *NEP17TransferInfo) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(bs.NextTransferBatch)
w.WriteBool(bs.NewBatch)
w.WriteVarUint(uint64(len(bs.Trackers)))
for k, v := range bs.Trackers {
w.WriteVarUint(uint64(len(bs.LastUpdated)))
for k, v := range bs.LastUpdated {
w.WriteU32LE(uint32(k))
v.EncodeBinary(w)
}

View file

@ -8,19 +8,19 @@ import (
// KeyPrefix constants.
const (
DataBlock KeyPrefix = 0x01
DataTransaction KeyPrefix = 0x02
DataMPT KeyPrefix = 0x03
STAccount KeyPrefix = 0x40
STNotification KeyPrefix = 0x4d
STContractID KeyPrefix = 0x51
STStorage KeyPrefix = 0x70
STNEP17Transfers KeyPrefix = 0x72
STNEP17Balances KeyPrefix = 0x73
IXHeaderHashList KeyPrefix = 0x80
SYSCurrentBlock KeyPrefix = 0xc0
SYSCurrentHeader KeyPrefix = 0xc1
SYSVersion KeyPrefix = 0xf0
DataBlock KeyPrefix = 0x01
DataTransaction KeyPrefix = 0x02
DataMPT KeyPrefix = 0x03
STAccount KeyPrefix = 0x40
STNotification KeyPrefix = 0x4d
STContractID KeyPrefix = 0x51
STStorage KeyPrefix = 0x70
STNEP17Transfers KeyPrefix = 0x72
STNEP17TransferInfo KeyPrefix = 0x73
IXHeaderHashList KeyPrefix = 0x80
SYSCurrentBlock KeyPrefix = 0xc0
SYSCurrentHeader KeyPrefix = 0xc1
SYSVersion KeyPrefix = 0xf0
)
const (

View file

@ -676,7 +676,7 @@ func (s *Server) getNEP17Balances(ps request.Params) (interface{}, *response.Err
}
if as != nil {
cache := make(map[int32]util.Uint160)
for id, bal := range as.Trackers {
for id, bal := range as.LastUpdated {
h, err := s.getHash(id, cache)
if err != nil {
continue