Example #1
0
File: Block.cs Project: wyk125/AElf
        /// <summary>
        /// Add transaction Hash to the block
        /// </summary>
        /// <returns><c>true</c>, if the hash was added, <c>false</c> otherwise.</returns>
        /// <param name="tx">the transactions hash</param>
        public bool AddTransaction(Transaction tx)
        {
            if (Body == null)
            {
                Body = new BlockBody();
            }

            return(Body.AddTransaction(tx));
        }
Example #2
0
File: Block.cs Project: wyk125/AElf
        /// <summary>
        /// Add transaction Hashes to the block
        /// </summary>
        /// <returns><c>true</c>, if the hash was added, <c>false</c> otherwise.</returns>
        /// <param name="txs">the transactions hash</param>
        public bool AddTransactions(IEnumerable <Hash> txs)
        {
            if (Body == null)
            {
                Body = new BlockBody();
            }

            return(Body.AddTransactions(txs));
        }
        /// <summary>
        /// Calculate merkle tree root of transaction.
        /// </summary>
        /// <returns></returns>
        public static Hash CalculateMerkleTreeRoot(this BlockBody blockBody)
        {
            if (blockBody.TransactionsCount == 0)
            {
                return(Hash.Empty);
            }
            var merkleTreeRoot = BinaryMerkleTree.FromLeafNodes(blockBody.TransactionIds).Root;

            return(merkleTreeRoot);
        }
Example #4
0
        /// <summary>
        /// Calculate merkle tree root of transaction and side chain block info.
        /// </summary>
        /// <returns></returns>
        public static Hash CalculateMerkleTreeRoots(this BlockBody blockBody)
        {
            // side chain info
            if (blockBody.TransactionsCount == 0)
            {
                return(Hash.Empty);
            }
            if (blockBody.BinaryMerkleTree.Root != null)
            {
                return(blockBody.BinaryMerkleTree.Root);
            }
            blockBody.BinaryMerkleTree.AddNodes(blockBody.Transactions);
            blockBody.BinaryMerkleTree.ComputeRootHash();

            return(blockBody.BinaryMerkleTree.Root);
        }
        public void BlockBody_Test()
        {
            var blockBody = new BlockBody();

            blockBody.CalculateMerkleTreeRoot().ShouldBe(Hash.Empty);

            var transaction1 = _kernelTestHelper.GenerateTransaction();
            var transaction2 = _kernelTestHelper.GenerateTransaction();
            var transaction3 = _kernelTestHelper.GenerateTransaction();

            blockBody.AddTransaction(transaction1);
            blockBody.TransactionIds.Count.ShouldBe(1);
            blockBody.TransactionIds.ShouldContain(transaction1.GetHash());

            blockBody.AddTransactions(new[] { transaction2.GetHash(), transaction3.GetHash() });
            blockBody.TransactionIds.Count.ShouldBe(3);
            blockBody.TransactionIds.ShouldContain(transaction2.GetHash());
            blockBody.TransactionIds.ShouldContain(transaction3.GetHash());

            blockBody.CalculateMerkleTreeRoot().ShouldBe(BinaryMerkleTree.FromLeafNodes(blockBody.TransactionIds).Root);
        }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:AElf.Kernel.Block"/> class.
 /// A previous block must be referred, except for the genesis block.
 /// </summary>
 /// <param name="preBlockHash">Pre block hash.</param>
 public Block(Hash preBlockHash)
 {
     Header = new BlockHeader(preBlockHash);
     Body   = new BlockBody();
 }
 public static bool AddTransactions(this BlockBody blockBody, IEnumerable <Hash> txs)
 {
     blockBody.TransactionIds.Add(txs);
     return(true);
 }
 public static bool AddTransaction(this BlockBody blockBody, Transaction tx)
 {
     blockBody.TransactionIds.Add(tx.GetHash());
     return(true);
 }