Esempio n. 1
0
        public IRpcMethodResult GetBaseTarget(long blockHeight)
        {
            try
            {
                var      blockComponent = new BlockComponent();
                BlockMsg lastBlock      = null;
                BlockMsg prevStepBlock  = null;

                if (blockHeight > 0)
                {
                    lastBlock     = blockComponent.GetBlockMsgByHeight(blockHeight - 1);
                    prevStepBlock = blockComponent.GetBlockMsgByHeight(blockHeight - POC.DIFFIUCLTY_ADJUST_STEP);
                }
                long baseTarget;
                if (lastBlock != null)
                {
                    baseTarget = POC.CalculateBaseTarget(blockHeight, lastBlock, prevStepBlock);
                }
                else
                {
                    baseTarget = POC.CalculateBaseTarget(0, null, null);
                }
                return(Ok(baseTarget));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Esempio n. 2
0
        public IRpcMethodResult GetDifficulty()
        {
            try
            {
                var blockComponent = new BlockComponent();
                var height         = blockComponent.GetLatestHeight();
                var newHeight      = height + 1;

                var previousBlockMsg = blockComponent.GetBlockMsgByHeight(height);
                var prevStepBlockMsg = blockComponent.GetBlockMsgByHeight(newHeight - POC.DIFFIUCLTY_ADJUST_STEP - 1);
                var bits             = POC.CalculateBaseTarget(height, previousBlockMsg, prevStepBlockMsg).ToString();

                var result = new GetDifficultyOM()
                {
                    height     = newHeight,
                    hashTarget = bits
                };
                return(Ok(result));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Esempio n. 3
0
        public IRpcMethodResult GetBlockByHeight(long height, int format = 0)
        {
            try
            {
                var blockComponent = new BlockComponent();

                var block = blockComponent.GetBlockMsgByHeight(height);

                if (block != null)
                {
                    if (format == 0)
                    {
                        var bytes  = block.Serialize();
                        var result = Base16.Encode(bytes);
                        return(Ok(result));
                    }
                    else
                    {
                        return(Ok(block));
                    }
                }
                else
                {
                    return(Ok());
                }
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Esempio n. 4
0
        public IRpcMethodResult GetBlockHash(long blockHeight)
        {
            try
            {
                var blockComponent = new BlockComponent();
                var block          = blockComponent.GetBlockMsgByHeight(blockHeight);

                if (block != null)
                {
                    return(Ok(block.Header.Hash));
                }
                else
                {
                    return(Ok());
                }
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Esempio n. 5
0
        public IRpcMethodResult GetTxOutSetInfo()
        {
            try
            {
                GetTxOutSetInfoOM result = new GetTxOutSetInfoOM();
                var blockComponent       = new BlockComponent();
                var txComponent          = new TransactionComponent();
                var utxoComponent        = new UtxoComponent();

                result.height    = blockComponent.GetLatestHeight();
                result.bestblock = null;

                if (result.height >= 0)
                {
                    var bestBlock = blockComponent.GetBlockMsgByHeight(result.height);
                    result.bestblock = bestBlock != null ? bestBlock.Header.Hash : null;
                }

                result.transactions = utxoComponent.GetTransactionCounts();
                result.txouts       = utxoComponent.GetOutputCounts();

                long confirmedBalance, unconfirmedBalance;
                utxoComponent.GetBalanceInDB(out confirmedBalance, out unconfirmedBalance);
                result.total_amount = confirmedBalance;
                return(Ok(result));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Esempio n. 6
0
        public IRpcMethodResult GetBaseTarget(long blockHeight)
        {
            try
            {
                var      blockComponent = new BlockComponent();
                BlockMsg lastBlock      = null;
                BlockMsg prevStepBlock  = null;

                if (blockHeight > 0)
                {
                    lastBlock = blockComponent.GetBlockMsgByHeight(blockHeight - 1);
                    if (blockHeight >= POC.DIFFIUCLTY_ADJUST_STEP)
                    {
                        long prevStepHeight = 0;
                        if (!GlobalParameters.IsTestnet && blockHeight <= POC.DIFFICULTY_CALCULATE_LOGIC_ADJUST_HEIGHT)
                        {
                            prevStepHeight = blockHeight - POC.DIFFIUCLTY_ADJUST_STEP - 1;
                        }
                        else
                        {
                            prevStepHeight = blockHeight - POC.DIFFIUCLTY_ADJUST_STEP;
                        }
                        prevStepBlock = blockComponent.GetBlockMsgByHeight(prevStepHeight);
                    }
                }
                long baseTarget;
                if (lastBlock != null)
                {
                    baseTarget = POC.CalculateBaseTarget(blockHeight, lastBlock, prevStepBlock);
                }

                else
                {
                    baseTarget = POC.CalculateBaseTarget(0, null, null);
                }
                return(Ok(baseTarget));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Esempio n. 7
0
        public IRpcMethodResult GetDifficulty()
        {
            try
            {
                var blockComponent = new BlockComponent();
                var height         = blockComponent.GetLatestHeight();
                var newHeight      = height + 1;

                var      previousBlockEntity = blockComponent.GetBlockMsgByHeight(height);
                BlockMsg prevStepBlock       = null;
                if (newHeight >= POC.DIFFIUCLTY_ADJUST_STEP)
                {
                    var prevStepHeight = 0L;
                    if (!GlobalParameters.IsTestnet && newHeight <= POC.DIFFICULTY_CALCULATE_LOGIC_ADJUST_HEIGHT)
                    {
                        prevStepHeight = newHeight - POC.DIFFIUCLTY_ADJUST_STEP - 1;
                    }
                    else
                    {
                        prevStepHeight = newHeight - POC.DIFFIUCLTY_ADJUST_STEP;
                    }
                    prevStepBlock = blockComponent.GetBlockMsgByHeight(prevStepHeight);
                }

                var bits = POC.CalculateBaseTarget(height, previousBlockEntity, prevStepBlock).ToString();

                var result = new GetDifficultyOM()
                {
                    height     = newHeight,
                    hashTarget = bits
                };
                return(Ok(result));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Esempio n. 8
0
        public IRpcMethodResult GetDifficulty()
        {
            try
            {
                var blockComponent = new BlockComponent();
                var height         = blockComponent.GetLatestHeight();
                var newHeight      = height + 1;
                var previousBlock  = blockComponent.GetBlockMsgByHeight(height);
                var prev4032Block  = blockComponent.GetBlockMsgByHeight(newHeight - POW.DiffiucltyAdjustStep);

                long prevBits = 0;

                if (previousBlock != null)
                {
                    prevBits = previousBlock.Header.Bits;
                }

                var work   = new POW(newHeight);
                var bits   = work.CalculateNextWorkTarget(height, prevBits, prev4032Block);
                var target = work.ConvertBitsToBigInt(bits).ToString("X").PadLeft(64, '0');

                var result = new GetDifficultyOM()
                {
                    height     = newHeight,
                    hashTarget = target
                };
                return(Ok(result));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Esempio n. 9
0
        public IRpcMethodResult SubmitBlock(string blockData)
        {
            try
            {
                var bytes = Base16.Decode(blockData);
                var block = new BlockMsg();
                int index = 0;
                try
                {
                    block.Deserialize(bytes, ref index);
                }
                catch
                {
                    throw new CommonException(ErrorCode.Service.BlockChain.BLOCK_DESERIALIZE_FAILED);
                }

                var blockComponent = new BlockComponent();
                var blockInDB      = blockComponent.GetBlockMsgByHeight(block.Header.Height);
                if (blockInDB == null)
                {
                    var result = blockComponent.SaveBlockIntoDB(block);
                    if (result)
                    {
                        Startup.P2PBroadcastBlockHeaderAction(block.Header);
                    }
                    else
                    {
                        throw new CommonException(ErrorCode.Service.BlockChain.BLOCK_SAVE_FAILED);
                    }
                }
                else
                {
                    throw new CommonException(ErrorCode.Service.BlockChain.SAME_HEIGHT_BLOCK_HAS_BEEN_GENERATED);
                }

                return(Ok());
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }