Пример #1
0
        public void WhenExecutePrecompiledContractThenHelloWorldIsDisplayed()
        {
            const string operationSignature = "get()";
            var          hash                 = HashFactory.Crypto.SHA3.CreateKeccak256();
            var          msgData              = hash.ComputeBytes(System.Text.Encoding.ASCII.GetBytes(operationSignature)).GetBytes().Take(4);
            var          serviceProvider      = BuildServiceProvider();
            var          smartContractFactory = serviceProvider.GetService <ISmartContractFactory>();
            var          smartContracts       = smartContractFactory.Build(_network);
            var          smartContractStore   = serviceProvider.GetService <ISmartContractStore>();
            var          pgInvoke             = new SolidityProgramInvoke(msgData, new byte[0], new DataWord(), new DataWord(), smartContractStore.GetSmartContracts());

            RemoveSmartContracts();

            var contract = smartContracts.GetSmartContract("0000000000000000000000000000000000000001".FromHexString());

            var program = new SolidityProgram(contract.Code.ToList(), pgInvoke);
            var vm      = new SolidityVm();

            while (!program.IsStopped())
            {
                vm.Step(program);
            }

            var hResult = program.GetResult().GetHReturn();
            var res     = hResult.ToHexString();

            Assert.IsTrue(res == "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000");
        }
        public void Check(SmartContractTransaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            var blockChain       = _blockChainStore.GetBlockChain();
            var smartContracts   = _smartContractStore.GetSmartContracts();
            var vm               = new SolidityVm();
            var defaultCallValue = new DataWord(new byte[] { 0x00 });

            if (transaction.To == null || !transaction.To.Any())
            {
                if (transaction.From == null || !transaction.From.Any())
                {
                    throw new ArgumentNullException(nameof(transaction.From));
                }

                if (transaction.From.Count() != 20)
                {
                    throw new ValidationException(ErrorCodes.FromInvalidLength);
                }

                if (transaction.Data == null || !transaction.Data.Any())
                {
                    throw new ArgumentNullException(nameof(transaction.Data));
                }

                var scs     = _smartContractStore.GetSmartContracts();
                var program = new SolidityProgram(transaction.Data.ToList(), new SolidityProgramInvoke(new byte[0], new DataWord(transaction.From.ToArray()), defaultCallValue, scs)); // TRY TO GET THE CONTRACT.
                try
                {
                    while (!program.IsStopped())
                    {
                        vm.Step(program);
                    }

                    scs.Rollback();
                    var hReturn = program.GetResult().GetHReturn();
                    if (hReturn == null || !hReturn.Any())
                    {
                        throw new ValidationException(ErrorCodes.SmartContractNotValid);
                    }
                }
                catch (Exception)
                {
                    throw new ValidationException(ErrorCodes.SmartContractNotValid);
                }
            }
            else
            {
                if (transaction.To.Count() != 20)
                {
                    throw new ValidationException(ErrorCodes.ToInvalidLength);
                }

                var smartContract = smartContracts.GetSmartContract(transaction.To);
                if (smartContract == null)
                {
                    throw new ValidationException(ErrorCodes.SmartContractDoesntExist);
                }
            }
        }