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

GetBlocks by block index #1397

Merged
merged 123 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
123 commits
Select commit Hold shift + click to select a range
f74620b
Merge pull request #2 from neo-project/master
ShawnYun Nov 26, 2019
6f6cf5e
Merge pull request #5 from neo-project/master
ShawnYun Dec 2, 2019
4152a15
Merge pull request #6 from neo-project/master
ShawnYun Dec 3, 2019
e0ae6d7
Merge pull request #7 from neo-project/master
ShawnYun Dec 4, 2019
ca56875
Merge pull request #8 from neo-project/master
ShawnYun Dec 11, 2019
985bef4
get-full-block
Dec 17, 2019
702c6ac
Merge pull request #10 from neo-project/master
ShawnYun Dec 23, 2019
990cd12
update get-full-block
Dec 26, 2019
126dde0
fix
Dec 26, 2019
6cf3231
modify
Dec 27, 2019
05440f9
Merge pull request #14 from neo-project/master
ShawnYun Dec 27, 2019
49f4966
Merge branch 'master' into get-full-blocks
Dec 27, 2019
3a97136
fix
Dec 31, 2019
117f363
fix
Dec 31, 2019
dc5b117
fix
Dec 31, 2019
3d6cc46
Merge pull request #15 from neo-project/master
ShawnYun Dec 31, 2019
b907ad3
fix
Dec 31, 2019
74ab7d7
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
Dec 31, 2019
9120d1f
del blank
Dec 31, 2019
bd955a4
fix
Dec 31, 2019
af8bd41
modify
Jan 2, 2020
3fafae6
modify uncompleted tasks
Jan 3, 2020
74b0958
del merkleblockdata
Jan 8, 2020
c062bb4
modify highestBlock
Jan 8, 2020
fc697eb
Merge branch 'master' into get-full-blocks
shargon Jan 8, 2020
2acad27
Optimize
shargon Jan 8, 2020
be28125
Adding comment
vncoelho Jan 8, 2020
7ee60da
Renaming to unverified_blocks_candidates
vncoelho Jan 8, 2020
9e7ae62
Renaming II
vncoelho Jan 8, 2020
b925f4f
Renaming III
vncoelho Jan 8, 2020
ea825b6
add NodeSession
Jan 16, 2020
b9b1488
del header
Jan 16, 2020
1993044
fix
Jan 17, 2020
8322bcf
Merge pull request #16 from neo-project/master
ShawnYun Jan 17, 2020
191fadf
modify
Jan 17, 2020
a486a78
Merge branch 'master' into get-full-blocks
vncoelho Jan 19, 2020
5c20549
fix
Jan 20, 2020
71bb88d
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
Jan 20, 2020
e3f75a8
modify every task to get 1 block
Feb 5, 2020
1d9c6d7
Merge branch 'master' into get-full-blocks
vncoelho Feb 5, 2020
70f06ca
modify SyncManger
Feb 7, 2020
f6f1962
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
Feb 7, 2020
5162f17
fix
Feb 7, 2020
7d2d48e
fix
Feb 8, 2020
f7e9ff6
del MaxTaskPerSession
Feb 8, 2020
5148851
fix
Feb 10, 2020
8ac9129
fix
Feb 11, 2020
c2f725e
del TaskManager.Update
Feb 11, 2020
370bb9f
Merge branch 'master' into get-full-blocks
shargon Feb 11, 2020
2a584a4
fix assigntask bug
Feb 12, 2020
dc27f2f
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
Feb 12, 2020
45e6858
Merge pull request #18 from neo-project/master
ShawnYun Feb 17, 2020
c54eb7e
modify get headers
Feb 17, 2020
cb27dfb
test
Feb 17, 2020
adf1afe
Rename
erikzhang Feb 21, 2020
f15f73e
Fixes
erikzhang Feb 21, 2020
1ac378b
Fixes
erikzhang Feb 21, 2020
de3047a
Merge branch 'get-full-blocks' into modify-get-headers
ShawnYun Feb 21, 2020
6ddcba1
Merge pull request #20 from ShawnYun/modify-get-headers
ShawnYun Feb 21, 2020
f95bc62
Merge branch 'master' into get-full-blocks
shargon Mar 13, 2020
94655fc
update blockchain
Mar 25, 2020
fbfe4d7
use event stream
Mar 26, 2020
5826c36
Merge branch 'master' into get-full-blocks
erikzhang Mar 26, 2020
e5a8ecf
fix
Mar 26, 2020
0949e57
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
Mar 26, 2020
efb4c6b
Ignore Headers messages
erikzhang Mar 27, 2020
3815e26
Merge branch 'master' into get-full-blocks
vncoelho Mar 27, 2020
4802cba
rename GetBlockByIndexPayload
Mar 30, 2020
0ea71c6
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
Mar 30, 2020
d17dd27
Remove MaxBlocksCount
erikzhang Mar 31, 2020
4dcb72e
Optimize GetBlockByIndexPayload
erikzhang Mar 31, 2020
f7b0dc3
fix
Mar 31, 2020
0668722
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
Mar 31, 2020
1232b41
merge
Apr 10, 2020
da21bb1
fix
Apr 10, 2020
5a5c6e0
Merge branch 'master' into get-full-blocks
ShawnYun Apr 10, 2020
1121c5f
Merge branch 'master' into get-full-blocks
erikzhang Apr 10, 2020
b50d61a
Merge branch 'master' of https://github.com/neo-project/neo into get-…
Apr 10, 2020
b35b09b
del subscribe
Apr 10, 2020
7f4d41c
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
Apr 10, 2020
7db6571
Optimize
shargon Apr 11, 2020
fc16c11
Merge branch 'master' into get-full-blocks
vncoelho Apr 13, 2020
7855a62
Merge branch 'master' of https://github.com/neo-project/neo into get-…
Apr 23, 2020
2729447
merge SyncManager and TaskManager to one class
Apr 26, 2020
e79e910
merge to SyncManager and fix
Apr 28, 2020
51f30c5
Rename
erikzhang May 6, 2020
7c2a2ef
Merge branch 'master' into get-full-blocks
shargon May 6, 2020
f543017
fix
May 6, 2020
82df8f0
Merge branch 'master' into get-full-blocks
shargon May 7, 2020
1a9e590
Merge branch 'master' into get-full-blocks
shargon May 8, 2020
80bea1d
fix header_index
May 12, 2020
9ba203f
Merge branch 'get-full-blocks' of https://github.com/ShawnYun/neo int…
May 12, 2020
77485b4
modify header_index
May 13, 2020
dd1a9b7
Rename
erikzhang May 14, 2020
eb762b2
Optimize
erikzhang May 14, 2020
9eb5f18
Optimize
erikzhang May 14, 2020
4855a23
internal
erikzhang May 14, 2020
9c96d11
Remove useless code
erikzhang May 14, 2020
6302754
Reorder fields
erikzhang May 14, 2020
5a645a9
Format
erikzhang May 14, 2020
aaa1a0b
Reorder methods
erikzhang May 14, 2020
7b4b411
Merge branch 'master' into get-full-blocks
erikzhang May 14, 2020
a967ce1
fix
May 15, 2020
acd11c8
del not used
May 15, 2020
f7e1692
Merge branch 'master' into get-full-blocks
erikzhang May 15, 2020
502c5f4
Merge branch 'master' into get-full-blocks
vncoelho May 15, 2020
98f0855
resolve conflict and fix
May 18, 2020
a6e0000
add comment
May 18, 2020
de15cf9
Merge branch 'master' into get-full-blocks
erikzhang May 18, 2020
91b25b8
Merge branch 'master' into get-full-blocks
vncoelho May 19, 2020
b5cc115
Merge branch 'master' into get-full-blocks
erikzhang May 22, 2020
2529218
resolve conflicts
May 26, 2020
1149c03
format
May 26, 2020
189bd02
fix
May 26, 2020
9a69957
Optimize
erikzhang May 26, 2020
9543b27
Update TaskManager.cs
erikzhang May 26, 2020
77286f6
Update TaskManager.cs
erikzhang May 26, 2020
f93ba3a
Merge branch 'master' into get-full-blocks
erikzhang May 26, 2020
a1b6809
Improve global tasks
shargon May 26, 2020
cd41d38
Merge pull request #23 from shargon/improve-global-tasks
ShawnYun May 27, 2020
03cf387
resolve conflicts
May 27, 2020
423a7bd
Merge branch 'master' into get-full-blocks
shargon Jun 1, 2020
aec66f6
Revert counter
shargon Jun 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 13 additions & 64 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private class ParallelVerified { public Transaction Transaction; public bool Sho
private readonly NeoSystem system;
private readonly List<UInt256> header_index = new List<UInt256>();
private uint stored_header_count = 0;
private readonly Dictionary<UInt256, Block> block_cache = new Dictionary<UInt256, Block>();
private readonly Dictionary<uint, LinkedList<Block>> block_cache_unverified = new Dictionary<uint, LinkedList<Block>>();
internal readonly RelayCache ConsensusRelayCache = new RelayCache(100);
private SnapshotView currentSnapshot;
Expand Down Expand Up @@ -142,7 +141,6 @@ public Blockchain(NeoSystem system, IStore store)

public bool ContainsBlock(UInt256 hash)
{
if (block_cache.ContainsKey(hash)) return true;
return View.ContainsBlock(hash);
}

Expand Down Expand Up @@ -189,8 +187,6 @@ public Block GetBlock(uint index)

public Block GetBlock(UInt256 hash)
{
if (block_cache.TryGetValue(hash, out Block block))
return block;
return View.GetBlock(hash);
}

Expand All @@ -215,8 +211,6 @@ public Header GetHeader(uint index)

public Header GetHeader(UInt256 hash)
{
if (block_cache.TryGetValue(hash, out Block block))
return block.Header;
return View.GetHeader(hash);
}

Expand Down Expand Up @@ -259,7 +253,11 @@ private void AddUnverifiedBlockToCache(Block block)
blocks = new LinkedList<Block>();
block_cache_unverified.Add(block.Index, blocks);
}

