Exemple #1
0
        public async void Run()
        {
            await Task.Delay(2000);

            var httpRpc = Entity.Root.Find("HttpRpc")?.GetComponent <HttpRpc>();

            if (httpRpc == null)
            {
                return;
            }
            var consensus = Entity.Root.GetComponent <Consensus>();
            var luaVMEnv  = Entity.Root.GetComponent <LuaVMEnv>();

            var    httpMessage = new HttpMessage();
            string address     = Wallet.GetWallet().GetCurWallet().ToAddress();
            string consAddress = "";
            int    delayTime   = 60 * 1000 * 2;

            while (true)
            {
                await Task.Delay(delayTime);

                try
                {
                    if (consensus.IsRule(consensus.transferHeight, address))
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(consAddress))
                    {
                        if (httpMessage.map == null)
                        {
                            httpMessage.map = new Dictionary <string, string>();
                            httpMessage.map.Add("1", consensus.PledgeFactory);
                            httpMessage.map.Add("2", $"getPair(\"{address}\")");
                        }
                        httpRpc.callFun(httpMessage);
                        if (!httpMessage.result.Contains("error"))
                        {
                            consAddress = JsonHelper.FromJson <string>(httpMessage.result);
                            using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot())
                            {
                                if (!luaVMEnv.IsERC(dbSnapshot, consAddress, null))
                                {
                                    consAddress = "";
                                }
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(consAddress))
                    {
                        continue;
                    }

                    if (!Wallet.CheckAddress(consAddress))
                    {
                        continue;
                    }

                    Account account = null;
                    using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
                    {
                        account = dbSnapshot.Accounts.Get(consAddress);
                    }
                    if (account == null)
                    {
                        continue;
                    }

                    var rulers = consensus.GetRule(consensus.transferHeight);
                    if (rulers == null)
                    {
                        continue;
                    }

                    int    rulerCount     = 0;
                    string rulerAmountMin = "";
                    foreach (RuleInfo info in rulers.Values)
                    {
                        if (info.End == -1 || info.End > consensus.transferHeight)
                        {
                            rulerCount++;
                            rulerAmountMin = rulerAmountMin == "" ? info.Amount : BigHelper.Min(rulerAmountMin, info.Amount);
                        }
                    }

                    if ((rulerCount < 25 && BigHelper.Greater(account.amount, "3000000", true)) ||
                        BigHelper.Greater(account.amount, rulerAmountMin, false))
                    {
                        httpRpc.OnBeRulerReal(httpMessage);
                    }
                }
                catch (Exception)
                {
                }
            }
        }
Exemple #2
0
        // Miner reward, only after confirming that it cannot be rolled back
        public Dictionary <string, BlockSub> MinerReward_PPLNS(string today, long minHeight, long maxHeight)
        {
            Dictionary <string, BlockSub> minerTransfer = new Dictionary <string, BlockSub>();

            if (httpPool != null)
            {
                WalletKey walletKey = Wallet.GetWallet().GetCurWallet();
                for (long rewardheight = minHeight; rewardheight < maxHeight; rewardheight++)
                {
                    Dictionary <string, MinerTask> miners = null;
                    using (DbSnapshot snapshot = PoolDBStore.GetSnapshot())
                    {
                        string json = snapshot.Get("Pool_H_" + rewardheight);
                        if (!string.IsNullOrEmpty(json))
                        {
                            miners = JsonHelper.FromJson <Dictionary <string, MinerTask> >(json);
                        }
                    }

                    //var miners = httpPool.GetMiner(rewardheight);
                    if (miners != null)
                    {
                        string ownerAddress = Wallet.GetWallet().GetCurWallet().ToAddress();

                        var mcblk = BlockChainHelper.GetMcBlock(rewardheight);
                        if (mcblk != null && mcblk.Address == ownerAddress)
                        {
                            BigFloat reward = new BigFloat(Consensus.GetReward(rewardheight));
                            reward = reward * (1.0f - serviceFee);

                            var miner = miners.Values.FirstOrDefault(c => c.random == mcblk.random);
                            if (miner == null)
                            {
                                continue;
                            }

                            // Total power
                            BigFloat diffsum = new BigFloat(0);
                            foreach (var dic in miners.Values)
                            {
                                if (string.IsNullOrEmpty(dic.address))
                                {
                                    continue;
                                }
                                if (dic.diff < 0.99999f)
                                {
                                    continue;
                                }
                                diffsum += new BigFloat(dic.diff);
                            }

                            // Reward for participation
                            foreach (var dic in miners.Values)
                            {
                                if (string.IsNullOrEmpty(dic.address))
                                {
                                    continue;
                                }
                                if (dic.diff < 0.99999f)
                                {
                                    continue;
                                }

                                var    v   = new BigFloat(dic.diff);
                                string pay = BigHelper.Round8((v * reward / diffsum).ToString());

                                if (minerTransfer.TryGetValue(dic.address, out BlockSub transfer))
                                {
                                    transfer.amount = BigHelper.Add(transfer.amount, pay);
                                }
                                else
                                if (BigHelper.Greater(pay, "0.002", false))
                                {
                                    transfer            = new BlockSub();
                                    transfer.addressIn  = ownerAddress;
                                    transfer.addressOut = dic.address;
                                    transfer.amount     = BigHelper.Sub(pay, "0.002"); // 扣除交易手续费
                                    transfer.type       = "transfer";
                                    transfer.data       = CryptoHelper.Sha256($"{today}_{maxHeight}_{ownerAddress}_{dic.address}_MinerReward");
                                    minerTransfer.Add(transfer.addressOut, transfer);
                                }
                            }
                        }
                    }
                }
            }
            return(minerTransfer);
        }