public Logger(Context context, ILoggerFactory loggerFactory, LogFilter logFilter, WalletStore walletStore, WalletService walletService, BlockMiner blockMiner, ChainService chainService, NodeConnectionService nodeConnectionService, MinerService minerService) : base(context) { this.context = context; this.logFilter = logFilter; this.walletStore = walletStore; this.walletService = walletService; this.blockMiner = blockMiner; this.chainService = chainService; this.nodeConnectionService = nodeConnectionService; this.minerService = minerService; this.logger = loggerFactory.CreateLogger <Staker>(); }
public void Test1() { var b = new Block { Data = Encoding.UTF8.GetBytes("hello world"), PreviousBlockHash = String.Empty }; var x = BlockMiner.ComputeBlockNonce(b, 4); b.Nonce = x; var computedHash = BlockMiner.ComputeHashForBlock(b); Assert.IsTrue(BlockMiner.LeadingZeroes(computedHash) >= 4); }
static void Main(string[] args) { Console.WriteLine("Welcome to Loan Star Blockchain!"); Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine("+++ Step 1: Read blockchain difficulty of work parameters"); Console.ReadKey(); Console.WriteLine($"Average mining time (msec):{ChainHelper.MINING_PERIOD}, Difficulty of work (#leading zeroes):{ChainHelper.NUMBER_ZEROES}"); Console.ReadKey(); Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine("+++ Step 2: Add first set of transactions to blockchain"); Console.ReadKey(); var blockchain = new Blockchain(); BlockMiner miner = new BlockMiner(blockchain); string error; var trxList1 = new List<Tuple<string,string,double>>(); trxList1.Add(new Tuple<string, string, double>("Anton-Georgescu","Loan003.Repayment.Pay",200)); trxList1.Add(new Tuple<string, string, double>("Alex-Georgescu", "Loan002.Repayment.Pay", 214)); trxList1.Add(new Tuple<string, string, double>("Loan002.Repayment.Credit", "Alex-Georgescu", 200)); Console.WriteLine($"adding {trxList1.Count} raw transactions..."); foreach (var trx in trxList1) Console.WriteLine($"...[Transaction] from_account:{trx.Item1},to_account:{trx.Item2},amount:{trx.Item3} "); Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine("+++ Step 3: Run blockchain mining process"); Console.ReadKey(); miner.TransactionPool = AddTransactionsToPool(trxList1); Console.WriteLine($"{DateTime.Now}: start blockchained mining..."); miner.DoGenerateBlockTest(blockchain); Console.WriteLine($"{DateTime.Now}: end blockchained mining."); Console.WriteLine($"blockchain is valid:{blockchain.IsChainValid(out error)}"); Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine("+++ Step 4: Add next set of transactions to blockchain"); Console.ReadKey(); var trxList2 = new List<Tuple<string, string, double>>(); trxList2.Add(new Tuple<string, string, double>("Jake-Trajanovich", "Loan002.Repayment.Pay", 200)); trxList2.Add(new Tuple<string, string, double>("Cora-Trajanovich", "Loan003.Repayment.Pay", 214)); trxList2.Add(new Tuple<string, string, double>("Alex-Georgescu", "Loan002.Repayment.Pay", 72)); Console.WriteLine($"adding {trxList2.Count} raw transactions..."); foreach (var trx in trxList1) Console.WriteLine($"...[Transaction] from_account:{trx.Item1},to_account:{trx.Item2},amount:{trx.Item3} "); Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine("+++ Step 5: Run blockchain mining process"); Console.ReadKey(); miner.TransactionPool = AddTransactionsToPool(trxList2); Console.WriteLine($"{DateTime.Now}: start blockchained mining..."); miner.DoGenerateBlockTest(blockchain); Console.WriteLine($"{DateTime.Now}: end blockchained mining."); Console.WriteLine($"blockchain is valid:{blockchain.IsChainValid(out error)}"); string testAccount = "Alex-Georgescu"; Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine($"+++ Step 6: List existing transactions for {testAccount}"); Console.ReadKey(); Console.WriteLine($"list existing transactions for:{testAccount}"); var trxs = blockchain.GetAllTransactionsForAccount(testAccount); foreach (var tx in trxs) Console.WriteLine($"timestamp:{tx.timestamp},from:{tx.fromAccount},to:{tx.toAccount},amount:{tx.amount},isvalid:{tx.IsValid()}"); Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine($"+++ Step 7: Tamper with an existing transaction"); Console.ReadKey(); Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine($"tampering with blockchain transaction..."); Console.WriteLine($"[original] timestamp:{blockchain.blocks[2].transactions[0].timestamp},from:{blockchain.blocks[2].transactions[0].fromAccount},to:{blockchain.blocks[2].transactions[0].toAccount},amount:{blockchain.blocks[2].transactions[0].amount},isvalid:{blockchain.blocks[2].transactions[0].IsValid()}"); blockchain.blocks[2].transactions[0].amount = 50; Console.WriteLine($"[tampered] timestamp:{blockchain.blocks[2].transactions[0].timestamp},from:{blockchain.blocks[2].transactions[0].fromAccount},to:{blockchain.blocks[2].transactions[0].toAccount},amount:{blockchain.blocks[2].transactions[0].amount},isvalid:{blockchain.blocks[2].transactions[0].IsValid()}"); var isChainValid = blockchain.IsChainValid(out error); Console.WriteLine($"blockchain is valid:{isChainValid} {error}"); Console.WriteLine($"trying to revert transaction to initial value..."); Console.WriteLine($"[original] timestamp:{blockchain.blocks[2].transactions[0].timestamp},from:{blockchain.blocks[2].transactions[0].fromAccount},to:{blockchain.blocks[2].transactions[0].toAccount},amount:{blockchain.blocks[2].transactions[0].amount},isvalid:{blockchain.blocks[2].transactions[0].IsValid()}"); blockchain.blocks[2].transactions[0].amount = 200; Console.WriteLine($"[tampered] timestamp:{blockchain.blocks[2].transactions[0].timestamp},from:{blockchain.blocks[2].transactions[0].fromAccount},to:{blockchain.blocks[2].transactions[0].toAccount},amount:{blockchain.blocks[2].transactions[0].amount},isvalid:{blockchain.blocks[2].transactions[0].IsValid()}"); isChainValid = blockchain.IsChainValid(out error); Console.WriteLine($"blockchain is valid:{isChainValid} {error}"); Console.WriteLine("*********************************************************************************************************************"); Console.WriteLine($"+++ Step 8: Run Blockchain Explorer"); Console.ReadKey(); var chainExplore = ChainHelper.BlockchainExplorer(blockchain); foreach (var line in chainExplore) Console.WriteLine(line); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("Begin"); var rsa = RSA.Create(); var key = rsa.ExportParameters(true); var ZeroHexHash = ""; for (int i = 0; i < 64; i++) { ZeroHexHash += "0"; } Console.WriteLine("Generating key files"); #region Save Public Key string publicKey = RSAHelper.ExportPublicKeyToPEMFormat(rsa); FileInfo pem = new FileInfo("public.pem"); if (pem.Exists) { pem.Delete(); } using (var stream = pem.OpenWrite()) using (var writer = new StreamWriter(stream)) writer.Write(publicKey); #endregion #region Save Private Key string privateKey = RSAHelper.ExportPrivateKeyToPfxFormat(rsa); FileInfo pfx = new FileInfo("private.pfx"); if (pfx.Exists) { pfx.Delete(); } using (var stream = pfx.OpenWrite()) using (var writer = new StreamWriter(stream)) writer.Write(privateKey); #endregion Console.WriteLine("Public and private key file saved"); Console.WriteLine("Mining Genesis block"); #region Generate Genesis Block var transactionContent = new SycoinTransactionContent() { Outputs = new TransactionOutput[] { new TransactionOutput { Amount = 50, Receiver = publicKey } } }; var contentHash = HashingHelper.HashObject(transactionContent); var transactions = new SyCoinTransaction[] { new SyCoinTransaction { Content = transactionContent, Hash = HashingHelper.ByteArrayToHexDigit(contentHash) } }; var blockData = new SyCoinBlock(transactions, 1, ZeroHexHash, 4); var(nonce, timestamp) = new BlockMiner().Mine(blockData); blockData.Seal(nonce, timestamp); PersistedBlock genesisBlock = new PersistedBlock() { Header = new BlockHeader(), Block = blockData }; FileInfo genesisJson = new FileInfo("genesis.json"); if (genesisJson.Exists) { genesisJson.Delete(); } using (var stream = genesisJson.OpenWrite()) using (var writer = new StreamWriter(stream)) writer.Write(Newtonsoft.Json.JsonConvert.SerializeObject(genesisBlock)); #endregion Console.WriteLine("Genesis block saved in genesis.json"); Console.ReadLine(); }
public BlockChainService(TransactionPool transactionPool, BlockMiner blockMiner) { TransactionPool = transactionPool; BlockMiner = blockMiner; }
public BlockChainService() { TransactionPool = new TransactionPool(); BlockMiner = new BlockMiner(TransactionPool); }