2020-03-05 07:45:50 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-08-05 09:59:08 +00:00
|
|
|
"bytes"
|
2020-07-09 09:57:24 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2020-07-09 09:57:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-03-05 07:45:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-03-05 14:11:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-03-05 07:45:50 +00:00
|
|
|
)
|
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
// TokenTransferBatchSize is the maximum number of entries for TokenTransferLog.
|
|
|
|
const TokenTransferBatchSize = 128
|
2020-09-21 18:46:40 +00:00
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
// TokenTransferLog is a serialized log of token transfers.
|
|
|
|
type TokenTransferLog struct {
|
2020-03-05 14:11:58 +00:00
|
|
|
Raw []byte
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// NEP17Transfer represents a single NEP-17 Transfer event.
|
2020-11-24 08:14:25 +00:00
|
|
|
type NEP17Transfer struct {
|
2021-11-18 13:37:42 +00:00
|
|
|
// Asset is a NEP-17 contract ID.
|
2020-07-28 16:05:16 +00:00
|
|
|
Asset int32
|
2020-03-05 14:11:58 +00:00
|
|
|
// Address is the address of the sender.
|
|
|
|
From util.Uint160
|
|
|
|
// To is the address of the receiver.
|
|
|
|
To util.Uint160
|
|
|
|
// Amount is the amount of tokens transferred.
|
|
|
|
// It is negative when tokens are sent and positive if they are received.
|
2020-07-09 09:57:24 +00:00
|
|
|
Amount big.Int
|
2020-08-14 09:16:24 +00:00
|
|
|
// Block is a number of block when the event occurred.
|
2020-03-05 14:11:58 +00:00
|
|
|
Block uint32
|
2020-08-14 09:16:24 +00:00
|
|
|
// Timestamp is the timestamp of the block where transfer occurred.
|
2020-04-21 11:26:57 +00:00
|
|
|
Timestamp uint64
|
2020-03-05 14:11:58 +00:00
|
|
|
// Tx is a hash the transaction.
|
|
|
|
Tx util.Uint256
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
// NEP11Transfer represents a single NEP-11 Transfer event.
|
|
|
|
type NEP11Transfer struct {
|
|
|
|
NEP17Transfer
|
|
|
|
|
|
|
|
// ID is a NEP-11 token ID.
|
|
|
|
ID []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenTransferInfo stores map of the contract IDs to the balance's last updated
|
|
|
|
// block trackers along with information about NEP-17 and NEP-11 transfer batch.
|
|
|
|
type TokenTransferInfo struct {
|
2021-07-25 12:00:44 +00:00
|
|
|
LastUpdated map[int32]uint32
|
2021-11-16 16:18:06 +00:00
|
|
|
// NextNEP11Batch stores the index of the next NEP-17 transfer batch.
|
|
|
|
NextNEP11Batch uint32
|
|
|
|
// NextNEP17Batch stores the index of the next NEP-17 transfer batch.
|
|
|
|
NextNEP17Batch uint32
|
2022-01-18 15:28:24 +00:00
|
|
|
// NextNEP11NewestTimestamp stores the block timestamp of the first NEP-11 transfer in raw.
|
|
|
|
NextNEP11NewestTimestamp uint64
|
|
|
|
// NextNEP17NewestTimestamp stores the block timestamp of the first NEP-17 transfer in raw.
|
|
|
|
NextNEP17NewestTimestamp uint64
|
2021-11-16 16:18:06 +00:00
|
|
|
// NewNEP11Batch is true if batch with the `NextNEP11Batch` index should be created.
|
|
|
|
NewNEP11Batch bool
|
|
|
|
// NewNEP17Batch is true if batch with the `NextNEP17Batch` index should be created.
|
|
|
|
NewNEP17Batch bool
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
// NewTokenTransferInfo returns new TokenTransferInfo.
|
|
|
|
func NewTokenTransferInfo() *TokenTransferInfo {
|
|
|
|
return &TokenTransferInfo{
|
2021-11-16 19:52:10 +00:00
|
|
|
NewNEP11Batch: true,
|
|
|
|
NewNEP17Batch: true,
|
|
|
|
LastUpdated: make(map[int32]uint32),
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
2021-11-16 16:18:06 +00:00
|
|
|
func (bs *TokenTransferInfo) DecodeBinary(r *io.BinReader) {
|
|
|
|
bs.NextNEP11Batch = r.ReadU32LE()
|
|
|
|
bs.NextNEP17Batch = r.ReadU32LE()
|
2022-01-18 15:28:24 +00:00
|
|
|
bs.NextNEP11NewestTimestamp = r.ReadU64LE()
|
|
|
|
bs.NextNEP17NewestTimestamp = r.ReadU64LE()
|
2021-11-16 16:18:06 +00:00
|
|
|
bs.NewNEP11Batch = r.ReadBool()
|
|
|
|
bs.NewNEP17Batch = r.ReadBool()
|
2020-03-11 15:22:46 +00:00
|
|
|
lenBalances := r.ReadVarUint()
|
2021-07-25 12:00:44 +00:00
|
|
|
m := make(map[int32]uint32, lenBalances)
|
2020-03-11 15:22:46 +00:00
|
|
|
for i := 0; i < int(lenBalances); i++ {
|
2020-07-28 09:23:58 +00:00
|
|
|
key := int32(r.ReadU32LE())
|
2021-07-25 12:00:44 +00:00
|
|
|
m[key] = r.ReadU32LE()
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
2021-07-25 10:48:50 +00:00
|
|
|
bs.LastUpdated = m
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
2021-11-16 16:18:06 +00:00
|
|
|
func (bs *TokenTransferInfo) EncodeBinary(w *io.BinWriter) {
|
|
|
|
w.WriteU32LE(bs.NextNEP11Batch)
|
|
|
|
w.WriteU32LE(bs.NextNEP17Batch)
|
2022-01-18 15:28:24 +00:00
|
|
|
w.WriteU64LE(bs.NextNEP11NewestTimestamp)
|
|
|
|
w.WriteU64LE(bs.NextNEP17NewestTimestamp)
|
2021-11-16 16:18:06 +00:00
|
|
|
w.WriteBool(bs.NewNEP11Batch)
|
|
|
|
w.WriteBool(bs.NewNEP17Batch)
|
2021-07-25 10:48:50 +00:00
|
|
|
w.WriteVarUint(uint64(len(bs.LastUpdated)))
|
|
|
|
for k, v := range bs.LastUpdated {
|
2020-07-28 09:23:58 +00:00
|
|
|
w.WriteU32LE(uint32(k))
|
2021-07-25 12:00:44 +00:00
|
|
|
w.WriteU32LE(v)
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
// Append appends single transfer to a log.
|
2021-11-16 16:18:06 +00:00
|
|
|
func (lg *TokenTransferLog) Append(tr io.Serializable) error {
|
2020-09-21 18:46:40 +00:00
|
|
|
// The first entry, set up counter.
|
|
|
|
if len(lg.Raw) == 0 {
|
2021-08-05 09:59:08 +00:00
|
|
|
lg.Raw = append(lg.Raw, 0)
|
2020-09-21 18:46:40 +00:00
|
|
|
}
|
2021-08-05 09:59:08 +00:00
|
|
|
|
|
|
|
b := bytes.NewBuffer(lg.Raw)
|
|
|
|
w := io.NewBinWriterFromIO(b)
|
|
|
|
|
|
|
|
tr.EncodeBinary(w)
|
2020-03-05 14:11:58 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return w.Err
|
|
|
|
}
|
2021-08-05 09:59:08 +00:00
|
|
|
lg.Raw = b.Bytes()
|
|
|
|
lg.Raw[0]++
|
2020-03-05 14:11:58 +00:00
|
|
|
return nil
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
// ForEachNEP11 iterates over transfer log returning on first error.
|
|
|
|
func (lg *TokenTransferLog) ForEachNEP11(f func(*NEP11Transfer) (bool, error)) (bool, error) {
|
2020-09-08 09:57:45 +00:00
|
|
|
if lg == nil || len(lg.Raw) == 0 {
|
2020-09-08 12:29:07 +00:00
|
|
|
return true, nil
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
2021-11-16 16:18:06 +00:00
|
|
|
transfers := make([]NEP11Transfer, lg.Size())
|
2020-09-08 09:57:45 +00:00
|
|
|
r := io.NewBinReaderFromBuf(lg.Raw[1:])
|
|
|
|
for i := 0; i < lg.Size(); i++ {
|
|
|
|
transfers[i].DecodeBinary(r)
|
|
|
|
}
|
|
|
|
if r.Err != nil {
|
2020-09-08 12:29:07 +00:00
|
|
|
return false, r.Err
|
2020-09-08 09:57:45 +00:00
|
|
|
}
|
|
|
|
for i := len(transfers) - 1; i >= 0; i-- {
|
2020-09-08 12:29:07 +00:00
|
|
|
cont, err := f(&transfers[i])
|
2021-11-16 16:18:06 +00:00
|
|
|
if err != nil || !cont {
|
2020-09-08 12:29:07 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2021-11-16 16:18:06 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForEachNEP17 iterates over transfer log returning on first error.
|
|
|
|
func (lg *TokenTransferLog) ForEachNEP17(f func(*NEP17Transfer) (bool, error)) (bool, error) {
|
|
|
|
if lg == nil || len(lg.Raw) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
transfers := make([]NEP17Transfer, lg.Size())
|
|
|
|
r := io.NewBinReaderFromBuf(lg.Raw[1:])
|
|
|
|
for i := 0; i < lg.Size(); i++ {
|
|
|
|
transfers[i].DecodeBinary(r)
|
|
|
|
}
|
|
|
|
if r.Err != nil {
|
|
|
|
return false, r.Err
|
|
|
|
}
|
|
|
|
for i := len(transfers) - 1; i >= 0; i-- {
|
|
|
|
cont, err := f(&transfers[i])
|
|
|
|
if err != nil || !cont {
|
|
|
|
return false, err
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 12:29:07 +00:00
|
|
|
return true, nil
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 09:32:24 +00:00
|
|
|
// Size returns an amount of transfer written in log.
|
2021-11-16 16:18:06 +00:00
|
|
|
func (lg *TokenTransferLog) Size() int {
|
2020-09-21 18:46:40 +00:00
|
|
|
if len(lg.Raw) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int(lg.Raw[0])
|
2020-03-12 09:32:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
// EncodeBinary implements io.Serializable interface.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (t *NEP17Transfer) EncodeBinary(w *io.BinWriter) {
|
2020-07-28 16:05:16 +00:00
|
|
|
w.WriteU32LE(uint32(t.Asset))
|
2020-03-05 14:11:58 +00:00
|
|
|
w.WriteBytes(t.Tx[:])
|
|
|
|
w.WriteBytes(t.From[:])
|
|
|
|
w.WriteBytes(t.To[:])
|
|
|
|
w.WriteU32LE(t.Block)
|
2020-04-21 11:26:57 +00:00
|
|
|
w.WriteU64LE(t.Timestamp)
|
2020-09-21 18:51:33 +00:00
|
|
|
amount := bigint.ToBytes(&t.Amount)
|
|
|
|
w.WriteVarBytes(amount)
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (t *NEP17Transfer) DecodeBinary(r *io.BinReader) {
|
2020-07-28 16:05:16 +00:00
|
|
|
t.Asset = int32(r.ReadU32LE())
|
2020-03-05 14:11:58 +00:00
|
|
|
r.ReadBytes(t.Tx[:])
|
|
|
|
r.ReadBytes(t.From[:])
|
|
|
|
r.ReadBytes(t.To[:])
|
|
|
|
t.Block = r.ReadU32LE()
|
2020-04-21 11:26:57 +00:00
|
|
|
t.Timestamp = r.ReadU64LE()
|
2020-09-21 18:51:33 +00:00
|
|
|
amount := r.ReadVarBytes(bigint.MaxBytesLen)
|
|
|
|
t.Amount = *bigint.FromBytes(amount)
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
2021-11-16 16:18:06 +00:00
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (t *NEP11Transfer) EncodeBinary(w *io.BinWriter) {
|
|
|
|
t.NEP17Transfer.EncodeBinary(w)
|
|
|
|
w.WriteVarBytes(t.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (t *NEP11Transfer) DecodeBinary(r *io.BinReader) {
|
|
|
|
t.NEP17Transfer.DecodeBinary(r)
|
|
|
|
t.ID = r.ReadVarBytes(storage.MaxStorageKeyLen)
|
|
|
|
}
|