Skip to content

Instantly share code, notes, and snippets.

@josemmo
Created March 28, 2018 09:26
Show Gist options
  • Save josemmo/3d5a0c27948e0da7205fe8bb1b7e648f to your computer and use it in GitHub Desktop.
Save josemmo/3d5a0c27948e0da7205fe8bb1b7e648f to your computer and use it in GitHub Desktop.
void onNewMessage(ObjectOutputStream outputStream, Object message) {
if (message instanceof Transaction) {
// Guardamos la transacción para minería
cutrecoin.addPendingTransaction((Transaction) message);
} else if (message instanceof Block) {
// Guardamos el bloque como candidato
Block b = (Block) message;
// Pedimos el bloque anterior al recibido
if (cutrecoin.addCandidateBlock(b)) {
send(outputStream, "getBlock=" + b.getPreviousHash());
}
} else if (message instanceof String) {
String cmd = (String) message;
if (cmd.equals("getPendingTransactions")) {
// Enviamos la cola de TX pendientes
ArrayList<Transaction> ts = cutrecoin.getPendingTransactions();
for (Transaction t : ts) send(outputStream, t);
} else if (cmd.startsWith("getBlock=")) {
// Enviamos el bloque solicitado (si lo tenemos)
String hash = cmd.split("=")[1];
Block b = hash.equals("latest") ?
cutrecoin.getLastBlock() :
cutrecoin.getBlock(hash);
if (b != null) send(outputStream, b);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment