Esempio n. 1
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));
            }
        }
Esempio n. 2
0
        public BlockMsg GetBlockByHeight(long height)
        {
            var hash = this.SendRpcRequest <string>("GetBlockHash", new object[] { height });

            if (string.IsNullOrWhiteSpace(hash))
            {
                return(null);
            }

            var data = this.SendRpcRequest <string>("GetBlock", new object[] { hash, 0 });

            var blockMsg = new BlockMsg();
            int index    = 0;

            blockMsg.Deserialize(Base16.Decode(data), ref index);

            return(blockMsg);
        }
Esempio n. 3
0
        public BlockMsg GenerateMiningBlock(string poolName, string account)
        {
            var data     = this.SendRpcRequest <string>("GenerateNewBlock", new object[] { poolName, account, 0 });
            var blockMsg = new BlockMsg();
            int index    = 0;

            blockMsg.Deserialize(Base16.Decode(data), ref index);

            if (blockMsg.Transactions[0].Outputs[0].Amount < 0)
            {
                LogHelper.Warn("Coinbase output can not be less than 0");
                LogHelper.Warn("Block Info:" + data);

                throw new Exception("Coinbase output can not be less than 0");
            }

            //LogHelper.Warn("GenerateNewBlock Result :" + Newtonsoft.Json.JsonConvert.SerializeObject(blockMsg));
            return(blockMsg);
        }
Esempio n. 4
0
        public IRpcMethodResult GenerateNewBlock(string minerName, string address = null, string remark = null,
                                                 int format = 0)
        {
            try
            {
                BlockComponent blockComponent = new BlockComponent();
                var            block          = blockComponent.CreateNewBlock(minerName, address, remark);

                if (block != null)
                {
                    if (format == 0)
                    {
                        var bytes = block.Serialize();

                        var block1 = new BlockMsg();
                        int index  = 0;
                        block1.Deserialize(bytes, ref index);
                        var result = Base16.Encode(bytes);
                        return(Ok(result));
                    }
                    else
                    {
                        LogHelper.Warn(block.ToString());
                        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));
            }
        }