Example #1
0
        private bool CheckPrepareResponse()
        {
            if (context.TransactionHashes.Length == context.Transactions.Count)
            {
                // if we are the primary for this view, but acting as a backup because we recovered our own
                // previously sent prepare request, then we don't want to send a prepare response.
                if (context.IsPrimary || context.WatchOnly)
                {
                    return(true);
                }

                // Check maximum block size via Native Contract policy
                if (context.GetExpectedBlockSize() > NativeContract.Policy.GetMaxBlockSize(context.Snapshot))
                {
                    Log($"rejected block: {context.Block.Index}{Environment.NewLine} The size exceed the policy", LogLevel.Warning);
                    RequestChangeView(ChangeViewReason.BlockRejectedByPolicy);
                    return(false);
                }

                // Timeout extension due to prepare response sent
                // around 2*15/M=30.0/5 ~ 40% block time (for M=5)
                ExtendTimerByFactor(2);

                Log($"send prepare response");
                localNode.Tell(new LocalNode.SendDirectly {
                    Inventory = context.MakePrepareResponse()
                });
                CheckPreparations();
            }
            return(true);
        }
Example #2
0
 private bool AddTransaction(Transaction tx, bool verify)
 {
     if (Blockchain.Default.ContainsTransaction(tx.Hash) ||
         (verify && !tx.Verify(context.Transactions.Values)) ||
         !CheckPolicy(tx))
     {
         Log($"reject tx: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}");
         RequestChangeView();
         return(false);
     }
     context.Transactions[tx.Hash] = tx;
     if (context.TransactionHashes.Length == context.Transactions.Count)
     {
         if (Blockchain.GetConsensusAddress(Blockchain.Default.GetValidators(context.Transactions.Values).ToArray()).Equals(context.NextConsensus))
         {
             Log($"send perpare response");
             // 设置共识状态为已发送签名
             context.State |= ConsensusState.SignatureSent;
             // 添加本地签名到签名列表
             context.Signatures[context.MyIndex] = context.MakeHeader().Sign(context.KeyPair);
             // 广播共识响应
             SignAndRelay(context.MakePrepareResponse(context.Signatures[context.MyIndex]));
             // 检查签名状态是否符合共识要求
             CheckSignatures();
         }
         else
         {
             RequestChangeView();
             return(false);
         }
     }
     return(true);
 }
Example #3
0
 private bool AddTransaction(Transaction tx, bool verify)
 {
     if (context.Snapshot.ContainsTransaction(tx.Hash) ||
         (verify && !tx.Verify(context.Snapshot, context.Transactions.Values)) ||
         !Plugin.CheckPolicy(tx))
     {
         Log($"reject tx: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}", LogLevel.Warning);
         RequestChangeView();
         return(false);
     }
     context.Transactions[tx.Hash] = tx;
     if (context.TransactionHashes.Length == context.Transactions.Count)
     {
         if (context.VerifyRequest())
         {
             Log($"send prepare response");
             context.State |= ConsensusState.SignatureSent;
             context.Signatures[context.MyIndex] = context.MakeHeader().Sign(context.KeyPair);
             SignAndRelay(context.MakePrepareResponse(context.Signatures[context.MyIndex]));
             CheckSignatures();
         }
         else
         {
             RequestChangeView();
             return(false);
         }
     }
     return(true);
 }
Example #4
0
        private bool AddTransaction(Transaction tx, bool verify)
        {
            ReportNeoBlockchain reportObj = new ReportNeoBlockchain("[NeoConsensusService-AddTransaction]");

            if (Blockchain.Default.ContainsTransaction(tx.Hash) ||
                (verify && !tx.Verify(context.Transactions.Values)) ||
                !CheckPolicy(tx))
            {
                Log($"reject tx: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}");
                RequestChangeView();
                return(false);
            }
            context.Transactions[tx.Hash] = tx;
            if (context.TransactionHashes.Length == context.Transactions.Count)
            {
                if (Blockchain.GetConsensusAddress(Blockchain.Default.GetValidators(context.Transactions.Values).ToArray()).Equals(context.NextConsensus))
                {
                    Log($"send perpare response");
                    context.State |= ConsensusState.SignatureSent;
                    context.Signatures[context.MyIndex] = context.MakeHeader().Sign(context.KeyPair);
                    SignAndRelay(context.MakePrepareResponse(context.Signatures[context.MyIndex]));
                    CheckSignatures();
                }
                else
                {
                    RequestChangeView();
                    return(false);
                }
            }

            reportObj.appendElapsedTime();
            return(true);
        }
Example #5
0
        private bool AddTransaction(Transaction tx, bool verify)
        {
            if (verify && !tx.Verify(context.Snapshot, context.Transactions.Values))
            {
                Log($"Invalid transaction: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}", LogLevel.Warning);
                RequestChangeView(ChangeViewReason.TxInvalid);
                return(false);
            }
            if (!NativeContract.Policy.CheckPolicy(tx, context.Snapshot))
            {
                Log($"reject tx: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}", LogLevel.Warning);
                RequestChangeView(ChangeViewReason.TxRejectedByPolicy);
                return(false);
            }
            context.Transactions[tx.Hash] = tx;
            if (context.TransactionHashes.Length == context.Transactions.Count)
            {
                // if we are the primary for this view, but acting as a backup because we recovered our own
                // previously sent prepare request, then we don't want to send a prepare response.
                if (context.IsPrimary || context.WatchOnly)
                {
                    return(true);
                }

                // Timeout extension due to prepare response sent
                // around 2*15/M=30.0/5 ~ 40% block time (for M=5)
                ExtendTimerByFactor(2);

                Log($"send prepare response");
                localNode.Tell(new LocalNode.SendDirectly {
                    Inventory = context.MakePrepareResponse()
                });
                CheckPreparations();
            }
            return(true);
        }