Skip to content

Commit

Permalink
Removed Fork()
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Sep 5, 2024
1 parent 9fa4ebd commit f7ed600
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 507 deletions.
87 changes: 0 additions & 87 deletions src/Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,93 +708,6 @@ public IImmutableSet<TxId> GetStagedTransactionIds()
return new Tuple<long?, IReadOnlyList<BlockHash>>(branchpointIndex, result);
}

/// <summary>
/// Forks the chain at <paramref name="point"/> and returns the newly forked chain.
/// </summary>
/// <param name="point">The hash in which to fork from.</param>
/// <returns>An instance of the newly forked chain.</returns>
/// <exception cref="ArgumentException">Throws when the provided <paramref name="point"/>
/// does not exist in the current chain.</exception>
public BlockChain Fork(BlockHash point)
{
if (!ContainsBlock(point))
{
throw new ArgumentException(
$"The block [{point}] doesn't exist.",
nameof(point));
}

Block pointBlock = this[point];

if (!point.Equals(this[pointBlock.Index].Hash))
{
throw new ArgumentException(
$"The block [{point}] doesn't exist in the chain index.",
nameof(point));
}

var forkedId = Guid.NewGuid();
try
{
_rwlock.EnterReadLock();

Store.AppendIndex(forkedId, Genesis.Hash);
Store.ForkBlockIndexes(Id, forkedId, point);
if (GetBlockCommit(point) is { } p)
{
Store.PutChainBlockCommit(forkedId, GetBlockCommit(point));
}

Store.ForkTxNonces(Id, forkedId);
for (Block block = Tip;
block.PreviousHash is { } hash && !block.Hash.Equals(point);
block = _blocks[hash])
{
IEnumerable<(Address, int)> signers = block
.Transactions
.GroupBy(tx => tx.Signer)
.Select(g => (g.Key, g.Count()));

foreach ((Address address, int txCount) in signers)
{
Store.IncreaseTxNonce(forkedId, address, -txCount);
}
}

var forked = new BlockChain(
Policy,
StagePolicy,
Store,
StateStore,
forkedId,
Genesis,
_blockChainStates,
ActionEvaluator,
Enumerable.Empty<IRenderer>());

_logger.Information(
"Forked chain at #{Index} {Hash} from id {PreviousId} to id {ForkedId}",
pointBlock.Index,
pointBlock.Hash,
Id,
forkedId);

return forked;
}
catch (Exception e)
{
_logger.Error(
e,
"An unexpected exception occurred while forking a chain");
Store.DeleteChainId(forkedId);
throw;
}
finally
{
_rwlock.ExitReadLock();
}
}

/// <summary>
/// Returns a new <see cref="BlockLocator"/> from the tip of current chain.
/// </summary>
Expand Down
40 changes: 12 additions & 28 deletions test/Libplanet.Explorer.Tests/Indexing/BlockChainIndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,24 @@ protected BlockChainIndexTest()
public async Task Synchronize()
{
var index = Fx.CreateEphemeralIndexInstance();
await index.SynchronizeAsync(
ChainFx.Chain.Store, CancellationToken.None);
await index.SynchronizeAsync(ChainFx.Chain.Store, CancellationToken.None);

var forkedChain = ChainFx.Chain.Fork(ChainFx.Chain.Tip.PreviousHash!.Value);
var divergentBlock = forkedChain.ProposeBlock(
var chain = ChainFx.Chain;
var block1 = chain.ProposeBlock(
ChainFx.PrivateKeys[0],
forkedChain.GetBlockCommit(forkedChain.Tip.Hash));
forkedChain.Append(
divergentBlock,
new BlockCommit(
forkedChain.Tip.Index + 1,
0,
divergentBlock.Hash,
ChainFx.PrivateKeys
.OrderBy(pk => pk.Address.ToHex())
.Select(pk => new VoteMetadata(
forkedChain.Tip.Index + 1,
0,
divergentBlock.Hash,
DateTimeOffset.UtcNow,
pk.PublicKey,
BigInteger.One,
VoteFlag.PreCommit)
.Sign(pk))
.ToImmutableArray()));
chain.GetBlockCommit(chain.Tip.Hash));
var block2 = chain.ProposeBlock(
ChainFx.PrivateKeys[0],
chain.GetBlockCommit(chain.Tip.Hash));
await index.IndexAsync(
ChainFx.Chain.Store.GetBlockDigest(ChainFx.Chain.Tip.Hash)!.Value,
ChainFx.Chain.Tip.Transactions,
new Store.BlockDigest(block1.MarshalBlock()),
block1.Transactions,
CancellationToken.None);
await Assert.ThrowsAsync<IndexMismatchException>(
async () => await index.IndexAsync(
forkedChain.Store.GetBlockDigest(forkedChain.Tip.Hash)!.Value,
forkedChain.Tip.Transactions,
CancellationToken.None));
new Store.BlockDigest(block2.MarshalBlock()),
block2.Transactions,
CancellationToken.None));
}

[Fact]
Expand Down
33 changes: 0 additions & 33 deletions test/Libplanet.Net.Tests/SwarmTest.Preload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -811,39 +811,6 @@ public async Task GetDemandBlockHashes()
CleaningSwarm(receiverSwarm);
}

[Fact(Timeout = Timeout)]
public async Task PreloadDeleteOnlyTempChain()
{
Swarm minerSwarm = await CreateSwarm().ConfigureAwait(false);
Swarm receiverSwarm = await CreateSwarm().ConfigureAwait(false);
BlockChain minerChain = minerSwarm.BlockChain;
BlockChain receiverChain = minerSwarm.BlockChain;

receiverChain = receiverChain.Fork(receiverChain.Genesis.Hash);
Block[] blocks =
MakeFixtureBlocksForPreloadAsyncCancellationTest().Item2;

foreach (Block block in blocks)
{
minerChain.Append(block, CreateBlockCommit(block));
}

try
{
await StartAsync(minerSwarm);
await receiverSwarm.AddPeersAsync(new[] { minerSwarm.AsPeer }, null);
await receiverSwarm.PreloadAsync();
}
finally
{
CleaningSwarm(minerSwarm);
CleaningSwarm(receiverSwarm);
}

// Check PreloadAsync() preserves chain that forked before preloading.
Assert.Equal(2, receiverChain.Store.ListChainIds().Count());
}

[Fact(Timeout = Timeout)]
public async Task PreloadToTheHighestTipIndexChain()
{
Expand Down
Loading

0 comments on commit f7ed600

Please sign in to comment.