core: simplify and correct notification handling

* both 'to' and 'from' are either Null or Hash160, there is no other
   possibility for valid NEP-17. So returning util.Uint160{} in case of
   parsing error is wrong.
 * but this is what allowed burns/mints to work at the expense of error
   allocation inside of util.Uint160DecodeBytesBE()
 * Uint160 can technically fit into regular VM integer, so even though it'd be
   quite surprising to see it there, TryBytes() is more correct (and easier!)
   to use
 * same thing with `amount`, we have `TryInteger()` that easily covers all
   possible cases and does appropriate error checking inside
This commit is contained in:
Roman Khimov 2021-08-20 11:26:16 +03:00
parent abc48229a3
commit a68a8aa8fc

View file

@ -27,7 +27,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
@ -1038,49 +1037,35 @@ func (bc *Blockchain) handleNotification(note *state.NotificationEvent, d dao.DA
if !ok || len(arr) != 3 { if !ok || len(arr) != 3 {
return return
} }
var from []byte from, err := parseUint160(arr[0])
fromValue := arr[0].Value() if err != nil {
// we don't have `from` set when we are minting tokens
if fromValue != nil {
from, ok = fromValue.([]byte)
if !ok {
return return
} }
} to, err := parseUint160(arr[1])
var to []byte if err != nil {
toValue := arr[1].Value()
// we don't have `to` set when we are burning tokens
if toValue != nil {
to, ok = toValue.([]byte)
if !ok {
return return
} }
} amount, err := arr[2].TryInteger()
amount, ok := arr[2].Value().(*big.Int) if err != nil {
if !ok {
bs, ok := arr[2].Value().([]byte)
if !ok {
return return
} }
if len(bs) > bigint.MaxBytesLen {
return // Not a proper number.
}
amount = bigint.FromBytes(bs)
}
bc.processNEP17Transfer(d, transCache, h, b, note.ScriptHash, from, to, amount) bc.processNEP17Transfer(d, transCache, h, b, note.ScriptHash, from, to, amount)
} }
func parseUint160(addr []byte) util.Uint160 { func parseUint160(itm stackitem.Item) (util.Uint160, error) {
if u, err := util.Uint160DecodeBytesBE(addr); err == nil { _, ok := itm.(stackitem.Null) // Minting or burning.
return u if ok {
return util.Uint160{}, nil
} }
return util.Uint160{} bytes, err := itm.TryBytes()
if err != nil {
return util.Uint160{}, err
}
return util.Uint160DecodeBytesBE(bytes)
} }
func (bc *Blockchain) processNEP17Transfer(cache dao.DAO, transCache map[util.Uint160]transferData, func (bc *Blockchain) processNEP17Transfer(cache dao.DAO, transCache map[util.Uint160]transferData,
h util.Uint256, b *block.Block, sc util.Uint160, from, to []byte, amount *big.Int) { h util.Uint256, b *block.Block, sc util.Uint160, from util.Uint160, to util.Uint160, amount *big.Int) {
toAddr := parseUint160(to)
fromAddr := parseUint160(from)
var id int32 var id int32
nativeContract := bc.contracts.ByHash(sc) nativeContract := bc.contracts.ByHash(sc)
if nativeContract != nil { if nativeContract != nil {
@ -1094,21 +1079,21 @@ func (bc *Blockchain) processNEP17Transfer(cache dao.DAO, transCache map[util.Ui
} }
transfer := &state.NEP17Transfer{ transfer := &state.NEP17Transfer{
Asset: id, Asset: id,
From: fromAddr, From: from,
To: toAddr, To: to,
Block: b.Index, Block: b.Index,
Timestamp: b.Timestamp, Timestamp: b.Timestamp,
Tx: h, Tx: h,
} }
if !fromAddr.Equals(util.Uint160{}) { if !from.Equals(util.Uint160{}) {
_ = transfer.Amount.Neg(amount) // We already have the Int. _ = transfer.Amount.Neg(amount) // We already have the Int.
if appendNEP17Transfer(cache, transCache, fromAddr, transfer) != nil { if appendNEP17Transfer(cache, transCache, from, transfer) != nil {
return return
} }
} }
if !toAddr.Equals(util.Uint160{}) { if !to.Equals(util.Uint160{}) {
_ = transfer.Amount.Set(amount) // We already have the Int. _ = transfer.Amount.Set(amount) // We already have the Int.
_ = appendNEP17Transfer(cache, transCache, toAddr, transfer) // Nothing useful we can do. _ = appendNEP17Transfer(cache, transCache, to, transfer) // Nothing useful we can do.
} }
} }