Skip to content

Commit

Permalink
Better block height determination
Browse files Browse the repository at this point in the history
  • Loading branch information
slothbag committed Aug 19, 2015
1 parent 5d7a30e commit 86afac2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
84 changes: 58 additions & 26 deletions NxtLite/Nodes.cs
Expand Up @@ -32,15 +32,67 @@ public class Node {
this.address = address;
}
}

public class PeerBlockHeightHistory {
private int last_agreed_block_height = 0;

private struct PeerBlockHeight {
public string ip_address;
public int block_height;
}

private PeerBlockHeight[] History = new PeerBlockHeight[3];

public int AgreedBlockHeight {
get { return this.last_agreed_block_height; }
}

private bool TryUpdateToLatestBlockHeight() {
int min = 0;
int max = 0;
for (int a = 0; a < History.Length; a++) {
//if the history buffer is not full then no agreement yet
if (History[a].block_height == 0)
return false;

if (min == 0 || History[a].block_height < min)
min = History[a].block_height;
if (max == 0 || History[a].block_height > max)
max = History[a].block_height;
}
if (max - min < 3 && max > this.last_agreed_block_height) {
this.last_agreed_block_height = max;
return true;
}
else
return false;
}

public void Add(string ip_address, int block_height) {
bool bFound = false;
for (int a = 0; a < History.Length; a++) {
if (History[a].ip_address == ip_address) {
History[a].block_height = block_height;
bFound = true;
}
}
if (!bFound) {
//not found, add it
History[0] = History[1];
History[1] = History[2];
History[2] = new PeerBlockHeight() {ip_address = ip_address, block_height = block_height};
}

if (this.TryUpdateToLatestBlockHeight())
Console.WriteLine("Last block set to " + this.AgreedBlockHeight);
}
}

public static List<Nodes.Node> _nodes = new List<Nodes.Node>();
public static int[] block_height_history = new int[3];

public static int latest_block_height = 0;
public static PeerBlockHeightHistory peer_block_height_history = new PeerBlockHeightHistory();

public static bool AddNodes(string address) {
_nodes.Add(new Node(address));

return true;
}

Expand Down Expand Up @@ -154,28 +206,8 @@ orderby p.last_checked
return lNodes.First();
}

public static int ScanLatestBlockHeight() {
foreach (Node tmpNode in _nodes) {
if (tmpNode.block_height > latest_block_height) {
latest_block_height = tmpNode.block_height;
break;
}
}
return latest_block_height;
}

public static void SetLatestBlockHeight(int block_height) {
if (block_height_history.Max() - block_height_history.Min() < 3) {
if (block_height_history.Max() > latest_block_height) {
latest_block_height = block_height_history.Max();
}
}

//cycle the block history queue
block_height_history[0] = block_height_history[1];
block_height_history[1] = block_height_history[2];
block_height_history[2] = block_height;

public static void SetLatestBlockHeight(string ip_address, int block_height) {
peer_block_height_history.Add(ip_address, block_height);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion NxtLite/Program.cs
Expand Up @@ -20,7 +20,7 @@ private static void Main(string[] args)
else {
//try load existing settings
Nodes.LoadFromFile();
Nodes.ScanLatestBlockHeight();
//Nodes.ScanLatestBlockHeight();
}

//start the Core
Expand Down
4 changes: 2 additions & 2 deletions NxtLite/WebServer.cs
Expand Up @@ -215,7 +215,7 @@ public void Run()
}

if (hasBlockHeight) {
if (publicNode.block_height < (Nodes.latest_block_height - 2)) {
if (publicNode.block_height < (Nodes.peer_block_height_history.AgreedBlockHeight - 2)) {
publicNode.block_sync_failures ++;
publicNode.consecutive_errors ++;
publicNode.last_error = "Not fully sync'd";
Expand All @@ -225,7 +225,7 @@ public void Run()

publicNode.consecutive_errors = 0;
if (hasBlockHeight)
Nodes.SetLatestBlockHeight(publicNode.block_height);
Nodes.peer_block_height_history.Add(publicNode.address, publicNode.block_height);

byte[] response_bytes = System.Text.Encoding.UTF8.GetBytes(response_data);
try {
Expand Down
2 changes: 1 addition & 1 deletion gui_app/main.js
Expand Up @@ -41,7 +41,7 @@ app.on('ready', function() {
});

// Create the browser window.
mainWindow = new BrowserWindow({width: 1360, height: 768, "node-integration": false, title: "NxtLite", icon: "icon32.png"});
mainWindow = new BrowserWindow({width: 1450, height: 768, "node-integration": false, title: "NxtLite", icon: "icon32.png"});
mainWindow.setMenu(null);

// and load the index.html of the app.
Expand Down

0 comments on commit 86afac2

Please sign in to comment.