Exemplo n.º 1
0
        private async Task <byte[]> CallReadOnlyAsync(Transaction tx)
        {
            var chainContext = await GetChainContextAsync();

            var trace = await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, tx,
                                                                                DateTime.UtcNow.ToTimestamp());

            if (!string.IsNullOrEmpty(trace.Error))
            {
                throw new Exception(trace.Error);
            }

            return(trace.ReturnValue.ToByteArray());
        }
Exemplo n.º 2
0
        private async Task UpdateSmartContractAddressesAsync(BlockHeader blockHeader,
                                                             ISmartContractAddressNameProvider smartContractAddressNameProvider)
        {
            var t = new Transaction()
            {
                From       = _smartContractAddressService.GetZeroSmartContractAddress(),
                To         = _smartContractAddressService.GetZeroSmartContractAddress(),
                MethodName = nameof(Acs0.ACS0Container.ACS0Stub.GetContractAddressByName),
                Params     = smartContractAddressNameProvider.ContractName.ToByteString()
            };

            var transactionResult =
                (await _transactionExecutingService.ExecuteAsync(
                     new ChainContext()
            {
                BlockHash = blockHeader.GetHash(), BlockHeight = blockHeader.Height
            }, t,
                     TimestampHelper.GetUtcNow()));

            if (!transactionResult.IsSuccessful())
            {
                throw new InvalidOperationException();
            }

            var address = Address.Parser.ParseFrom(transactionResult.ReturnValue);

            if (!address.Value.IsEmpty)
            {
                _smartContractAddressService.SetAddress(smartContractAddressNameProvider.ContractName, address);
            }
        }
        public async Task <RequiredAcs> GetRequiredAcsInContractsAsync(Hash blockHash, long blockHeight)
        {
            var tx = new Transaction
            {
                From       = FromAddress,
                To         = ConfigurationContractAddress,
                MethodName = nameof(ConfigurationContainer.ConfigurationStub.GetConfiguration),
                Params     = new StringValue {
                    Value = RequiredAcsInContractsConfigurationNameProvider.Name
                }.ToByteString(),
                Signature = ByteString.CopyFromUtf8(KernelConstants.SignaturePlaceholder)
            };

            var returned = await _transactionReadOnlyExecutionService.ExecuteAsync <BytesValue>(
                new ChainContext
            {
                BlockHash   = blockHash,
                BlockHeight = blockHeight
            }, tx, TimestampHelper.GetUtcNow(), false);

            var requiredAcsInContracts = new RequiredAcsInContracts();

            requiredAcsInContracts.MergeFrom(returned.Value);
            return(new RequiredAcs
            {
                AcsList = requiredAcsInContracts.AcsList.ToList(),
                RequireAll = requiredAcsInContracts.RequireAll
            });
        }
Exemplo n.º 4
0
        private async Task <ByteString> CallContractMethodAsync(Address contractAddress, string methodName,
                                                                IMessage input)
        {
            var tx = new Transaction
            {
                From       = FromAddress,
                To         = contractAddress,
                MethodName = methodName,
                Params     = input.ToByteString(),
                Signature  = ByteString.CopyFromUtf8("SignaturePlaceholder")
            };
            var chain = await _blockchainService.GetChainAsync();

            if (chain == null)
            {
                return(ByteString.Empty);
            }
            var transactionTrace = await _transactionReadOnlyExecutionService.ExecuteAsync(new ChainContext
            {
                BlockHash   = chain.LastIrreversibleBlockHash,
                BlockHeight = chain.LastIrreversibleBlockHeight
            }, tx, TimestampHelper.GetUtcNow());

            return(transactionTrace.ReturnValue);
        }
