/// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="kvPropertyService">Key-value property service.</param>
 /// /// <param name="consensusContext">Consensus context.</param>
 public KvPropertiesController(
     IKvPropertyService kvPropertyService,
     IConsensusContext consensusContext)
 {
     _kvPropertyService = kvPropertyService;
     _consensusContext  = consensusContext;
 }
Beispiel #2
0
        public ConsensusPayload[] GetPrepareResponsePayloads(IConsensusContext context, ConsensusPayload payload)
        {
            UInt256 preparationHash = PreparationHash ?? context.PreparationPayloads[context.PrimaryIndex]?.Hash;

            if (preparationHash is null)
            {
                return(new ConsensusPayload[0]);
            }
            return(PreparationMessages.Values.Where(p => p.ValidatorIndex != context.PrimaryIndex).Select(p => new ConsensusPayload
            {
                Version = payload.Version,
                PrevHash = payload.PrevHash,
                BlockIndex = payload.BlockIndex,
                ValidatorIndex = p.ValidatorIndex,
                ConsensusMessage = new PrepareResponse
                {
                    ViewNumber = ViewNumber,
                    PreparationHash = preparationHash
                },
                Witness = new Witness
                {
                    InvocationScript = p.InvocationScript,
                    VerificationScript = Contract.CreateSignatureRedeemScript(context.Validators[p.ValidatorIndex])
                }
            }).ToArray());
        }
Beispiel #3
0
        public Candidate(IConsensusContext consensusContext, ILogger <IConsensusContext> logger)
        {
            _consensusContext = consensusContext;
            _logger           = logger;

            ClearVoteGranted();
        }
 public ConsensusService(IActorRef localNode, IActorRef taskManager, IConsensusContext context)
 {
     this.localNode   = localNode;
     this.taskManager = taskManager;
     this.context     = context;
     Context.System.EventStream.Subscribe(Self, typeof(Blockchain.PersistCompleted));
 }
Beispiel #5
0
        public Leader(IConsensusContext consensusContext, ILogger <IConsensusContext> logger)
        {
            _consensusContext = consensusContext;
            _logger           = logger;

            _consensusContext.NodeVote.LeaderNode = _consensusContext.CurrentNode;
            _consensusContext.NodeVote.VoteTerm   = 0;
        }
Beispiel #6
0
        public static ulong GetNonce(this IConsensusContext context)
        {
            byte[] nonce = new byte[sizeof(ulong)];
            Random rand  = new Random();

            rand.NextBytes(nonce);
            return(nonce.ToUInt64(0));
        }
