public bool TryGetValue(ScopedStatePath key, out byte[] value) { var found = _blockStateSet.Changes.TryGetValue(key.ToStateKey(), out var bs); value = found ? bs.ToByteArray() : null; return(found); }
public async Task GetStateAsync_Test() { var chain = await _smartContractHelper.CreateChainAsync(); await Assert.ThrowsAsync <InvalidOperationException>(() => _smartContractBridgeService.GetStateAsync( SampleAddress.AddressList[0], string.Empty, chain.BestChainHeight, chain.BestChainHash)); var scopedStatePath = new ScopedStatePath { Address = SampleAddress.AddressList[0], Path = new StatePath { Parts = { "part" } } }; var state = await _smartContractBridgeService.GetStateAsync(SampleAddress.AddressList[0], scopedStatePath.ToStateKey(), chain.BestChainHeight, chain.BestChainHash); state.ShouldBeNull(); var blockStateSet = await _blockStateSetManger.GetBlockStateSetAsync(chain.BestChainHash); blockStateSet.Changes[scopedStatePath.ToStateKey()] = ByteString.Empty; await _blockStateSetManger.SetBlockStateSetAsync(blockStateSet); state = await _smartContractBridgeService.GetStateAsync(SampleAddress.AddressList[0], scopedStatePath.ToStateKey(), chain.BestChainHeight, chain.BestChainHash); state.ShouldBe(ByteString.Empty); }
public async Task <byte[]> GetAsync(StatePath path) { var scoped = new ScopedStatePath() { Address = ContractAddress, Path = path }; var byteString = await HostSmartContractBridgeContext.GetStateAsync(scoped.ToStateKey()); return(byteString?.ToByteArray()); }
public byte[] Get(StatePath path) { var scoped = new ScopedStatePath() { Address = ContractAddress, Path = path }; var byteString = AsyncHelper.RunSync(() => HostSmartContractBridgeContext.GetStateAsync(scoped.ToStateKey())); return(byteString?.ToByteArray()); }
public bool TryGetValue(ScopedStatePath key, out byte[] value) { var originalFound = TryGetOriginalValue(key, out value); var currentFound = _currentValues.TryGetValue(key.ToStateKey(), out var currentValue); if (currentFound) { value = currentValue; } return(originalFound || currentFound); }
private async Task <CachedStateProvider> GenerateStateProviderAsync() { var chain = await _smartContractHelper.CreateChainAsync(); var smartContractBridgeContext = _hostSmartContractBridgeContextService.Create(); smartContractBridgeContext.TransactionContext = new TransactionContext { Transaction = new Transaction { To = SampleAddress.AddressList[0] }, BlockHeight = chain.BestChainHeight, PreviousBlockHash = chain.BestChainHash }; _innerProvider.ContractAddress = SampleAddress.AddressList[0]; _innerProvider.HostSmartContractBridgeContext = smartContractBridgeContext; var statePath = new ScopedStatePath { Address = _innerProvider.ContractAddress, Path = new StatePath { Parts = { "test2" } } }; await _blockStateSetManger.SetBlockStateSetAsync(new BlockStateSet { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight, Changes = { { statePath.ToStateKey(), ByteString.CopyFrom(1, 2, 3, 4) } } }); var cachedStateProvider = new CachedStateProvider(_innerProvider); var cacheProvider = cachedStateProvider as CachedStateProvider; cacheProvider.Cache[new ScopedStatePath { Address = _innerProvider.ContractAddress, Path = new StatePath { Parts = { "test1" } } }] = new byte[] { 0, 1, 2, 3 }; return(cachedStateProvider); }
public bool TryGetValue(ScopedStatePath key, out byte[] value) { var stateKey = key.ToStateKey(); var found = _blockStateSet.Deletes.Contains(stateKey); if (found) { value = null; return(true); } found = _blockStateSet.Changes.TryGetValue(stateKey, out var bs); value = found ? bs.ToByteArray() : null; return(found); }
public bool TryGetValue(ScopedStatePath key, out byte[] value) { // if the original value doesn't exist, then the state is not in the cache if (!TryGetOriginalValue(key, out value)) { return(false); } // the original value was found, check if the value is changed if (_currentValues.TryGetValue(key.ToStateKey(), out var currentValue)) { value = currentValue; } return(true); }
public void TryGetValue_Test() { var stateCache = new NullStateCache(); var scopedStatePath = new ScopedStatePath { Address = SampleAddress.AddressList[0], Path = new StatePath() }; stateCache.TryGetValue(scopedStatePath, out var value).ShouldBeFalse(); value.ShouldBeNull(); stateCache[scopedStatePath].ShouldBeNull(); stateCache[scopedStatePath] = new byte[0]; stateCache[scopedStatePath].ShouldBeNull(); }
public void TieredStateCache_Test() { var path = new ScopedStatePath { Address = SampleAddress.AddressList[2], Path = new StatePath() }; var cache = new TieredStateCache(); cache.TryGetValue(path, out var value).ShouldBeFalse(); value.ShouldBeNull(); cache[path].ShouldBeNull(); cache.Update(GetTransactionExecutingStateSets()); cache.TryGetValue(FirstPath, out var firstValue).ShouldBeTrue(); firstValue.ShouldBeNull(); cache.TryGetValue(SecondPath, out var secondValue).ShouldBeTrue(); secondValue.ShouldBe(SecondValue.ToByteArray()); var stateCacheFromPartialBlockStateSet = new StateCacheFromPartialBlockStateSet(new BlockStateSet { Changes = { { SecondPath.ToStateKey(), ThirdValue } } }); cache = new TieredStateCache(stateCacheFromPartialBlockStateSet); cache.TryGetValue(SecondPath, out secondValue).ShouldBeTrue(); secondValue.ShouldBe(ThirdValue.ToByteArray()); cache[SecondPath] = FirstValue.ToByteArray(); cache[SecondPath].ShouldBe(FirstValue.ToByteArray()); cache.TryGetValue(SecondPath, out secondValue); secondValue.ShouldBe(FirstValue.ToByteArray()); cache.Update(GetTransactionExecutingStateSets()); cache.TryGetValue(SecondPath, out secondValue); secondValue.ShouldBe(SecondValue.ToByteArray()); stateCacheFromPartialBlockStateSet = new StateCacheFromPartialBlockStateSet(new BlockStateSet { Changes = { { SecondPath.ToStateKey(), ThirdValue } } }); cache.Update(GetTransactionExecutingStateSets()); cache.TryGetValue(SecondPath, out secondValue); secondValue.ShouldBe(SecondValue.ToByteArray()); }
public async Task <byte[]> GetAsync(StatePath path) { var scoped = new ScopedStatePath() { Address = ContractAddress, Path = path }; if (Cache.TryGetValue(scoped, out var value)) { return(value); } var bytes = await _inner.GetAsync(path); Cache[scoped] = bytes; return(bytes); }
public byte[] Get(StatePath path) { var scoped = new ScopedStatePath() { Address = ContractAddress, Path = path }; if (Cache.TryGetValue(scoped, out var value)) { return(value); } var bytes = _inner.Get(path); Cache[scoped] = bytes; return(bytes); }
public void StateCacheFromPartialBlockStateSet_Test() { var path = new StatePath(); var deleteStatePath = new ScopedStatePath { Address = SampleAddress.AddressList[0], Path = path }; var changeStatePath = new ScopedStatePath { Address = SampleAddress.AddressList[1], Path = path }; var blockStateSet = new BlockStateSet { Deletes = { deleteStatePath.ToStateKey() }, Changes = { { changeStatePath.ToStateKey(), ByteString.Empty } }, }; var stateCacheFromPartialBlockStateSet = new StateCacheFromPartialBlockStateSet(blockStateSet); var notExistStatePath = new ScopedStatePath { Address = SampleAddress.AddressList[2], Path = path }; stateCacheFromPartialBlockStateSet.TryGetValue(deleteStatePath, out var deletedValue).ShouldBeTrue(); deletedValue.ShouldBeNull(); stateCacheFromPartialBlockStateSet[deleteStatePath].ShouldBeNull(); stateCacheFromPartialBlockStateSet.TryGetValue(changeStatePath, out var changeValue).ShouldBeTrue(); changeValue.ShouldBe(ByteString.Empty); stateCacheFromPartialBlockStateSet[changeStatePath].ShouldBe(ByteString.Empty.ToByteArray()); stateCacheFromPartialBlockStateSet.TryGetValue(notExistStatePath, out var value).ShouldBeFalse(); value.ShouldBeNull(); stateCacheFromPartialBlockStateSet[notExistStatePath].ShouldBeNull(); stateCacheFromPartialBlockStateSet[notExistStatePath] = ByteString.Empty.ToByteArray(); stateCacheFromPartialBlockStateSet[notExistStatePath].ShouldBeNull(); }
public void StateKeyExtensions_ToStateKey_Test() { var statePath = new StatePath { Parts = { "1", "2", "3" } }; var privateKey = ByteArrayHelper.HexStringToByteArray("5945c176c4269dc2aa7daf7078bc63b952832e880da66e5f2237cdf79bc59c5f"); var keyPair = CryptoHelper.FromPrivateKey(privateKey); var address = Address.FromPublicKey(keyPair.PublicKey); var addressBase58String = address.ToBase58(); var targetKeyString = string.Join("/", new[] { addressBase58String }.Concat(statePath.Parts)); var stateKey1 = statePath.ToStateKey(address); var scopedStatePath = new ScopedStatePath { Path = statePath, Address = address }; var stateKey2 = scopedStatePath.ToStateKey(); stateKey1.ShouldBe(targetKeyString); stateKey2.ShouldBe(stateKey1); }
public byte[] this[ScopedStatePath key] { get => TryGetValue(key, out var value) ? value : null;
public byte[] this[ScopedStatePath key] { get => null;
public bool TryGetValue(ScopedStatePath key, out byte[] value) { value = null; return(false); }
public bool TryGetValue(ScopedStatePath key, out byte[] value) { return(_data.TryGetValue(key, out value)); }
public static string ToStateKey(this ScopedStatePath scopedStatePath) { return(string.Join("/", new[] { scopedStatePath.Address.ToBase58() }.Concat(scopedStatePath.Path.Parts))); }