コード例 #1
0
        public void PruneStates(bool secure)
        {
            var values = ImmutableDictionary <string, IValue> .Empty
                         .Add("foo", (Binary)GetRandomBytes(4096))
                         .Add("bar", (Text)ByteUtil.Hex(GetRandomBytes(2048)))
                         .Add("baz", (Bencodex.Types.Boolean)false)
                         .Add("qux", Bencodex.Types.Dictionary.Empty)
                         .Add(
                "zzz",
                Bencodex.Types.Dictionary.Empty
                .Add("binary", GetRandomBytes(4096))
                .Add("text", ByteUtil.Hex(GetRandomBytes(2048))));

            var   stateStore = new TrieStateStore(_stateKeyValueStore, secure);
            ITrie first      = stateStore.Commit(null, values);

            int prevStatesCount = _stateKeyValueStore.ListKeys().Count();
            ImmutableDictionary <string, IValue> nextStates =
                values.SetItem("foo", (Binary)GetRandomBytes(4096));
            ITrie second = stateStore.Commit(first.Hash, nextStates);

            // foo = 0x666f6f
            // updated branch node (0x6, aka root) + updated branch node (0x66) +
            // updated short node + new value nodes
            Assert.Equal(prevStatesCount + 4, _stateKeyValueStore.ListKeys().Count());

            stateStore.PruneStates(ImmutableHashSet <HashDigest <SHA256> > .Empty.Add(second.Hash));

            // It will stay at the same count of nodes.
            // FIXME: Bencodex fingerprints also should be tracked.
            //        https://github.com/planetarium/libplanet/issues/1653
            Assert.Equal(prevStatesCount, _stateKeyValueStore.ListKeys().Count());
        }
コード例 #2
0
        public void PruneStates(bool secure)
        {
            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);

            var   stateStore = new TrieStateStore(_stateKeyValueStore, secure);
            ITrie first      = stateStore.Commit(null, values);

            int prevStatesCount = _stateKeyValueStore.ListKeys().Count();
            ImmutableDictionary <string, IValue> nextStates =
                values.SetItem("foo", (Binary)GetRandomBytes(32));
            ITrie second = stateStore.Commit(first.Hash, nextStates);

            // foo = 0x666f6f
            // updated branch node (0x6, aka root) + updated branch node (0x66) +
            // updated short node + new value node
            Assert.Equal(prevStatesCount + 4, _stateKeyValueStore.ListKeys().Count());

            stateStore.PruneStates(ImmutableHashSet <HashDigest <SHA256> > .Empty.Add(second.Hash));

            // It will stay at the same count of nodes.
            Assert.Equal(prevStatesCount, _stateKeyValueStore.ListKeys().Count());
        }
コード例 #3
0
        public void GetStateRoot(bool secure)
        {
            var   stateStore = new TrieStateStore(_stateKeyValueStore, secure);
            ITrie empty      = stateStore.GetStateRoot(null);

            Assert.True(empty.Recorded);
            Assert.Null(empty.Get(new[] { KeyFoo })[0]);
            Assert.Null(empty.Get(new[] { KeyBar })[0]);
            Assert.Null(empty.Get(new[] { KeyBaz })[0]);
            Assert.Null(empty.Get(new[] { KeyQux })[0]);
            Assert.Null(empty.Get(new[] { KeyQuux })[0]);

            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).Hash;
            ITrie found = stateStore.GetStateRoot(hash);

            Assert.True(found.Recorded);
            AssertBencodexEqual(values["foo"], found.Get(new[] { KeyFoo })[0]);
            AssertBencodexEqual(values["bar"], found.Get(new[] { KeyBar })[0]);
            AssertBencodexEqual(values["baz"], found.Get(new[] { KeyBaz })[0]);
            AssertBencodexEqual(values["qux"], found.Get(new[] { KeyQux })[0]);
            Assert.Null(found.Get(new[] { KeyQuux })[0]);
        }
コード例 #4
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 _));
        }
コード例 #5
0
        public void CopyStates(bool secure)
        {
            var values = ImmutableDictionary <string, IValue> .Empty
                         .Add("foo", (Binary)GetRandomBytes(4096))
                         .Add("bar", (Text)ByteUtil.Hex(GetRandomBytes(2048)))
                         .Add("baz", (Bencodex.Types.Boolean)false)
                         .Add("qux", Bencodex.Types.Dictionary.Empty)
                         .Add(
                "zzz",
                Bencodex.Types.Dictionary.Empty
                .Add("binary", GetRandomBytes(4096))
                .Add("text", ByteUtil.Hex(GetRandomBytes(2048))));

            var stateStore = new TrieStateStore(_stateKeyValueStore, secure);

            IKeyValueStore targetStateKeyValueStore = new MemoryKeyValueStore();
            var            targetStateStore         = new TrieStateStore(targetStateKeyValueStore, secure);
            ITrie          trie            = stateStore.Commit(null, values);
            int            prevStatesCount = _stateKeyValueStore.ListKeys().Count();

            _stateKeyValueStore.Set(
                new KeyBytes("alpha", Encoding.UTF8),
                ByteUtil.ParseHex("00"));
            _stateKeyValueStore.Set(
                new KeyBytes("beta", Encoding.UTF8),
                ByteUtil.ParseHex("00"));

            Assert.Equal(prevStatesCount + 2, _stateKeyValueStore.ListKeys().Count());
            Assert.Empty(targetStateKeyValueStore.ListKeys());

            stateStore.CopyStates(
                ImmutableHashSet <HashDigest <SHA256> > .Empty.Add(trie.Hash),
                targetStateStore);

            // It will stay at the same count of nodes.
            // FIXME: Bencodex fingerprints also should be tracked.
            //        https://github.com/planetarium/libplanet/issues/1653
            Assert.Equal(prevStatesCount, targetStateKeyValueStore.ListKeys().Count());
        }