Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix consensus #425

Merged
merged 3 commits into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ private void OnChangeViewReceived(ConsensusPayload payload, ChangeView message)

private void OnConsensusPayload(ConsensusPayload payload)
{
if (context.State.HasFlag(ConsensusState.BlockSent)) return;
if (payload.ValidatorIndex == context.MyIndex) return;
if (payload.Version != ConsensusContext.Version)
return;
Expand Down Expand Up @@ -248,16 +249,32 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m
context.NextConsensus = message.NextConsensus;
context.TransactionHashes = message.TransactionHashes;
context.Transactions = new Dictionary<UInt256, Transaction>();
if (!Crypto.Default.VerifySignature(context.MakeHeader().GetHashData(), message.Signature, context.Validators[payload.ValidatorIndex].EncodePoint(false))) return;
context.Signatures = new byte[context.Validators.Length][];
byte[] hashData = context.MakeHeader().GetHashData();
if (!Crypto.Default.VerifySignature(hashData, message.Signature, context.Validators[payload.ValidatorIndex].EncodePoint(false))) return;
for (int i = 0; i < context.Signatures.Length; i++)
if (context.Signatures[i] != null)
if (!Crypto.Default.VerifySignature(hashData, context.Signatures[i], context.Validators[i].EncodePoint(false)))
context.Signatures[i] = null;
context.Signatures[payload.ValidatorIndex] = message.Signature;
Dictionary<UInt256, Transaction> mempool = Blockchain.Singleton.GetMemoryPool().ToDictionary(p => p.Hash);
List<Transaction> unverified = new List<Transaction>();
foreach (UInt256 hash in context.TransactionHashes.Skip(1))
{
if (mempool.TryGetValue(hash, out Transaction tx))
{
if (!AddTransaction(tx, false))
return;
}
else
{
tx = Blockchain.Singleton.GetUnverifiedTransaction(hash);
if (tx != null)
unverified.Add(tx);
}
}
foreach (Transaction tx in unverified)
if (!AddTransaction(tx, true))
return;
if (!AddTransaction(message.MinerTransaction, true)) return;
if (context.Transactions.Count < context.TransactionHashes.Length)
{
Expand All @@ -272,12 +289,17 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m
private void OnPrepareResponseReceived(ConsensusPayload payload, PrepareResponse message)
{
Log($"{nameof(OnPrepareResponseReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex}");
if (context.State.HasFlag(ConsensusState.BlockSent)) return;
if (context.Signatures[payload.ValidatorIndex] != null) return;
Block header = context.MakeHeader();
if (header == null || !Crypto.Default.VerifySignature(header.GetHashData(), message.Signature, context.Validators[payload.ValidatorIndex].EncodePoint(false))) return;
context.Signatures[payload.ValidatorIndex] = message.Signature;
CheckSignatures();
byte[] hashData = context.MakeHeader()?.GetHashData();
if (hashData == null)
{
context.Signatures[payload.ValidatorIndex] = message.Signature;
Copy link
Member

@vncoelho vncoelho Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikzhang, I am double checking here and just to be sure I am thinking why message.Signature is assigned here to the Signatures array.

Should not it be context.Signatures[payload.ValidatorIndex] = null?
If the hashData is null it means that the payload is kind of empty, right?

}
else if (Crypto.Default.VerifySignature(hashData, message.Signature, context.Validators[payload.ValidatorIndex].EncodePoint(false)))
{
context.Signatures[payload.ValidatorIndex] = message.Signature;
CheckSignatures();
}
}

protected override void OnReceive(object message)
Expand Down Expand Up @@ -310,6 +332,7 @@ private void OnStart()

private void OnTimer(Timer timer)
{
if (context.State.HasFlag(ConsensusState.BlockSent)) return;
if (timer.Height != context.BlockIndex || timer.ViewNumber != context.ViewNumber) return;
Log($"timeout: height={timer.Height} view={timer.ViewNumber} state={context.State}");
if (context.State.HasFlag(ConsensusState.Primary) && !context.State.HasFlag(ConsensusState.RequestSent))
Expand Down Expand Up @@ -339,7 +362,7 @@ private void OnTimer(Timer timer)
private void OnTransaction(Transaction transaction)
{
if (transaction.Type == TransactionType.MinerTransaction) return;
if (!context.State.HasFlag(ConsensusState.Backup) || !context.State.HasFlag(ConsensusState.RequestReceived) || context.State.HasFlag(ConsensusState.SignatureSent) || context.State.HasFlag(ConsensusState.ViewChanging))
if (!context.State.HasFlag(ConsensusState.Backup) || !context.State.HasFlag(ConsensusState.RequestReceived) || context.State.HasFlag(ConsensusState.SignatureSent) || context.State.HasFlag(ConsensusState.ViewChanging) || context.State.HasFlag(ConsensusState.BlockSent))
return;
if (context.Transactions.ContainsKey(transaction.Hash)) return;
if (!context.TransactionHashes.Contains(transaction.Hash)) return;
Expand Down
12 changes: 11 additions & 1 deletion neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public class ImportCompleted { }
private readonly Dictionary<UInt256, Block> block_cache = new Dictionary<UInt256, Block>();
private readonly Dictionary<uint, Block> block_cache_unverified = new Dictionary<uint, Block>();
private readonly ConcurrentDictionary<UInt256, Transaction> mem_pool = new ConcurrentDictionary<UInt256, Transaction>();
private readonly ConcurrentDictionary<UInt256, Transaction> mem_pool_unverified = new ConcurrentDictionary<UInt256, Transaction>();
internal readonly RelayCache RelayCache = new RelayCache(100);
private readonly HashSet<IActorRef> subscribers = new HashSet<IActorRef>();
private Snapshot currentSnapshot;
Expand Down Expand Up @@ -233,6 +234,12 @@ public Transaction GetTransaction(UInt256 hash)
return Store.GetTransaction(hash);
}

internal Transaction GetUnverifiedTransaction(UInt256 hash)
{
mem_pool_unverified.TryGetValue(hash, out Transaction transaction);
return transaction;
}

private void OnImport(IEnumerable<Block> blocks)
{
foreach (Block block in blocks)
Expand Down Expand Up @@ -380,12 +387,15 @@ private void OnPersistCompleted(Block block)
block_cache.Remove(block.Hash);
foreach (Transaction tx in block.Transactions)
mem_pool.TryRemove(tx.Hash, out _);

mem_pool_unverified.Clear();
foreach (Transaction tx in mem_pool.Values
.OrderByDescending(p => p.NetworkFee / p.Size)
.ThenByDescending(p => p.NetworkFee)
.ThenByDescending(p => new BigInteger(p.Hash.ToArray())))
{
mem_pool_unverified.TryAdd(tx.Hash, tx);
Self.Tell(tx, ActorRefs.NoSender);
}
mem_pool.Clear();
PersistCompleted completed = new PersistCompleted { Block = block };
system.Consensus?.Tell(completed);
Expand Down