Ejemplo n.º 1
0
 private void receivedNewMiningPoolMessage(P2PState state, NewMiningPoolMsg msg)
 {
     if (msg == null || state == null)
     {
         return;
     }
     if (new MiningPoolComponent().AddMiningToPool(msg.MinerInfo))
     {
         var nodes = this.p2pComponent.GetNodes();
         nodes.ForEach(peer =>
         {
             if (!peer.IsTrackerServer)
             {
                 var command = P2PCommand.CreateCommand(this.Identity.ToString(), CommandNames.MiningPool.NewMiningPool, msg);
                 this.p2pComponent.SendCommand(peer.IP, peer.Port, command);
             }
         });
     }
 }
Ejemplo n.º 2
0
        public bool AddMiningToPool(MiningMsg msg)
        {
            if (!POC.VerifyMiningPoolSignature(msg.PublicKey, msg.Signature))
            {
                return(false);
            }

            var result = false;
            var item   = currentMiningPools.FirstOrDefault(x => x.PublicKey == msg.PublicKey && x.Signature == msg.Signature);

            if (item == null)
            {
                MiningPoolDac miningPoolDac = new MiningPoolDac();
                MiningPool    miningPool    = ConvertToMiningPool(msg);
                result = miningPoolDac.SaveToDB(miningPool) > 0;
            }
            else if (item.Name != msg.Name)
            {
                MiningPoolDac miningPoolDac = new MiningPoolDac();
                MiningPool    miningPool    = new MiningPool()
                {
                    Name = msg.Name, PublicKey = msg.PublicKey, Signature = msg.Signature
                };
                miningPoolDac.UpdateMiningPool(miningPool);
                result = true;
            }
            if (result && OnNewMiningPoolHandle != null)
            {
                NewMiningPoolMsg newMsg = new NewMiningPoolMsg();
                newMsg.MinerInfo = new MiningMsg()
                {
                    Name = msg.Name, PublicKey = msg.PublicKey, Signature = msg.Signature
                };
                OnNewMiningPoolHandle(newMsg);
            }

            return(false);
        }
Ejemplo n.º 3
0
        //private void processLongTimeCommand()
        //{
        //    LogHelper.Warn("Thread : In to thread threadProcessLongTimeCommand's process action : processLongTimeCommand");
        //    P2PState state = default(P2PState);

        //    //System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        //    while (this.isRunning)
        //    {
        //        if (longTimeCommandQueue.TryDequeue(out state))
        //        {
        //            if(state != null)
        //            {
        //                try
        //                {
        //                    //stopwatch.Reset();
        //                    //stopwatch.Start();
        //                    int index = 0;
        //                    switch (state.Command.CommandName)
        //                    {
        //                        case CommandNames.Block.GetHeaders:
        //                            var getHeadersMsg = new GetHeadersMsg();
        //                            getHeadersMsg.Deserialize(state.Command.Payload, ref index);
        //                            this.receivedGetHeaders(state.IP, state.Port, getHeadersMsg, state.Command.Nonce);
        //                            break;
        //                        case CommandNames.Block.GetBlocks:
        //                            var getBlocksMsg = new GetBlocksMsg();
        //                            getBlocksMsg.Deserialize(state.Command.Payload, ref index);
        //                            this.receivedGetBlocks(state.IP, state.Port, getBlocksMsg, state.Command.Nonce);
        //                            break;
        //                        default:
        //                            break;
        //                    }
        //                    //stopwatch.Stop();
        //                    //LogHelper.Warn($"processLongTimeCommand : {state.Command.CommandName}  -- {stopwatch.ElapsedMilliseconds}  , longTimeCommandQueue count = {longTimeCommandQueue.Count()}" );

        //                }
        //                catch (Exception ex)
        //                {
        //                    LogHelper.Error(ex.ToString());
        //                    //LogHelper.Warn($"processLongTimeCommand : {state.Command.CommandName}  -- {stopwatch.ElapsedMilliseconds}  , longTimeCommandQueue count = {longTimeCommandQueue.Count()} -- Exception msg: {ex.ToString()} ");
        //                }

        //            }
        //            Thread.Sleep(10);
        //        }
        //        else
        //        {
        //            Thread.Sleep(50);
        //        }

        //    }
        //    LogHelper.Warn("Thread : out of thread threadProcessLongTimeCommand's process action : processLongTimeCommand");
        //}

        #endregion

        private void dataReceived(P2PState state)
        {
            int index = 0;

            switch (state.Command.CommandName)
            {
            case CommandNames.Transaction.GetTxPool:
                this.receivedGetTransactionPool(state.IP, state.Port, state.Command.Nonce);
                break;

            case CommandNames.Transaction.TxPool:
                var txPoolMsg = new TxPoolMsg();
                txPoolMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedTransacitonPoolMessage(state.IP, state.Port, txPoolMsg);
                break;

            case CommandNames.Transaction.GetTx:
                var getTxMsg = new GetTxsMsg();
                getTxMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetTransaction(state.IP, state.Port, getTxMsg, state.Command.Nonce);
                break;

            case CommandNames.Transaction.Tx:
                var txsMsg = new TxsMsg();
                txsMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedTransactionMessage(state.IP, state.Port, txsMsg);
                break;

            case CommandNames.Transaction.NewTx:
                var newTxMsg = new NewTxMsg();
                newTxMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedNewTransactionMessage(state.IP, state.Port, newTxMsg);
                break;

            case CommandNames.Block.GetHeight:
                this.receivedGetHeight(state.IP, state.Port, state.Command.Nonce);
                break;

            case CommandNames.Block.Height:
                var heightMsg = new HeightMsg();
                heightMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedHeightMessage(state.IP, state.Port, heightMsg);
                break;

            case CommandNames.Block.GetHeaders:
                var getHeadersMsg = new GetHeadersMsg();
                getHeadersMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetHeaders(state.IP, state.Port, getHeadersMsg, state.Command.Nonce);
                //longTimeCommandQueue.Enqueue(state);
                break;

            case CommandNames.Block.Headers:
                var headersMsg = new HeadersMsg();
                headersMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedHeadersMessage(state.IP, state.Port, headersMsg);
                break;

            case CommandNames.Block.GetBlocks:
                var getBlocksMsg = new GetBlocksMsg();
                getBlocksMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedGetBlocks(state.IP, state.Port, getBlocksMsg, state.Command.Nonce);
                //longTimeCommandQueue.Enqueue(state);
                break;

            case CommandNames.Block.Blocks:
                var blocksMsg = new BlocksMsg();
                blocksMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedBlocksMessage(state.IP, state.Port, blocksMsg);
                break;

            case CommandNames.Block.NewBlock:
                var newBlockMsg = new NewBlockMsg();
                newBlockMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedNewBlockMessage(state.IP, state.Port, newBlockMsg, state.Command.Nonce);
                break;

            case CommandNames.MiningPool.GetMiningPools:
                this.receivedGetMiningPoolsMessage(state.IP, state.Port);
                break;

            case CommandNames.MiningPool.MiningPools:
                var miningPoolMsg = new MiningPoolMsg();
                miningPoolMsg.Deserialize(state.Command.Payload, ref index);
                receivedMiningPoolsMessage(miningPoolMsg);
                break;

            case CommandNames.MiningPool.NewMiningPool:
                var newMiningPoolMsg = new NewMiningPoolMsg();
                newMiningPoolMsg.Deserialize(state.Command.Payload, ref index);
                this.receivedNewMiningPoolMessage(state, newMiningPoolMsg);
                break;

            case CommandNames.Other.Reject:
            case CommandNames.Other.NotFound:
            default:
                break;
            }
        }