Merge pull request #2865 from nspcc-dev/fix-applog-values

Fix applog values
This commit is contained in:
Roman Khimov 2023-01-11 20:40:44 +07:00 committed by GitHub
commit e037249d37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 65 deletions

View file

@ -45,7 +45,7 @@ import (
// Tuning parameters. // Tuning parameters.
const ( const (
version = "0.2.7" version = "0.2.8"
defaultInitialGAS = 52000000_00000000 defaultInitialGAS = 52000000_00000000
defaultGCPeriod = 10000 defaultGCPeriod = 10000
@ -1765,25 +1765,23 @@ func (bc *Blockchain) processTokenTransfer(cache *dao.Simple, transCache map[uti
var isNEP11 = (tokenID != nil) var isNEP11 = (tokenID != nil)
if !isNEP11 { if !isNEP11 {
nep17xfer = &state.NEP17Transfer{ nep17xfer = &state.NEP17Transfer{
Asset: id, Asset: id,
Amount: *amount, Amount: amount,
From: from, Block: b.Index,
To: to, Counterparty: to,
Block: b.Index, Timestamp: b.Timestamp,
Timestamp: b.Timestamp, Tx: h,
Tx: h,
} }
transfer = nep17xfer transfer = nep17xfer
} else { } else {
nep11xfer := &state.NEP11Transfer{ nep11xfer := &state.NEP11Transfer{
NEP17Transfer: state.NEP17Transfer{ NEP17Transfer: state.NEP17Transfer{
Asset: id, Asset: id,
Amount: *amount, Amount: amount,
From: from, Block: b.Index,
To: to, Counterparty: to,
Block: b.Index, Timestamp: b.Timestamp,
Timestamp: b.Timestamp, Tx: h,
Tx: h,
}, },
ID: tokenID, ID: tokenID,
} }
@ -1791,14 +1789,15 @@ func (bc *Blockchain) processTokenTransfer(cache *dao.Simple, transCache map[uti
nep17xfer = &nep11xfer.NEP17Transfer nep17xfer = &nep11xfer.NEP17Transfer
} }
if !from.Equals(util.Uint160{}) { 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) 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 { if err != nil {
return return
} }
} }
if !to.Equals(util.Uint160{}) { if !to.Equals(util.Uint160{}) {
nep17xfer.Counterparty = from
_ = appendTokenTransfer(cache, transCache, to, transfer, id, b.Index, b.Timestamp, isNEP11) // Nothing useful we can do. _ = 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 { type NEP17Transfer struct {
// Asset is a NEP-17 contract ID. // Asset is a NEP-17 contract ID.
Asset int32 Asset int32
// Address is the address of the sender. // Counterparty is the address of the sender/receiver (the other side of the transfer).
From util.Uint160 Counterparty util.Uint160
// To is the address of the receiver.
To util.Uint160
// Amount is the amount of tokens transferred. // Amount is the amount of tokens transferred.
// It is negative when tokens are sent and positive if they are received. // 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 is a number of block when the event occurred.
Block uint32 Block uint32
// Timestamp is the timestamp of the block where transfer occurred. // 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.WriteU32LE(uint32(t.Asset))
w.WriteBytes(t.Tx[:]) w.WriteBytes(t.Tx[:])
w.WriteBytes(t.From[:]) w.WriteBytes(t.Counterparty[:])
w.WriteBytes(t.To[:])
w.WriteU32LE(t.Block) w.WriteU32LE(t.Block)
w.WriteU64LE(t.Timestamp) w.WriteU64LE(t.Timestamp)
amount := bigint.ToPreallocatedBytes(&t.Amount, buf[:]) amount := bigint.ToPreallocatedBytes(t.Amount, buf[:])
w.WriteVarBytes(amount) w.WriteVarBytes(amount)
} }
@ -207,12 +204,11 @@ func (t *NEP17Transfer) EncodeBinary(w *io.BinWriter) {
func (t *NEP17Transfer) DecodeBinary(r *io.BinReader) { func (t *NEP17Transfer) DecodeBinary(r *io.BinReader) {
t.Asset = int32(r.ReadU32LE()) t.Asset = int32(r.ReadU32LE())
r.ReadBytes(t.Tx[:]) r.ReadBytes(t.Tx[:])
r.ReadBytes(t.From[:]) r.ReadBytes(t.Counterparty[:])
r.ReadBytes(t.To[:])
t.Block = r.ReadU32LE() t.Block = r.ReadU32LE()
t.Timestamp = r.ReadU64LE() t.Timestamp = r.ReadU64LE()
amount := r.ReadVarBytes(bigint.MaxBytesLen) amount := r.ReadVarBytes(bigint.MaxBytesLen)
t.Amount = *bigint.FromBytes(amount) t.Amount = bigint.FromBytes(amount)
} }
// EncodeBinary implements the io.Serializable interface. // EncodeBinary implements the io.Serializable interface.

View file

@ -86,13 +86,12 @@ func BenchmarkTokenTransferLog_Append(b *testing.B) {
func TestNEP17Transfer_DecodeBinary(t *testing.T) { func TestNEP17Transfer_DecodeBinary(t *testing.T) {
expected := &NEP17Transfer{ expected := &NEP17Transfer{
Asset: 123, Asset: 123,
From: util.Uint160{5, 6, 7}, Counterparty: util.Uint160{5, 6, 7},
To: util.Uint160{8, 9, 10}, Amount: big.NewInt(42),
Amount: *big.NewInt(42), Block: 12345,
Block: 12345, Timestamp: 54321,
Timestamp: 54321, Tx: util.Uint256{8, 5, 3},
Tx: util.Uint256{8, 5, 3},
} }
testserdes.EncodeDecodeBinary(t, expected, new(NEP17Transfer)) testserdes.EncodeDecodeBinary(t, expected, new(NEP17Transfer))
@ -101,13 +100,12 @@ func TestNEP17Transfer_DecodeBinary(t *testing.T) {
func TestNEP11Transfer_DecodeBinary(t *testing.T) { func TestNEP11Transfer_DecodeBinary(t *testing.T) {
expected := &NEP11Transfer{ expected := &NEP11Transfer{
NEP17Transfer: NEP17Transfer{ NEP17Transfer: NEP17Transfer{
Asset: 123, Asset: 123,
From: util.Uint160{5, 6, 7}, Counterparty: util.Uint160{5, 6, 7},
To: util.Uint160{8, 9, 10}, Amount: big.NewInt(42),
Amount: *big.NewInt(42), Block: 12345,
Block: 12345, Timestamp: 54321,
Timestamp: 54321, Tx: util.Uint256{8, 5, 3},
Tx: util.Uint256{8, 5, 3},
}, },
ID: []byte{42, 42, 42}, ID: []byte{42, 42, 42},
} }
@ -117,24 +115,22 @@ func TestNEP11Transfer_DecodeBinary(t *testing.T) {
func random17Transfer(r *rand.Rand) *NEP17Transfer { func random17Transfer(r *rand.Rand) *NEP17Transfer {
return &NEP17Transfer{ return &NEP17Transfer{
Amount: *big.NewInt(int64(r.Uint64())), Amount: big.NewInt(int64(r.Uint64())),
Block: r.Uint32(), Block: r.Uint32(),
Asset: int32(random.Int(10, 10000000)), Asset: int32(random.Int(10, 10000000)),
From: random.Uint160(), Counterparty: random.Uint160(),
To: random.Uint160(), Tx: random.Uint256(),
Tx: random.Uint256(),
} }
} }
func random11Transfer(r *rand.Rand) *NEP11Transfer { func random11Transfer(r *rand.Rand) *NEP11Transfer {
return &NEP11Transfer{ return &NEP11Transfer{
NEP17Transfer: NEP17Transfer{ NEP17Transfer: NEP17Transfer{
Amount: *big.NewInt(int64(r.Uint64())), Amount: big.NewInt(int64(r.Uint64())),
Block: r.Uint32(), Block: r.Uint32(),
Asset: int32(random.Int(10, 10000000)), Asset: int32(random.Int(10, 10000000)),
From: random.Uint160(), Counterparty: random.Uint160(),
To: random.Uint160(), Tx: random.Uint256(),
Tx: random.Uint256(),
}, },
ID: random.Uint256().BytesBE(), ID: random.Uint256().BytesBE(),
} }

View file

@ -7,9 +7,11 @@ import (
func BenchmarkToPreallocatedBytes(b *testing.B) { func BenchmarkToPreallocatedBytes(b *testing.B) {
v := big.NewInt(100500) v := big.NewInt(100500)
vn := big.NewInt(-100500)
buf := make([]byte, 4) buf := make([]byte, 4)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = ToPreallocatedBytes(v, buf[:0]) _ = ToPreallocatedBytes(v, buf[:0])
_ = ToPreallocatedBytes(vn, buf[:0])
} }
} }

View file

@ -1,6 +1,7 @@
package bigint package bigint
import ( import (
"math"
"math/big" "math/big"
"math/bits" "math/bits"
@ -113,9 +114,28 @@ func ToPreallocatedBytes(n *big.Int, data []byte) []byte {
} }
if sign < 0 { if sign < 0 {
n.Add(n, bigOne) bits := n.Bits()
defer func() { n.Sub(n, bigOne) }() carry := true
if n.Sign() == 0 { // n == -1 nonZero := false
for i := range bits {
if carry {
bits[i]--
carry = (bits[i] == math.MaxUint)
}
nonZero = nonZero || (bits[i] != 0)
}
defer func() {
var carry = true
for i := range bits {
if carry {
bits[i]++
carry = (bits[i] == 0)
} else {
break
}
}
}()
if !nonZero { // n == -1
return append(data[:0], 0xFF) return append(data[:0], 0xFF)
} }
} }

View file

@ -106,8 +106,14 @@ var testCases = []struct {
func TestIntToBytes(t *testing.T) { func TestIntToBytes(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := ToBytes(big.NewInt(tc.number)) num := big.NewInt(tc.number)
var numC = *num // See #2864.
buf := ToBytes(num)
assert.Equal(t, tc.buf, buf, "error while converting %d", tc.number) assert.Equal(t, tc.buf, buf, "error while converting %d", tc.number)
_ = numC.Neg(&numC)
_ = ToBytes(&numC)
_ = numC.Neg(&numC)
assert.Equal(t, num, &numC, "number mismatch after converting %d", tc.number)
} }
} }

View file

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