public void AddSignature(BlockSignatures signatures, BlockProposalSignatures proposalSignatures, short issuer) { BlockData <BlockType> resultBlockData = null; BlockProposalSignatures resultProposalSignatures = null; Log.Trace($"Signature revision: {signatures.Revision} issuer: {issuer}.", this); lock (_lock) { _voteKeyProposals[signatures.BlockIssuer].TryGetValue(signatures.Revision, out var proposal); if (proposal != null) { proposal.AddSignature(signatures, proposalSignatures, issuer); if (proposal.IsBlockSignatureValid && proposal.IsBlockProposalSignatureValid) { resultBlockData = new BlockData <BlockType>(proposal.Block as BlockType, proposal.Signatures); resultProposalSignatures = proposal.ProposalSignatures; } } } if (resultBlockData != null && resultProposalSignatures != null) { _voteHandler.NewBlockAvailable(this, resultBlockData, resultProposalSignatures); } }
public void AddSignature(BlockSignatures signatures, BlockProposalSignatures proposalSignatures, short issuer) { var block = Block; if (block != null) { var memeberKey = _members.GetKey(issuer); if (signatures.IsSignatureValid(memeberKey?.PublicKey, issuer, block) && proposalSignatures.IsSignatureValid(memeberKey?.PublicKey, issuer, block)) { lock (_votes) { if (Signatures == null) { Signatures = new BlockSignatures(block); } if (ProposalSignatures == null) { ProposalSignatures = new BlockProposalSignatures(block); } Signatures.AddSignature(signatures.GetSignature(issuer)); ProposalSignatures.AddSignature(proposalSignatures.GetSignature(issuer)); } } } }
public bool IsBlockProposalSignatureValid(Block block, BlockProposalSignatures proposalSignatures) { if (block != null && proposalSignatures != null) { var voteKeys = GetVoteMembers(block.ChainType, block.ChainId, block.ChainIndex, block.Timestamp); if (voteKeys != null) { return(voteKeys.IsBlockSignatureValid(block, proposalSignatures)); } } return(false); }
async Task PhaseTimerLoop() { while (true) { try { if (_cancelToken.IsCancellationRequested) { return; } _voteHandler.BroadcastCurrentRevision(this, NextBlockId, _localRevision); AddCurrentRevision(NextBlockId, _localRevision, LocalIssuer); await Task.Delay(750, _cancelToken.Token); if (_cancelToken.IsCancellationRequested) { return; } if (!HasEnoughLegitProposals()) { var localBlock = _voteHandler.GetBlockProposal(this, LastBlock, _localRevision); if (localBlock != null) { var vote = AddProposal(NextBlockId, _localRevision, localBlock, LocalIssuer); _voteHandler.BroadcastProposal(this, NextBlockId, _localRevision, localBlock); } } if (_cancelToken.IsCancellationRequested) { return; } await Task.Delay(750, _cancelToken.Token); if (_cancelToken.IsCancellationRequested) { return; } var proposal = GetBestProposal(); if (proposal == null) { goto repeat; } var block = proposal.Block; var blockSignatures = new BlockSignatures(block); blockSignatures.AddSignature(LocalIssuer, block, LocalIssuerKey); var blockProposalSignatures = new BlockProposalSignatures(block); blockProposalSignatures.AddSignature(LocalIssuer, block, LocalIssuerKey); _voteHandler.BroadcastSignature(this, blockSignatures, blockProposalSignatures); AddSignature(blockSignatures, blockProposalSignatures, LocalIssuer); if (_cancelToken.IsCancellationRequested) { return; } await Task.Delay(750, _cancelToken.Token); if (_cancelToken.IsCancellationRequested) { return; } repeat: Log.Trace($"No approved block found for blockid {NextBlockId} with revision {_localRevision}.", this); // get the highest revision with a certain amount of approval var approvedRevision = 0; lock (_lock) { var revisions = _revisionsVoteKeys.Reverse(); foreach (var r in revisions) { if ((r.Value.Count / (float)_members.Count + float.Epsilon) >= 0.5f) { approvedRevision = r.Key; break; } } } _localRevision++; if (approvedRevision > _localRevision) { Log.Trace($"Resetting revision from {_localRevision} to {approvedRevision} for blockid {NextBlockId}.", this); } _localRevision = Math.Max(_localRevision, approvedRevision); } catch (TaskCanceledException) { } catch (Exception ex) { Log.HandleException(ex, this); } } }
public override void NewBlockAvailable(VoteProcess <MaintainBlock> voteProcess, BlockData <MaintainBlock> blockData, BlockProposalSignatures blockProposalSignatures) { base.NewBlockAvailable(voteProcess, blockData, blockProposalSignatures); var block = blockData.Block; TaskRunner.Run(async() => { await _node.SyncManager.HandleBlockData(blockData, GetVoteConnections()); await _node.NodeServer.Broadcast(new NodeBlockDataMessage(block.ChainType, block.BlockId, block.ChainId, block.ChainIndex, _node.NodeConfiguration.LocaleNodeInfo.IsPublicEndPoint ? null : blockData) { SignKey = _node.NodeConfiguration.LocaleNodePrivateKey }, null); }); }
protected override void Unpack(Unpacker unpacker) { base.Unpack(unpacker); ServiceBlock = Block.Restore <ServiceBlock>(unpacker); ProposalSignatures = new BlockProposalSignatures(unpacker); }
public ServiceBlockCoreTransaction(ServiceBlock serviceBlock, BlockProposalSignatures proposalSignatures, long accountId) : base(CoreTransactionTypes.ServiceBlock, accountId) { ServiceBlock = serviceBlock; ProposalSignatures = proposalSignatures; }
public override void NewBlockAvailable(VoteProcess <ServiceBlock> voteProcess, BlockData <ServiceBlock> blockData, BlockProposalSignatures blockProposalSignatures) { base.NewBlockAvailable(voteProcess, blockData, blockProposalSignatures); var block = blockData.Block; if (block.Issuer == LocalKeyIndex) { var message = new NodeTransactionMessage(new ServiceBlockCoreTransaction(blockData.Block, blockProposalSignatures, _chainAccountId) { SignKey = _localKey }); message.ToByteArray(); _node.TransactionManager.AddNodeTransaction(message, null); _ = _node.NodeServer.Broadcast(message, null); } }
protected override void Unpack(Unpacker unpacker) { base.Unpack(unpacker); BlockSignatures = new BlockSignatures(unpacker); ProposalSignatures = new BlockProposalSignatures(unpacker); }
public CouncilBlockSignatureMessage(BlockSignatures signatures, BlockProposalSignatures proposalSignatures, ChainType chainType, int chainId, uint chainIndex, short issuer, Key issuerKey) : base(CouncilMessageTypes.BlockSignature, chainType, chainId, chainIndex, issuer, issuerKey) { BlockSignatures = signatures; ProposalSignatures = proposalSignatures; }