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
|
|
|
)
|
|
|
|
|
|
|
|
// NEP5Tracker contains info about a single account in a NEP5 contract.
|
|
|
|
type NEP5Tracker struct {
|
|
|
|
// 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
|
|
|
|
// account occured.
|
|
|
|
LastUpdatedBlock uint32
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
// NEP5TransferLog is a log of NEP5 token transfers for the specific command.
|
|
|
|
type NEP5TransferLog struct {
|
|
|
|
Raw []byte
|
2020-07-09 07:41:53 +00:00
|
|
|
// size is the number of NEP5Transfers written into Raw
|
|
|
|
size int
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NEP5Transfer represents a single NEP5 Transfer event.
|
|
|
|
type NEP5Transfer struct {
|
|
|
|
// Asset is a NEP5 contract hash.
|
|
|
|
Asset util.Uint160
|
|
|
|
// 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-03-05 14:11:58 +00:00
|
|
|
// Block is a number of block when the event occured.
|
|
|
|
Block uint32
|
|
|
|
// Timestamp is the timestamp of the block where transfer occured.
|
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-03-11 15:22:46 +00:00
|
|
|
// NEP5Balances is a map of the NEP5 contract hashes
|
|
|
|
// to the corresponding structures.
|
|
|
|
type NEP5Balances struct {
|
|
|
|
Trackers map[util.Uint160]NEP5Tracker
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NewNEP5Balances returns new NEP5Balances.
|
|
|
|
func NewNEP5Balances() *NEP5Balances {
|
|
|
|
return &NEP5Balances{
|
|
|
|
Trackers: make(map[util.Uint160]NEP5Tracker),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (bs *NEP5Balances) 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()
|
|
|
|
m := make(map[util.Uint160]NEP5Tracker, lenBalances)
|
|
|
|
for i := 0; i < int(lenBalances); i++ {
|
|
|
|
var key util.Uint160
|
|
|
|
var tr NEP5Tracker
|
|
|
|
r.ReadBytes(key[:])
|
|
|
|
tr.DecodeBinary(r)
|
|
|
|
m[key] = tr
|
|
|
|
}
|
|
|
|
bs.Trackers = m
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (bs *NEP5Balances) 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 {
|
|
|
|
w.WriteBytes(k[:])
|
|
|
|
v.EncodeBinary(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
// Append appends single transfer to a log.
|
|
|
|
func (lg *NEP5TransferLog) Append(tr *NEP5Transfer) error {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
tr.EncodeBinary(w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
return w.Err
|
|
|
|
}
|
|
|
|
lg.Raw = append(lg.Raw, w.Bytes()...)
|
2020-07-09 07:41:53 +00:00
|
|
|
lg.size++
|
2020-03-05 14:11:58 +00:00
|
|
|
return nil
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ForEach iterates over transfer log returning on first error.
|
|
|
|
func (lg *NEP5TransferLog) ForEach(f func(*NEP5Transfer) error) error {
|
|
|
|
if lg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tr := new(NEP5Transfer)
|
2020-07-09 07:41:53 +00:00
|
|
|
var bytesRead int
|
|
|
|
for i := 0; i < len(lg.Raw); i += bytesRead {
|
|
|
|
r := io.NewBinReaderFromBuf(lg.Raw[i:])
|
|
|
|
bytesRead = tr.DecodeBinaryReturnCount(r)
|
2020-03-05 12:16:03 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return r.Err
|
|
|
|
} else if err := f(tr); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 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.
|
|
|
|
func (lg *NEP5TransferLog) Size() int {
|
2020-07-09 07:41:53 +00:00
|
|
|
return lg.size
|
2020-03-12 09:32:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 07:45:50 +00:00
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (t *NEP5Tracker) 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.
|
|
|
|
func (t *NEP5Tracker) 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.
|
|
|
|
func (t *NEP5Transfer) EncodeBinary(w *io.BinWriter) {
|
|
|
|
w.WriteBytes(t.Asset[:])
|
|
|
|
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-07-09 09:57:24 +00:00
|
|
|
amountBytes := bigint.ToBytes(&t.Amount)
|
|
|
|
w.WriteU64LE(uint64(len(amountBytes)))
|
|
|
|
w.WriteBytes(amountBytes)
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (t *NEP5Transfer) DecodeBinary(r *io.BinReader) {
|
2020-07-09 07:41:53 +00:00
|
|
|
_ = t.DecodeBinaryReturnCount(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinaryReturnCount decodes NEP5Transfer and returns the number of bytes read.
|
|
|
|
func (t *NEP5Transfer) DecodeBinaryReturnCount(r *io.BinReader) int {
|
2020-03-05 14:11:58 +00:00
|
|
|
r.ReadBytes(t.Asset[:])
|
|
|
|
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-07-09 09:57:24 +00:00
|
|
|
amountLen := r.ReadU64LE()
|
|
|
|
amountBytes := make([]byte, amountLen)
|
|
|
|
r.ReadBytes(amountBytes)
|
|
|
|
t.Amount = *bigint.FromBytes(amountBytes)
|
|
|
|
return util.Uint160Size*3 + 8 + 4 + (8 + len(amountBytes)) + +util.Uint256Size
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|