예제 #1
0
        public IActionResult OnGet(string id)
        {
            if (ActiveProject == null)
            {
                return(NotFound());
            }

            Client client = Rpc();

            Block?block;

            if (id.StartsWith("0x"))
            {
                block = client.GetBlock(id);
            }
            else
            {
                block = client.GetBlockByNumber(UInt64.Parse(id));
            }
            if (block == null)
            {
                return(NotFound());
            }

            BlockDetail = new()
            {
                BlockHash         = block.Header.Hash,
                TransactionsRoot  = block.Header.TransactionsRoot,
                Number            = $"{Hex.HexToUInt64(block.Header.Number)}",
                Version           = $"{Hex.HexToUInt32(block.Header.Version)}",
                ProposalsCount    = $"{block.Proposals.Length}",
                UnclesCount       = $"{block.Uncles.Length}",
                Timestamp         = $"{Hex.HexToUInt64(block.Header.Timestamp)}",
                TransactionsCount = $"{block.Transactions.Length}",
                Nonce             = $"{Hex.HexToBigInteger(block.Header.Nonce)}",
                Difficulty        = Difficulty.CompactToDifficulty(Hex.HexToUInt32(block.Header.CompactTarget)).ToString()
            };

            // Miner Address
            string prefix          = ActiveProject.Chain == Project.ChainType.Mainnet ? "ckb" : "ckt";
            string cellbaseWitness = block.Transactions[0].Witnesses[0];
            Script script          = CellbaseWitness.Parse(cellbaseWitness);
            string minerAddress    = Ckb.Address.Address.GenerateAddress(script, prefix);

            BlockDetail.MinerHash = minerAddress;

            // Epoch info
            var epochInfo = EpochInfo.Parse(Hex.HexToUInt64(block.Header.Epoch));

            BlockDetail.StartNumber       = "0";
            BlockDetail.Length            = epochInfo.Length.ToString();
            BlockDetail.Epoch             = epochInfo.Number.ToString();
            BlockDetail.BlockIndexInEpoch = epochInfo.Index.ToString();

            EpochView?epochView = client.GetEpochByNumber(epochInfo.Number);

            if (epochView != null)
            {
                BlockDetail.StartNumber = Hex.HexToUInt64(epochView.StartNumber).ToString();
            }

            // reward
            string blockHash = block.Header.Hash;

            BlockDetail.RewardStatus = "pending";
            BlockDetail.MinerReward  = "";
            BlockEconomicState?economicState = client.GetBlockEconomicState(blockHash);

            if (economicState != null)
            {
                MinerReward reward  = economicState.MinerReward;
                string[]    rewards = new string[]
                {
                    reward.Primary,
                    reward.Secondary,
                    reward.Proposal,
                    reward.Committed,
                };
                UInt64 minerReward = rewards.Select(r => Hex.HexToUInt64(r)).Aggregate((sum, cur) => sum + cur);
                BlockDetail.MinerReward  = minerReward.ToString();
                BlockDetail.RewardStatus = "issued";
            }

            return(Page());
        }
    }
예제 #2
0
        private BlockResult?GetBlock(Client client, int num)
        {
            Block?block = client.GetBlockByNumber((UInt64)num);

            if (block == null)
            {
                return(null);
            }

            var header       = block.Header;
            var transactions = block.Transactions;

            int inputsCount  = transactions.Select(tx => tx.Inputs.Length).Aggregate(0, (acc, cur) => acc + cur);
            int outputsCount = transactions.Select(tx => tx.Outputs.Length).Aggregate(0, (acc, cur) => acc + cur);

            ulong  number            = Hex.HexToUInt64(header.Number);
            int    transactionsCount = transactions.Length;
            string timestamp         = $"{ Hex.HexToUInt64(header.Timestamp) }";

            // Reward
            string CalcluateReward()
            {
                string             blockHash     = header.Hash;
                BlockEconomicState?economicState = client.GetBlockEconomicState(blockHash);

                if (economicState != null)
                {
                    MinerReward reward = economicState.MinerReward;
                    return(Hex.HexToUInt64(reward.Primary).ToString());
                }
                else
                {
                    EpochInfo epochInfo = EpochInfo.Parse(Hex.HexToUInt64(block.Header.Epoch));
                    try
                    {
                        ulong primaryReward = PrimaryReward(client, number, epochInfo.Number);
                        return(primaryReward.ToString());
                    }
                    catch
                    {
                        return("");
                    }
                }
            }

            // Miner Address
            string prefix          = IsMainnet() ? "ckb" : "ckt";
            string cellbaseWitness = block.Transactions[0].Witnesses[0];
            Script script          = CellbaseWitness.Parse(cellbaseWitness);
            string minerAddress    = Ckb.Address.Address.GenerateAddress(script, prefix);

            BlockResult result = new()
            {
                Number            = number.ToString(),
                BlockHash         = header.Hash,
                TransactionsCount = $"{transactionsCount}",
                Timestamp         = timestamp,
                LiveCellChanges   = $"{outputsCount - inputsCount}",
                Reward            = CalcluateReward(),
                MinerHash         = minerAddress
            };

            return(result);
        }