Beispiel #7
0
 public LogReplication(
     IConsensusContext consensusContext,
     HttpClient httpClient,
     ILogger <LogReplication> logger)
 {
     _consensusContext = consensusContext;
     _httpClient       = httpClient;
     _logger           = logger;
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="servicesService">Services service.</param>
 /// <param name="serviceHealthService">Searvices health service.</param>
 /// <param name="consensusContext">Context of consensus protocol.</param>
 public ServicesController(
     IServicesService servicesService,
     IServiceHealthService serviceHealthService,
     IConsensusContext consensusContext)
 {
     _servicesService      = servicesService;
     _serviceHealthService = serviceHealthService;
     _consensusContext     = consensusContext;
 }
Beispiel #9
0
 public ConsensusService(IActorRef localNode, IActorRef taskManager, Wallet wallet, UInt160 chainHash)
 {
     this.chainHash    = chainHash;
     this.localNode    = localNode;
     this.taskManager  = taskManager;
     this.blockchain   = ZoroChainSystem.Singleton.AskBlockchain(chainHash);
     this.localNodeObj = ZoroChainSystem.Singleton.AskLocalNode(chainHash);
     this.context      = new ConsensusContext(blockchain, wallet);
     Context.System.EventStream.Subscribe(Self, typeof(Blockchain.PersistCompleted));
 }
 public KvPropertyService(
     IRepository <KvProperty> kvPropertyRepository,
     IKvPropertyValidator kvPropertyValidator,
     ILogReplication logReplication,
     IConsensusContext consensusContext)
 {
     _kvPropertyRepository = kvPropertyRepository;
     _kvPropertyValidator  = kvPropertyValidator;
     _logReplication       = logReplication;
     _consensusContext     = consensusContext;
 }
Beispiel #11
0
 public ServicesService(
     IRepository <Service> serviceRepository,
     IServiceValidator serviceValidator,
     IMemoryCache memoryCache,
     IServiceHealthWorker serviceHealthWorker,
     ILogReplication logReplication,
     IConsensusContext consensusContext)
 {
     _serviceRepository   = serviceRepository;
     _serviceValidator    = serviceValidator;
     _memoryCache         = memoryCache;
     _serviceHealthWorker = serviceHealthWorker;
     _logReplication      = logReplication;
     _consensusContext    = consensusContext;
 }
Beispiel #12
0
 private static ConsensusPayload MakeSignedPayload(IConsensusContext context, ConsensusMessage message, ushort validatorIndex, byte[] witnessInvocationScript)
 {
     return(new ConsensusPayload
     {
         Version = ConsensusContext.Version,
         PrevHash = context.PrevHash,
         BlockIndex = context.BlockIndex,
         ValidatorIndex = validatorIndex,
         ConsensusMessage = message,
         Witness = new Witness
         {
             InvocationScript = witnessInvocationScript,
             VerificationScript = Contract.CreateSignatureRedeemScript(context.Validators[validatorIndex])
         }
     });
 }
Beispiel #13
0
 public ConsensusPayload[] GetChangeViewPayloads(IConsensusContext context, ConsensusPayload payload)
 {
     return(ChangeViewMessages.Values.Select(p => new ConsensusPayload
     {
         Version = payload.Version,
         PrevHash = payload.PrevHash,
         BlockIndex = payload.BlockIndex,
         ValidatorIndex = p.ValidatorIndex,
         ConsensusMessage = new ChangeView
         {
             ViewNumber = p.OriginalViewNumber,
             Timestamp = p.Timestamp
         },
         Witness = new Witness
         {
             InvocationScript = p.InvocationScript,
             VerificationScript = Contract.CreateSignatureRedeemScript(context.Validators[p.ValidatorIndex])
         }
     }).ToArray());
 }
Beispiel #14
0
 public ConsensusPayload[] GetCommitPayloadsFromRecoveryMessage(IConsensusContext context, ConsensusPayload payload)
 {
     return(CommitMessages.Values.Select(p => new ConsensusPayload
     {
         Version = payload.Version,
         PrevHash = payload.PrevHash,
         BlockIndex = payload.BlockIndex,
         ValidatorIndex = p.ValidatorIndex,
         ConsensusMessage = new Commit
         {
             ViewNumber = p.ViewNumber,
             Signature = p.Signature
         },
         Witness = new Witness
         {
             InvocationScript = p.InvocationScript,
             VerificationScript = Contract.CreateSignatureRedeemScript(context.Validators[p.ValidatorIndex])
         }
     }).ToArray());
 }
Beispiel #15
0
        public static bool Load(this IConsensusContext context, Store store)
        {
            byte[] data = store.Get(CN_Context, new byte[0]);

            if (data is null || data.Length == 0)
            {
                return(false);
            }

            using (MemoryStream ms = new MemoryStream(data, false))
                using (BinaryReader reader = new BinaryReader(ms))
                {
                    try
                    {
                        context.Deserialize(reader);
                    }
                    catch
                    {
                        return(false);
                    }
                    return(true);
                }
        }
Beispiel #16
0
 public ConsensusPayload GetPrepareRequestPayload(IConsensusContext context, ConsensusPayload payload)
 {
     if (PrepareRequestMessage == null)
     {
         return(null);
     }
     if (!PreparationMessages.TryGetValue((int)context.PrimaryIndex, out RecoveryMessage.PreparationPayloadCompact compact))
     {
         return(null);
     }
     return(new ConsensusPayload
     {
         Version = payload.Version,
         PrevHash = payload.PrevHash,
         BlockIndex = payload.BlockIndex,
         ValidatorIndex = (ushort)context.PrimaryIndex,
         ConsensusMessage = PrepareRequestMessage,
         Witness = new Witness
         {
             InvocationScript = compact.InvocationScript,
             VerificationScript = Contract.CreateSignatureRedeemScript(context.Validators[context.PrimaryIndex])
         }
     });
 }
Beispiel #17
0
        public static uint GetPrimaryIndex(this IConsensusContext context, byte viewNumber)
        {
            int p = ((int)context.BlockIndex - viewNumber) % context.Validators.Length;

            return(p >= 0 ? (uint)p : (uint)(p + context.Validators.Length));
        }
Beispiel #18
0
 // A possible attack can happen if the last node to commit is malicious and either sends change view after his
 // commit to stall nodes in a higher view, or if he refuses to send recovery messages. In addition, if a node
 // asking change views loses network or crashes and comes back when nodes are committed in more than one higher
 // numbered view, it is possible for the node accepting recovery and commit in any of the higher views, thus
 // potentially splitting nodes among views and stalling the network.
 public static bool MoreThanFNodesCommitted(this IConsensusContext context) => context.CommitPayloads.Count(p => p != null) > context.F();
Beispiel #19
0
 public static bool NotAcceptingPayloadsDueToViewChanging(this IConsensusContext context) => context.ViewChanging() && !context.MoreThanFNodesCommitted();
Beispiel #20
0
 public static bool ViewChanging(this IConsensusContext context) => !context.WatchOnly() && context.ChangeViewPayloads[context.MyIndex]?.GetDeserializedMessage <ChangeView>().NewViewNumber > context.ViewNumber;
Beispiel #21
0
 public static bool BlockSent(this IConsensusContext context) => context.Block != null;
Beispiel #22
0
 public static bool CommitSent(this IConsensusContext context) => !context.WatchOnly() && context.CommitPayloads[context.MyIndex] != null;
Beispiel #23
0
 public static bool RequestSentOrReceived(this IConsensusContext context) => context.PreparationPayloads[context.PrimaryIndex] != null;
Beispiel #24
0
 public static Header PrevHeader(this IConsensusContext context) => context.Snapshot.GetHeader(context.PrevHash);
Beispiel #25
0
 public static bool WatchOnly(this IConsensusContext context) => context.MyIndex < 0;
Beispiel #26
0
 public Follower(IConsensusContext consensusContext, ILogger <IConsensusContext> logger)
 {
     _consensusContext = consensusContext;
     _logger           = logger;
 }
Beispiel #27
0
 public static bool IsPrimary(this IConsensusContext context) => context.MyIndex == context.PrimaryIndex;
Beispiel #28
0
 public static bool ResponseSent(this IConsensusContext context) => !context.WatchOnly() && context.PreparationPayloads[context.MyIndex] != null;
Beispiel #29
0
 public ConsensusService(IActorRef localNode, IActorRef taskManager, IConsensusContext context)
 {
     this.localNode   = localNode;
     this.taskManager = taskManager;
     this.context     = context;
 }
Beispiel #30
0
 public static bool IsBackup(this IConsensusContext context) => context.MyIndex >= 0 && context.MyIndex != context.PrimaryIndex;