foreach (var unverifiedBlock in blocks)
{
if (block.Hash == unverifiedBlock.Hash)
return;
}
blocks.AddLast(block);
}

Expand Down Expand Up @@ -292,78 +290,30 @@ private RelayResultReason OnNewBlock(Block block)
{
if (block.Index <= Height)
return RelayResultReason.AlreadyExists;
if (block_cache.ContainsKey(block.Hash))
return RelayResultReason.AlreadyExists;
if (block.Index - 1 >= header_index.Count)
if (block.Index - 1 > Height)
{
AddUnverifiedBlockToCache(block);
return RelayResultReason.UnableToVerify;
}
if (block.Index == header_index.Count)
{
if (!block.Verify(currentSnapshot))
return RelayResultReason.Invalid;
}
else
{
if (!block.Hash.Equals(header_index[(int)block.Index]))
return RelayResultReason.Invalid;
}
if (block.Index == Height + 1)
{
Block block_persist = block;
List<Block> blocksToPersistList = new List<Block>();
while (true)
{
blocksToPersistList.Add(block_persist);
if (block_persist.Index + 1 >= header_index.Count) break;
UInt256 hash = header_index[(int)block_persist.Index + 1];
if (!block_cache.TryGetValue(hash, out block_persist)) break;
}

int blocksPersisted = 0;
foreach (Block blockToPersist in blocksToPersistList)
if (!block.Verify(currentSnapshot))
{
block_cache_unverified.Remove(blockToPersist.Index);
Persist(blockToPersist);

// 15000 is the default among of seconds per block, while MilliSecondsPerBlock is the current
uint extraBlocks = (15000 - MillisecondsPerBlock) / 1000;

if (blocksPersisted++ < blocksToPersistList.Count - (2 + Math.Max(0, extraBlocks))) continue;
// Empirically calibrated for relaying the most recent 2 blocks persisted with 15s network
// Increase in the rate of 1 block per second in configurations with faster blocks

if (blockToPersist.Index + 100 >= header_index.Count)
system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = blockToPersist });
system.SyncManager.Tell(new SyncManager.InvalidBlockIndex { InvalidIndex = block.Index });
return RelayResultReason.Invalid;
}
block_cache_unverified.Remove(block.Index);
Persist(block);
system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = block });
system.SyncManager.Tell(new SyncManager.PersistedBlockIndex { PersistedIndex = block.Index });
SaveHeaderHashList();

