Example #1
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);
        }
Example #2
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());
        }
    }
Example #3
0
        public static (DisplayInput[] DisplayInputs, DisplayOutput[] DisplayOutputs) GenerateCellbaseDisplayInfos(Client client, string txHash, Output[] outputs, UInt64 blockNumber, string prefix)
        {
            if (blockNumber < (UInt64)TxProposalWindow)
            {
                var dInputs = new DisplayInput[]
                {
                    new DisplayInput
                    {
                        FromCellbase      = true,
                        Capacity          = "",
                        AddressHash       = "",
                        TargetBlockNumber = "0",
                        GeneratedTxHash   = txHash,
                    }
                };

                // var dOutputs = Array.Empty<DisplayOutput>();
                var dOutputs = outputs.Select((output, i) =>
                {
                    return(new DisplayOutput
                    {
                        Id = $"{txHash}:{i}",
                        Capacity = Hex.HexToUInt64(output.Capacity).ToString(),
                        AddressHash = Ckb.Address.Address.GenerateAddress(output.Lock, prefix),
                        TargetBlockNumber = blockNumber.ToString(),
                        RealId = i,
                        //PrimaryReward = Hex.HexToUInt64(minerReward.Primary).ToString(),
                        //SecondaryReward = Hex.HexToUInt64(minerReward.Secondary).ToString(),
                        //CommitReward = Hex.HexToUInt64(minerReward.Committed).ToString(),
                        //ProposalReward = Hex.HexToUInt64(minerReward.Proposal).ToString(),

                        // TODO: update Status & ConsumedTxHash
                        Status = "live",
                        ConsumedTxHash = "",
                    });
                }).ToArray();
                var doutlist = dOutputs.ToList();
                //if the 0 block,specal deal unuse data
                if (blockNumber == 0 && doutlist.Count > 9)
                {
                    doutlist.RemoveRange(0, 5);                    //by the dev setting cell
                    doutlist.RemoveRange(doutlist.Count() - 4, 4); //by the dev setting cell
                }
                return(dInputs, doutlist.ToArray());
            }
            UInt64 targetBlockNumber = blockNumber + 1 - (UInt64)TxProposalWindow;
            var    displayInputs     = new DisplayInput[]
            {
                new DisplayInput
                {
                    FromCellbase      = true,
                    Capacity          = "",
                    AddressHash       = "",
                    TargetBlockNumber = targetBlockNumber.ToString(),
                    GeneratedTxHash   = txHash,
                }
            };
            Header?targetBlockHeader = client.GetHeaderByNumber(targetBlockNumber);

            if (targetBlockHeader == null)
            {
                throw new Exception("Target header not found!");
            }
            BlockEconomicState?targetEconomicState = client.GetBlockEconomicState(targetBlockHeader.Hash);

            if (targetEconomicState == null)
            {
                throw new Exception("Target economic state not found!");
            }
            MinerReward minerReward    = targetEconomicState.MinerReward;
            var         displayOutputs = outputs.Select((output, i) =>
            {
                return(new DisplayOutput
                {
                    Id = $"{txHash}:{i}",
                    Capacity = Hex.HexToUInt64(output.Capacity).ToString(),
                    AddressHash = Ckb.Address.Address.GenerateAddress(output.Lock, prefix),
                    TargetBlockNumber = targetBlockNumber.ToString(),
                    RealId = i,
                    PrimaryReward = Hex.HexToUInt64(minerReward.Primary).ToString(),
                    SecondaryReward = Hex.HexToUInt64(minerReward.Secondary).ToString(),
                    CommitReward = Hex.HexToUInt64(minerReward.Committed).ToString(),
                    ProposalReward = Hex.HexToUInt64(minerReward.Proposal).ToString(),

                    // TODO: update Status & ConsumedTxHash
                    Status = "live",
                    ConsumedTxHash = "",
                });
            }).ToArray();

            return(displayInputs, displayOutputs);
        }