Skip to content

BigInteger 0 isn't able to be convert to Byte[]{ 0 } #201

@superboyiii

Description

@superboyiii

Due to #196, we've improved a lot on smart contract convertion method. However, I still find a little issue that BigInteger 0 isn't able to be convert to Byte[]{ 0 } by ToByteArray method. This will cause many issues such like balance exception when make transaction. I've made a little example to replay this situation. For example:

using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.Numerics;

namespace TestConvert
{
    [Features(ContractFeatures.HasStorage | ContractFeatures.Payable)]
    public class Contract1 : SmartContract
    {
        public static object Main(string operation, object[] args)
        {
            BigInteger num = 0;
            byte[] byteNum = num.ToByteArray();
            Storage.Put(Storage.CurrentContext, "key", byteNum);
            return "TestConvertZero";
        }
    }
}

Then I could deploy it successfully, but I find the value is null in storage.

neo> deploy D:\Master-smoking\2020-03-13\neo-devpack-dotnet\TestConvert\bin\Debug\netstandard2.1\TestConvert.nef D:\Master-smoking\2020-03-13\neo-devpack-dotnet\TestConvert\bin\Debug\netstandard2.1\TestConvert.manifest.json
Script hash: 0x7b5ae605948a00b1beb37ffdb985fc385907d05b
Gas: 100000000

Signed and relayed transaction with hash=0x710cc5f6fb4b2b175ef90c6438112192f8d018539053d6de7f4bb1556d6325a7
neo> invoke 0x7b5ae605948a00b1beb37ffdb985fc385907d05b Main
Invoking script with: '10c00c044d61696e0c145bd0075938fc85b9fd7fb3beb1008a9405e65a7b41627d5b52'
VM State: HALT
Gas Consumed: 1389470
Evaluation Stack: [{"type":"ByteArray","value":"VGVzdENvbnZlcnRaZXJv"}]

relay tx(no|yes): y
Signed and relayed transaction with hash=0xde9bff4f091583f6dd681d1b3a007938d37a011c8bc0b2eb68435ec9d4aaf37c
neo>

Try GetStorage:
image

If I change this num to any num except 0, it works well.

using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.Numerics;

namespace TestConvert
{
    [Features(ContractFeatures.HasStorage | ContractFeatures.Payable)]
    public class Contract1 : SmartContract
    {
        public static object Main(string operation, object[] args)
        {
            BigInteger num = 20;
            byte[] byteNum = num.ToByteArray();
            Storage.Put(Storage.CurrentContext, "key", byteNum);
            return "TestConvertZero";
        }
    }
}
neo> deploy D:\Master-smoking\2020-03-13\neo-devpack-dotnet\TestConvert\bin\Debug\netstandard2.1\TestConvert.nef D:\Master-smoking\2020-03-13\neo-devpack-dotnet\TestConvert\bin\Debug\netstandard2.1\TestConvert.manifest.json
Script hash: 0xa6fc45f3c8eeefcb2c7969bc0aab46a032ddd224
Gas: 100000000

Signed and relayed transaction with hash=0x784196a4a8a563c1559137e50550685fb87d2823fd1f884bca01140afcf3b2ed
neo> invoke 0xa6fc45f3c8eeefcb2c7969bc0aab46a032ddd224 Main
Invoking script with: '10c00c044d61696e0c1424d2dd32a046ab0abc69792ccbefeec8f345fca641627d5b52'
VM State: HALT
Gas Consumed: 1569620
Evaluation Stack: [{"type":"ByteArray","value":"VGVzdENvbnZlcnRaZXJv"}]

relay tx(no|yes): y
Signed and relayed transaction with hash=0x560797ed5900eb78aa9d6d3d667944861073b44f9b89e2395eb14116eca189e7
neo>

Try GetStorage:
image

@lightszero Sorry to bother you again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions