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

Add Swarm.BroadcastBlocksAsync() #59

Merged
merged 6 commits into from
Feb 12, 2019

Conversation

longfin
Copy link
Member

@longfin longfin commented Feb 8, 2019

This PR implements Swarm.BroadcastBlocksAsync() for miner can broadcast mined block to known peers. the following points have also been changed to accomplish it.

  • Added DeleteIndex() to IStore / FileStore to able to give up own block indexes when other longest chain appears.
  • Added Blockchain.DeleteAfter() to same reason.
  • Added Blockchain.GetBlockLocator() to query block hashes to broadcasting peer.
  • Blockchain.FindNextHash() returns result containing branch point.

@dahlia dahlia added this to the 0.1.0 milestone Feb 8, 2019
@longfin longfin force-pushed the feature/block-sync branch 4 times, most recently from a49c460 to 0f1b996 Compare February 11, 2019 05:37
@longfin longfin changed the title WIP block sync Add Swarm.BroadcastBlocksAsync() Feb 11, 2019
@longfin longfin requested review from dahlia and kijun February 11, 2019 06:06
@longfin longfin self-assigned this Feb 11, 2019
@longfin longfin added the suggestion Suggestion or feature request label Feb 11, 2019
#pragma warning disable CS4014
Task.Run(async () => await swarmA.RunAsync(chainA, 250));
Task.Run(async () => await swarmB.RunAsync(chainB, 250));
Task.Run(async () => await swarmC.RunAsync(chainC, 250));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these Tasks be awaited in finally block at the latest? (As like we join all spawned threads until a program terminates?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't await these tasks because they run in infinite loop.
but we can cancel these task in finally block. I'll add it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, make sense. 👌🏼

{
_fx = fixture;
_fx = new FileStoreFixture();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you change it to use xUnit's fixture mechanism no more?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xUnit's fixture is shared within the running cases, which causes unnecessary side effects.

@@ -42,6 +42,8 @@ public Blockchain(IStore store)

public IStore Store { get; }

public Block<T> Tip => this[-1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a negative index usually mean a backward offset?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

{
HashDigest<SHA256>? current = Store.IndexBlockHash(-1);

while (current is HashDigest<SHA256> hash && current != point)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more tolerant of later changes if the latter condition becomes hash != point.


while (current is HashDigest<SHA256> hash && current != point)
{
HashDigest<SHA256>? next = Blocks[hash].PreviousHash;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's next to access, but actually previous in the chain. Both maybe confusing, but I find “previous” is slightly more intuitive than “next” here.

else
{
_logger.Information(
"Received index is lower than current chain's tip." +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word “lower” here is assuming the highest is the tip and the lower is the genesis. I doesn't find it necessarily intuitive, so the term should be more concrete IMHO, like “older.”

}
}

File.WriteAllBytes(_indexPath, writer.ToArray());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem very scalable to buffer the whole index on the memory. It should be fixed in the future. It also would be good if we write a FIXME comment here.

{
// We assume that the blocks are sorted in order.
Block<T> oldest = blocks.First();
Block<T> newest = blocks.Last();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more natural to be the latest than the newest here to me?

Block<T> newest = blocks.Last();
HashDigest<SHA256>? tip = blockchain.Store.IndexBlockHash(-1);

if (tip == null || oldest.PreviousHash == tip)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean the newest is expected to be the most closest to the genesis, unlike the intuition its name give us?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, newest is most far from genesis.

This condition means that if oldest in received blocks is next to our tip, we can connect it directly.

@longfin longfin force-pushed the feature/block-sync branch 2 times, most recently from 7c1e723 to c7d9243 Compare February 11, 2019 17:02
@longfin
Copy link
Member Author

longfin commented Feb 11, 2019

In fixing #59 (comment), I realize current Swarm's interface is a little weird. so I'll do the related work in #69 .

@longfin
Copy link
Member Author

longfin commented Feb 12, 2019

I've rebased due to merging #63. PTAL @dahlia @kijun

@longfin longfin merged commit c38bc65 into planetarium:master Feb 12, 2019
dahlia pushed a commit to dahlia/libplanet that referenced this pull request Mar 9, 2021
Bump Libplanet version to 0.6.0
limebell pushed a commit to limebell/libplanet that referenced this pull request Jul 7, 2021
…lash-exceptions

Define exceptions in HackAndSlash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
suggestion Suggestion or feature request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants