From 5f13de3a7679906fbfbb8e45b6950dbe0b1eef94 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 19 Jul 2021 15:15:14 +0300 Subject: [PATCH] *: simplify some integer checks with IsUint64() If we need a positive number we can do `IsUint64()` instead of checking that `Int64()` is `> 0`. --- pkg/core/interop_system.go | 7 ++++--- pkg/core/native/ledger.go | 6 +++--- pkg/core/native/native_nep17.go | 10 +++++----- pkg/core/state/contract.go | 4 ++-- pkg/core/state/deposit.go | 6 +++--- pkg/vm/vm.go | 2 +- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pkg/core/interop_system.go b/pkg/core/interop_system.go index e7578180f..e83e3880f 100644 --- a/pkg/core/interop_system.go +++ b/pkg/core/interop_system.go @@ -219,8 +219,9 @@ func storageFind(ic *interop.Context) error { // given m and a set of public keys. func contractCreateMultisigAccount(ic *interop.Context) error { m := ic.VM.Estack().Pop().BigInt() - if !m.IsInt64() || m.Int64() > math.MaxInt32 { - return errors.New("m should fit int32") + mu64 := m.Uint64() + if !m.IsUint64() || mu64 > math.MaxInt32 { + return errors.New("m must be positive and fit int32") } arr := ic.VM.Estack().Pop().Array() pubs := make(keys.PublicKeys, len(arr)) @@ -231,7 +232,7 @@ func contractCreateMultisigAccount(ic *interop.Context) error { } pubs[i] = p } - script, err := smartcontract.CreateMultiSigRedeemScript(int(m.Int64()), pubs) + script, err := smartcontract.CreateMultiSigRedeemScript(int(mu64), pubs) if err != nil { return err } diff --git a/pkg/core/native/ledger.go b/pkg/core/native/ledger.go index ad9b68185..0abc315ab 100644 --- a/pkg/core/native/ledger.go +++ b/pkg/core/native/ledger.go @@ -157,9 +157,9 @@ func isTraceableBlock(bc blockchainer.Blockchainer, index uint32) bool { // be called within VM context, so it panics if anything goes wrong. func getBlockHashFromItem(bc blockchainer.Blockchainer, item stackitem.Item) util.Uint256 { bigindex, err := item.TryInteger() - if err == nil && bigindex.IsInt64() { - index := bigindex.Int64() - if index < 0 || index > math.MaxUint32 { + if err == nil && bigindex.IsUint64() { + index := bigindex.Uint64() + if index > math.MaxUint32 { panic("bad block index") } if uint32(index) > bc.BlockHeight() { diff --git a/pkg/core/native/native_nep17.go b/pkg/core/native/native_nep17.go index 0bf47a615..3a6612121 100644 --- a/pkg/core/native/native_nep17.go +++ b/pkg/core/native/native_nep17.go @@ -343,12 +343,12 @@ func toUint160(s stackitem.Item) util.Uint160 { func toUint32(s stackitem.Item) uint32 { bigInt := toBigInt(s) - if !bigInt.IsInt64() { - panic("bigint is not an int64") + if !bigInt.IsUint64() { + panic("bigint is not an uint64") } - int64Value := bigInt.Int64() - if int64Value < 0 || int64Value > math.MaxUint32 { + uint64Value := bigInt.Uint64() + if uint64Value > math.MaxUint32 { panic("bigint does not fit into uint32") } - return uint32(int64Value) + return uint32(uint64Value) } diff --git a/pkg/core/state/contract.go b/pkg/core/state/contract.go index b5a526cbc..9d7cf8514 100644 --- a/pkg/core/state/contract.go +++ b/pkg/core/state/contract.go @@ -73,10 +73,10 @@ func (c *Contract) FromStackItem(item stackitem.Item) error { if !ok { return errors.New("UpdateCounter is not an integer") } - if !bi.IsInt64() || bi.Int64() > math.MaxUint16 || bi.Int64() < 0 { + if !bi.IsUint64() || bi.Uint64() > math.MaxUint16 { return errors.New("UpdateCounter not in uint16 range") } - c.UpdateCounter = uint16(bi.Int64()) + c.UpdateCounter = uint16(bi.Uint64()) bytes, err := arr[2].TryBytes() if err != nil { return err diff --git a/pkg/core/state/deposit.go b/pkg/core/state/deposit.go index 3efd76d7a..59c97fe54 100644 --- a/pkg/core/state/deposit.go +++ b/pkg/core/state/deposit.go @@ -41,11 +41,11 @@ func (d *Deposit) FromStackItem(it stackitem.Item) error { if err != nil { return fmt.Errorf("invalid till: %w", err) } - ti64 := till.Int64() - if !till.IsInt64() || ti64 > math.MaxUint32 || ti64 < 0 { + tiu64 := till.Uint64() + if !till.IsUint64() || tiu64 > math.MaxUint32 { return errors.New("wrong till value") } d.Amount = amount - d.Till = uint32(ti64) + d.Till = uint32(tiu64) return nil } diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 0d65f3bf2..2f1d57d1d 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -918,7 +918,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro case opcode.POW: exp := v.estack.Pop().BigInt() a := v.estack.Pop().BigInt() - if ei := exp.Int64(); !exp.IsInt64() || ei > maxSHLArg || ei < 0 { + if ei := exp.Uint64(); !exp.IsUint64() || ei > maxSHLArg { panic("invalid exponent") } v.estack.PushVal(new(big.Int).Exp(a, exp, nil))