*: replace all NEP5 occurences to NEP17
This commit is contained in:
parent
b97dfae8d8
commit
31eca342eb
33 changed files with 335 additions and 335 deletions
|
@ -791,7 +791,7 @@ func (bc *Blockchain) handleNotification(note *state.NotificationEvent, d *dao.C
|
|||
}
|
||||
amount = bigint.FromBytes(bs)
|
||||
}
|
||||
bc.processNEP5Transfer(d, h, b, note.ScriptHash, from, to, amount)
|
||||
bc.processNEP17Transfer(d, h, b, note.ScriptHash, from, to, amount)
|
||||
}
|
||||
|
||||
func parseUint160(addr []byte) util.Uint160 {
|
||||
|
@ -801,7 +801,7 @@ func parseUint160(addr []byte) util.Uint160 {
|
|||
return util.Uint160{}
|
||||
}
|
||||
|
||||
func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, h util.Uint256, b *block.Block, sc util.Uint160, from, to []byte, amount *big.Int) {
|
||||
func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b *block.Block, sc util.Uint160, from, to []byte, amount *big.Int) {
|
||||
toAddr := parseUint160(to)
|
||||
fromAddr := parseUint160(from)
|
||||
var id int32
|
||||
|
@ -815,7 +815,7 @@ func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, h util.Uint256, b *
|
|||
}
|
||||
id = assetContract.ID
|
||||
}
|
||||
transfer := &state.NEP5Transfer{
|
||||
transfer := &state.NEP17Transfer{
|
||||
Asset: id,
|
||||
From: fromAddr,
|
||||
To: toAddr,
|
||||
|
@ -824,7 +824,7 @@ func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, h util.Uint256, b *
|
|||
Tx: h,
|
||||
}
|
||||
if !fromAddr.Equals(util.Uint160{}) {
|
||||
balances, err := cache.GetNEP5Balances(fromAddr)
|
||||
balances, err := cache.GetNEP17Balances(fromAddr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -833,19 +833,19 @@ func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, h util.Uint256, b *
|
|||
bs.LastUpdatedBlock = b.Index
|
||||
balances.Trackers[id] = bs
|
||||
transfer.Amount = *new(big.Int).Sub(&transfer.Amount, amount)
|
||||
isBig, err := cache.AppendNEP5Transfer(fromAddr, balances.NextTransferBatch, transfer)
|
||||
isBig, err := cache.AppendNEP17Transfer(fromAddr, balances.NextTransferBatch, transfer)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if isBig {
|
||||
balances.NextTransferBatch++
|
||||
}
|
||||
if err := cache.PutNEP5Balances(fromAddr, balances); err != nil {
|
||||
if err := cache.PutNEP17Balances(fromAddr, balances); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if !toAddr.Equals(util.Uint160{}) {
|
||||
balances, err := cache.GetNEP5Balances(toAddr)
|
||||
balances, err := cache.GetNEP17Balances(toAddr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -855,27 +855,27 @@ func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, h util.Uint256, b *
|
|||
balances.Trackers[id] = bs
|
||||
|
||||
transfer.Amount = *amount
|
||||
isBig, err := cache.AppendNEP5Transfer(toAddr, balances.NextTransferBatch, transfer)
|
||||
isBig, err := cache.AppendNEP17Transfer(toAddr, balances.NextTransferBatch, transfer)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if isBig {
|
||||
balances.NextTransferBatch++
|
||||
}
|
||||
if err := cache.PutNEP5Balances(toAddr, balances); err != nil {
|
||||
if err := cache.PutNEP17Balances(toAddr, balances); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ForEachNEP5Transfer executes f for each nep5 transfer in log.
|
||||
func (bc *Blockchain) ForEachNEP5Transfer(acc util.Uint160, f func(*state.NEP5Transfer) (bool, error)) error {
|
||||
balances, err := bc.dao.GetNEP5Balances(acc)
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
for i := int(balances.NextTransferBatch); i >= 0; i-- {
|
||||
lg, err := bc.dao.GetNEP5TransferLog(acc, uint32(i))
|
||||
lg, err := bc.dao.GetNEP17TransferLog(acc, uint32(i))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -890,9 +890,9 @@ func (bc *Blockchain) ForEachNEP5Transfer(acc util.Uint160, f func(*state.NEP5Tr
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetNEP5Balances returns NEP5 balances for the acc.
|
||||
func (bc *Blockchain) GetNEP5Balances(acc util.Uint160) *state.NEP5Balances {
|
||||
bs, err := bc.dao.GetNEP5Balances(acc)
|
||||
// GetNEP17Balances returns NEP17 balances for the acc.
|
||||
func (bc *Blockchain) GetNEP17Balances(acc util.Uint160) *state.NEP17Balances {
|
||||
bs, err := bc.dao.GetNEP17Balances(acc)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -901,7 +901,7 @@ func (bc *Blockchain) GetNEP5Balances(acc util.Uint160) *state.NEP5Balances {
|
|||
|
||||
// GetUtilityTokenBalance returns utility token (GAS) balance for the acc.
|
||||
func (bc *Blockchain) GetUtilityTokenBalance(acc util.Uint160) *big.Int {
|
||||
bs, err := bc.dao.GetNEP5Balances(acc)
|
||||
bs, err := bc.dao.GetNEP17Balances(acc)
|
||||
if err != nil {
|
||||
return big.NewInt(0)
|
||||
}
|
||||
|
@ -912,7 +912,7 @@ func (bc *Blockchain) GetUtilityTokenBalance(acc util.Uint160) *big.Int {
|
|||
// 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.GetNEP5Balances(acc)
|
||||
bs, err := bc.dao.GetNEP17Balances(acc)
|
||||
if err != nil {
|
||||
return big.NewInt(0), 0
|
||||
}
|
||||
|
|
|
@ -129,7 +129,7 @@ func TestAddBlockStateRoot(t *testing.T) {
|
|||
sr, err := bc.GetStateRoot(bc.BlockHeight())
|
||||
require.NoError(t, err)
|
||||
|
||||
tx := newNEP5Transfer(bc.contracts.NEO.Hash, neoOwner, util.Uint160{}, 1)
|
||||
tx := newNEP17Transfer(bc.contracts.NEO.Hash, neoOwner, util.Uint160{}, 1)
|
||||
tx.ValidUntilBlock = bc.BlockHeight() + 1
|
||||
addSigners(tx)
|
||||
require.NoError(t, signTx(bc, tx))
|
||||
|
|
|
@ -32,7 +32,7 @@ type Blockchainer interface {
|
|||
GetContractScriptHash(id int32) (util.Uint160, error)
|
||||
GetEnrollments() ([]state.Validator, error)
|
||||
GetGoverningTokenBalance(acc util.Uint160) (*big.Int, uint32)
|
||||
ForEachNEP5Transfer(util.Uint160, func(*state.NEP5Transfer) (bool, error)) error
|
||||
ForEachNEP17Transfer(util.Uint160, func(*state.NEP17Transfer) (bool, error)) error
|
||||
GetHeaderHash(int) util.Uint256
|
||||
GetHeader(hash util.Uint256) (*block.Header, error)
|
||||
CurrentHeaderHash() util.Uint256
|
||||
|
@ -42,7 +42,7 @@ type Blockchainer interface {
|
|||
GetAppExecResults(util.Uint256, trigger.Type) ([]state.AppExecResult, error)
|
||||
GetNativeContractScriptHash(string) (util.Uint160, error)
|
||||
GetNextBlockValidators() ([]*keys.PublicKey, error)
|
||||
GetNEP5Balances(util.Uint160) *state.NEP5Balances
|
||||
GetNEP17Balances(util.Uint160) *state.NEP17Balances
|
||||
GetValidators() ([]*keys.PublicKey, error)
|
||||
GetStandByCommittee() keys.PublicKeys
|
||||
GetStandByValidators() keys.PublicKeys
|
||||
|
|
|
@ -14,17 +14,17 @@ import (
|
|||
type Cached struct {
|
||||
DAO
|
||||
contracts map[util.Uint160]*state.Contract
|
||||
balances map[util.Uint160]*state.NEP5Balances
|
||||
transfers map[util.Uint160]map[uint32]*state.NEP5TransferLog
|
||||
balances map[util.Uint160]*state.NEP17Balances
|
||||
transfers map[util.Uint160]map[uint32]*state.NEP17TransferLog
|
||||
|
||||
dropNEP5Cache bool
|
||||
dropNEP17Cache bool
|
||||
}
|
||||
|
||||
// NewCached returns new Cached wrapping around given backing store.
|
||||
func NewCached(d DAO) *Cached {
|
||||
ctrs := make(map[util.Uint160]*state.Contract)
|
||||
balances := make(map[util.Uint160]*state.NEP5Balances)
|
||||
transfers := make(map[util.Uint160]map[uint32]*state.NEP5TransferLog)
|
||||
balances := make(map[util.Uint160]*state.NEP17Balances)
|
||||
transfers := make(map[util.Uint160]map[uint32]*state.NEP17TransferLog)
|
||||
return &Cached{d.GetWrapped(), ctrs, balances, transfers, false}
|
||||
}
|
||||
|
||||
|
@ -52,50 +52,50 @@ func (cd *Cached) DeleteContractState(hash util.Uint160) error {
|
|||
return cd.DAO.DeleteContractState(hash)
|
||||
}
|
||||
|
||||
// GetNEP5Balances retrieves NEP5Balances for the acc.
|
||||
func (cd *Cached) GetNEP5Balances(acc util.Uint160) (*state.NEP5Balances, error) {
|
||||
// GetNEP17Balances retrieves NEP17Balances for the acc.
|
||||
func (cd *Cached) GetNEP17Balances(acc util.Uint160) (*state.NEP17Balances, error) {
|
||||
if bs := cd.balances[acc]; bs != nil {
|
||||
return bs, nil
|
||||
}
|
||||
return cd.DAO.GetNEP5Balances(acc)
|
||||
return cd.DAO.GetNEP17Balances(acc)
|
||||
}
|
||||
|
||||
// PutNEP5Balances saves NEP5Balances for the acc.
|
||||
func (cd *Cached) PutNEP5Balances(acc util.Uint160, bs *state.NEP5Balances) error {
|
||||
// PutNEP17Balances saves NEP17Balances for the acc.
|
||||
func (cd *Cached) PutNEP17Balances(acc util.Uint160, bs *state.NEP17Balances) error {
|
||||
cd.balances[acc] = bs
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetNEP5TransferLog retrieves NEP5TransferLog for the acc.
|
||||
func (cd *Cached) GetNEP5TransferLog(acc util.Uint160, index uint32) (*state.NEP5TransferLog, error) {
|
||||
// GetNEP17TransferLog retrieves NEP17TransferLog for the acc.
|
||||
func (cd *Cached) GetNEP17TransferLog(acc util.Uint160, index uint32) (*state.NEP17TransferLog, error) {
|
||||
ts := cd.transfers[acc]
|
||||
if ts != nil && ts[index] != nil {
|
||||
return ts[index], nil
|
||||
}
|
||||
return cd.DAO.GetNEP5TransferLog(acc, index)
|
||||
return cd.DAO.GetNEP17TransferLog(acc, index)
|
||||
}
|
||||
|
||||
// PutNEP5TransferLog saves NEP5TransferLog for the acc.
|
||||
func (cd *Cached) PutNEP5TransferLog(acc util.Uint160, index uint32, bs *state.NEP5TransferLog) error {
|
||||
// PutNEP17TransferLog saves NEP17TransferLog for the acc.
|
||||
func (cd *Cached) PutNEP17TransferLog(acc util.Uint160, index uint32, bs *state.NEP17TransferLog) error {
|
||||
ts := cd.transfers[acc]
|
||||
if ts == nil {
|
||||
ts = make(map[uint32]*state.NEP5TransferLog, 2)
|
||||
ts = make(map[uint32]*state.NEP17TransferLog, 2)
|
||||
cd.transfers[acc] = ts
|
||||
}
|
||||
ts[index] = bs
|
||||
return nil
|
||||
}
|
||||
|
||||
// AppendNEP5Transfer appends new transfer to a transfer event log.
|
||||
func (cd *Cached) AppendNEP5Transfer(acc util.Uint160, index uint32, tr *state.NEP5Transfer) (bool, error) {
|
||||
lg, err := cd.GetNEP5TransferLog(acc, index)
|
||||
// AppendNEP17Transfer appends new transfer to a transfer event log.
|
||||
func (cd *Cached) AppendNEP17Transfer(acc util.Uint160, index uint32, tr *state.NEP17Transfer) (bool, error) {
|
||||
lg, err := cd.GetNEP17TransferLog(acc, index)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := lg.Append(tr); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return lg.Size() >= state.NEP5TransferBatchSize, cd.PutNEP5TransferLog(acc, index, lg)
|
||||
return lg.Size() >= state.NEP17TransferBatchSize, cd.PutNEP17TransferLog(acc, index, lg)
|
||||
}
|
||||
|
||||
// Persist flushes all the changes made into the (supposedly) persistent
|
||||
|
@ -107,8 +107,8 @@ func (cd *Cached) Persist() (int, error) {
|
|||
// usage scenario it should be good enough if cd doesn't modify object
|
||||
// caches (accounts/contracts/etc) in any way.
|
||||
if ok {
|
||||
if cd.dropNEP5Cache {
|
||||
lowerCache.balances = make(map[util.Uint160]*state.NEP5Balances)
|
||||
if cd.dropNEP17Cache {
|
||||
lowerCache.balances = make(map[util.Uint160]*state.NEP17Balances)
|
||||
}
|
||||
var simpleCache *Simple
|
||||
for simpleCache == nil {
|
||||
|
@ -125,7 +125,7 @@ func (cd *Cached) Persist() (int, error) {
|
|||
buf := io.NewBufBinWriter()
|
||||
|
||||
for acc, bs := range cd.balances {
|
||||
err := cd.DAO.putNEP5Balances(acc, bs, buf)
|
||||
err := cd.DAO.putNEP17Balances(acc, bs, buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -133,7 +133,7 @@ func (cd *Cached) Persist() (int, error) {
|
|||
}
|
||||
for acc, ts := range cd.transfers {
|
||||
for ind, lg := range ts {
|
||||
err := cd.DAO.PutNEP5TransferLog(acc, ind, lg)
|
||||
err := cd.DAO.PutNEP17TransferLog(acc, ind, lg)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ var (
|
|||
// DAO is a data access object.
|
||||
type DAO interface {
|
||||
AppendAppExecResult(aer *state.AppExecResult, buf *io.BufBinWriter) error
|
||||
AppendNEP5Transfer(acc util.Uint160, index uint32, tr *state.NEP5Transfer) (bool, error)
|
||||
AppendNEP17Transfer(acc util.Uint160, index uint32, tr *state.NEP17Transfer) (bool, error)
|
||||
DeleteContractState(hash util.Uint160) error
|
||||
DeleteStorageItem(id int32, key []byte) error
|
||||
GetAndDecode(entity io.Serializable, key []byte) error
|
||||
|
@ -44,8 +44,8 @@ type DAO interface {
|
|||
GetCurrentHeaderHeight() (i uint32, h util.Uint256, err error)
|
||||
GetCurrentStateRootHeight() (uint32, error)
|
||||
GetHeaderHashes() ([]util.Uint256, error)
|
||||
GetNEP5Balances(acc util.Uint160) (*state.NEP5Balances, error)
|
||||
GetNEP5TransferLog(acc util.Uint160, index uint32) (*state.NEP5TransferLog, error)
|
||||
GetNEP17Balances(acc util.Uint160) (*state.NEP17Balances, error)
|
||||
GetNEP17TransferLog(acc util.Uint160, index uint32) (*state.NEP17TransferLog, error)
|
||||
GetAndUpdateNextContractID() (int32, error)
|
||||
GetStateRoot(height uint32) (*state.MPTRootState, error)
|
||||
PutStateRoot(root *state.MPTRootState) error
|
||||
|
@ -60,15 +60,15 @@ type DAO interface {
|
|||
PutAppExecResult(aer *state.AppExecResult, buf *io.BufBinWriter) error
|
||||
PutContractState(cs *state.Contract) error
|
||||
PutCurrentHeader(hashAndIndex []byte) error
|
||||
PutNEP5Balances(acc util.Uint160, bs *state.NEP5Balances) error
|
||||
PutNEP5TransferLog(acc util.Uint160, index uint32, lg *state.NEP5TransferLog) error
|
||||
PutNEP17Balances(acc util.Uint160, bs *state.NEP17Balances) error
|
||||
PutNEP17TransferLog(acc util.Uint160, index uint32, lg *state.NEP17TransferLog) error
|
||||
PutStorageItem(id int32, key []byte, si *state.StorageItem) error
|
||||
PutVersion(v string) error
|
||||
Seek(id int32, prefix []byte, f func(k, v []byte))
|
||||
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
|
||||
putNEP5Balances(acc util.Uint160, bs *state.NEP5Balances, buf *io.BufBinWriter) error
|
||||
putNEP17Balances(acc util.Uint160, bs *state.NEP17Balances, buf *io.BufBinWriter) error
|
||||
}
|
||||
|
||||
// Simple is memCached wrapper around DB, simple DAO implementation.
|
||||
|
@ -197,12 +197,12 @@ func (dao *Simple) GetContractScriptHash(id int32) (util.Uint160, error) {
|
|||
|
||||
// -- end contracts.
|
||||
|
||||
// -- start nep5 balances.
|
||||
// -- start nep17 balances.
|
||||
|
||||
// GetNEP5Balances retrieves nep5 balances from the cache.
|
||||
func (dao *Simple) GetNEP5Balances(acc util.Uint160) (*state.NEP5Balances, error) {
|
||||
key := storage.AppendPrefix(storage.STNEP5Balances, acc.BytesBE())
|
||||
bs := state.NewNEP5Balances()
|
||||
// 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()
|
||||
err := dao.GetAndDecode(bs, key)
|
||||
if err != nil && err != storage.ErrKeyNotFound {
|
||||
return nil, err
|
||||
|
@ -210,61 +210,61 @@ func (dao *Simple) GetNEP5Balances(acc util.Uint160) (*state.NEP5Balances, error
|
|||
return bs, nil
|
||||
}
|
||||
|
||||
// PutNEP5Balances saves nep5 balances from the cache.
|
||||
func (dao *Simple) PutNEP5Balances(acc util.Uint160, bs *state.NEP5Balances) error {
|
||||
return dao.putNEP5Balances(acc, bs, io.NewBufBinWriter())
|
||||
// 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())
|
||||
}
|
||||
|
||||
func (dao *Simple) putNEP5Balances(acc util.Uint160, bs *state.NEP5Balances, buf *io.BufBinWriter) error {
|
||||
key := storage.AppendPrefix(storage.STNEP5Balances, acc.BytesBE())
|
||||
func (dao *Simple) putNEP17Balances(acc util.Uint160, bs *state.NEP17Balances, buf *io.BufBinWriter) error {
|
||||
key := storage.AppendPrefix(storage.STNEP17Balances, acc.BytesBE())
|
||||
return dao.putWithBuffer(bs, key, buf)
|
||||
}
|
||||
|
||||
// -- end nep5 balances.
|
||||
// -- end nep17 balances.
|
||||
|
||||
// -- start transfer log.
|
||||
|
||||
func getNEP5TransferLogKey(acc util.Uint160, index uint32) []byte {
|
||||
func getNEP17TransferLogKey(acc util.Uint160, index uint32) []byte {
|
||||
key := make([]byte, 1+util.Uint160Size+4)
|
||||
key[0] = byte(storage.STNEP5Transfers)
|
||||
key[0] = byte(storage.STNEP17Transfers)
|
||||
copy(key[1:], acc.BytesBE())
|
||||
binary.LittleEndian.PutUint32(key[util.Uint160Size:], index)
|
||||
return key
|
||||
}
|
||||
|
||||
// GetNEP5TransferLog retrieves transfer log from the cache.
|
||||
func (dao *Simple) GetNEP5TransferLog(acc util.Uint160, index uint32) (*state.NEP5TransferLog, error) {
|
||||
key := getNEP5TransferLogKey(acc, index)
|
||||
// GetNEP17TransferLog retrieves transfer log from the cache.
|
||||
func (dao *Simple) GetNEP17TransferLog(acc util.Uint160, index uint32) (*state.NEP17TransferLog, error) {
|
||||
key := getNEP17TransferLogKey(acc, index)
|
||||
value, err := dao.Store.Get(key)
|
||||
if err != nil {
|
||||
if err == storage.ErrKeyNotFound {
|
||||
return new(state.NEP5TransferLog), nil
|
||||
return new(state.NEP17TransferLog), nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &state.NEP5TransferLog{Raw: value}, nil
|
||||
return &state.NEP17TransferLog{Raw: value}, nil
|
||||
}
|
||||
|
||||
// PutNEP5TransferLog saves given transfer log in the cache.
|
||||
func (dao *Simple) PutNEP5TransferLog(acc util.Uint160, index uint32, lg *state.NEP5TransferLog) error {
|
||||
key := getNEP5TransferLogKey(acc, index)
|
||||
// PutNEP17TransferLog saves given transfer log in the cache.
|
||||
func (dao *Simple) PutNEP17TransferLog(acc util.Uint160, index uint32, lg *state.NEP17TransferLog) error {
|
||||
key := getNEP17TransferLogKey(acc, index)
|
||||
return dao.Store.Put(key, lg.Raw)
|
||||
}
|
||||
|
||||
// AppendNEP5Transfer appends a single NEP5 transfer to a log.
|
||||
// AppendNEP17Transfer appends a single NEP17 transfer to a log.
|
||||
// First return value signalizes that log size has exceeded batch size.
|
||||
func (dao *Simple) AppendNEP5Transfer(acc util.Uint160, index uint32, tr *state.NEP5Transfer) (bool, error) {
|
||||
lg, err := dao.GetNEP5TransferLog(acc, index)
|
||||
func (dao *Simple) AppendNEP17Transfer(acc util.Uint160, index uint32, tr *state.NEP17Transfer) (bool, error) {
|
||||
lg, err := dao.GetNEP17TransferLog(acc, index)
|
||||
if err != nil {
|
||||
if err != storage.ErrKeyNotFound {
|
||||
return false, err
|
||||
}
|
||||
lg = new(state.NEP5TransferLog)
|
||||
lg = new(state.NEP17TransferLog)
|
||||
}
|
||||
if err := lg.Append(tr); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return lg.Size() >= state.NEP5TransferBatchSize, dao.PutNEP5TransferLog(acc, index, lg)
|
||||
return lg.Size() >= state.NEP17TransferBatchSize, dao.PutNEP17TransferLog(acc, index, lg)
|
||||
}
|
||||
|
||||
// -- end transfer log.
|
||||
|
|
|
@ -201,7 +201,7 @@ func TestCreateBasicChain(t *testing.T) {
|
|||
|
||||
require.Equal(t, big.NewInt(5000_0000), bc.GetUtilityTokenBalance(priv0ScriptHash)) // gas bounty
|
||||
// Move some NEO to one simple account.
|
||||
txMoveNeo := newNEP5Transfer(neoHash, neoOwner, priv0ScriptHash, neoAmount)
|
||||
txMoveNeo := newNEP17Transfer(neoHash, neoOwner, priv0ScriptHash, neoAmount)
|
||||
txMoveNeo.ValidUntilBlock = validUntilBlock
|
||||
txMoveNeo.Nonce = getNextNonce()
|
||||
txMoveNeo.Signers = []transaction.Signer{{
|
||||
|
@ -212,7 +212,7 @@ func TestCreateBasicChain(t *testing.T) {
|
|||
}}
|
||||
require.NoError(t, signTx(bc, txMoveNeo))
|
||||
// Move some GAS to one simple account.
|
||||
txMoveGas := newNEP5Transfer(gasHash, neoOwner, priv0ScriptHash, int64(util.Fixed8FromInt64(1000)))
|
||||
txMoveGas := newNEP17Transfer(gasHash, neoOwner, priv0ScriptHash, int64(util.Fixed8FromInt64(1000)))
|
||||
txMoveGas.ValidUntilBlock = validUntilBlock
|
||||
txMoveGas.Nonce = getNextNonce()
|
||||
txMoveGas.Signers = []transaction.Signer{{
|
||||
|
@ -270,7 +270,7 @@ func TestCreateBasicChain(t *testing.T) {
|
|||
t.Logf("txInv: %s", txInv.Hash().StringLE())
|
||||
|
||||
priv1 := testchain.PrivateKeyByID(1)
|
||||
txNeo0to1 := newNEP5Transfer(neoHash, priv0ScriptHash, priv1.GetScriptHash(), 1000)
|
||||
txNeo0to1 := newNEP17Transfer(neoHash, priv0ScriptHash, priv1.GetScriptHash(), 1000)
|
||||
txNeo0to1.Nonce = getNextNonce()
|
||||
txNeo0to1.ValidUntilBlock = validUntilBlock
|
||||
txNeo0to1.Signers = []transaction.Signer{
|
||||
|
@ -295,7 +295,7 @@ func TestCreateBasicChain(t *testing.T) {
|
|||
initTx.Signers = []transaction.Signer{{Account: priv0ScriptHash}}
|
||||
require.NoError(t, addNetworkFee(bc, initTx, acc0))
|
||||
require.NoError(t, acc0.SignTx(initTx))
|
||||
transferTx := newNEP5Transfer(sh, sh, priv0.GetScriptHash(), 1000)
|
||||
transferTx := newNEP17Transfer(sh, sh, priv0.GetScriptHash(), 1000)
|
||||
transferTx.Nonce = getNextNonce()
|
||||
transferTx.ValidUntilBlock = validUntilBlock
|
||||
transferTx.Signers = []transaction.Signer{
|
||||
|
@ -313,7 +313,7 @@ func TestCreateBasicChain(t *testing.T) {
|
|||
require.NoError(t, bc.AddBlock(b))
|
||||
t.Logf("recieveRublesTx: %v", transferTx.Hash().StringLE())
|
||||
|
||||
transferTx = newNEP5Transfer(sh, priv0.GetScriptHash(), priv1.GetScriptHash(), 123)
|
||||
transferTx = newNEP17Transfer(sh, priv0.GetScriptHash(), priv1.GetScriptHash(), 123)
|
||||
transferTx.Nonce = getNextNonce()
|
||||
transferTx.ValidUntilBlock = validUntilBlock
|
||||
transferTx.Signers = []transaction.Signer{
|
||||
|
@ -364,7 +364,7 @@ func TestCreateBasicChain(t *testing.T) {
|
|||
}
|
||||
|
||||
// Prepare some transaction for future submission.
|
||||
txSendRaw := newNEP5Transfer(neoHash, priv0ScriptHash, priv1.GetScriptHash(), int64(util.Fixed8FromInt64(1000)))
|
||||
txSendRaw := newNEP17Transfer(neoHash, priv0ScriptHash, priv1.GetScriptHash(), int64(util.Fixed8FromInt64(1000)))
|
||||
txSendRaw.ValidUntilBlock = validUntilBlock
|
||||
txSendRaw.Nonce = getNextNonce()
|
||||
txSendRaw.Signers = []transaction.Signer{{
|
||||
|
@ -380,7 +380,7 @@ func TestCreateBasicChain(t *testing.T) {
|
|||
t.Logf("sendrawtransaction: %s", hex.EncodeToString(bw.Bytes()))
|
||||
}
|
||||
|
||||
func newNEP5Transfer(sc, from, to util.Uint160, amount int64) *transaction.Transaction {
|
||||
func newNEP17Transfer(sc, from, to util.Uint160, amount int64) *transaction.Transaction {
|
||||
w := io.NewBufBinWriter()
|
||||
emit.AppCallWithOperationAndArgs(w.BinWriter, sc, "transfer", from, to, amount, nil)
|
||||
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
||||
|
|
|
@ -45,7 +45,7 @@ func newGAS() *GAS {
|
|||
}
|
||||
|
||||
func (g *GAS) increaseBalance(_ *interop.Context, _ util.Uint160, si *state.StorageItem, amount *big.Int) error {
|
||||
acc, err := state.NEP5BalanceStateFromBytes(si.Value)
|
||||
acc, err := state.NEP17BalanceStateFromBytes(si.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -238,7 +238,7 @@ func (c *nep17TokenNative) TransferInternal(ic *interop.Context, from, to util.U
|
|||
|
||||
func (c *nep17TokenNative) balanceOf(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
||||
h := toUint160(args[0])
|
||||
bs, err := ic.DAO.GetNEP5Balances(h)
|
||||
bs, err := ic.DAO.GetNEP17Balances(h)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -313,7 +313,7 @@ func TestNEO_TransferOnPayment(t *testing.T) {
|
|||
require.NoError(t, bc.dao.PutContractState(cs))
|
||||
|
||||
const amount = 2
|
||||
tx := newNEP5Transfer(bc.contracts.NEO.Hash, neoOwner, cs.ScriptHash(), amount)
|
||||
tx := newNEP17Transfer(bc.contracts.NEO.Hash, neoOwner, cs.ScriptHash(), amount)
|
||||
tx.SystemFee += 1_000_000
|
||||
tx.NetworkFee = 10_000_000
|
||||
tx.ValidUntilBlock = bc.BlockHeight() + 1
|
||||
|
|
|
@ -9,21 +9,21 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
)
|
||||
|
||||
// NEP5BalanceState represents balance state of a NEP5-token.
|
||||
type NEP5BalanceState struct {
|
||||
// NEP17BalanceState represents balance state of a NEP17-token.
|
||||
type NEP17BalanceState struct {
|
||||
Balance big.Int
|
||||
}
|
||||
|
||||
// NEOBalanceState represents balance state of a NEO-token.
|
||||
type NEOBalanceState struct {
|
||||
NEP5BalanceState
|
||||
NEP17BalanceState
|
||||
BalanceHeight uint32
|
||||
VoteTo *keys.PublicKey
|
||||
}
|
||||
|
||||
// NEP5BalanceStateFromBytes converts serialized NEP5BalanceState to structure.
|
||||
func NEP5BalanceStateFromBytes(b []byte) (*NEP5BalanceState, error) {
|
||||
balance := new(NEP5BalanceState)
|
||||
// NEP17BalanceStateFromBytes converts serialized NEP17BalanceState to structure.
|
||||
func NEP17BalanceStateFromBytes(b []byte) (*NEP17BalanceState, error) {
|
||||
balance := new(NEP17BalanceState)
|
||||
if len(b) == 0 {
|
||||
return balance, nil
|
||||
}
|
||||
|
@ -35,8 +35,8 @@ func NEP5BalanceStateFromBytes(b []byte) (*NEP5BalanceState, error) {
|
|||
return balance, nil
|
||||
}
|
||||
|
||||
// Bytes returns serialized NEP5BalanceState.
|
||||
func (s *NEP5BalanceState) Bytes() []byte {
|
||||
// Bytes returns serialized NEP17BalanceState.
|
||||
func (s *NEP17BalanceState) Bytes() []byte {
|
||||
w := io.NewBufBinWriter()
|
||||
s.EncodeBinary(w.BinWriter)
|
||||
if w.Err != nil {
|
||||
|
@ -45,22 +45,22 @@ func (s *NEP5BalanceState) Bytes() []byte {
|
|||
return w.Bytes()
|
||||
}
|
||||
|
||||
func (s *NEP5BalanceState) toStackItem() stackitem.Item {
|
||||
func (s *NEP17BalanceState) toStackItem() stackitem.Item {
|
||||
return stackitem.NewStruct([]stackitem.Item{stackitem.NewBigInteger(&s.Balance)})
|
||||
}
|
||||
|
||||
func (s *NEP5BalanceState) fromStackItem(item stackitem.Item) {
|
||||
func (s *NEP17BalanceState) fromStackItem(item stackitem.Item) {
|
||||
s.Balance = *item.(*stackitem.Struct).Value().([]stackitem.Item)[0].Value().(*big.Int)
|
||||
}
|
||||
|
||||
// EncodeBinary implements io.Serializable interface.
|
||||
func (s *NEP5BalanceState) EncodeBinary(w *io.BinWriter) {
|
||||
func (s *NEP17BalanceState) EncodeBinary(w *io.BinWriter) {
|
||||
si := s.toStackItem()
|
||||
stackitem.EncodeBinaryStackItem(si, w)
|
||||
}
|
||||
|
||||
// DecodeBinary implements io.Serializable interface.
|
||||
func (s *NEP5BalanceState) DecodeBinary(r *io.BinReader) {
|
||||
func (s *NEP17BalanceState) DecodeBinary(r *io.BinReader) {
|
||||
si := stackitem.DecodeBinaryStackItem(r)
|
||||
if r.Err != nil {
|
||||
return
|
||||
|
@ -109,7 +109,7 @@ func (s *NEOBalanceState) DecodeBinary(r *io.BinReader) {
|
|||
}
|
||||
|
||||
func (s *NEOBalanceState) toStackItem() stackitem.Item {
|
||||
result := s.NEP5BalanceState.toStackItem().(*stackitem.Struct)
|
||||
result := s.NEP17BalanceState.toStackItem().(*stackitem.Struct)
|
||||
result.Append(stackitem.NewBigInteger(big.NewInt(int64(s.BalanceHeight))))
|
||||
if s.VoteTo != nil {
|
||||
result.Append(stackitem.NewByteArray(s.VoteTo.Bytes()))
|
||||
|
|
|
@ -8,11 +8,11 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
)
|
||||
|
||||
// NEP5TransferBatchSize is the maximum number of entries for NEP5TransferLog.
|
||||
const NEP5TransferBatchSize = 128
|
||||
// NEP17TransferBatchSize is the maximum number of entries for NEP17TransferLog.
|
||||
const NEP17TransferBatchSize = 128
|
||||
|
||||
// NEP5Tracker contains info about a single account in a NEP5 contract.
|
||||
type NEP5Tracker struct {
|
||||
// NEP17Tracker contains info about a single account in a NEP17 contract.
|
||||
type NEP17Tracker struct {
|
||||
// Balance is the current balance of the account.
|
||||
Balance big.Int
|
||||
// LastUpdatedBlock is a number of block when last `transfer` to or from the
|
||||
|
@ -20,14 +20,14 @@ type NEP5Tracker struct {
|
|||
LastUpdatedBlock uint32
|
||||
}
|
||||
|
||||
// NEP5TransferLog is a log of NEP5 token transfers for the specific command.
|
||||
type NEP5TransferLog struct {
|
||||
// NEP17TransferLog is a log of NEP17 token transfers for the specific command.
|
||||
type NEP17TransferLog struct {
|
||||
Raw []byte
|
||||
}
|
||||
|
||||
// NEP5Transfer represents a single NEP5 Transfer event.
|
||||
type NEP5Transfer struct {
|
||||
// Asset is a NEP5 contract ID.
|
||||
// NEP17Transfer represents a single NEP17 Transfer event.
|
||||
type NEP17Transfer struct {
|
||||
// Asset is a NEP17 contract ID.
|
||||
Asset int32
|
||||
// Address is the address of the sender.
|
||||
From util.Uint160
|
||||
|
@ -44,29 +44,29 @@ type NEP5Transfer struct {
|
|||
Tx util.Uint256
|
||||
}
|
||||
|
||||
// NEP5Balances is a map of the NEP5 contract IDs
|
||||
// NEP17Balances is a map of the NEP17 contract IDs
|
||||
// to the corresponding structures.
|
||||
type NEP5Balances struct {
|
||||
Trackers map[int32]NEP5Tracker
|
||||
type NEP17Balances struct {
|
||||
Trackers map[int32]NEP17Tracker
|
||||
// NextTransferBatch stores an index of the next transfer batch.
|
||||
NextTransferBatch uint32
|
||||
}
|
||||
|
||||
// NewNEP5Balances returns new NEP5Balances.
|
||||
func NewNEP5Balances() *NEP5Balances {
|
||||
return &NEP5Balances{
|
||||
Trackers: make(map[int32]NEP5Tracker),
|
||||
// NewNEP17Balances returns new NEP17Balances.
|
||||
func NewNEP17Balances() *NEP17Balances {
|
||||
return &NEP17Balances{
|
||||
Trackers: make(map[int32]NEP17Tracker),
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeBinary implements io.Serializable interface.
|
||||
func (bs *NEP5Balances) DecodeBinary(r *io.BinReader) {
|
||||
func (bs *NEP17Balances) DecodeBinary(r *io.BinReader) {
|
||||
bs.NextTransferBatch = r.ReadU32LE()
|
||||
lenBalances := r.ReadVarUint()
|
||||
m := make(map[int32]NEP5Tracker, lenBalances)
|
||||
m := make(map[int32]NEP17Tracker, lenBalances)
|
||||
for i := 0; i < int(lenBalances); i++ {
|
||||
key := int32(r.ReadU32LE())
|
||||
var tr NEP5Tracker
|
||||
var tr NEP17Tracker
|
||||
tr.DecodeBinary(r)
|
||||
m[key] = tr
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ func (bs *NEP5Balances) DecodeBinary(r *io.BinReader) {
|
|||
}
|
||||
|
||||
// EncodeBinary implements io.Serializable interface.
|
||||
func (bs *NEP5Balances) EncodeBinary(w *io.BinWriter) {
|
||||
func (bs *NEP17Balances) EncodeBinary(w *io.BinWriter) {
|
||||
w.WriteU32LE(bs.NextTransferBatch)
|
||||
w.WriteVarUint(uint64(len(bs.Trackers)))
|
||||
for k, v := range bs.Trackers {
|
||||
|
@ -84,7 +84,7 @@ func (bs *NEP5Balances) EncodeBinary(w *io.BinWriter) {
|
|||
}
|
||||
|
||||
// Append appends single transfer to a log.
|
||||
func (lg *NEP5TransferLog) Append(tr *NEP5Transfer) error {
|
||||
func (lg *NEP17TransferLog) Append(tr *NEP17Transfer) error {
|
||||
w := io.NewBufBinWriter()
|
||||
// The first entry, set up counter.
|
||||
if len(lg.Raw) == 0 {
|
||||
|
@ -102,11 +102,11 @@ func (lg *NEP5TransferLog) Append(tr *NEP5Transfer) error {
|
|||
}
|
||||
|
||||
// ForEach iterates over transfer log returning on first error.
|
||||
func (lg *NEP5TransferLog) ForEach(f func(*NEP5Transfer) (bool, error)) (bool, error) {
|
||||
func (lg *NEP17TransferLog) ForEach(f func(*NEP17Transfer) (bool, error)) (bool, error) {
|
||||
if lg == nil || len(lg.Raw) == 0 {
|
||||
return true, nil
|
||||
}
|
||||
transfers := make([]NEP5Transfer, lg.Size())
|
||||
transfers := make([]NEP17Transfer, lg.Size())
|
||||
r := io.NewBinReaderFromBuf(lg.Raw[1:])
|
||||
for i := 0; i < lg.Size(); i++ {
|
||||
transfers[i].DecodeBinary(r)
|
||||
|
@ -127,7 +127,7 @@ func (lg *NEP5TransferLog) ForEach(f func(*NEP5Transfer) (bool, error)) (bool, e
|
|||
}
|
||||
|
||||
// Size returns an amount of transfer written in log.
|
||||
func (lg *NEP5TransferLog) Size() int {
|
||||
func (lg *NEP17TransferLog) Size() int {
|
||||
if len(lg.Raw) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
@ -135,19 +135,19 @@ func (lg *NEP5TransferLog) Size() int {
|
|||
}
|
||||
|
||||
// EncodeBinary implements io.Serializable interface.
|
||||
func (t *NEP5Tracker) EncodeBinary(w *io.BinWriter) {
|
||||
func (t *NEP17Tracker) EncodeBinary(w *io.BinWriter) {
|
||||
w.WriteVarBytes(bigint.ToBytes(&t.Balance))
|
||||
w.WriteU32LE(t.LastUpdatedBlock)
|
||||
}
|
||||
|
||||
// DecodeBinary implements io.Serializable interface.
|
||||
func (t *NEP5Tracker) DecodeBinary(r *io.BinReader) {
|
||||
func (t *NEP17Tracker) DecodeBinary(r *io.BinReader) {
|
||||
t.Balance = *bigint.FromBytes(r.ReadVarBytes())
|
||||
t.LastUpdatedBlock = r.ReadU32LE()
|
||||
}
|
||||
|
||||
// EncodeBinary implements io.Serializable interface.
|
||||
func (t *NEP5Transfer) EncodeBinary(w *io.BinWriter) {
|
||||
func (t *NEP17Transfer) EncodeBinary(w *io.BinWriter) {
|
||||
w.WriteU32LE(uint32(t.Asset))
|
||||
w.WriteBytes(t.Tx[:])
|
||||
w.WriteBytes(t.From[:])
|
||||
|
@ -159,7 +159,7 @@ func (t *NEP5Transfer) EncodeBinary(w *io.BinWriter) {
|
|||
}
|
||||
|
||||
// DecodeBinary implements io.Serializable interface.
|
||||
func (t *NEP5Transfer) DecodeBinary(r *io.BinReader) {
|
||||
func (t *NEP17Transfer) DecodeBinary(r *io.BinReader) {
|
||||
t.Asset = int32(r.ReadU32LE())
|
||||
r.ReadBytes(t.Tx[:])
|
||||
r.ReadBytes(t.From[:])
|
|
@ -12,16 +12,16 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNEP5TransferLog_Append(t *testing.T) {
|
||||
func TestNEP17TransferLog_Append(t *testing.T) {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
expected := []*NEP5Transfer{
|
||||
expected := []*NEP17Transfer{
|
||||
randomTransfer(r),
|
||||
randomTransfer(r),
|
||||
randomTransfer(r),
|
||||
randomTransfer(r),
|
||||
}
|
||||
|
||||
lg := new(NEP5TransferLog)
|
||||
lg := new(NEP17TransferLog)
|
||||
for _, tr := range expected {
|
||||
require.NoError(t, lg.Append(tr))
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ func TestNEP5TransferLog_Append(t *testing.T) {
|
|||
require.Equal(t, len(expected), lg.Size())
|
||||
|
||||
i := len(expected) - 1
|
||||
cont, err := lg.ForEach(func(tr *NEP5Transfer) (bool, error) {
|
||||
cont, err := lg.ForEach(func(tr *NEP17Transfer) (bool, error) {
|
||||
require.Equal(t, expected[i], tr)
|
||||
i--
|
||||
return true, nil
|
||||
|
@ -38,17 +38,17 @@ func TestNEP5TransferLog_Append(t *testing.T) {
|
|||
require.True(t, cont)
|
||||
}
|
||||
|
||||
func TestNEP5Tracker_EncodeBinary(t *testing.T) {
|
||||
expected := &NEP5Tracker{
|
||||
func TestNEP17Tracker_EncodeBinary(t *testing.T) {
|
||||
expected := &NEP17Tracker{
|
||||
Balance: *big.NewInt(int64(rand.Uint64())),
|
||||
LastUpdatedBlock: rand.Uint32(),
|
||||
}
|
||||
|
||||
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Tracker))
|
||||
testserdes.EncodeDecodeBinary(t, expected, new(NEP17Tracker))
|
||||
}
|
||||
|
||||
func TestNEP5Transfer_DecodeBinary(t *testing.T) {
|
||||
expected := &NEP5Transfer{
|
||||
func TestNEP17Transfer_DecodeBinary(t *testing.T) {
|
||||
expected := &NEP17Transfer{
|
||||
Asset: 123,
|
||||
From: util.Uint160{5, 6, 7},
|
||||
To: util.Uint160{8, 9, 10},
|
||||
|
@ -58,11 +58,11 @@ func TestNEP5Transfer_DecodeBinary(t *testing.T) {
|
|||
Tx: util.Uint256{8, 5, 3},
|
||||
}
|
||||
|
||||
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Transfer))
|
||||
testserdes.EncodeDecodeBinary(t, expected, new(NEP17Transfer))
|
||||
}
|
||||
|
||||
func randomTransfer(r *rand.Rand) *NEP5Transfer {
|
||||
return &NEP5Transfer{
|
||||
func randomTransfer(r *rand.Rand) *NEP17Transfer {
|
||||
return &NEP17Transfer{
|
||||
Amount: *big.NewInt(int64(r.Uint64())),
|
||||
Block: r.Uint32(),
|
||||
Asset: int32(random.Int(10, 10000000)),
|
|
@ -16,8 +16,8 @@ const (
|
|||
STContract KeyPrefix = 0x50
|
||||
STContractID KeyPrefix = 0x51
|
||||
STStorage KeyPrefix = 0x70
|
||||
STNEP5Transfers KeyPrefix = 0x72
|
||||
STNEP5Balances KeyPrefix = 0x73
|
||||
STNEP17Transfers KeyPrefix = 0x72
|
||||
STNEP17Balances KeyPrefix = 0x73
|
||||
IXHeaderHashList KeyPrefix = 0x80
|
||||
SYSCurrentBlock KeyPrefix = 0xc0
|
||||
SYSCurrentHeader KeyPrefix = 0xc1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue