コード例 #1
0
        public void Get_hash_code_is_consistent()
        {
            var wrappedA = Bytes32.Wrap(TestItem.KeccakA.Bytes);

            Bytes32.Zero.GetHashCode().Should().NotBe(wrappedA.GetHashCode());
            wrappedA.GetHashCode().Should().Be(wrappedA.GetHashCode());
        }
コード例 #2
0
        public void Some_is_not_null()
        {
            Bytes32 bytes32A = Bytes32.Wrap(TestItem.KeccakA.Bytes);

            bytes32A.Equals(null).Should().BeFalse();
            bytes32A.Equals((object)null).Should().BeFalse();
        }
コード例 #3
0
        public void Some_is_not_zero()
        {
            Bytes32 bytes32A = Bytes32.Wrap(TestItem.KeccakA.Bytes);

            bytes32A.Equals(Bytes32.Zero).Should().BeFalse();
            bytes32A.Equals((object)Bytes32.Zero).Should().BeFalse();
        }
コード例 #4
0
        public void Same_byte_arrays_give_equal_bytes32()
        {
            Bytes32 bytes32A = Bytes32.Wrap(TestItem.KeccakA.Bytes);
            Bytes32 bytes32B = Bytes32.Wrap(TestItem.KeccakA.Bytes);

            bytes32A.Should().Be(bytes32B);
        }
コード例 #5
0
        public void Equal_byte_arrays_give_equal_bytes32()
        {
            Bytes32 bytes32A = Bytes32.Wrap(TestItem.KeccakA.Bytes);
            Bytes32 bytes32B = Bytes32.Wrap((byte[])TestItem.KeccakA.Bytes.Clone());

            bytes32A.Should().Be(bytes32B);
        }
コード例 #6
0
 public void Setup()
 {
     for (int i = 0; i < _testLeaves.Length; i++)
     {
         byte[] bytes = new byte[32];
         bytes[i]       = (byte)(i + 1);
         _testLeaves[i] = Bytes32.Wrap(bytes);
     }
 }
コード例 #7
0
 public void GlobalSetup()
 {
     for (int i = 0; i < _bytes.Length; i++)
     {
         byte[] bytes = new byte[32];
         bytes[i % 32] = (byte)i;
         _bytes[i]     = Bytes32.Wrap(bytes);
     }
 }
コード例 #8
0
        public void On_creation_sets_the_fields_properly()
        {
            byte[] bytes = new byte[32];
            bytes[1] = 44;
            Bytes32        hash           = Bytes32.Wrap(bytes);
            MerkleTreeNode merkleTreeNode = new MerkleTreeNode(hash, 5);

            merkleTreeNode.Hash.Should().Be(hash);
            merkleTreeNode.Index.Should().Be(5);
        }
コード例 #9
0
        public Bytes32 Hash(ReadOnlySpan <byte> bytes)
        {
            byte[] result  = new byte[Bytes32.Length];
            bool   success = s_hashAlgorithm.TryComputeHash(bytes, result, out int bytesWritten);

            if (!success || bytesWritten != Bytes32.Length)
            {
                throw new Exception("Error generating hash value.");
            }
            return(Bytes32.Wrap(result));
        }
コード例 #10
0
        public bool Verify(Deposit deposit)
        {
            // TODO: need to be able to delete?
            // generally need to understand how the verification would work here and the current code at least
            // encapsulates deposits creation and verification

            Root    depositDataRoot = _crypto.HashTreeRoot(deposit.Data);
            Bytes32 rootBytes       = new Bytes32(depositDataRoot.AsSpan());

            VerificationData.Insert(Bytes32.Wrap(deposit.Data.Root.Bytes));
            bool isValid = VerificationData.VerifyProof(rootBytes, deposit.Proof, VerificationData.Count - 1);

            return(isValid);
        }