Exemplo n.º 5
0
        public IMethodStub <TInput, TOutput> Create <TInput, TOutput>(Method <TInput, TOutput> method)
            where TInput : IMessage <TInput>, new() where TOutput : IMessage <TOutput>, new()
        {
            Task <IExecutionResult <TOutput> > SendAsync(TInput input)
            {
                throw new NotSupportedException();
            }

            async Task <TOutput> CallAsync(TInput input)
            {
                var chainContext = _chainContext;
                var transaction  = new Transaction()
                {
                    From       = FromAddress,
                    To         = ConsensusContractAddress,
                    MethodName = method.Name,
                    Params     = ByteString.CopyFrom(method.RequestMarshaller.Serializer(input))
                };

                var trace =
                    await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction,
                                                                            _contextService.GetBlockTime());

                return(trace.IsSuccessful()
                    ? method.ResponseMarshaller.Deserializer(trace.ReturnValue.ToByteArray())
                    : default);
Exemplo n.º 6
0
        public IMethodStub <TInput, TOutput> Create <TInput, TOutput>(Method <TInput, TOutput> method)
            where TInput : IMessage <TInput>, new() where TOutput : IMessage <TOutput>, new()
        {
            Task <IExecutionResult <TOutput> > SendAsync(TInput input)
            {
                throw new NotSupportedException();
            }

            async Task <TOutput> CallAsync(TInput input)
            {
                var chainContext = _chainContext;
                var transaction  = new Transaction()
                {
                    From       = FromAddress,
                    To         = CrossChainContractMethodAddress,
                    MethodName = method.Name,
                    Params     = ByteString.CopyFrom(method.RequestMarshaller.Serializer(input))
                };

                var trace =
                    await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction, TimestampHelper.GetUtcNow());

                if (trace.IsSuccessful())
                {
                    return(method.ResponseMarshaller.Deserializer(trace.ReturnValue.ToByteArray()));
                }

                return(default(TOutput));
            }

            return(new MethodStub <TInput, TOutput>(method, SendAsync, CallAsync));
        }
Exemplo n.º 7
0
 public async Task Fibonacci16()
 {
     _transactionTrace = await _transactionReadOnlyExecutionService.ExecuteAsync(new ChainContext
     {
         BlockHash   = _chain.BestChainHash,
         BlockHeight = _chain.BestChainHeight
     },
                                                                                 _transaction,
                                                                                 DateTime.UtcNow);
 }
Exemplo n.º 8
0
 public async Task LoopDivAdd10M()
 {
     _transactionTrace = await _transactionReadOnlyExecutionService.ExecuteAsync(new ChainContext
     {
         BlockHash   = _chain.BestChainHash,
         BlockHeight = _chain.BestChainHeight
     },
                                                                                 _transaction,
                                                                                 TimestampHelper.GetUtcNow());
 }
Exemplo n.º 9
0
        public async Task <ByteString> ExecuteReadOnlyAsync(Transaction transaction, Hash blockHash, long blockHeight)
        {
            var transactionTrace = await _transactionReadOnlyExecutionService.ExecuteAsync(new ChainContext
            {
                BlockHash   = blockHash,
                BlockHeight = blockHeight
            },
                                                                                           transaction,
                                                                                           DateTime.UtcNow.ToTimestamp());

            return(transactionTrace.ReturnValue);
        }
Exemplo n.º 10
0
        public async Task <T> ExecuteContractAsync <T>(IChainContext chainContext, string consensusMethodName,
                                                       IMessage input, DateTime dateTime) where T : class, IMessage <T>, new()
        {
            var tx = new Transaction
            {
                From = Address.Generate(),
                To   = _smartContractAddressService.GetAddressByContractName(ConsensusSmartContractAddressNameProvider
                                                                             .Name),
                MethodName = consensusMethodName,
                Params     = input?.ToByteString() ?? ByteString.Empty
            };

            return(await _transactionReadOnlyExecutionService.ExecuteAsync <T>(chainContext, tx, dateTime));
        }
        public async Task <SmartContractRegistration> GetSmartContractRegistrationAsync(IChainContext chainContext, Address address)
        {
            var zeroAddress = _defaultContractZeroCodeProvider.ContractZeroAddress;
            var tx          = new Transaction
            {
                From       = zeroAddress,
                To         = zeroAddress,
                MethodName = nameof(ACS0Container.ACS0Stub.GetSmartContractRegistrationByAddress),
                Params     = address.ToByteString()
            };

            return(await _transactionReadOnlyExecutionService.ExecuteAsync <SmartContractRegistration>(
                       chainContext, tx, TimestampHelper.GetUtcNow(), false));
        }
