Skip to content

Commit

Permalink
fix: Proposal Validation to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Nov 20, 2023
1 parent 71e163a commit 95fc6b0
Show file tree
Hide file tree
Showing 6 changed files with 491 additions and 271 deletions.
7 changes: 7 additions & 0 deletions Libplanet.Net.Tests/Consensus/ConsensusContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public async void NewHeightIncreasing()
await proposalMessageSent.WaitAsync();
Assert.NotNull(proposal?.BlockHash);

consensusContext.HandleMessage(new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[0], 3, hash: proposal.BlockHash, flag: VoteFlag.PreVote)));
consensusContext.HandleMessage(new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[1], 3, hash: proposal.BlockHash, flag: VoteFlag.PreVote)));
consensusContext.HandleMessage(new ConsensusPreVoteMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[2], 3, hash: proposal.BlockHash, flag: VoteFlag.PreVote)));

consensusContext.HandleMessage(new ConsensusPreCommitMsg(TestUtils.CreateVote(
TestUtils.PrivateKeys[0], 3, hash: proposal.BlockHash, flag: VoteFlag.PreCommit)));
consensusContext.HandleMessage(new ConsensusPreCommitMsg(TestUtils.CreateVote(
Expand Down
56 changes: 56 additions & 0 deletions Libplanet.Net.Tests/Consensus/ContextNonProposerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Action.Tests.Common;
using Libplanet.Blockchain;
Expand Down Expand Up @@ -613,5 +614,60 @@ public async Task TimeoutPreCommit()
Assert.Equal(1, context.Height);
Assert.Equal(1, context.Round);
}

[Fact(Timeout = Timeout)]
public async void CancelValidationOnTimeoutPropose()
{
var privateKey = new PrivateKey();
var proposeTimeout = new AsyncAutoResetEvent();
var proposalValidated = new AsyncAutoResetEvent();

var contextTimeoutOption = new ContextTimeoutOption();

var (blockChain, context) = TestUtils.CreateDummyContext(
height: 1,
actionLoader: new SingleActionLoader(typeof(DelayAction)),
privateKey: TestUtils.PrivateKeys[0],
contextTimeoutOptions: contextTimeoutOption,
validatorSet: TestUtils.ValidatorSet);

context.TimeoutProcessed += (_, eventArgs) =>
{
if (eventArgs.Step.Equals(ConsensusStep.Propose))
{
proposeTimeout.Set();
}
};

context.StateChanged += (_, eventArgs) =>
{
if (eventArgs.ProposalValidationExists)
{
proposalValidated.Set();
}
};

var delayAction = new DelayAction(
contextTimeoutOption.PreVoteSecondBase * 1000 + 1000);
var tx = Transaction.Create(
0,
new PrivateKey(),
blockChain.Genesis.Hash,
new[] { delayAction }.ToPlainValues());

blockChain.StageTransaction(tx);
var block = blockChain.ProposeBlock(TestUtils.PrivateKeys[1]);
context.Start();
context.ProduceMessage(
TestUtils.CreateConsensusPropose(
block, TestUtils.PrivateKeys[1], round: 0));

await Task.Delay(contextTimeoutOption.ProposeSecondBase * 1000 + 500);
Assert.True(proposeTimeout.IsSet);
Assert.False(proposalValidated.IsSet);

await Task.Delay(1000);
Assert.False(proposalValidated.IsSet);
}
}
}
13 changes: 8 additions & 5 deletions Libplanet.Net.Tests/Consensus/ContextProposerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public async void VoteNilOnSelfProposedInvalidBlock()
}

[Fact(Timeout = Timeout)]
public async void CancelOnTimeoutPropose()
public async void CancelProposeOnTimeoutPropose()
{
var privateKey = new PrivateKey();
var proposeTimeout = new AsyncAutoResetEvent();
Expand All @@ -369,7 +369,7 @@ public async void CancelOnTimeoutPropose()

context.TimeoutProcessed += (_, eventArgs) =>
{
if (eventArgs.Equals(ConsensusStep.Propose))
if (eventArgs.Step.Equals(ConsensusStep.Propose))
{
proposeTimeout.Set();
}
Expand All @@ -383,7 +383,8 @@ public async void CancelOnTimeoutPropose()
}
};

var delayAction = new DelayAction(6000);
var delayAction = new DelayAction(
contextTimeoutOption.ProposeSecondBase * 1000 + 1000);
var tx = Transaction.Create(
0,
new PrivateKey(),
Expand All @@ -398,10 +399,12 @@ public async void CancelOnTimeoutPropose()

context.Start();

await Task.Delay(contextTimeoutOption.ProposeSecondBase * 2000);

await Task.Delay(contextTimeoutOption.ProposeSecondBase * 1000 + 500);
Assert.True(proposeTimeout.IsSet);
Assert.False(proposalSent.IsSet);

await Task.Delay(1000);
Assert.False(proposalSent.IsSet);
}
}
}
Loading

0 comments on commit 95fc6b0

Please sign in to comment.