コード例 #11
0
        public Deposit Place(DepositData depositData)
        {
            Ref <DepositData> depositDataRef = depositData.OrRoot;
            Root    leaf      = _crypto.HashTreeRoot(depositDataRef);
            Bytes32 leafBytes = Bytes32.Wrap(leaf.Bytes);

            DepositData.Insert(leafBytes);

            var proof = DepositData.GetProof(DepositData.Count - 1);

            Deposit deposit = new Deposit(proof, depositDataRef);

            Deposits.Add(deposit);
            return(deposit);
        }
コード例 #12
0
        private BaselineTree RebuildEntireTree(Address treeAddress)
        {
            // bad

            Keccak    leavesTopic        = new Keccak("0x8ec50f97970775682a68d3c6f9caedf60fd82448ea40706b8b65d6c03648b922");
            LogFilter insertLeavesFilter = new LogFilter(
                0,
                new BlockParameter(0L),
                new BlockParameter(_blockFinder.Head.Number),
                new AddressFilter(treeAddress),
                new TopicsFilter(new SpecificTopic(leavesTopic)));

            Keccak    leafTopic        = new Keccak("0x6a82ba2aa1d2c039c41e6e2b5a5a1090d09906f060d32af9c1ac0beff7af75c0");
            LogFilter insertLeafFilter = new LogFilter(
                0,
                new BlockParameter(0L),
                new BlockParameter(_blockFinder.Head.Number),
                new AddressFilter(treeAddress),
                new TopicsFilter(new SpecificTopic(leafTopic))); // find tree topics

            var          insertLeavesLogs = _logFinder.FindLogs(insertLeavesFilter);
            var          insertLeafLogs   = _logFinder.FindLogs(insertLeafFilter);
            BaselineTree baselineTree     = new ShaBaselineTree(new MemDb(), new byte[0], 5);

            // Keccak leafTopic = new Keccak("0x8ec50f97970775682a68d3c6f9caedf60fd82448ea40706b8b65d6c03648b922");
            foreach (FilterLog filterLog in insertLeavesLogs)
            {
                for (int i = 0; i < (filterLog.Data.Length - 128) / 32; i++)
                {
                    Bytes32 leafHash = Bytes32.Wrap(filterLog.Data.Slice(128 + 32 * i, 32).ToArray());
                    baselineTree.Insert(leafHash);
                }
            }

            foreach (FilterLog filterLog in insertLeafLogs)
            {
                Bytes32 leafHash = Bytes32.Wrap(filterLog.Data.Slice(32, 32).ToArray());
                baselineTree.Insert(leafHash);
            }

            return(baselineTree);
        }
コード例 #13
0
        public void Can_get_proof_on_a_populated_trie_on_an_index(uint nodesCount)
        {
            MerkleTree merkleTree = new ShaMerkleTree(new MemDb());

            for (int i = 0; i < nodesCount; i++)
            {
                merkleTree.Insert(_testLeaves[0]);
            }

            MerkleTreeNode[] proof = merkleTree.GetProof(0);
            proof.Should().HaveCount(MerkleTree.TreeDepth - 1);

            for (int proofLevel = 0; proofLevel < MerkleTree.TreeDepth - 1; proofLevel++)
            {
                if (nodesCount > 1 >> proofLevel)
                {
                    proof[proofLevel].Should().NotBe(ShaMerkleTree.ZeroHashes[proofLevel], proofLevel.ToString());
                }
                else
                {
                    proof[proofLevel].Hash.Should().Be(Bytes32.Wrap(ShaMerkleTree.ZeroHashes[proofLevel]), proofLevel.ToString());
                }
            }
        }
コード例 #14
0
        public void Different_type_is_not_equal()
        {
            Bytes32 bytes32A = Bytes32.Wrap(TestItem.KeccakA.Bytes);

            bytes32A.Equals(TestItem.KeccakA.Bytes).Should().BeFalse();
        }
コード例 #15
0
        public void Same_is_equal()
        {
            Bytes32 bytes32A = Bytes32.Wrap(TestItem.KeccakA.Bytes);

            bytes32A.Equals(bytes32A).Should().BeTrue();
        }
コード例 #16
0
 public void Invalid_length_throws(int length)
 {
     Assert.Throws <ArgumentException>(() => Bytes32.Wrap(new byte[length]));
 }