Exemplo n.º 1
0
        public void GetWithHigestFee()
        {
            var pool = new TransactionPool();
            var tx1  = new Transaction()
            {
                fee = 1
            };
            var tx2 = new Transaction()
            {
                fee = 2
            };
            var tx3 = new Transaction()
            {
                fee = 3
            };
            var tx4 = new Transaction()
            {
                fee = 4
            };

            pool.AddTransaction(tx1);
            pool.AddTransaction(tx2);
            pool.AddTransaction(tx3);
            pool.AddTransaction(tx4);

            var txs = pool.GetTransactionsWithHighestFee(3);

            Assert.AreEqual(3, txs.Length);
            Assert.AreEqual(4, txs[0].fee);
            Assert.AreEqual(3, txs[1].fee);
            Assert.AreEqual(2, txs[2].fee);
        }
Exemplo n.º 2
0
        public void CannotAddDuplicatedTransaction()
        {
            var pool = new TransactionPool();
            var tx   = new Transaction()
            {
            };

            pool.AddTransaction(tx);
            pool.AddTransaction(tx);
            pool.AddTransaction(tx);

            Assert.AreEqual(pool.count, 1);
        }
        public void AddTransactionTwice()
        {
            var sender   = new Address();
            var receiver = new Address();

            Transaction transaction = new Transaction(sender, receiver, new BigInteger(100));

            TransactionPool pool = new TransactionPool();

            pool.AddTransaction(transaction);
            pool.AddTransaction(transaction);

            Assert.AreEqual(1, pool.Transactions.Count);
            Assert.IsTrue(pool.Transactions.Contains(transaction));
        }
Exemplo n.º 4
0
        private void Arrange()
        {
            var txn1 = new ClaimSettlement("ABC123", 1000.00m, DateTime.Now, "QWE123", 10000, ClaimType.TotalLoss);
            var txn2 = new ClaimSettlement("VBG345", 2000.00m, DateTime.Now, "JKH567", 20000, ClaimType.TotalLoss);
            var txn3 = new ClaimSettlement("XCF234", 3000.00m, DateTime.Now, "DH23ED", 30000, ClaimType.TotalLoss);
            var txn4 = new ClaimSettlement("CBHD45", 4000.00m, DateTime.Now, "DH34K6", 40000, ClaimType.TotalLoss);

            txnPool.AddTransaction(txn1);
            txnPool.AddTransaction(txn2);
            txnPool.AddTransaction(txn3);
            txnPool.AddTransaction(txn4);

            block1.Add(txnPool.GetTransaction());
            block1.Add(txnPool.GetTransaction());

            block2.Add(txnPool.GetTransaction());
            block2.Add(txnPool.GetTransaction());
        }
Exemplo n.º 5
0
        public void CanUpdateTransaction()
        {
            var pool = new TransactionPool();
            var txV1 = new Transaction()
            {
                version = 0, fee = 0
            };

            pool.AddTransaction(txV1);

            // update transaction
            txV1.version = 1;
            txV1.fee     = 1;
            pool.AddTransaction(txV1);

            Assert.AreEqual(pool.count, 1);
            Assert.AreEqual(
                pool.GetTransactionsWithHighestFee(1)[0].fee,
                1);
        }
        public void MineBlockWithTwoTransactions()
        {
            TransactionPool transactionPool = new TransactionPool();
            Transaction     transaction1    = FactoryHelper.CreateTransaction(1000);
            Transaction     transaction2    = FactoryHelper.CreateTransaction(2000);

            transactionPool.AddTransaction(transaction1);
            transactionPool.AddTransaction(transaction2);

            MinerProcessor processor = new MinerProcessor(transactionPool);
            Block          genesis   = FactoryHelper.CreateGenesisBlock();

            Block block = processor.MineBlock(genesis);

            Assert.IsNotNull(block);
            Assert.AreEqual(1ul, block.Number);
            Assert.AreEqual(2, block.Transactions.Count);
            Assert.IsTrue(block.Transactions.Contains(transaction1));
            Assert.IsTrue(block.Transactions.Contains(transaction2));
        }
        public void MineBlockWithTransaction()
        {
            TransactionPool transactionPool = new TransactionPool();
            Transaction     transaction     = FactoryHelper.CreateTransaction(1000);

            transactionPool.AddTransaction(transaction);

            MinerProcessor processor = new MinerProcessor(transactionPool);
            Block          genesis   = FactoryHelper.CreateGenesisBlock();

            Block block = processor.MineBlock(genesis);

            Assert.IsNotNull(block);
            Assert.AreEqual(1ul, block.Number);
            Assert.AreEqual(1, block.Transactions.Count);
            Assert.AreSame(transaction, block.Transactions[0]);
            Assert.AreEqual(genesis.Hash, block.ParentHash);
        }
