Skip to content

Commit

Permalink
Remove BlockChain<T>.Reorged event
Browse files Browse the repository at this point in the history
which is replaced by IRenderer<T>.RenderReorg() method.

See also planetarium#945
  • Loading branch information
dahlia committed Aug 24, 2020
1 parent 4380959 commit 4d080b5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 52 deletions.
4 changes: 1 addition & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ To be released.
- Added `PolymorphicRenderer<T>` class. [[#959], [#963]]
- Added `AnonymousRenderer<T>` class. [[#959], [#963]]
- Added `LoggedRenderer<T>` class. [[#959], [#963]]
- Added `BlockChain<T>.Renderers` property. [[#959], [#963]]
- Added `Reorged` event on `BlockChain<T>`. [[#945]]
- Added `ReorgedEventArgs` class. [[#945]]
- Added `BlockChain<T>.Renderers` property. [[#945], [#959], [#963]]
- Added `Swarm<T>.AppProtocolVersion` property. [[#949]]
- (Libplanet.RocksDB) `RocksDBStore` became to implement `IBlockStatesStore`. [[#950]]
- `DefaultStore` became to implement `IBlockStatesStore`. [[#950]]
Expand Down
16 changes: 6 additions & 10 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,11 @@ public async void BlockActionWithMultipleAddress()
/// </code>
/// </summary>
internal static (Address, Address[] addresses, BlockChain<DumbAction> chain)
MakeIncompleteBlockStates(IStore store, IBlockStatesStore blockStatesStore)
MakeIncompleteBlockStates(
IStore store,
IBlockStatesStore blockStatesStore,
IRenderer<DumbAction> renderer = null
)
{
store = new StoreTracker(store);
Guid chainId = Guid.NewGuid();
Expand All @@ -2052,7 +2056,7 @@ internal static (Address, Address[] addresses, BlockChain<DumbAction> chain)
blockStatesStore,
chainId,
TestUtils.MineGenesis<DumbAction>(),
renderers: null
renderers: renderer is null ? null : new[] { renderer }
);
var privateKey = new PrivateKey();
Address signer = privateKey.ToAddress();
Expand Down Expand Up @@ -2199,10 +2203,7 @@ private async void TipChanged()
[Fact]
private async void Reorged()
{
BlockChain<DumbAction>.ReorgedEventArgs eventLog = null;

_renderer.ResetRecords();
_blockChain.Reorged += (target, args) => eventLog = args;
var branchpoint = _blockChain.Tip;
var fork = _blockChain.Fork(_blockChain.Tip.Hash);
await fork.MineBlock(_fx.Address1);
Expand All @@ -2213,7 +2214,6 @@ private async void Reorged()
var newTip = fork.Tip;

Assert.Empty(_renderer.ReorgRecords);
Assert.Null(eventLog);

_blockChain.Swap(fork, false);

Expand All @@ -2222,10 +2222,6 @@ private async void Reorged()
Assert.Equal(oldTip, old);
Assert.Equal(newTip, @new);
Assert.Equal(branchpoint, bp);
Assert.NotNull(eventLog);
Assert.Equal(old, eventLog.OldTip);
Assert.Equal(@new, eventLog.NewTip);
Assert.Equal(bp, eventLog.Branchpoint);
}

[Fact]
Expand Down
20 changes: 15 additions & 5 deletions Libplanet.Tests/Net/SwarmTest.TrustedStateCompleter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Bencodex.Types;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Renderers;
using Libplanet.Blocks;
using Libplanet.Crypto;
using Libplanet.Net;
Expand Down Expand Up @@ -101,8 +102,18 @@ await incompleteSwarm.GetTrustedStateCompleterAsync(
public async Task TrustNewTipStatesAfterReog()
{
var incompleteStore = new DefaultStore(null);
(Block<DumbAction> Old, Block<DumbAction> New, Block<DumbAction> Bp)? reorged = null;
IRenderer<DumbAction> renderer = new AnonymousRenderer<DumbAction>
{
ReorgRenderer = (old, @new, bp) => reorged = (old, @new, bp),
};
renderer = new LoggedRenderer<DumbAction>(renderer, _logger);
(_, Address[] addresses, BlockChain<DumbAction> incomplete) =
BlockChainTest.MakeIncompleteBlockStates(incompleteStore, incompleteStore);
BlockChainTest.MakeIncompleteBlockStates(
incompleteStore,
incompleteStore,
renderer
);
Block<DumbAction> genesis = incomplete.Genesis;
Swarm<DumbAction> incompleteSwarm = CreateSwarm(incomplete);

Expand Down Expand Up @@ -149,8 +160,7 @@ public async Task TrustNewTipStatesAfterReog()

Block<DumbAction> staleTip = incomplete.Tip;
Block<DumbAction> canonTip = eventualCanon.Tip;
BlockChain<DumbAction>.ReorgedEventArgs reorged = null;
incomplete.Reorged += (sender, args) => reorged = args;
reorged = null;

await StartAsync(
incompleteSwarm,
Expand All @@ -176,8 +186,8 @@ await StartAsync(
eventualCanon
);
Assert.Equal(incomplete.Tip, canonTip);
Assert.Equal(reorged.OldTip, staleTip);
Assert.Equal(reorged.NewTip, canonTip);
Assert.Equal(reorged?.Old, staleTip);
Assert.Equal(reorged?.New, canonTip);
Assert.Equal(
eventualCanonStore.GetBlockStates(canonTip.Hash),
incompleteStore.GetBlockStates(canonTip.Hash)
Expand Down
34 changes: 0 additions & 34 deletions Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@ IEnumerable<IRenderer<T>> renderers
_rwlock?.Dispose();
}

/// <summary>
/// An event which is invoked when the chain is reorged.
/// </summary>
public event EventHandler<ReorgedEventArgs> Reorged;

/// <summary>
/// An event which is invoked when <see cref="Tip"/> is changed.
/// </summary>
Expand Down Expand Up @@ -1478,19 +1473,11 @@ IEnumerable<TxId> GetTxIdsWithRange(BlockChain<T> chain, Block<T> start, Block<T
_rwlock.EnterWriteLock();

Block<T> oldTip = Tip ?? Genesis, newTip = other.Tip ?? other.Genesis;
var reorgedEventArgs = new ReorgedEventArgs
{
OldTip = oldTip,
NewTip = newTip,
Branchpoint = topmostCommon,
};

Guid obsoleteId = Id;
Id = other.Id;
Store.SetCanonicalChainId(Id);
_blocks = new BlockSet<T>(Store);
TipChanged?.Invoke(this, (oldTip, newTip));
Reorged?.Invoke(this, reorgedEventArgs);
foreach (IRenderer<T> renderer in Renderers)
{
renderer.RenderReorg(oldTip, newTip, branchpoint: topmostCommon);
Expand Down Expand Up @@ -1736,26 +1723,5 @@ Func<BlockChain<T>, HashDigest<SHA256>, IValue> rawStateCompleter
_rwlock.ExitUpgradeableReadLock();
}
}

/// <summary>
/// Provides data for the <see cref="BlockChain{T}.Reorged"/> event.
/// </summary>
public class ReorgedEventArgs : EventArgs
{
/// <summary>
/// The <see cref="BlockChain{T}.Tip"/> before the chain is reorged.
/// </summary>
public Block<T> OldTip { get; set; }

/// <summary>
/// The <see cref="BlockChain{T}.Tip"/> after the chain is reorged.
/// </summary>
public Block<T> NewTip { get; set; }

/// <summary>
/// The <see cref="Block{T}"/> point of the chain branches off.
/// </summary>
public Block<T> Branchpoint { get; set; }
}
}
}

0 comments on commit 4d080b5

Please sign in to comment.