예제 #1
0
        private Block SignBlock(ConsensusContext context)
        {
            context.Block.MerkleRoot = null;

            for (int x = 0; x < _validatorKeys.Length; x++)
            {
                _context.MyIndex = x;

                var com = _context.MakeCommit();
                _context.CommitPayloads[_context.MyIndex] = com;
            }

            // Manual block sign

            Contract contract            = Contract.CreateMultiSigContract(context.M, context.Validators);
            ContractParametersContext sc = new ContractParametersContext(context.Block);

            for (int i = 0, j = 0; i < context.Validators.Length && j < context.M; i++)
            {
                if (context.CommitPayloads[i]?.ConsensusMessage.ViewNumber != context.ViewNumber)
                {
                    continue;
                }
                sc.AddSignature(contract, context.Validators[i], context.CommitPayloads[i].GetDeserializedMessage <Commit>().Signature);
                j++;
            }
            context.Block.Witness      = sc.GetWitnesses()[0];
            context.Block.Transactions = context.TransactionHashes.Select(p => context.Transactions[p]).ToArray();
            return(context.Block);
        }
예제 #2
0
        private Block SignBlock(ConsensusContext context)
        {
            context.Block.MerkleRoot = null;

            // Fake commits

            for (int x = 0; x < _validatorKeys.Length; x++)
            {
                _context.MyIndex = x;

                var com = _context.MakeCommit();
                _context.CommitPayloads[_context.MyIndex] = com;
            }

            return(context.CreateBlock());
        }
예제 #3
0
        Block RunConsensus()
        {
            if (chain.ConsensusNodes.Count == 1)
            {
                var ctx = new ConsensusContext(nodeWallet, Blockchain.Singleton.Store);
                ctx.Reset(0);
                ctx.MakePrepareRequest();
                ctx.MakeCommit();
                return(ctx.CreateBlock());
            }

            // create ConsensusContext for each ConsensusNode
            var contexts = new ConsensusContext[chain.ConsensusNodes.Count];

            for (int x = 0; x < contexts.Length; x++)
            {
                contexts[x] = new ConsensusContext(DevWallet.FromExpressWallet(chain.ConsensusNodes[x].Wallet), Blockchain.Singleton.Store);
                contexts[x].Reset(0);
            }

            // find the primary node for this consensus round
            var primary = contexts.Single(c => c.IsPrimary);
            var prepareRequestPayload = primary.MakePrepareRequest();

            for (int x = 0; x < contexts.Length; x++)
            {
                var context = contexts[x];
                if (context.MyIndex == primary.MyIndex)
                {
                    continue;
                }
                var prepareRequestMessage = context.GetMessage <PrepareRequest>(prepareRequestPayload);
                OnPrepareRequestReceived(context, prepareRequestPayload, prepareRequestMessage);
                var commitPayload = context.MakeCommit();
                var commitMessage = primary.GetMessage <Commit>(commitPayload);
                OnCommitReceived(primary, commitPayload, commitMessage);
            }

            return(primary.CreateBlock());
        }