Exemplo n.º 12
0
        private async Task <Address> GetSmartContractAddressFromStateAsync(IChainContext chainContext, string name)
        {
            var zeroAddress = _defaultContractZeroCodeProvider.ContractZeroAddress;
            var tx          = new Transaction
            {
                From       = zeroAddress,
                To         = zeroAddress,
                MethodName = nameof(ACS0Container.ACS0Stub.GetContractAddressByName),
                Params     = Hash.LoadFromBase64(name).ToByteString()
            };
            var address = await _transactionReadOnlyExecutionService.ExecuteAsync <Address>(
                chainContext, tx, TimestampHelper.GetUtcNow(), false);

            return(address == null || address.Value.IsEmpty ? null : address);
        }
Exemplo n.º 13
0
        private async Task <ByteString> GetBlockTransactionLimitAsync(IChainContext chainContext)
        {
            var tx = new Transaction
            {
                From       = FromAddress,
                To         = ConfigurationContractAddress,
                MethodName = nameof(ConfigurationContainer.ConfigurationStub.GetBlockTransactionLimit),
                Params     = new Empty().ToByteString(),
                Signature  = ByteString.CopyFromUtf8("SignaturePlaceholder")
            };
            var transactionTrace =
                await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, tx, TimestampHelper.GetUtcNow());

            return(transactionTrace.ReturnValue);
        }
Exemplo n.º 14
0
        private async Task <T> ReadByTransactionAsync <T>(Transaction readOnlyTransaction, Hash blockHash, long blockHeight)
            where T : IMessage <T>, new()
        {
            var chainContext = GenerateChainContext(blockHash, blockHeight);
            var trace        =
                await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, readOnlyTransaction, DateTime.UtcNow);

            if (trace.IsSuccessful())
            {
                var obj = new T();
                obj.MergeFrom(trace.ReturnValue);
                return(obj);
            }
            return(default(T));
        }
