コード例 #1
0
ファイル: ConsensusMessage.cs プロジェクト: MFChain/mfc-core
        public static ConsensusMessage DeserializeFrom(byte[] data)
        {
            ConsensusMessage message = ReflectionCache.CreateInstance <ConsensusMessage>(data[0]);

            if (message == null)
            {
                throw new FormatException();
            }

            using (MemoryStream ms = new MemoryStream(data, false))
                using (BinaryReader r = new BinaryReader(ms))
                {
                    message.Deserialize(r);
                }
            return(message);
        }
コード例 #2
0
        private void LocalNode_InventoryReceived(object sender, IInventory inventory)
        {
            ConsensusPayload payload = inventory as ConsensusPayload;

            if (payload != null)
            {
                lock (context)
                {
                    if (payload.ValidatorIndex == context.MyIndex)
                    {
                        return;
                    }

                    if (payload.Version != ConsensusContext.Version)
                    {
                        return;
                    }
                    if (payload.PrevHash != context.PrevHash || payload.BlockIndex != context.BlockIndex)
                    {
                        // Request blocks

                        if (Blockchain.Default?.Height + 1 < payload.BlockIndex)
                        {
                            Log($"chain sync: expected={payload.BlockIndex} current: {Blockchain.Default?.Height} nodes={localNode.RemoteNodeCount}");

                            localNode.RequestGetBlocks();
                        }

                        return;
                    }

                    if (payload.ValidatorIndex >= context.Validators.Length)
                    {
                        return;
                    }
                    ConsensusMessage message;
                    try
                    {
                        message = ConsensusMessage.DeserializeFrom(payload.Data);
                    }
                    catch
                    {
                        return;
                    }
                    if (message.ViewNumber != context.ViewNumber && message.Type != ConsensusMessageType.ChangeView)
                    {
                        return;
                    }
                    switch (message.Type)
                    {
                    case ConsensusMessageType.ChangeView:
                        OnChangeViewReceived(payload, (ChangeView)message);
                        break;

                    case ConsensusMessageType.PrepareRequest:
                        OnPrepareRequestReceived(payload, (PrepareRequest)message);
                        break;

                    case ConsensusMessageType.PrepareResponse:
                        OnPrepareResponseReceived(payload, (PrepareResponse)message);
                        break;
                    }
                }
            }
        }