public void StateValue_Test() { var hashArray = Hash.FromString("hash").ToByteArray(); var stateValue = StateValue.Create(hashArray); var isDirty = stateValue.IsDirty; isDirty.ShouldBeFalse(); var hashArray1 = stateValue.Get(); hashArray.ShouldBe(hashArray1); var hashArray2 = Hash.FromString("hash1").ToByteArray(); stateValue.Set(hashArray2); isDirty = stateValue.IsDirty; isDirty.ShouldBeTrue(); var hashArray3 = stateValue.Get(); hashArray3.ShouldBe(hashArray2); stateValue.OriginalValue.ShouldBe(hashArray); stateValue.CurrentValue.ShouldBe(hashArray2); }
public async Task <JObject> GetDbValue(string key) { string type = string.Empty; JToken id; try { object value; if (key.StartsWith(GlobalConfig.StatePrefix)) { type = "State"; id = key.Substring(GlobalConfig.StatePrefix.Length, key.Length - GlobalConfig.StatePrefix.Length); var valueBytes = KeyValueDatabase.GetAsync(type, key).Result; value = StateValue.Create(valueBytes); } else if (key.StartsWith(GlobalConfig.TransactionReceiptPrefix)) { type = "TransactionReceipt"; id = key.Substring(GlobalConfig.TransactionReceiptPrefix.Length, key.Length - GlobalConfig.TransactionReceiptPrefix.Length); var valueBytes = KeyValueDatabase.GetAsync(type, key).Result; value = valueBytes?.Deserialize <TransactionReceipt>(); } else { var keyObj = Key.Parser.ParseFrom(ByteArrayHelpers.FromHexString(key)); type = keyObj.Type; id = JObject.Parse(keyObj.ToString()); var valueBytes = KeyValueDatabase.GetAsync(type, key).Result; var obj = this.GetInstance(type); obj.MergeFrom(valueBytes); value = obj; } var response = new JObject { ["Type"] = type, ["Id"] = id, ["Value"] = JObject.Parse(value.ToString()) }; return(JObject.FromObject(response)); } catch (Exception e) { var response = new JObject { ["Type"] = type, ["Value"] = e.Message }; return(JObject.FromObject(response)); } }
public async Task <byte[]> GetAsync(string key) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (_cache.TryGetValue(key, out var value)) { return(value.Get()); } var bytes = await _stateStore.GetAsync(GetStatePathFor(key)); _cache[key] = StateValue.Create(bytes); return(bytes); }