Пример #1
0
        internal AccountProofCollector(byte[] hashedAddress, params byte[][] storageKeys)
        {
            storageKeys ??= new byte[0][];
            _fullAccountPath = Nibbles.FromBytes(hashedAddress);

            Keccak[] localStorageKeys = storageKeys.Select(ToKey).ToArray();

            _accountProof = new AccountProof();
            _accountProof.StorageProofs = new StorageProof[localStorageKeys.Length];
            _accountProof.Address       = _address;

            _fullStoragePaths  = new Nibble[localStorageKeys.Length][];
            _storageProofItems = new List <byte[]> [localStorageKeys.Length];
            for (int i = 0; i < _storageProofItems.Length; i++)
            {
                _storageProofItems[i] = new List <byte[]>();
            }

            for (int i = 0; i < localStorageKeys.Length; i++)
            {
                _fullStoragePaths[i] = Nibbles.FromBytes(localStorageKeys[i].Bytes);

                _accountProof.StorageProofs[i]       = new StorageProof();
                _accountProof.StorageProofs[i].Key   = storageKeys[i];
                _accountProof.StorageProofs[i].Value = null;
            }
        }
Пример #2
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());
        }
Пример #3
0
        public ProofCollector(Address address, Keccak[] storageKeys)
        {
            _address     = address ?? throw new ArgumentNullException(nameof(address));
            _storageKeys = storageKeys ?? new Keccak[0];

            _accountProof = new AccountProof();
            _accountProof.StorageProofs = new StorageProof[_storageKeys.Length];

            _storagePrefixes  = new Nibble[_storageKeys.Length][];
            _storageProofBits = new List <byte[]> [_storageKeys.Length];
            for (int i = 0; i < _storageProofBits.Length; i++)
            {
                _storageProofBits[i] = new List <byte[]>();
            }

            for (int i = 0; i < _storageKeys.Length; i++)
            {
                _storagePrefixes[i] = Nibbles.FromBytes(_storageKeys[i].Bytes);

                _accountProof.StorageProofs[i]     = new StorageProof();
                _accountProof.StorageProofs[i].Key = _storageKeys[i];
            }
        }
Пример #4
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());
        }