if (block_cache_unverified.TryGetValue(Height + 1, out LinkedList<Block> unverifiedBlocks))
{
foreach (var unverifiedBlock in unverifiedBlocks)
Self.Tell(unverifiedBlock, ActorRefs.NoSender);
block_cache_unverified.Remove(Height + 1);
}
}
else
{
block_cache.Add(block.Hash, block);
if (block.Index + 100 >= header_index.Count)
system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = block });
if (block.Index == header_index.Count)
{
header_index.Add(block.Hash);
using (SnapshotView snapshot = GetSnapshot())
{
snapshot.Blocks.Add(block.Hash, block.Header.Trim());
snapshot.HeaderHashIndex.GetAndChange().Set(block);
SaveHeaderHashList(snapshot);
snapshot.Commit();
}
UpdateCurrentSnapshot();
}
}
return RelayResultReason.Succeed;
}

Expand Down Expand Up @@ -441,7 +391,6 @@ private void OnParallelVerified(ParallelVerified parallelVerified)

private void OnPersistCompleted(Block block)
{
block_cache.Remove(block.Hash);
MemPool.UpdatePoolForBlockPersisted(block, currentSnapshot);
Context.System.EventStream.Publish(new PersistCompleted { Block = block });
}
Expand Down
2 changes: 2 additions & 0 deletions src/neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class NeoSystem : IDisposable
public IActorRef Blockchain { get; }
public IActorRef LocalNode { get; }
internal IActorRef TaskManager { get; }
internal IActorRef SyncManager { get; }
public IActorRef Consensus { get; private set; }

