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)); }
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 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)); }