Exemplo n.º 1
0
        private void RunTest(TrieTest test, bool secure)
        {
            string permutationDescription =
                string.Join(Environment.NewLine, test.Input.Select(p => $"{p.Key} -> {p.Value}"));

            TestContext.WriteLine(Surrounded(permutationDescription));

            PatriciaTree patriciaTree = secure ? new SecurePatriciaTree(_db) : new PatriciaTree(_db, Keccak.EmptyTreeHash, false);

            foreach (KeyValuePair <string, string> keyValuePair in test.Input)
            {
                string keyString   = keyValuePair.Key;
                string valueString = keyValuePair.Value;

                Nibble[] key = keyString.StartsWith("0x")
                    ? Hex.ToNibbles(keyString)
                    : Nibbles.FromBytes(Encoding.ASCII.GetBytes(keyString));

                byte[] value = valueString.StartsWith("0x")
                    ? Hex.ToBytes(valueString)
                    : Encoding.ASCII.GetBytes(valueString);

                TestContext.WriteLine();
                TestContext.WriteLine($"Setting {keyString} -> {valueString}");
                patriciaTree.Set(key, value);
            }

            patriciaTree.UpdateRootHash();
            Assert.AreEqual(test.ExpectedRoot, patriciaTree.RootHash.ToString());
        }
Exemplo n.º 2
0
        private void RunTest(TrieTest test, bool secure)
        {
            if (secure)
            {
                // removed the implementation of secure trie as it was not used outside of tests
                return;
            }

            string permutationDescription =
                string.Join(Environment.NewLine, test.Input.Select(p => $"{p.Key} -> {p.Value}"));

            TestContext.WriteLine(Surrounded(permutationDescription));

            PatriciaTree patriciaTree = new PatriciaTree(_db, Keccak.EmptyTreeHash, false, true);

            foreach (KeyValuePair <string, string> keyValuePair in test.Input)
            {
                string keyString   = keyValuePair.Key;
                string valueString = keyValuePair.Value;

                Nibble[] key = keyString.StartsWith("0x")
                    ? Nibbles.FromHexString(keyString)
                    : Nibbles.FromBytes(Encoding.ASCII.GetBytes(keyString));

                byte[] value = valueString.StartsWith("0x")
                    ? Bytes.FromHexString(valueString)
                    : Encoding.ASCII.GetBytes(valueString);

                TestContext.WriteLine();
                TestContext.WriteLine($"Setting {keyString} -> {valueString}");
                patriciaTree.Set(key.ToPackedByteArray(), value);
            }

            patriciaTree.UpdateRootHash();
            Assert.AreEqual(test.ExpectedRoot, patriciaTree.RootHash.ToString());
        }
Exemplo n.º 3
0
 public void Test_hex_encoded_secure(TrieTest test)
 {
     RunTest(test, true);
 }
Exemplo n.º 4
0
 public void Test_any_order_secure(TrieTest test)
 {
     RunTest(test, true);
 }
Exemplo n.º 5
0
 public void Test_any_order(TrieTest test)
 {
     RunTest(test, false);
 }
Exemplo n.º 6
0
 public void Test_secure(TrieTest test)
 {
     RunTest(test, true);
 }
Exemplo n.º 7
0
 public void Test(TrieTest test)
 {
     RunTest(test, false);
 }