Exemplo n.º 8
0
        // Composition Root - 组合根:
        // 设计模式。在程序的入口处进行抽象-具体的依赖关系配置。
        // Pure DI
        public static void Main()
        {
            // 出创世区块:通过执行一批交易来初始化整条链,一般包括部署系统合约,如共识合约,多资产合约等,并对系统合约做初步配置
            //   准备交易
            var transactionPool = new TransactionPool
            {
                Logger = new ConsoleLogger()
            };

            foreach (var genesisTransaction in TransactionGenerationHelper.GetGenesisTransactions())
            {
                transactionPool.AddTransaction(genesisTransaction);
            }

            //   打包创世区块
            //     准备MineService
            var blockValidationService = new BlockValidationService(new List <IBlockValidationProvider>
            {
                new BlockTransactionValidationProvider
                {
                    Logger = new ConsoleLogger()
                },
                new BlockBasicInformationValidationProvider
                {
                    Logger = new ConsoleLogger()
                }
            })
            {
                Logger = new ConsoleLogger()
            };
            var blockChainService = new BlockChainService(new PureDIChainIdProvider(), blockValidationService)
            {
                Logger = new ConsoleLogger()
            };
            var transactionExecutingService = new TransactionExecutingService
            {
                Logger = new ConsoleLogger()
            };

            var mineService = new MineService(transactionPool, blockChainService, transactionExecutingService);

            //     生产创世区块
            var genesisBlock = mineService.Mine();

            //     把创世区块添加到链上
            blockChainService.AppendBlock(genesisBlock);
            //   创世区块不广播,每一个节点都独立生成创世区块

            // 所以NetworkService实例较晚才出现
            var networkService = new NetworkService
            {
                Logger = new ConsoleLogger()
            };

            // 开始产生其他区块 - 挖矿
            // Demo: 出2个块
            var count = 2;

            while (count > 0)
            {
                // 交易应该是从网络收取的,这里随机生成一些
                var txs = TransactionGenerationHelper.GetSomeRandomTransactions();
                // 正常流程:从网络中收到交易,验证交易,然后丢进交易池
                foreach (var tx in txs)
                {
                    transactionPool.AddTransaction(tx);
                }
                // 交易池里已经有交易了,然后开始出块
                var block = mineService.Mine();
                // 拿到区块以后,自己做一些列验证,没问题的话就广播,并添加到本地的链上
                networkService.BroadcastBlock(block);
                blockChainService.AppendBlock(block);
                count--;
            }
            // 实际上,共识决定了谁应该在什么时候出块,以及收到一个区块以后该怎么验证这个区块的合法性
        }
Exemplo n.º 9
0
        private static ITransaction SetupTransactions()
        {
            var txn1  = new ClaimSettlement("ABC123", 1000.00m, DateTime.Now, "QWE123", 10000, ClaimType.TotalLoss);
            var txn2  = new ClaimSettlement("VBG345", 2000.00m, DateTime.Now, "JKH567", 20000, ClaimType.TotalLoss);
            var txn3  = new ClaimSettlement("XCF234", 3000.00m, DateTime.Now, "DH23ED", 30000, ClaimType.TotalLoss);
            var txn4  = new ClaimSettlement("CBHD45", 4000.00m, DateTime.Now, "DH34K6", 40000, ClaimType.TotalLoss);
            var txn5  = new ClaimSettlement("AJD345", 5000.00m, DateTime.Now, "28FNF4", 50000, ClaimType.TotalLoss);
            var txn6  = new ClaimSettlement("QAX367", 6000.00m, DateTime.Now, "FJK676", 60000, ClaimType.TotalLoss);
            var txn7  = new ClaimSettlement("CGO444", 7000.00m, DateTime.Now, "LKU234", 70000, ClaimType.TotalLoss);
            var txn8  = new ClaimSettlement("PLO254", 8000.00m, DateTime.Now, "VBN456", 80000, ClaimType.TotalLoss);
            var txn9  = new ClaimSettlement("ABC123", 1000.00m, DateTime.Now, "QWE123", 10000, ClaimType.TotalLoss);
            var txn10 = new ClaimSettlement("VBG345", 2000.00m, DateTime.Now, "JKH567", 20000, ClaimType.TotalLoss);
            var txn11 = new ClaimSettlement("XCF234", 3000.00m, DateTime.Now, "DH23ED", 30000, ClaimType.TotalLoss);
            var txn12 = new ClaimSettlement("CBHD45", 4000.00m, DateTime.Now, "DH34K6", 40000, ClaimType.TotalLoss);
            var txn13 = new ClaimSettlement("AJD345", 5000.00m, DateTime.Now, "28FNF4", 50000, ClaimType.TotalLoss);
            var txn14 = new ClaimSettlement("QAX367", 6000.00m, DateTime.Now, "FJK676", 60000, ClaimType.TotalLoss);
            var txn15 = new ClaimSettlement("CGO444", 7000.00m, DateTime.Now, "LKU234", 70000, ClaimType.TotalLoss);
            var txn16 = new ClaimSettlement("PLO254", 8000.00m, DateTime.Now, "VBN456", 80000, ClaimType.TotalLoss);

            txnPool.AddTransaction(txn1);
            txnPool.AddTransaction(txn2);
            txnPool.AddTransaction(txn3);
            txnPool.AddTransaction(txn4);
            txnPool.AddTransaction(txn5);
            txnPool.AddTransaction(txn6);
            txnPool.AddTransaction(txn7);
            txnPool.AddTransaction(txn8);
            txnPool.AddTransaction(txn9);
            txnPool.AddTransaction(txn10);
            txnPool.AddTransaction(txn11);
            txnPool.AddTransaction(txn12);
            txnPool.AddTransaction(txn13);
            txnPool.AddTransaction(txn14);
            txnPool.AddTransaction(txn15);
            txnPool.AddTransaction(txn16);

            return(txn5);
        }