Exemple #1
0
 /// <summary>Gets a value.</summary>
 /// <typeparam name="T">The type of value.</typeparam>
 /// <typeparam name="K">The type of the keys.</typeparam>
 /// <param name="trie">The trie to get the value from.</param>
 /// <param name="stepper">The keys of the relative value.</param>
 /// <returns>The value.</returns>
 public static T Get <K, T>(this ITrie <K, T> trie, Stepper <K> stepper)
 {
     if (!trie.TryGet(stepper, out T value, out Exception exception))
     {
         throw exception;
     }
     return(value);
 }
Exemple #2
0
        public void Commit(int addressCount)
        {
            IKeyValueStore keyValueStore = new MemoryKeyValueStore();
            var            codec         = new Codec();

            ITrie trieA = new MerkleTrie(keyValueStore);

            var addresses = new Address[addressCount];
            var states    = new IValue[addressCount];

            for (int i = 0; i < addressCount; ++i)
            {
                addresses[i] = new PrivateKey().ToAddress();
                states[i]    = (Binary)TestUtils.GetRandomBytes(128);

                trieA = trieA.Set(addresses[i].ToByteArray(), states[i]);
            }

            byte[] path = TestUtils.GetRandomBytes(32);
            trieA = trieA.Set(path, (Text)"foo");
            HashDigest <SHA256> rootHash = trieA.Hash;

            Assert.True(trieA.TryGet(path, out IValue stateA));
            Assert.Equal((Text)"foo", stateA);

            ITrie trieB = trieA.Commit();

            Assert.True(trieB.TryGet(path, out IValue stateB));
            Assert.Equal((Text)"foo", stateB);

            trieB = trieB.Set(path, (Text)"bar");

            Assert.True(trieA.TryGet(path, out stateA));
            Assert.Equal((Text)"foo", stateA);
            Assert.True(trieB.TryGet(path, out stateB));
            Assert.Equal((Text)"bar", stateB);

            ITrie trieC = trieB.Commit();
            ITrie trieD = trieC.Commit();

            Assert.NotEqual(trieA.Hash, trieB.Hash);
            Assert.NotEqual(trieA.Hash, trieC.Hash);
            Assert.NotEqual(trieB.Hash, trieC.Hash);
            Assert.Equal(trieC.Hash, trieD.Hash);
        }
Exemple #3
0
 /// <summary>Gets a value.</summary>
 /// <typeparam name="T">The type of value.</typeparam>
 /// <typeparam name="TData">The type of the data.</typeparam>
 /// <param name="trie">The trie to get the value from.</param>
 /// <param name="stepper">The keys of the relative value.</param>
 /// <returns>The value.</returns>
 public static TData Get <T, TData>(this ITrie <T, TData> trie, Action <Action <T> > stepper)
 {
     var(success, value, exception) = trie.TryGet(stepper);
     if (!success)
     {
         throw exception ?? new ArgumentException($"{nameof(Get)} failed but the {nameof(exception)} is null");
     }
     return(value !);
 }
        public static IValue?GetState(
            this IStateStore stateStore,
            string rawStateKey,
            HashDigest <SHA256>?stateRootHash
            )
        {
            byte[] keyBytes = KeyEncoding.GetBytes(rawStateKey);
            ITrie  trie     = stateStore.GetStateRoot(stateRootHash);

            return(trie.TryGet(keyBytes, out IValue? value) ? value : null);
        }
Exemple #5
0
        public void GetStateRoot(bool secure)
        {
            var   stateStore = new TrieStateStore(_stateKeyValueStore, secure);
            ITrie empty      = stateStore.GetStateRoot(null);

            Assert.True(empty.Recorded);
            Assert.False(empty.TryGet(KeyFoo, out _));
            Assert.False(empty.TryGet(KeyBar, out _));
            Assert.False(empty.TryGet(KeyBaz, out _));
            Assert.False(empty.TryGet(KeyQux, out _));
            Assert.False(empty.TryGet(KeyQuux, out _));

            var values = ImmutableDictionary <string, IValue> .Empty
                         .Add("foo", (Binary)GetRandomBytes(32))
                         .Add("bar", (Text)ByteUtil.Hex(GetRandomBytes(32)))
                         .Add("baz", (Bencodex.Types.Boolean)false)
                         .Add("qux", Bencodex.Types.Dictionary.Empty);

            HashDigest <SHA256> hash = stateStore.Commit(null, values, rehearsal: true).Hash;

            ITrie notFound = stateStore.GetStateRoot(hash);

            Assert.False(notFound.Recorded);

            IValue value;

            stateStore.Commit(null, values, rehearsal: false);
            ITrie found = stateStore.GetStateRoot(hash);

            Assert.True(found.Recorded);
            Assert.True(found.TryGet(KeyFoo, out value));
            AssertBencodexEqual(values["foo"], value);
            Assert.True(found.TryGet(KeyBar, out value));
            AssertBencodexEqual(values["bar"], value);
            Assert.True(found.TryGet(KeyBaz, out value));
            AssertBencodexEqual(values["baz"], value);
            Assert.True(found.TryGet(KeyQux, out value));
            AssertBencodexEqual(values["qux"], value);
            Assert.False(empty.TryGet(KeyQuux, out _));
        }
Exemple #6
0
 /// <summary>Gets a value.</summary>
 /// <typeparam name="T">The type of value.</typeparam>
 /// <typeparam name="K">The type of the keys.</typeparam>
 /// <param name="trie">The trie to get the value from.</param>
 /// <param name="stepper">The keys of the relative value.</param>
 /// <returns>The value.</returns>
 public static T Get <K, T>(this ITrie <K, T> trie, Stepper <K> stepper) =>
 trie.TryGet(stepper, out T value, out Exception exception)
Exemple #7
0
 /// <summary>Tries to get a value.</summary>
 /// <typeparam name="T">The type of value.</typeparam>
 /// <typeparam name="K">The type of the keys.</typeparam>
 /// <param name="trie">The trie to get the value from.</param>
 /// <param name="stepper">The keys of the relative value.</param>
 /// <param name="value">The value if found.</param>
 /// <returns>True if the get was successful or false if not.</returns>
 public static bool TryGet <K, T>(this ITrie <K, T> trie, Stepper <K> stepper, out T value) =>
 trie.TryGet(stepper, out value, out _);