Skip to content

Commit

Permalink
fix consensus (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Dec 10, 2018
1 parent 4f97a4b commit f454e98
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion neo/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class ConsensusContext : IConsensusContext

public int M => Validators.Length - (Validators.Length - 1) / 3;
public Header PrevHeader => snapshot.GetHeader(PrevHash);
public bool ContainsTransaction(UInt256 hash) => snapshot.ContainsTransaction(hash);
public bool TransactionExists(UInt256 hash) => snapshot.ContainsTransaction(hash);
public bool VerifyTransaction(Transaction tx) => tx.Verify(snapshot, Transactions.Values);

public ConsensusContext(Wallet wallet)
Expand Down
15 changes: 12 additions & 3 deletions neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ public ConsensusService(IActorRef localNode, IActorRef taskManager, IConsensusCo

private bool AddTransaction(Transaction tx, bool verify)
{
if (context.ContainsTransaction(tx.Hash) ||
(verify && !context.VerifyTransaction(tx)) ||
!Plugin.CheckPolicy(tx))
if (verify && !context.VerifyTransaction(tx))
{
Log($"Invalid transaction: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}", LogLevel.Warning);
RequestChangeView();
return false;
}
if (!Plugin.CheckPolicy(tx))
{
Log($"reject tx: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}", LogLevel.Warning);
RequestChangeView();
Expand Down Expand Up @@ -197,6 +201,11 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m
Log($"Timestamp incorrect: {payload.Timestamp}", LogLevel.Warning);
return;
}
if (message.TransactionHashes.Any(p => context.TransactionExists(p)))
{
Log($"Invalid request: transaction already exists", LogLevel.Warning);
return;
}
context.State |= ConsensusState.RequestReceived;
context.Timestamp = payload.Timestamp;
context.Nonce = message.Nonce;
Expand Down
2 changes: 1 addition & 1 deletion neo/Consensus/IConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IConsensusContext : IDisposable

Header PrevHeader { get; }

bool ContainsTransaction(UInt256 hash);
bool TransactionExists(UInt256 hash);
bool VerifyTransaction(Transaction tx);

void ChangeView(byte view_number);
Expand Down

0 comments on commit f454e98

Please sign in to comment.