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

Ensure that the block is valid before relay it on CheckCommits #1220

Merged
merged 16 commits into from
Nov 24, 2019
25 changes: 10 additions & 15 deletions neo.UnitTests/Consensus/UT_Consensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,36 +177,31 @@ public void TestSerializeAndDeserializeConsensusContext()
consensusContext.Transactions = txs.ToDictionary(p => p.Hash);

consensusContext.PreparationPayloads = new ConsensusPayload[consensusContext.Validators.Length];
var prepareRequestMessage = new PrepareRequest
{
TransactionHashes = consensusContext.TransactionHashes,
Timestamp = 23
};
consensusContext.PreparationPayloads[6] = MakeSignedPayload(consensusContext, prepareRequestMessage, 6, new[] { (byte)'3', (byte)'!' });
consensusContext.PreparationPayloads[0] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 0, new[] { (byte)'t', (byte)'e' });
consensusContext.PreparationPayloads[1] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 1, new[] { (byte)'s', (byte)'t' });
consensusContext.PreparationPayloads[6] = MakeSignedPayload(consensusContext, new PrepareRequest { TransactionHashes = consensusContext.TransactionHashes, Timestamp = 23, ViewNumber = 2 }, 6, new[] { (byte)'3', (byte)'!' });
consensusContext.PreparationPayloads[0] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 0, new[] { (byte)'t', (byte)'e' });
consensusContext.PreparationPayloads[1] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 1, new[] { (byte)'s', (byte)'t' });
consensusContext.PreparationPayloads[2] = null;
consensusContext.PreparationPayloads[3] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 3, new[] { (byte)'1', (byte)'2' });
consensusContext.PreparationPayloads[3] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 3, new[] { (byte)'1', (byte)'2' });
consensusContext.PreparationPayloads[4] = null;
consensusContext.PreparationPayloads[5] = null;

consensusContext.CommitPayloads = new ConsensusPayload[consensusContext.Validators.Length];
using (SHA256 sha256 = SHA256.Create())
{
consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()) }, 3, new[] { (byte)'3', (byte)'4' });
consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()) }, 3, new[] { (byte)'6', (byte)'7' });
consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()), ViewNumber = 2 }, 3, new[] { (byte)'3', (byte)'4' });
consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()), ViewNumber = 2 }, 3, new[] { (byte)'6', (byte)'7' });
}

consensusContext.Block.Timestamp = TimeProvider.Current.UtcNow.ToTimestampMS();

consensusContext.ChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length];
consensusContext.ChangeViewPayloads[0] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 6 }, 0, new[] { (byte)'A' });
consensusContext.ChangeViewPayloads[1] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 5 }, 1, new[] { (byte)'B' });
consensusContext.ChangeViewPayloads[0] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 6 }, 0, new[] { (byte)'A' });
consensusContext.ChangeViewPayloads[1] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 5 }, 1, new[] { (byte)'B' });
consensusContext.ChangeViewPayloads[2] = null;
consensusContext.ChangeViewPayloads[3] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = uint.MaxValue }, 3, new[] { (byte)'C' });
consensusContext.ChangeViewPayloads[3] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = uint.MaxValue }, 3, new[] { (byte)'C' });
consensusContext.ChangeViewPayloads[4] = null;
consensusContext.ChangeViewPayloads[5] = null;
consensusContext.ChangeViewPayloads[6] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 1 }, 6, new[] { (byte)'D' });
consensusContext.ChangeViewPayloads[6] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 1 }, 6, new[] { (byte)'D' });

consensusContext.LastChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length];

Expand Down
17 changes: 3 additions & 14 deletions neo.UnitTests/Consensus/UT_ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Neo.Consensus;
using Neo.IO;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.Wallets;
using System;
Expand Down Expand Up @@ -119,6 +118,8 @@ private Block SignBlock(ConsensusContext context)
{
context.Block.MerkleRoot = null;

// Fake commits

for (int x = 0; x < _validatorKeys.Length; x++)
{
_context.MyIndex = x;
Expand All @@ -127,19 +128,7 @@ private Block SignBlock(ConsensusContext context)
_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;
return context.CreateBlock();
}

private void EnsureContext(ConsensusContext context, params Transaction[] expected)
Expand Down
27 changes: 23 additions & 4 deletions neo/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public ConsensusContext(Wallet wallet, Store store)

public Block CreateBlock()
{
EnsureHeader();
Contract contract = Contract.CreateMultiSigContract(M, Validators);
ContractParametersContext sc = new ContractParametersContext(Block);
for (int i = 0, j = 0; i < Validators.Length && j < M; i++)
Expand All @@ -108,10 +109,10 @@ public void Deserialize(BinaryReader reader)
ViewNumber = reader.ReadByte();
TransactionHashes = reader.ReadSerializableArray<UInt256>();
Transaction[] transactions = reader.ReadSerializableArray<Transaction>(Block.MaxTransactionsPerBlock);
PreparationPayloads = reader.ReadNullableArray<ConsensusPayload>(Blockchain.MaxValidators);
CommitPayloads = reader.ReadNullableArray<ConsensusPayload>(Blockchain.MaxValidators);
ChangeViewPayloads = reader.ReadNullableArray<ConsensusPayload>(Blockchain.MaxValidators);
LastChangeViewPayloads = reader.ReadNullableArray<ConsensusPayload>(Blockchain.MaxValidators);
PreparationPayloads = ReadConsensusPayload(reader, ViewNumber);
CommitPayloads = ReadConsensusPayload(reader, ViewNumber);
ChangeViewPayloads = ReadConsensusPayload(reader, ViewNumber);
LastChangeViewPayloads = ReadConsensusPayload(reader, ViewNumber);
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (TransactionHashes.Length == 0 && !RequestSentOrReceived)
TransactionHashes = null;
Transactions = transactions.Length == 0 && !RequestSentOrReceived ? null : transactions.ToDictionary(p => p.Hash);
Expand All @@ -123,6 +124,24 @@ public void Deserialize(BinaryReader reader)
}
}

private static ConsensusPayload[] ReadConsensusPayload(BinaryReader reader, byte viewNumber)
{
// Read
var payloads = reader.ReadNullableArray<ConsensusPayload>(Blockchain.MaxValidators);

// Remove other views
for (int x = 0; x < payloads.Length; x++)
{
var p = payloads[x];
if (p != null && p.ConsensusMessage.ViewNumber != viewNumber)
{
payloads[x] = null;
}
}

return payloads;
}

public void Dispose()
{
Snapshot?.Dispose();
Expand Down