Exemplo n.º 15
0
        private async Task UpdateSmartContractAddressesAsync(BlockHeader blockHeader,
                                                             ISmartContractAddressNameProvider smartContractAddressNameProvider)
        {
            var transaction = new Transaction()
            {
                From       = _smartContractAddressService.GetZeroSmartContractAddress(),
                To         = _smartContractAddressService.GetZeroSmartContractAddress(),
                MethodName = nameof(Acs0.ACS0Container.ACS0Stub.GetContractAddressByName),
                Params     = smartContractAddressNameProvider.ContractName.ToByteString()
            };
            var address = await _transactionExecutingService.ExecuteAsync <Address>(
                new ChainContext { BlockHash = blockHeader.GetHash(), BlockHeight = blockHeader.Height }, transaction,
                TimestampHelper.GetUtcNow(), true);

            if (!address.Value.IsEmpty)
            {
                _smartContractAddressService.SetAddress(smartContractAddressNameProvider.ContractName, address);
            }
        }
        public static async Task <T> ExecuteAsync <T>(
            this ITransactionReadOnlyExecutionService transactionReadOnlyExecutionService,
            IChainContext chainContext, Transaction transaction,
            Timestamp currentBlockTime, bool failedThrowException) where T : class, IMessage <T>, new()
        {
            var trace = await transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction, currentBlockTime);

            if (trace.IsSuccessful())
            {
                var obj = new T();
                obj.MergeFrom(trace.ReturnValue);
                return(obj);
            }

            if (failedThrowException)
            {
                throw new SmartContractExecutingException(trace.Error);
            }

            return(default(T));
        }
        private async Task <ByteString> CallContractMethodAsync(Address contractAddress, string methodName,
                                                                IMessage input)
        {
            var tx = new Transaction
            {
                From       = FromAddress,
                To         = contractAddress,
                MethodName = methodName,
                Params     = input.ToByteString(),
                Signature  = ByteString.CopyFromUtf8("SignaturePlaceholder")
            };
            var preBlock = await _blockchainService.GetBestChainLastBlockHeaderAsync();

            var transactionTrace = await _transactionReadOnlyExecutionService.ExecuteAsync(new ChainContext
            {
                BlockHash   = preBlock.GetHash(),
                BlockHeight = preBlock.Height
            }, tx, TimestampHelper.GetUtcNow());

            return(transactionTrace.ReturnValue);
        }
        public async Task ExecuteAsync_Test()
        {
            var chain = await _smartContractHelper.CreateChainWithGenesisContractAsync();

            var chainContext = new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };
            var transaction = new Transaction
            {
                From       = SampleAddress.AddressList[0],
                To         = SampleAddress.AddressList[0],
                MethodName = "NotExistMethod",
                Params     = ByteString.Empty
            };

            _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction, TimestampHelper.GetUtcNow())
            .ShouldThrow <SmartContractFindRegistrationException>();
            _transactionReadOnlyExecutionService.ExecuteAsync <Address>(chainContext, transaction, TimestampHelper.GetUtcNow(), false)
            .ShouldThrow <SmartContractFindRegistrationException>();

            transaction = new Transaction
            {
                From       = SampleAddress.AddressList[0],
                To         = _defaultContractZeroCodeProvider.ContractZeroAddress,
                MethodName = "NotExistMethod",
                Params     = ByteString.Empty
            };
            var trace = await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction,
                                                                                TimestampHelper.GetUtcNow());

            trace.Error.ShouldContain("Failed to find handler for NotExistMethod");
            trace.ExecutionStatus.ShouldBe(ExecutionStatus.SystemError);
            var hash = await _transactionReadOnlyExecutionService.ExecuteAsync <Hash>(chainContext, transaction,
                                                                                      TimestampHelper.GetUtcNow(), false);

            hash.ShouldBeNull();
            _transactionReadOnlyExecutionService.ExecuteAsync <Hash>(chainContext, transaction,
                                                                     TimestampHelper.GetUtcNow(), true).ShouldThrow <SmartContractExecutingException>();

            _smartContractExecutiveProvider.GetPool(_defaultContractZeroCodeProvider.ContractZeroAddress).Single()
            .ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);
            transaction = new Transaction
            {
                From       = SampleAddress.AddressList[0],
                To         = _defaultContractZeroCodeProvider.ContractZeroAddress,
                MethodName = nameof(ACS0Container.ACS0Stub.GetSmartContractRegistrationByAddress),
                Params     = _defaultContractZeroCodeProvider.ContractZeroAddress.ToByteString()
            };
            trace = await _transactionReadOnlyExecutionService.ExecuteAsync(chainContext, transaction,
                                                                            TimestampHelper.GetUtcNow());

            trace.ExecutionStatus.ShouldBe(ExecutionStatus.Executed);
            var smartContractRegistration = SmartContractRegistration.Parser.ParseFrom(trace.ReturnValue);

            CheckSmartContractRegistration(smartContractRegistration);

            _smartContractExecutiveProvider.GetPool(_defaultContractZeroCodeProvider.ContractZeroAddress).Single()
            .ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);

            smartContractRegistration = await _transactionReadOnlyExecutionService.ExecuteAsync <SmartContractRegistration>(chainContext, transaction,
                                                                                                                            TimestampHelper.GetUtcNow(), true);

            CheckSmartContractRegistration(smartContractRegistration);

            _smartContractExecutiveProvider.GetPool(_defaultContractZeroCodeProvider.ContractZeroAddress).Single()
            .ContractHash.ShouldBe(_defaultContractZeroCodeProvider.DefaultContractZeroRegistration.CodeHash);
        }