1
using System.Linq;
2
using System.Security.Cryptography;
3
using Bencodex.Types;
4
using Libplanet.Store.Trie;
5
using Libplanet.Store.Trie.Nodes;
6
using Xunit;
7

8
namespace Libplanet.Tests.Store.Trie
9
{
10
    public class MerkleTrieTest
11
    {
12
        [Fact]
13
        public void ConstructWithHashDigest()
14 1
        {
15 1
            var hashDigest
16 1
                = new HashDigest<SHA256>(TestUtils.GetRandomBytes(HashDigest<SHA256>.Size));
17 1
            var merkleTrie = new MerkleTrie(new MemoryKeyValueStore(), hashDigest);
18 1
            Assert.Equal(hashDigest, merkleTrie.Hash);
19

20
            // See https://github.com/planetarium/libplanet/pull/1091
21 1
            merkleTrie = new MerkleTrie(new MemoryKeyValueStore(), MerkleTrie.EmptyRootHash);
22 1
            Assert.Null(merkleTrie.Root);
23 1
        }
24

25
        [Fact]
26
        public void ConstructWithRootNode()
27 1
        {
28 1
            var hashDigest
29 1
                = new HashDigest<SHA256>(TestUtils.GetRandomBytes(HashDigest<SHA256>.Size));
30 1
            INode rootNode = new HashNode(hashDigest);
31 1
            var merkleTrie = new MerkleTrie(new MemoryKeyValueStore(), rootNode);
32 1
            Assert.Equal(hashDigest, merkleTrie.Hash);
33 1
        }
34

35
        [Fact]
36
        public void IterateNodes()
37 1
        {
38 1
            var merkleTrie = new MerkleTrie(new MemoryKeyValueStore());
39
            // There is nothing.
40 1
            Assert.Empty(merkleTrie.IterateNodes());
41

42 1
            merkleTrie = (MerkleTrie)merkleTrie.Set(
43 1
                new byte[] { 0xbe, 0xef, },
44 1
                Dictionary.Empty.Add(TestUtils.GetRandomBytes(32), default(Null)));
45
            // There are (ShortNode, ValueNode)
46 1
            Assert.Equal(2, merkleTrie.IterateNodes().Count());
47

48 1
            merkleTrie = (MerkleTrie)merkleTrie.Commit();
49
            // There are (HashNode, ShortNode, HashNode, ValueNode)
50 1
            Assert.Equal(4, merkleTrie.IterateNodes().Count());
51 1
        }
52
    }
53
}

Read our documentation on viewing source code .

Loading