private void Mine() { if (miningAddress != null) { TransferTransaction[] ttxs = unconfirmedTtxs.Keys.Take(TransactionalBlock.maxTxs - 2).ToArray(); TransactionOutput[][] prevTxOutss = unconfirmedTtxs.Values.Take(TransactionalBlock.maxTxs - 2).ToArray(); mining.NewMiningBlock(TransactionalBlock.GetBlockTemplate(blockChain.headBlockIndex + 1, miningAddress, ttxs, prevTxOutss, (index) => blockChain.GetMainBlock(index) as TransactionalBlock, 0)); } }
public PacketBase Get(IDataKey key) { if (key is SyncHashKey syncHashKey) { TransactionalBlock transactionalBlock = DataAccessService.Instance.GetTransactionalBySyncAndHash(syncHashKey.SyncBlockHeight, syncHashKey.Hash); return(_mapperFactory.GetInstance <TransactionalBlock, PacketBase>().Translate(transactionalBlock)); } return(null); }
public PacketBase GetLastBlock(IKey key) { TransactionalBlock transactionalBlock = DataAccessService.Instance.GetLastTransactionalBlock(key); if (transactionalBlock != null) { ITranslator <TransactionalBlock, PacketBase> mapper = _mapperFactory.GetInstance <TransactionalBlock, PacketBase>(); PacketBase block = mapper?.Translate(transactionalBlock); return(block); } return(null); }
public TransactionalBlock GetLastTransactionalBlock(AccountIdentity accountIdentity) { TransactionalBlock transactionalBlock = null; if (accountIdentity == null) { throw new ArgumentNullException(nameof(accountIdentity)); } TransactionalIdentity transactionalIdentity = GetTransactionalIdentity(accountIdentity); if (transactionalIdentity != null) { transactionalBlock = GetLastTransactionalBlock(transactionalIdentity); } return(transactionalBlock); }
public TransactionalBlock GetTransactionalBySyncAndHash(ulong syncBlockHeight, Memory <byte> hash) { IEnumerable <BlockHashKey> blockHashKeys = _dataContext.BlockHashKeys.Where(b => b.SyncBlockHeight == syncBlockHeight); foreach (BlockHashKey item in blockHashKeys) { ArraySegment <byte> arraySegment = hash.ToArraySegment(); if (item.Hash.Equals32(arraySegment.Array, arraySegment.Offset, 32)) { TransactionalBlock transactionalBlock = _dataContext.TransactionalBlocks.FirstOrDefault(b => b.HashKey.BlockHashKeyId == item.BlockHashKeyId); return(transactionalBlock); } } return(null); }
public void AddTransactionalBlock(IKey key, ulong syncBlockHeight, ushort blockType, ulong blockHeight, byte[] blockContent) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (blockContent == null) { throw new ArgumentNullException(nameof(blockContent)); } TransactionalIdentity transactionalIdentity = GetTransactionalIdentity(key); if (transactionalIdentity == null) { AccountIdentity accountIdentity = GetOrAddIdentity(key); transactionalIdentity = AddTransactionalIdentity(accountIdentity); } BlockHashKey blockHashKey = new BlockHashKey { SyncBlockHeight = syncBlockHeight, Hash = _defaultHashCalculation.CalculateHash(blockContent) }; TransactionalBlock transactionalBlock = new TransactionalBlock() { Identity = transactionalIdentity, HashKey = blockHashKey, SyncBlockHeight = syncBlockHeight, BlockContent = blockContent, BlockHeight = blockHeight, BlockType = blockType }; lock (_sync) { _dataContext.BlockHashKeys.Add(blockHashKey); _dataContext.TransactionalBlocks.Add(transactionalBlock); } }
public IEnumerable <T> GetAllLastBlocksByType <T>() where T : PacketBase { List <T> blocks = new List <T>(); if (typeof(T) == typeof(TransactionalPacketBase)) { foreach (TransactionalIdentity transactionalIdentity in DataAccessService.Instance.GetAllTransctionalIdentities()) { TransactionalBlock transactionalBlock = DataAccessService.Instance.GetLastTransactionalBlock(transactionalIdentity); ITranslator <TransactionalBlock, PacketBase> translator = _mapperFactory.GetInstance <TransactionalBlock, PacketBase>(); PacketBase block = translator?.Translate(transactionalBlock); blocks.Add(block as T); } } return(blocks); }
//BlockChainのテスト(分岐がある場合・採掘・後ろが無効な場合) public static void Test24() { string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); BlockchainAccessDB bcadb = new BlockchainAccessDB(basepath); string bcadbPath = bcadb.GetPath(); if (File.Exists(bcadbPath)) File.Delete(bcadbPath); BlockManagerDB bmdb = new BlockManagerDB(basepath); string bmdbPath = bmdb.GetPath(); if (File.Exists(bmdbPath)) File.Delete(bmdbPath); BlockDB bdb = new BlockDB(basepath); string bdbPath = bdb.GetPath(0); if (File.Exists(bdbPath)) File.Delete(bdbPath); BlockFilePointersDB bfpdb = new BlockFilePointersDB(basepath); string bfpPath = bfpdb.GetPath(); if (File.Exists(bfpPath)) File.Delete(bfpPath); UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath); string ufadbPath = ufadb.GetPath(); if (File.Exists(ufadbPath)) File.Delete(ufadbPath); UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath); string ufpdbPath = ufpdb.GetPath(); if (File.Exists(ufpdbPath)) File.Delete(ufpdbPath); UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath); string ufptempdbPath = ufptempdb.GetPath(); if (File.Exists(ufptempdbPath)) File.Delete(ufptempdbPath); UtxoDB utxodb = new UtxoDB(basepath); string utxodbPath = utxodb.GetPath(); if (File.Exists(utxodbPath)) File.Delete(utxodbPath); BlockChain blockchain = new BlockChain(bcadb, bmdb, bdb, bfpdb, ufadb, ufpdb, ufptempdb, utxodb, 100, 300, 1000, 1000); BlockGenerator bg = new BlockGenerator(); Block[] blks = new Block[10]; BlockContext[] blkCons = new BlockContext[blks.Length]; for (int i = 0; i < blks.Length; i++) { blkCons[i] = bg.CreateNextValidBlock(); blks[i] = blkCons[i].block; Console.WriteLine("block" + i.ToString() + " created."); } Block[] blks2 = new Block[blks.Length]; double cumulativeDiff1 = 0.0; byte[] nonce = null; Func<long, TransactionalBlock> _indexToBlock = (index) => blks2[index] as TransactionalBlock; for (int i = 0; i < blks.Length; i++) { if (i == 0) { blks2[i] = blks[i]; cumulativeDiff1 += blks2[i].Difficulty.Diff; Console.WriteLine("block" + i.ToString() + "_1 " + blks2[i].Difficulty.Diff.ToString() + " " + cumulativeDiff1.ToString()); continue; } TransactionalBlock tblk = blks[i] as TransactionalBlock; TransactionalBlock tblk2 = TransactionalBlock.GetBlockTemplate(tblk.Index, tblk.coinbaseTxToMiner, tblk.transferTxs, _indexToBlock, 0); nonce = new byte[10]; while (true) { tblk2.UpdateTimestamp(DateTime.Now); tblk2.UpdateNonce(nonce); if (tblk2.Id.CompareTo(tblk2.header.difficulty.Target) <= 0) { blks2[i] = tblk2; cumulativeDiff1 += blks2[i].Difficulty.Diff; Console.WriteLine("block" + i.ToString() + "_1 mined. " + blks2[i].Difficulty.Diff.ToString() + " " + cumulativeDiff1.ToString()); break; } int index = nonce.Length.RandomNum(); int value = 256.RandomNum(); nonce[index] = (byte)value; } } TransactionalBlock tblk2_1 = blks2[1] as TransactionalBlock; TransactionalBlock tblk2_2 = blks2[2] as TransactionalBlock; TransactionalBlock blk3_2 = TransactionalBlock.GetBlockTemplate(2, tblk2_2.coinbaseTxToMiner, tblk2_2.transferTxs, _indexToBlock, 0); while (true) { blk3_2.UpdateTimestamp(DateTime.Now); blk3_2.UpdateNonce(nonce); if (blk3_2.Id.CompareTo(blk3_2.header.difficulty.Target) <= 0) { Console.WriteLine("block3_2 mined. " + blk3_2.Difficulty.Diff.ToString()); break; } int index = nonce.Length.RandomNum(); int value = 256.RandomNum(); nonce[index] = (byte)value; } BlockHeader bh3 = new BlockHeader(); bh3.LoadVersion0(3, blk3_2.Id, DateTime.Now, blks2[1].Difficulty, new byte[10]); NormalBlock blk3_3 = new NormalBlock(); blk3_3.LoadVersion0(bh3, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs); blk3_3.UpdateMerkleRootHash(); int forkLength = (int)Math.Floor(cumulativeDiff1 / blks2[1].Difficulty.Diff + 10); TransactionalBlock[] blks3 = new TransactionalBlock[forkLength]; blks3[0] = blk3_3; for (int i = 1; i < blks3.Length; i++) { BlockHeader bh = new BlockHeader(); bh.LoadVersion0(i + 3, blks3[i - 1].Id, DateTime.Now, blks2[1].Difficulty, new byte[10]); NormalBlock blk3 = new NormalBlock(); blk3.LoadVersion0(bh, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs); blk3.UpdateMerkleRootHash(); blks3[i] = blk3; } BlockHeader bh4 = new BlockHeader(); bh4.LoadVersion0(4, blk3_3.Id, DateTime.Now, blks2[1].Difficulty, new byte[10]); NormalBlock blk4 = new NormalBlock(); blk4.LoadVersion0(bh4, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs); blk4.UpdateMerkleRootHash(); BlockHeader bh10 = new BlockHeader(); bh10.LoadVersion0(10, blks2[9].Id, DateTime.Now, blks2[1].Difficulty, new byte[10]); NormalBlock blk5_10 = new NormalBlock(); blk5_10.LoadVersion0(bh10, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs); blk5_10.UpdateMerkleRootHash(); TransactionalBlock[] blks5 = new TransactionalBlock[5]; blks5[0] = blk5_10; for (int i = 1; i < blks5.Length; i++) { BlockHeader bh = new BlockHeader(); bh.LoadVersion0(i + 10, blks5[i - 1].Id, DateTime.Now, blks2[1].Difficulty, new byte[10]); NormalBlock blk5 = new NormalBlock(); blk5.LoadVersion0(bh, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs); blk5.UpdateMerkleRootHash(); blks5[i] = blk5; } BlockChain.UpdateChainReturnType type5 = blockchain.UpdateChain(blks2[0]); if (type5 != BlockChain.UpdateChainReturnType.updated) throw new Exception("test24_5"); BlockChain.UpdateChainReturnType type = blockchain.UpdateChain(blk4); if (type != BlockChain.UpdateChainReturnType.pending) throw new Exception("test24_1"); for (int i = 2; i < blks2.Length; i++) { BlockChain.UpdateChainReturnType type2 = blockchain.UpdateChain(blks2[i]); if (type2 != BlockChain.UpdateChainReturnType.pending) throw new Exception("test24_2"); } for (int i = 0; i < blks5.Length; i++) { BlockChain.UpdateChainReturnType type2 = blockchain.UpdateChain(blks5[i]); if (type2 != BlockChain.UpdateChainReturnType.pending) throw new Exception("test24_3"); } BlockChain.UpdateChainReturnType type3 = blockchain.UpdateChain(blk3_2); if (type3 != BlockChain.UpdateChainReturnType.pending) throw new Exception("test24_1"); for (int i = 0; i < blks3.Length; i++) { BlockChain.UpdateChainReturnType type2 = blockchain.UpdateChain(blks3[i]); if (type2 != BlockChain.UpdateChainReturnType.pending) throw new Exception("test24_4"); } BlockChain.UpdateChainReturnType type4 = blockchain.UpdateChain(blks2[1]); if (type4 != BlockChain.UpdateChainReturnType.updatedAndRejected) throw new Exception("test24_6"); if (blockchain.blocksCurrent.value != 10) throw new Exception("test19_2"); Console.WriteLine("test24_succeeded"); }