Exemplo n.º 1
0
        public void DifferentNodes()
        {
            IKeyValueStore keyValueStore = new MemoryKeyValueStore();
            MerkleTrie     trieA         = new MerkleTrie(keyValueStore),
                           trieB         = new MerkleTrie(keyValueStore);

            trieA = (MerkleTrie)trieA.Set(new KeyBytes(0x01), Null.Value)
                    .Set(new KeyBytes(0x02), Null.Value)
                    .Set(new KeyBytes(0x03), Null.Value)
                    .Commit();
            trieB = (MerkleTrie)trieB.Set(new KeyBytes(0x01), Dictionary.Empty)
                    .Set(new KeyBytes(0x02), Null.Value)
                    .Set(new KeyBytes(0x04), Null.Value)
                    .Commit();

            Dictionary <KeyBytes, (IValue OriginValue, IValue OtherValue)> differentNodes =
                trieA.DifferentNodes(trieB).ToDictionary(
                    diff => diff.Key,
                    diff => (diff.OriginValue, diff.OtherValue));

            Assert.Equal(2, differentNodes.Count);
            Assert.NotNull(differentNodes[new KeyBytes(1)].OtherValue);
            Assert.False(differentNodes.ContainsKey(new KeyBytes(2)));
            Assert.Null(differentNodes[new KeyBytes(3)].OtherValue);
            Assert.False(differentNodes.ContainsKey(new KeyBytes(4)));
        }
Exemplo n.º 2
0
        public void Diff(
            [Argument(
                 Name = "KV-STORE",
                 Description = KVStoreArgumentDescription)]
            string kvStoreUri,
            [Argument(
                 Name = "STATE-ROOT-HASH",
                 Description = "The state root hash to compare.")]
            string stateRootHashHex,
            [Argument(
                 Name = "OTHER-KV-STORE",
                 Description = KVStoreArgumentDescription)]
            string otherKvStoreUri,
            [Argument(
                 Name = "OTHER-STATE-ROOT-HASH",
                 Description = "Another state root hash to compare.")]
            string otherStateRootHashHex,
            [FromService] IConfigurationService <ToolConfiguration> configurationService)
        {
            ToolConfiguration toolConfiguration = configurationService.Load();

            kvStoreUri      = ConvertKVStoreUri(kvStoreUri, toolConfiguration);
            otherKvStoreUri = ConvertKVStoreUri(otherKvStoreUri, toolConfiguration);

            IKeyValueStore keyValueStore      = LoadKVStoreFromURI(kvStoreUri);
            IKeyValueStore otherKeyValueStore = LoadKVStoreFromURI(otherKvStoreUri);
            var            trie = new MerkleTrie(
                keyValueStore,
                HashDigest <SHA256> .FromString(stateRootHashHex));
            var otherTrie = new MerkleTrie(
                otherKeyValueStore,
                HashDigest <SHA256> .FromString(otherStateRootHashHex));

            foreach (var group in trie.DifferentNodes(otherTrie))
            {
                Console.Error.Write("Path: ");
                Console.WriteLine(Encoding.UTF8.GetString(ByteUtil.ParseHex(group.Key)));
                var values = group.ToArray();
                foreach (var pair in values)
                {
                    Console.Error.Write("At ");
                    Console.WriteLine(ByteUtil.Hex(pair.Root.ToByteArray()));
                    Console.WriteLine(pair.Value.Inspection);
                }

                Console.WriteLine();
            }
        }
Exemplo n.º 3
0
        public void Diff(
            [Argument(
                 Name = "KV-STORE",
                 Description = KVStoreArgumentDescription)]
            string kvStoreUri,
            [Argument(
                 Name = "STATE-ROOT-HASH",
                 Description = "The state root hash to compare.")]
            string stateRootHashHex,
            [Argument(
                 Name = "OTHER-KV-STORE",
                 Description = KVStoreArgumentDescription)]
            string otherKvStoreUri,
            [Argument(
                 Name = "OTHER-STATE-ROOT-HASH",
                 Description = "Another state root hash to compare.")]
            string otherStateRootHashHex,
            [FromService] IConfigurationService <ToolConfiguration> configurationService)
        {
            ToolConfiguration toolConfiguration = configurationService.Load();

            kvStoreUri      = ConvertKVStoreUri(kvStoreUri, toolConfiguration);
            otherKvStoreUri = ConvertKVStoreUri(otherKvStoreUri, toolConfiguration);

            IKeyValueStore keyValueStore      = LoadKVStoreFromURI(kvStoreUri);
            IKeyValueStore otherKeyValueStore = LoadKVStoreFromURI(otherKvStoreUri);
            var            trie = new MerkleTrie(
                keyValueStore,
                HashDigest <SHA256> .FromString(stateRootHashHex));
            var otherTrie = new MerkleTrie(
                otherKeyValueStore,
                HashDigest <SHA256> .FromString(otherStateRootHashHex));

            var codec = new Codec();
            HashDigest <SHA256> originRootHash = trie.Hash;
            HashDigest <SHA256> otherRootHash  = otherTrie.Hash;

            string originRootHashHex = ByteUtil.Hex(originRootHash.ByteArray);
            string otherRootHashHex  = ByteUtil.Hex(otherRootHash.ByteArray);

            foreach (var(key, originValue, otherValue) in trie.DifferentNodes(otherTrie))
            {
                var data = new DiffData(ByteUtil.Hex(key.ByteArray), new Dictionary <string, string>
                {
Exemplo n.º 4
0
        public void Diff(
            [Argument(
                 Name = "KV-STORE",
                 Description = KVStoreArgumentDescription)]
            string kvStoreUri,
            [Argument(
                 Name = "STATE-ROOT-HASH",
                 Description = "The state root hash to compare.")]
            string stateRootHashHex,
            [Argument(
                 Name = "OTHER-KV-STORE",
                 Description = KVStoreArgumentDescription)]
            string otherKvStoreUri,
            [Argument(
                 Name = "OTHER-STATE-ROOT-HASH",
                 Description = "Another state root hash to compare.")]
            string otherStateRootHashHex,
            [FromService] IConfigurationService <ToolConfiguration> configurationService)
        {
            ToolConfiguration toolConfiguration = configurationService.Load();

            kvStoreUri      = ConvertKVStoreUri(kvStoreUri, toolConfiguration);
            otherKvStoreUri = ConvertKVStoreUri(otherKvStoreUri, toolConfiguration);

            IKeyValueStore keyValueStore      = LoadKVStoreFromURI(kvStoreUri);
            IKeyValueStore otherKeyValueStore = LoadKVStoreFromURI(otherKvStoreUri);
            var            trie = new MerkleTrie(
                keyValueStore,
                HashDigest <SHA256> .FromString(stateRootHashHex));
            var otherTrie = new MerkleTrie(
                otherKeyValueStore,
                HashDigest <SHA256> .FromString(otherStateRootHashHex));

            var codec = new Codec();
            Dictionary <string, Dictionary <string, string> > dictionary =
                trie.DifferentNodes(otherTrie).ToDictionary(
                    group => group.Key,
                    group => group.ToDictionary(
                        pair => ByteUtil.Hex(pair.Root.ByteArray),
                        pair => ByteUtil.Hex(codec.Encode(pair.Value))));

            Console.Write(JsonSerializer.Serialize(dictionary));
        }