Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Added scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
n-y-z-o committed Nov 20, 2018
1 parent f655b3e commit fc6b983
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
@@ -0,0 +1,68 @@
package co.nyzo.verifier.scripts;

import co.nyzo.verifier.*;
import co.nyzo.verifier.messages.HashVoteOverrideRequest;
import co.nyzo.verifier.util.IpUtil;
import co.nyzo.verifier.util.UpdateUtil;

import java.util.concurrent.atomic.AtomicBoolean;

public class HashVoteOverrideRequestScript {

public static void main(String[] args) {

if (args.length != 3) {
System.out.println("\n\n\n*****************************************************************");
System.out.println("required arguments:");
System.out.println("- private seed of your in-cycle verifier");
System.out.println("- height of the override block vote");
System.out.println("- hash of the override block vote");
System.out.println("*****************************************************************\n\n\n");
return;
}

// Get the private seed and corresponding identifier that was provided as the argument.
byte[] privateSeed = ByteUtil.byteArrayFromHexString(args[0], FieldByteSize.seed);
byte[] inCycleVerifierIdentifier = KeyUtil.identifierForSeed(privateSeed);

// Get the IP address of the verifier.
byte[] ipAddress = ScriptUtil.ipAddressForVerifier(inCycleVerifierIdentifier);
if (ByteUtil.isAllZeros(ipAddress)) {
System.out.println("unable to find IP address of " +
ByteUtil.arrayAsStringWithDashes(inCycleVerifierIdentifier));
return;
}

// Get the height and hash from the arguments.
long height = 0L;
try {
height = Long.parseLong(args[1]);
} catch (Exception ignored) { }

byte[] hash = ByteUtil.byteArrayFromHexString(args[2], FieldByteSize.hash);

// Send the request to our verifier.
AtomicBoolean receivedResponse = new AtomicBoolean(false);
HashVoteOverrideRequest request = new HashVoteOverrideRequest(height, hash);
Message message = new Message(MessageType.HashVoteOverrideRequest29, request);
message.sign(privateSeed);
Message.fetch(IpUtil.addressAsString(ipAddress), MeshListener.standardPort, message, new MessageCallback() {
@Override
public void responseReceived(Message message) {

System.out.println("response is " + message);
receivedResponse.set(true);
}
});

// Wait for the response to return.
while (!receivedResponse.get()) {
try {
Thread.sleep(300L);
} catch (Exception ignored) { }
}

// Terminate the application.
UpdateUtil.terminate();
}
}
@@ -0,0 +1,85 @@
package co.nyzo.verifier.scripts;

import co.nyzo.verifier.*;
import co.nyzo.verifier.messages.NewVerifierVoteOverrideRequest;
import co.nyzo.verifier.messages.debug.NewVerifierTallyStatusResponse;
import co.nyzo.verifier.util.IpUtil;
import co.nyzo.verifier.util.UpdateUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class NewVerifierTallyStatusRequestScript {

public static void main(String[] args) {

if (args.length != 1) {
System.out.println("\n\n\n*****************************************************************");
System.out.println("required argument:");
System.out.println("- private seed of your in-cycle verifier");
System.out.println("*****************************************************************\n\n\n");
return;
}

// Get the private seed and corresponding identifier that was provided as the argument.
byte[] privateSeed = ByteUtil.byteArrayFromHexString(args[0], FieldByteSize.seed);
byte[] inCycleVerifierIdentifier = KeyUtil.identifierForSeed(privateSeed);

// Get the IP address of the verifier.
byte[] ipAddress = ScriptUtil.ipAddressForVerifier(inCycleVerifierIdentifier);
if (ByteUtil.isAllZeros(ipAddress)) {
System.out.println("unable to find IP address of " +
ByteUtil.arrayAsStringWithDashes(inCycleVerifierIdentifier));
return;
}

// Send the request to our verifier.
AtomicBoolean receivedResponse = new AtomicBoolean(false);
Message message = new Message(MessageType.NewVerifierTallyStatusRequest414, null);
message.sign(privateSeed);
Message.fetch(IpUtil.addressAsString(ipAddress), MeshListener.standardPort, message, new MessageCallback() {
@Override
public void responseReceived(Message message) {

if (message == null) {
System.out.println("response message is null");
} else {

// Get the response object from the message.
NewVerifierTallyStatusResponse response = (NewVerifierTallyStatusResponse) message.getContent();

// Sort the response descending on vote count.
List<String> lines = new ArrayList<>(response.getLines());
lines.sort(new Comparator<String>() {
@Override
public int compare(String line1, String line2) {

Integer value1 = Integer.parseInt(line1.split(":")[1].trim());
Integer value2 = Integer.parseInt(line2.split(":")[1].trim());

return value2.compareTo(value1);
}
});

// Print the response.
for (String line : lines) {
System.out.println(line);
}
}
receivedResponse.set(true);
}
});

// Wait for the response to return.
while (!receivedResponse.get()) {
try {
Thread.sleep(300L);
} catch (Exception ignored) { }
}

// Terminate the application.
UpdateUtil.terminate();
}
}
@@ -0,0 +1,67 @@
package co.nyzo.verifier.scripts;

