Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

sync block height #733

Merged
merged 2 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions neo-cli/CLI/MainService.Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Neo.ConsoleService;
using Neo.Ledger;
using Neo.SmartContract.Native;
using System;

namespace Neo.CLI
Expand All @@ -15,13 +16,14 @@ partial class MainService
[ConsoleCommand("export blocks", Category = "Blockchain Commands")]
private void OnExportBlocksStartCountCommand(uint start, uint count = uint.MaxValue, string path = null)
{
if (Blockchain.Singleton.Height < start)
uint height = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View);
if (height < start)
{
Console.WriteLine("Error: invalid start height.");
return;
}

count = Math.Min(count, Blockchain.Singleton.Height - start + 1);
count = Math.Min(count, height - start + 1);

if (string.IsNullOrEmpty(path))
{
Expand Down
7 changes: 5 additions & 2 deletions neo-cli/CLI/MainService.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract.Native;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -53,11 +54,13 @@ private void OnShowStateCommand()

Console.CursorVisible = false;
Console.Clear();

uint height = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View);
Task broadcast = Task.Run(async () =>
{
while (!cancel.Token.IsCancellationRequested)
{
NeoSystem.LocalNode.Tell(Message.Create(MessageCommand.Ping, PingPayload.Create(Blockchain.Singleton.Height)));
NeoSystem.LocalNode.Tell(Message.Create(MessageCommand.Ping, PingPayload.Create(height)));
await Task.Delay(Blockchain.TimePerBlock, cancel.Token);
}
});
Expand All @@ -68,7 +71,7 @@ private void OnShowStateCommand()
while (!cancel.Token.IsCancellationRequested)
{
Console.SetCursorPosition(0, 0);
WriteLineWithoutFlicker($"block: {Blockchain.Singleton.Height} connected: {LocalNode.Singleton.ConnectedCount} unconnected: {LocalNode.Singleton.UnconnectedCount}", Console.WindowWidth - 1);
WriteLineWithoutFlicker($"block: {height} connected: {LocalNode.Singleton.ConnectedCount} unconnected: {LocalNode.Singleton.UnconnectedCount}", Console.WindowWidth - 1);

int linesWritten = 1;
foreach (RemoteNode node in LocalNode.Singleton.GetRemoteNodes().OrderByDescending(u => u.LastBlockIndex).Take(Console.WindowHeight - 2).ToArray())
Expand Down
11 changes: 6 additions & 5 deletions neo-cli/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,16 @@ private static IEnumerable<Block> GetBlocks(Stream stream, bool read_start = fal
uint start = read_start ? r.ReadUInt32() : 0;
uint count = r.ReadUInt32();
uint end = start + count - 1;
if (end <= Blockchain.Singleton.Height) yield break;
uint currentHeight = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View);
if (end <= currentHeight) yield break;
for (uint height = start; height <= end; height++)
{
var size = r.ReadInt32();
if (size > Message.PayloadMaxSize)
throw new ArgumentException($"Block {height} exceeds the maximum allowed size");

byte[] array = r.ReadBytes(size);
if (height > Blockchain.Singleton.Height)
if (height > currentHeight)
{
Block block = array.AsSerializable<Block>();
yield return block;
Expand Down Expand Up @@ -215,9 +216,10 @@ private IEnumerable<Block> GetBlocksFromFile()
IsCompressed = p.EndsWith(".zip")
}).OrderBy(p => p.Start);

uint height = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View);
foreach (var path in paths)
{
if (path.Start > Blockchain.Singleton.Height + 1) break;
if (path.Start > height + 1) break;
if (path.IsCompressed)
using (FileStream fs = new FileStream(path.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read))
Expand Down Expand Up @@ -526,8 +528,7 @@ private bool OnInvokeWithResult(UInt160 scriptHash, string operation, out StackI
}
}

var snapshot = Blockchain.Singleton.GetSnapshot();
ContractState contract = NativeContract.ContractManagement.GetContract(snapshot, scriptHash);
ContractState contract = NativeContract.ContractManagement.GetContract(Blockchain.Singleton.View, scriptHash);
if (contract == null)
{
Console.WriteLine("Contract does not exist.");
Expand Down
4 changes: 2 additions & 2 deletions neo-cli/neo-cli.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Copyright>2016-2020 The Neo Project</Copyright>
<AssemblyTitle>Neo.CLI</AssemblyTitle>
Expand Down Expand Up @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI01171" />
<PackageReference Include="Neo" Version="3.0.0-CI01181" />
</ItemGroup>

<ItemGroup>
Expand Down