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));
            }
        }