state: optimize NEP17Transfer struct

We have both from and to here, so technically we can either drop the neg/neg
trick from the processTokenTransfer() or drop one field from the structure
(the other side is a part of the key). Drop the field since this can make the
DB a bit more compact. Change Amount to be a pointer along the way since
that's the "native" thing for big.Int, we've used non-pointer field
specifically to avoid Neg/Neg problems, but it looks like this is not
necessary.

This structure is only used by the RPC server and I doubt anyone uses it via
the *Blockchain.
This commit is contained in:
Roman Khimov 2023-01-10 22:37:44 +03:00
parent dfd4f6978f
commit 584675ec23
4 changed files with 49 additions and 61 deletions

View file

@ -45,7 +45,7 @@ import (
// Tuning parameters.
const (
version = "0.2.7"
version = "0.2.8"
defaultInitialGAS = 52000000_00000000
defaultGCPeriod = 10000
@ -1766,10 +1766,9 @@ func (bc *Blockchain) processTokenTransfer(cache *dao.Simple, transCache map[uti
if !isNEP11 {
nep17xfer = &state.NEP17Transfer{
Asset: id,
Amount: *amount,
From: from,
To: to,
Amount: amount,
Block: b.Index,
Counterparty: to,
Timestamp: b.Timestamp,
Tx: h,
}
@ -1778,10 +1777,9 @@ func (bc *Blockchain) processTokenTransfer(cache *dao.Simple, transCache map[uti
nep11xfer := &state.NEP11Transfer{
NEP17Transfer: state.NEP17Transfer{
Asset: id,
Amount: *amount,
From: from,
To: to,
Amount: amount,
Block: b.Index,
Counterparty: to,
Timestamp: b.Timestamp,
Tx: h,
},
@ -1791,14 +1789,15 @@ func (bc *Blockchain) processTokenTransfer(cache *dao.Simple, transCache map[uti
nep17xfer = &nep11xfer.NEP17Transfer
}
if !from.Equals(util.Uint160{}) {
_ = nep17xfer.Amount.Neg(&nep17xfer.Amount)
_ = nep17xfer.Amount.Neg(nep17xfer.Amount)
err := appendTokenTransfer(cache, transCache, from, transfer, id, b.Index, b.Timestamp, isNEP11)
_ = nep17xfer.Amount.Neg(&nep17xfer.Amount)
_ = nep17xfer.Amount.Neg(nep17xfer.Amount)
if err != nil {
return
}
}
if !to.Equals(util.Uint160{}) {
nep17xfer.Counterparty = from
_ = appendTokenTransfer(cache, transCache, to, transfer, id, b.Index, b.Timestamp, isNEP11) // Nothing useful we can do.
}
}

View file

@ -24,13 +24,11 @@ type TokenTransferLog struct {
type NEP17Transfer struct {
// Asset is a NEP-17 contract ID.
Asset int32
// Address is the address of the sender.
From util.Uint160
// To is the address of the receiver.
To util.Uint160
// Counterparty is the address of the sender/receiver (the other side of the transfer).
Counterparty util.Uint160
// Amount is the amount of tokens transferred.
// It is negative when tokens are sent and positive if they are received.
Amount big.Int
Amount *big.Int
// Block is a number of block when the event occurred.
Block uint32
// Timestamp is the timestamp of the block where transfer occurred.
@ -195,11 +193,10 @@ func (t *NEP17Transfer) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(uint32(t.Asset))
w.WriteBytes(t.Tx[:])
w.WriteBytes(t.From[:])
w.WriteBytes(t.To[:])
w.WriteBytes(t.Counterparty[:])
w.WriteU32LE(t.Block)
w.WriteU64LE(t.Timestamp)
amount := bigint.ToPreallocatedBytes(&t.Amount, buf[:])
amount := bigint.ToPreallocatedBytes(t.Amount, buf[:])
w.WriteVarBytes(amount)
}
@ -207,12 +204,11 @@ func (t *NEP17Transfer) EncodeBinary(w *io.BinWriter) {
func (t *NEP17Transfer) DecodeBinary(r *io.BinReader) {
t.Asset = int32(r.ReadU32LE())
r.ReadBytes(t.Tx[:])
r.ReadBytes(t.From[:])
r.ReadBytes(t.To[:])
r.ReadBytes(t.Counterparty[:])
t.Block = r.ReadU32LE()
t.Timestamp = r.ReadU64LE()
amount := r.ReadVarBytes(bigint.MaxBytesLen)
t.Amount = *bigint.FromBytes(amount)
t.Amount = bigint.FromBytes(amount)
}
// EncodeBinary implements the io.Serializable interface.

View file

@ -87,9 +87,8 @@ func BenchmarkTokenTransferLog_Append(b *testing.B) {
func TestNEP17Transfer_DecodeBinary(t *testing.T) {
expected := &NEP17Transfer{
Asset: 123,
From: util.Uint160{5, 6, 7},
To: util.Uint160{8, 9, 10},
Amount: *big.NewInt(42),
Counterparty: util.Uint160{5, 6, 7},
Amount: big.NewInt(42),
Block: 12345,
Timestamp: 54321,
Tx: util.Uint256{8, 5, 3},
@ -102,9 +101,8 @@ func TestNEP11Transfer_DecodeBinary(t *testing.T) {
expected := &NEP11Transfer{
NEP17Transfer: NEP17Transfer{
Asset: 123,
From: util.Uint160{5, 6, 7},
To: util.Uint160{8, 9, 10},
Amount: *big.NewInt(42),
Counterparty: util.Uint160{5, 6, 7},
Amount: big.NewInt(42),
Block: 12345,
Timestamp: 54321,
Tx: util.Uint256{8, 5, 3},
@ -117,11 +115,10 @@ func TestNEP11Transfer_DecodeBinary(t *testing.T) {
func random17Transfer(r *rand.Rand) *NEP17Transfer {
return &NEP17Transfer{
Amount: *big.NewInt(int64(r.Uint64())),
Amount: big.NewInt(int64(r.Uint64())),
Block: r.Uint32(),
Asset: int32(random.Int(10, 10000000)),
From: random.Uint160(),
To: random.Uint160(),
Counterparty: random.Uint160(),
Tx: random.Uint256(),
}
}
@ -129,11 +126,10 @@ func random17Transfer(r *rand.Rand) *NEP17Transfer {
func random11Transfer(r *rand.Rand) *NEP11Transfer {
return &NEP11Transfer{
NEP17Transfer: NEP17Transfer{
Amount: *big.NewInt(int64(r.Uint64())),
Amount: big.NewInt(int64(r.Uint64())),
Block: r.Uint32(),
Asset: int32(random.Int(10, 10000000)),
From: random.Uint160(),
To: random.Uint160(),
Counterparty: random.Uint160(),
Tx: random.Uint256(),
},
ID: random.Uint256().BytesBE(),

View file

@ -1264,18 +1264,15 @@ func (s *Server) getTokenTransfers(ps params.Params, isNEP11 bool) (interface{},
Index: tr.Block,
TxHash: tr.Tx,
}
if !tr.Counterparty.Equals(util.Uint160{}) {
transfer.Address = address.Uint160ToString(tr.Counterparty)
}
if tr.Amount.Sign() > 0 { // token was received
transfer.Amount = tr.Amount.String()
if !tr.From.Equals(util.Uint160{}) {
transfer.Address = address.Uint160ToString(tr.From)
}
received = &result.NEP17Transfer{}
*received = transfer // Make a copy, transfer is to be modified below.
} else {
transfer.Amount = new(big.Int).Neg(&tr.Amount).String()
if !tr.To.Equals(util.Uint160{}) {
transfer.Address = address.Uint160ToString(tr.To)
}
transfer.Amount = new(big.Int).Neg(tr.Amount).String()
sent = &result.NEP17Transfer{}
*sent = transfer
}