2020-03-05 07:45:50 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-07-09 09:57:24 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// NEP17TransferBatchSize is the maximum number of entries for NEP17TransferLog.
|
|
|
|
const NEP17TransferBatchSize = 128
|
2020-09-21 18:46:40 +00:00
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// NEP17Tracker contains info about a single account in a NEP17 contract.
|
|
|
|
type NEP17Tracker struct {
|
2020-03-05 07:45:50 +00:00
|
|
|
// Balance is the current balance of the account.
|
2020-07-09 09:57:24 +00:00
|
|
|
Balance big.Int
|
2020-03-05 07:45:50 +00:00
|
|
|
// LastUpdatedBlock is a number of block when last `transfer` to or from the
|
2020-08-14 09:16:24 +00:00
|
|
|
// account occurred.
|
2020-03-05 07:45:50 +00:00
|
|
|
LastUpdatedBlock uint32
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// NEP17TransferLog is a log of NEP17 token transfers for the specific command.
|
|
|
|
type NEP17TransferLog struct {
|
2020-03-05 14:11:58 +00:00
|
|
|
Raw []byte
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// NEP17Transfer represents a single NEP17 Transfer event.
|
|
|
|
type NEP17Transfer struct {
|
|
|
|
// Asset is a NEP17 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
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// NEP17Balances is a map of the NEP17 contract IDs
|
2020-03-11 15:22:46 +00:00
|
|
|
// to the corresponding structures.
|
2020-11-24 08:14:25 +00:00
|
|
|
type NEP17Balances struct {
|
|
|
|
Trackers map[int32]NEP17Tracker
|
2020-03-12 09:43:21 +00:00
|
|
|
// NextTransferBatch stores an index of the next transfer batch.
|
|
|
|
NextTransferBatch uint32
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// NewNEP17Balances returns new NEP17Balances.
|
|
|
|
func NewNEP17Balances() *NEP17Balances {
|
|
|
|
return &NEP17Balances{
|
|
|
|
Trackers: make(map[int32]NEP17Tracker),
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (bs *NEP17Balances) DecodeBinary(r *io.BinReader) {
|
2020-03-12 09:43:21 +00:00
|
|
|
bs.NextTransferBatch = r.ReadU32LE()
|
2020-03-11 15:22:46 +00:00
|
|
|
lenBalances := r.ReadVarUint()
|
2020-11-24 08:14:25 +00:00
|
|
|
m := make(map[int32]NEP17Tracker, 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())
|
2020-11-24 08:14:25 +00:00
|
|
|
var tr NEP17Tracker
|
2020-03-11 15:22:46 +00:00
|
|
|
tr.DecodeBinary(r)
|
|
|
|
m[key] = tr
|
|
|
|
}
|
|
|
|
bs.Trackers = m
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (bs *NEP17Balances) EncodeBinary(w *io.BinWriter) {
|
2020-03-12 09:43:21 +00:00
|
|
|
w.WriteU32LE(bs.NextTransferBatch)
|
2020-03-11 15:22:46 +00:00
|
|
|
w.WriteVarUint(uint64(len(bs.Trackers)))
|
|
|
|
for k, v := range bs.Trackers {
|
2020-07-28 09:23:58 +00:00
|
|
|
w.WriteU32LE(uint32(k))
|
2020-03-11 15:22:46 +00:00
|
|
|
v.EncodeBinary(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
// Append appends single transfer to a log.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (lg *NEP17TransferLog) Append(tr *NEP17Transfer) error {
|
2020-03-05 14:11:58 +00:00
|
|
|
w := io.NewBufBinWriter()
|
2020-09-21 18:46:40 +00:00
|
|
|
// The first entry, set up counter.
|
|
|
|
if len(lg.Raw) == 0 {
|
|
|
|
w.WriteB(1)
|
|
|
|
}
|
2020-03-05 14:11:58 +00:00
|
|
|
tr.EncodeBinary(w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
return w.Err
|
|
|
|
}
|
2020-09-21 18:46:40 +00:00
|
|
|
if len(lg.Raw) != 0 {
|
|
|
|
lg.Raw[0]++
|
|
|
|
}
|
2020-03-05 14:11:58 +00:00
|
|
|
lg.Raw = append(lg.Raw, w.Bytes()...)
|
|
|
|
return nil
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ForEach iterates over transfer log returning on first error.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (lg *NEP17TransferLog) ForEach(f func(*NEP17Transfer) (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
|
|
|
}
|
2020-11-24 08:14:25 +00:00
|
|
|
transfers := make([]NEP17Transfer, 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])
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !cont {
|
|
|
|
return false, nil
|
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.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (lg *NEP17TransferLog) 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 07:45:50 +00:00
|
|
|
// EncodeBinary implements io.Serializable interface.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (t *NEP17Tracker) EncodeBinary(w *io.BinWriter) {
|
2020-07-09 09:57:24 +00:00
|
|
|
w.WriteVarBytes(bigint.ToBytes(&t.Balance))
|
2020-03-05 07:45:50 +00:00
|
|
|
w.WriteU32LE(t.LastUpdatedBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (t *NEP17Tracker) DecodeBinary(r *io.BinReader) {
|
2020-07-09 09:57:24 +00:00
|
|
|
t.Balance = *bigint.FromBytes(r.ReadVarBytes())
|
2020-03-05 07:45:50 +00:00
|
|
|
t.LastUpdatedBlock = r.ReadU32LE()
|
|
|
|
}
|
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
|
|
|
}
|