import co.nyzo.verifier.*;
import co.nyzo.verifier.messages.HashVoteOverrideRequest;
import co.nyzo.verifier.messages.MeshResponse;
import co.nyzo.verifier.messages.NewVerifierVoteOverrideRequest;
import co.nyzo.verifier.util.IpUtil;
import co.nyzo.verifier.util.UpdateUtil;

import java.util.concurrent.atomic.AtomicBoolean;

public class NewVerifierVoteOverrideRequestScript {

// This class and the message it uses are a shortcut for overriding the automatic age-based selection of new
// verifiers for the mesh. If this functionality did not exist, it would still be trivial for owners of in-cycle
// verifiers to modify code to override the default behavior and vote for a new verifier of choice. While some
// might criticize this as unfair, it is simply the democratic nature of Nyzo, and hiding or obfuscating this
// ability would not change the fundamental nature of the system.

public static void main(String[] args) {

if (args.length != 2) {
System.out.println("\n\n\n*****************************************************************");
System.out.println("required arguments:");
System.out.println("- private seed of your in-cycle verifier");
System.out.println("- identifier of the new verifier for which you want to vote");
System.out.println("*****************************************************************\n\n\n");
return;
}

// Get the private seed and corresponding identifier that was provided as the argument.
byte[] privateSeed = ByteUtil.byteArrayFromHexString(args[0], FieldByteSize.seed);
byte[] inCycleVerifierIdentifier = KeyUtil.identifierForSeed(privateSeed);

// Get the IP address of the verifier.
byte[] ipAddress = ScriptUtil.ipAddressForVerifier(inCycleVerifierIdentifier);
if (ByteUtil.isAllZeros(ipAddress)) {
System.out.println("unable to find IP address of " +
ByteUtil.arrayAsStringWithDashes(inCycleVerifierIdentifier));
return;
}

// Send the override to our verifier.
AtomicBoolean receivedResponse = new AtomicBoolean(false);
byte[] newVerifierIdentifier = ByteUtil.byteArrayFromHexString(args[1], FieldByteSize.identifier);
NewVerifierVoteOverrideRequest request = new NewVerifierVoteOverrideRequest(newVerifierIdentifier);
Message message = new Message(MessageType.NewVerifierVoteOverrideRequest33, request);
message.sign(privateSeed);
Message.fetch(IpUtil.addressAsString(ipAddress), MeshListener.standardPort, message, new MessageCallback() {
@Override
public void responseReceived(Message message) {
System.out.println("response is " + message);
receivedResponse.set(true);
}
});

// Wait for the response to return.
while (!receivedResponse.get()) {
try {
Thread.sleep(300L);
} catch (Exception ignored) { }
}

// Terminate the application.
UpdateUtil.terminate();
}
}
44 changes: 44 additions & 0 deletions src/main/java/co/nyzo/verifier/scripts/ScriptUtil.java
@@ -0,0 +1,44 @@
package co.nyzo.verifier.scripts;

import co.nyzo.verifier.*;
import co.nyzo.verifier.messages.MeshResponse;
import co.nyzo.verifier.util.IpUtil;

import java.util.concurrent.atomic.AtomicBoolean;

public class ScriptUtil {

public static byte[] ipAddressForVerifier(byte[] identifier) {

// Ask Nyzo verifier 0 for the mesh. Get the IP address of the verifier.
byte[] ipAddress = new byte[FieldByteSize.ipAddress];
Message meshRequest = new Message(MessageType.MeshRequest15, null);
AtomicBoolean receivedResponse = new AtomicBoolean(false);
Message.fetch("verifier0.nyzo.co", MeshListener.standardPort, meshRequest, new MessageCallback() {

@Override
public void responseReceived(Message message) {

MeshResponse meshResponse = (MeshResponse) message.getContent();
for (Node node : meshResponse.getMesh()) {
if (ByteUtil.arraysAreEqual(identifier, node.getIdentifier())) {
for (int i = 0; i < FieldByteSize.ipAddress; i++) {
ipAddress[i] = node.getIpAddress()[i];
}
receivedResponse.set(true);
}
}
}
});

// Wait for the response to return.
while (!receivedResponse.get()) {
try {
Thread.sleep(300L);
} catch (Exception ignored) { }
}
System.out.println("found IP address: " + IpUtil.addressAsString(ipAddress));

return ipAddress;
}
}

0 comments on commit fc6b983

Please sign in to comment.