Example #1
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 || payload.PrevHash != context.PrevHash || payload.BlockIndex != context.BlockIndex)
                    {
                        return;
                    }
                    if (payload.ValidatorIndex >= context.Validators.Length)
                    {
                        return;
                    }
                    ConsensusMessage message = ConsensusMessage.DeserializeFrom(payload.Data);
                    if (message.ViewNumber != context.ViewNumber && message.Type != ConsensusMessageType.ChangeView)
                    {
                        return;
                    }
                    switch (message.Type)
                    {
                    case ConsensusMessageType.ChangeView:
                        OnChangeViewReceived(payload, (ChangeView)message);
                        break;

                    case ConsensusMessageType.PerpareRequest:
                        OnPerpareRequestReceived(payload, (PerpareRequest)message);
                        break;

                    case ConsensusMessageType.PerpareResponse:
                        OnPerpareResponseReceived(payload, (PerpareResponse)message);
                        break;
                    }
                }
            }
        }
        private void LocalNode_NewInventory(object sender, IInventory inventory)
        {
            ConsensusPayload payload = inventory as ConsensusPayload;

            if (payload != null)
            {
                lock (context)
                {
                    if (payload.MinerIndex == context.MinerIndex)
                    {
                        return;
                    }
                    if (payload.Version != ConsensusContext.Version || payload.PrevHash != context.PrevHash || payload.Height != context.Height)
                    {
                        return;
                    }
                    if (payload.MinerIndex >= context.Miners.Length)
                    {
                        return;
                    }
                    ConsensusMessage message = ConsensusMessage.DeserializeFrom(payload.Data);
                    if (message.ViewNumber != context.ViewNumber && message.Type != ConsensusMessageType.ChangeView)
                    {
                        return;
                    }
                    switch (message.Type)
                    {
                    case ConsensusMessageType.ChangeView:
                        OnChangeViewReceived(payload, (ChangeView)message);
                        break;

                    case ConsensusMessageType.PerpareRequest:
                        OnPerpareRequestReceived(payload, (PerpareRequest)message);
                        break;

                    case ConsensusMessageType.PerpareResponse:
                        OnPerpareResponseReceived(payload, (PerpareResponse)message);
                        break;
                    }
                }
            }
            Transaction tx = inventory as Transaction;

            if (tx != null)
            {
                lock (context)
                {
                    if (!context.State.HasFlag(ConsensusState.Backup) || !context.State.HasFlag(ConsensusState.RequestReceived) || context.State.HasFlag(ConsensusState.SignatureSent))
                    {
                        return;
                    }
                    if (context.Transactions.ContainsKey(tx.Hash))
                    {
                        return;
                    }
                    if (!context.TransactionHashes.Contains(tx.Hash))
                    {
                        return;
                    }
                    AddTransaction(tx);
                }
            }
        }