public void MinerReward() { if (httpPool != null) { Dictionary <string, MinerTask> miners = httpPool.GetMinerReward(out long miningHeight); if (miners != null && miningHeight + 3 < height) { string ownerAddress = Wallet.GetWallet().GetCurWallet().ToAddress(); var mcblk = BlockChainHelper.GetMcBlock(miningHeight); if (mcblk != null && mcblk.Address == ownerAddress) { var miner = miners.Values.First(c => c.random == mcblk.random); WalletKey walletKey = Wallet.GetWallet().GetCurWallet(); // 出块奖励 if (miner != null) { Transfer transfer = new Transfer(); transfer.addressIn = ownerAddress; transfer.addressOut = miner.address; transfer.amount = Consensus.GetReward(miningHeight).ToString(); transfer.nonce = TimeHelper.NowSeconds(); transfer.type = "tranfer"; transfer.hash = transfer.ToHash(); transfer.sign = transfer.ToSign(walletKey); minerTransfer.Add(mcblk.hash, transfer); } // 参与奖励 } httpPool.DelMiner(miningHeight); } } }
// 获取最新高度MC块 public Block GetLastMcBlock() { var levelDBStore = Entity.Root.GetComponent <LevelDBStore>(); // 取最新高度 去TopBlock long.TryParse(levelDBStore.Get("UndoHeight"), out long transferHeight); var chain1 = BlockChainHelper.GetBlockChain(transferHeight); var chain2 = chain1.GetMcBlockNext(); while (chain2 != null) { chain1 = chain2; chain2 = chain2.GetMcBlockNext(); } blockMgr.DelBlockWithWeight(consensus, chain1.hash, chain1.height); Block blk1 = chain1.GetMcBlock(); // 2F1 float timestamp = TimeHelper.NowSeconds(); double t_2max = consensus.GetRuleCount(blk1.height + 1); List <Block> blks = blockMgr.GetBlock(blk1.height + 1); blks = BlockChainHelper.GetRuleBlk(consensus, blks, chain1.hash); if (blks.Count >= BlockChainHelper.Get2F1(t_2max)) { chain2 = BlockChainHelper.GetMcBlockNextNotBeLink(chain1, timestamp, pooltime); if (chain2 != null) { var blk2 = chain2.GetMcBlock(); double t_1max = consensus.GetRuleCount(blk2.height - 1); if (blk2.linksblk.Count >= BlockChainHelper.Get2F1(t_1max)) { return(blk2); } } } // Super Address var blkSuper = blks.Find((x) => { return(x.Address == consensus.superAddress); }); //if (blkSuper != null && blkSuper.Address == consensus.superAddress && blks.Count >= Math.Min(2, consensus.GetRuleCount(blk1.height+1)) ) if (blkSuper != null && blkSuper.Address == consensus.superAddress && blks.Count >= Math.Max(2, (BlockChainHelper.Get2F1(t_2max) / 2))) { return(blkSuper); } return(blk1); }
public static void MakeGenesis() { if (true) { WalletKey key = Wallet.GetWallet().GetCurWallet(); // Genesis Block blk = new Block(); blk.Address = key.ToAddress(); blk.prehash = ""; blk.height = 1; blk.timestamp = TimeHelper.NowSeconds(); blk.random = RandomHelper.RandUInt64().ToString("x"); // Transfer { Transfer transfer = new Transfer(); transfer.addressIn = ""; transfer.addressOut = blk.Address; transfer.amount = (3L * 100L * 10000L * 10000L).ToString(); transfer.nonce = 1; transfer.type = "tranfer"; transfer.hash = transfer.ToHash(); transfer.sign = transfer.ToSign(key); blk.AddTransfer(0, transfer); } // rule Consensus { Transfer transfer = new Transfer(); transfer.addressIn = blk.Address; transfer.addressOut = ""; transfer.amount = "0"; transfer.nonce = 1; transfer.type = "contract"; transfer.data = Base58.Encode(FileHelper.GetFileData("./Data/Contract/RuleContract_v1.0.lua").ToByteArray()); transfer.hash = transfer.ToHash(); transfer.sign = transfer.ToSign(key); blk.AddTransfer(1, transfer); } blk.hash = blk.ToHash(); blk.sign = blk.ToSign(key); File.WriteAllText("./Data/genesisBlock.dat", JsonHelper.ToJson(blk)); } }