*: simplify some integer checks with IsUint64()

If we need a positive number we can do `IsUint64()` instead of checking that
`Int64()` is `> 0`.
This commit is contained in:
Roman Khimov 2021-07-19 15:15:14 +03:00
parent 233307aca5
commit 5f13de3a76
6 changed files with 18 additions and 17 deletions

View file

@ -219,8 +219,9 @@ func storageFind(ic *interop.Context) error {
// given m and a set of public keys. // given m and a set of public keys.
func contractCreateMultisigAccount(ic *interop.Context) error { func contractCreateMultisigAccount(ic *interop.Context) error {
m := ic.VM.Estack().Pop().BigInt() m := ic.VM.Estack().Pop().BigInt()
if !m.IsInt64() || m.Int64() > math.MaxInt32 { mu64 := m.Uint64()
return errors.New("m should fit int32") if !m.IsUint64() || mu64 > math.MaxInt32 {
return errors.New("m must be positive and fit int32")
} }
arr := ic.VM.Estack().Pop().Array() arr := ic.VM.Estack().Pop().Array()
pubs := make(keys.PublicKeys, len(arr)) pubs := make(keys.PublicKeys, len(arr))
@ -231,7 +232,7 @@ func contractCreateMultisigAccount(ic *interop.Context) error {
} }
pubs[i] = p pubs[i] = p
} }
script, err := smartcontract.CreateMultiSigRedeemScript(int(m.Int64()), pubs) script, err := smartcontract.CreateMultiSigRedeemScript(int(mu64), pubs)
if err != nil { if err != nil {
return err return err
} }

View file

@ -157,9 +157,9 @@ func isTraceableBlock(bc blockchainer.Blockchainer, index uint32) bool {
// be called within VM context, so it panics if anything goes wrong. // be called within VM context, so it panics if anything goes wrong.
func getBlockHashFromItem(bc blockchainer.Blockchainer, item stackitem.Item) util.Uint256 { func getBlockHashFromItem(bc blockchainer.Blockchainer, item stackitem.Item) util.Uint256 {
bigindex, err := item.TryInteger() bigindex, err := item.TryInteger()
if err == nil && bigindex.IsInt64() { if err == nil && bigindex.IsUint64() {
index := bigindex.Int64() index := bigindex.Uint64()
if index < 0 || index > math.MaxUint32 { if index > math.MaxUint32 {
panic("bad block index") panic("bad block index")
} }
if uint32(index) > bc.BlockHeight() { if uint32(index) > bc.BlockHeight() {

View file

@ -343,12 +343,12 @@ func toUint160(s stackitem.Item) util.Uint160 {
func toUint32(s stackitem.Item) uint32 { func toUint32(s stackitem.Item) uint32 {
bigInt := toBigInt(s) bigInt := toBigInt(s)
if !bigInt.IsInt64() { if !bigInt.IsUint64() {
panic("bigint is not an int64") panic("bigint is not an uint64")
} }
int64Value := bigInt.Int64() uint64Value := bigInt.Uint64()
if int64Value < 0 || int64Value > math.MaxUint32 { if uint64Value > math.MaxUint32 {
panic("bigint does not fit into uint32") panic("bigint does not fit into uint32")
} }
return uint32(int64Value) return uint32(uint64Value)
} }

View file

@ -73,10 +73,10 @@ func (c *Contract) FromStackItem(item stackitem.Item) error {
if !ok { if !ok {
return errors.New("UpdateCounter is not an integer") 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") return errors.New("UpdateCounter not in uint16 range")
} }
c.UpdateCounter = uint16(bi.Int64()) c.UpdateCounter = uint16(bi.Uint64())
bytes, err := arr[2].TryBytes() bytes, err := arr[2].TryBytes()
if err != nil { if err != nil {
return err return err

View file

@ -41,11 +41,11 @@ func (d *Deposit) FromStackItem(it stackitem.Item) error {
if err != nil { if err != nil {
return fmt.Errorf("invalid till: %w", err) return fmt.Errorf("invalid till: %w", err)
} }
ti64 := till.Int64() tiu64 := till.Uint64()
if !till.IsInt64() || ti64 > math.MaxUint32 || ti64 < 0 { if !till.IsUint64() || tiu64 > math.MaxUint32 {
return errors.New("wrong till value") return errors.New("wrong till value")
} }
d.Amount = amount d.Amount = amount
d.Till = uint32(ti64) d.Till = uint32(tiu64)
return nil return nil
} }

View file

@ -918,7 +918,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
case opcode.POW: case opcode.POW:
exp := v.estack.Pop().BigInt() exp := v.estack.Pop().BigInt()
a := 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") panic("invalid exponent")
} }
v.estack.PushVal(new(big.Int).Exp(a, exp, nil)) v.estack.PushVal(new(big.Int).Exp(a, exp, nil))