private readonly IStore store;
Expand All @@ -36,6 +37,7 @@ public NeoSystem(string storageEngine = null)
this.Blockchain = ActorSystem.ActorOf(Ledger.Blockchain.Props(this, store));
this.LocalNode = ActorSystem.ActorOf(Network.P2P.LocalNode.Props(this));
this.TaskManager = ActorSystem.ActorOf(Network.P2P.TaskManager.Props(this));
this.SyncManager = ActorSystem.ActorOf(Network.P2P.SyncManager.Props(this));
foreach (var plugin in Plugin.Plugins)
plugin.OnPluginsLoaded();
}
Expand Down
2 changes: 2 additions & 0 deletions src/neo/Network/P2P/MessageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public enum MessageCommand : byte
Block = 0x2c,
[ReflectionCache(typeof(ConsensusPayload))]
Consensus = 0x2d,
[ReflectionCache(typeof(Block))]
BlockData = 0x2e,
Reject = 0x2f,

//SPV protocol
Expand Down
11 changes: 10 additions & 1 deletion src/neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ private void OnMessage(Message msg)
case MessageCommand.Block:
OnInventoryReceived((Block)msg.Payload);
break;
case MessageCommand.BlockData:
OnBlockDataReceived((Block)msg.Payload);
break;
case MessageCommand.Consensus:
OnInventoryReceived((ConsensusPayload)msg.Payload);
break;
Expand Down Expand Up @@ -148,6 +151,12 @@ private void OnMessage(Message msg)
}
}

private void OnBlockDataReceived(Block payload)
{
if (payload != null)
system.SyncManager.Tell(payload, Context.Parent);
}

private void OnAddrMessageReceived(AddrPayload payload)
{
system.LocalNode.Tell(new Peer.Peers
Expand Down Expand Up @@ -229,7 +238,7 @@ private void OnGetBlockDataMessageReceived(GetBlockDataPayload payload)

if (bloom_filter == null)
{
Context.Parent.Tell(Message.Create(MessageCommand.Block, block));
Context.Parent.Tell(Message.Create(MessageCommand.BlockData, block));
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/neo/Network/P2P/RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private void OnPingPayload(PingPayload payload)
{
LastBlockIndex = payload.LastBlockIndex;
system.TaskManager.Tell(new TaskManager.Update { LastBlockIndex = LastBlockIndex });
system.SyncManager.Tell(new SyncManager.Update { LastBlockIndex = LastBlockIndex });
}
}

Expand Down Expand Up @@ -196,6 +197,7 @@ private void OnVerack()
{
verack = true;
system.TaskManager.Tell(new TaskManager.Register { Version = Version });
Copy link
Member

Choose a reason for hiding this comment

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

reuse the same Register object?

Copy link
Member

Choose a reason for hiding this comment

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

I prefer if we remove and merge SyncManager with TaskManager, as @Tommo-L mentioned recently.
What are the advantages of separated ones?

system.SyncManager.Tell(new SyncManager.Register { Version = Version });
CheckMessageQueue();
}

Expand Down
Loading