public IActionResult GetStorage([FromQuery] GetStorageRequest request)
        {
            if (!this.ModelState.IsValid)
            {
                this.logger.LogTrace("(-)[MODELSTATE_INVALID]");
                return(ModelStateErrors.BuildErrorResponse(this.ModelState));
            }

            uint160 addressNumeric = request.ContractAddress.ToUint160(this.network);

            byte[] storageValue = this.stateRoot.GetStorageValue(addressNumeric, Encoding.UTF8.GetBytes(request.StorageKey));

            if (storageValue == null)
            {
                return(Json(new
                {
                    Message = string.Format("No data at storage with key {0}", request.StorageKey)
                }));
            }

            // Interpret the storage bytes as an object of the given type
            var interpretedStorageValue = InterpretStorageValue(request.DataType, storageValue);

            // Use MethodParamStringSerializer to serialize the interpreted object to a string
            var serialized = MethodParameterStringSerializer.Serialize(interpretedStorageValue, this.network);

            return(Json(serialized));
        }
示例#2
0
        public SmartContractRPCTests()
        {
            var network = new SmartContractsPoARegTest();

            this.network     = network;
            this.builder     = SmartContractNodeBuilder.Create(this);
            this.nodeFactory = (nodeIndex) => this.builder.CreateSmartContractPoANode(network, nodeIndex).Start();
            this.methodParameterStringSerializer = new MethodParameterStringSerializer(this.network);
        }
示例#3
0
        public void Serialized_Address_Is_Base58()
        {
            var address = new Address();

            var serializedAddress = MethodParameterStringSerializer.Serialize(address, Network);

            // Will throw if address is invalid
            BitcoinAddress.Create(serializedAddress, Network);
        }
        public void Serialized_Method_Params_Are_Smaller_Than_Strings()
        {
            // Single comparative case for a sample byte vs. string encoded method params array
            var stringSerializer = new MethodParameterStringSerializer();

            var parameters = GetData(0).SelectMany(o => o).ToArray();

            var serializedBytes = this.Serializer.Serialize(parameters);
            var s = stringSerializer.Serialize(parameters);
            var serializedString = Encoding.UTF8.GetBytes(s);

            Assert.True(serializedBytes.Length <= serializedString.Length);
        }
        public static ValidationServiceResult Validate(string fileName, ContractCompilationResult compilationResult, IConsole console, string[] parameters)
        {
            var validationServiceResult = new ValidationServiceResult();

            byte[] compilation = compilationResult.Compilation;
            IContractModuleDefinition moduleDefinition = BuildModuleDefinition(console, compilation);

            console.WriteLine($"Validating file {fileName}...");

            Assembly smartContract = Assembly.Load(compilation);

            // Network does not matter here as we are only checking the deserialized Types of the params.
            var serializer = new MethodParameterStringSerializer(new SmartContractsRegTest());

            object[] methodParameters = null;
            if (parameters.Length != 0)
            {
                methodParameters = serializer.Deserialize(parameters);
            }

            validationServiceResult.ConstructorExists = Contract.ConstructorExists(smartContract.ExportedTypes.FirstOrDefault(), methodParameters);

            if (!validationServiceResult.ConstructorExists)
            {
                console.WriteLine("Smart contract construction failed.");
                console.WriteLine("No constructor exists with the provided parameters.");
            }

            validationServiceResult.DeterminismValidationResult = new SctDeterminismValidator().Validate(moduleDefinition);
            validationServiceResult.FormatValidationResult      = new SmartContractFormatValidator().Validate(moduleDefinition.ModuleDefinition);
            if (!validationServiceResult.DeterminismValidationResult.IsValid || !validationServiceResult.FormatValidationResult.IsValid)
            {
                console.WriteLine("Smart Contract failed validation. Run validate [FILE] for more info.");
            }

            console.WriteLine();

            return(validationServiceResult);
        }
        private static void ValidateContract(string fileName, IConsole console, string[] parameters, ValidationServiceResult validationServiceResult)
        {
            byte[] compilation;
            IContractModuleDefinition moduleDefinition;

            BuildModuleDefinition(console, validationServiceResult, out compilation, out moduleDefinition);

            console.WriteLine($"Validating file {fileName}...");

            Assembly smartContract = Assembly.Load(compilation);

            var serializer = new MethodParameterStringSerializer();

            object[] methodParameters = null;
            if (parameters.Length != 0)
            {
                methodParameters = serializer.Deserialize(parameters);
            }

            validationServiceResult.ConstructorExists = Contract.ConstructorExists(smartContract.ExportedTypes.FirstOrDefault(), methodParameters);

            if (!validationServiceResult.ConstructorExists)
            {
                console.WriteLine("Smart contract construction failed.");
                console.WriteLine("No constructor exists with the provided parameters.");
            }

            validationServiceResult.DeterminismValidationResult = new SctDeterminismValidator().Validate(moduleDefinition);
            validationServiceResult.FormatValidationResult      = new SmartContractFormatValidator().Validate(moduleDefinition.ModuleDefinition);
            if (!validationServiceResult.DeterminismValidationResult.IsValid || !validationServiceResult.FormatValidationResult.IsValid)
            {
                console.WriteLine("Smart Contract failed validation. Run validate [FILE] for more info.");
            }

            console.WriteLine();
        }
示例#7
0
        public IActionResult GetStorage([FromQuery] GetStorageRequest request)
        {
            if (!this.ModelState.IsValid)
            {
                this.logger.LogTrace("(-)[MODELSTATE_INVALID]");
                return(ModelStateErrors.BuildErrorResponse(this.ModelState));
            }

            var height = request.BlockHeight.HasValue ? request.BlockHeight.Value : (ulong)this.chainIndexer.Height;

            ChainedHeader chainedHeader = this.chainIndexer.GetHeader((int)height);

            var scHeader = chainedHeader?.Header as ISmartContractBlockHeader;

            IStateRepositoryRoot stateAtHeight = this.stateRoot.GetSnapshotTo(scHeader.HashStateRoot.ToBytes());

            uint160 addressNumeric = request.ContractAddress.ToUint160(this.network);

            byte[] storageValue = stateAtHeight.GetStorageValue(addressNumeric, Encoding.UTF8.GetBytes(request.StorageKey));

            if (storageValue == null)
            {
                return(this.NotFound(new
                {
                    Message = string.Format("No data at storage with key {0}", request.StorageKey)
                }));
            }

            // Interpret the storage bytes as an object of the given type
            object interpretedStorageValue = this.InterpretStorageValue(request.DataType, storageValue);

            // Use MethodParamStringSerializer to serialize the interpreted object to a string
            string serialized = MethodParameterStringSerializer.Serialize(interpretedStorageValue, this.network);

            return(this.Json(serialized));
        }