Skip to content

Commit

Permalink
import export txn to file, throw correct error if transaction doesn't
Browse files Browse the repository at this point in the history
exist
  • Loading branch information
spartacusrex99 committed Jan 28, 2022
1 parent 3f8ad72 commit 0cdeef3
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/org/minima/system/commands/txn/txncheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.minima.objects.base.MiniData;
import org.minima.objects.base.MiniNumber;
import org.minima.system.commands.Command;
import org.minima.system.commands.CommandException;
import org.minima.utils.json.JSONArray;
import org.minima.utils.json.JSONObject;

Expand All @@ -31,6 +32,10 @@ public JSONObject runCommand() throws Exception {

//Get the Transaction..
TxnRow txnrow = db.getTransactionRow(getParam("id"));
if(txnrow == null) {
throw new CommandException("Transaction not found : "+id);
}

Transaction txn = txnrow.getTransaction();
Witness wit = txnrow.getWitness();

Expand Down
5 changes: 5 additions & 0 deletions src/org/minima/system/commands/txn/txnclear.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.minima.database.userprefs.txndb.TxnDB;
import org.minima.database.userprefs.txndb.TxnRow;
import org.minima.system.commands.Command;
import org.minima.system.commands.CommandException;
import org.minima.utils.json.JSONObject;

public class txnclear extends Command {
Expand All @@ -22,6 +23,10 @@ public JSONObject runCommand() throws Exception {

//Get the Transaction..
TxnRow txnrow = db.getTransactionRow(getParam("id"));
if(txnrow == null) {
throw new CommandException("Transaction not found : "+id);
}

txnrow.clearWitness();

JSONObject resp = new JSONObject();
Expand Down
24 changes: 19 additions & 5 deletions src/org/minima/system/commands/txn/txnexport.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package org.minima.system.commands.txn;

import java.io.File;

import org.minima.database.MinimaDB;
import org.minima.database.userprefs.txndb.TxnDB;
import org.minima.database.userprefs.txndb.TxnRow;
import org.minima.objects.base.MiniData;
import org.minima.system.commands.Command;
import org.minima.system.commands.CommandException;
import org.minima.utils.MiniFile;
import org.minima.utils.MiniFormat;
import org.minima.utils.json.JSONObject;

public class txnexport extends Command {

public txnexport() {
super("txnexport","[id:] - Export a transaction");
super("txnexport","[id:] [file:] - Export a transaction to a file");
}

@Override
Expand All @@ -20,19 +24,29 @@ public JSONObject runCommand() throws Exception {

TxnDB db = MinimaDB.getDB().getCustomTxnDB();

String id = getParam("id");
String id = getParam("id");
String file = getParam("file");

//Get the Transaction..
TxnRow txnrow = db.getTransactionRow(getParam("id"));
if(txnrow == null) {
throw new CommandException("Transaction not found : "+id);
}

//Export it..
MiniData data = MiniData.getMiniDataVersion(txnrow);
//Create the file
File output = new File(file);
if(output.exists()) {
output.delete();
}

//Now export this to a file..
MiniFile.writeObjectToFile(output, txnrow);

JSONObject resp = new JSONObject();
ret.put("response", data.to0xString());
resp.put("file", output.getAbsolutePath());
resp.put("size", MiniFormat.formatSize(output.length()));

ret.put("response", resp);

return ret;
}
Expand Down
20 changes: 17 additions & 3 deletions src/org/minima/system/commands/txn/txnimport.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package org.minima.system.commands.txn;

import java.io.File;

import org.minima.database.MinimaDB;
import org.minima.database.userprefs.txndb.TxnDB;
import org.minima.database.userprefs.txndb.TxnRow;
import org.minima.objects.base.MiniData;
import org.minima.system.commands.Command;
import org.minima.system.commands.CommandException;
import org.minima.utils.MiniFile;
import org.minima.utils.json.JSONObject;

public class txnimport extends Command {

public txnimport() {
super("txnimport","[data:] (id:) - Import a transaction. Optionally specify the ID");
super("txnimport","[file:] (id:) - Import a transaction. Optionally specify the ID");
}

@Override
Expand All @@ -19,10 +23,20 @@ public JSONObject runCommand() throws Exception {

TxnDB db = MinimaDB.getDB().getCustomTxnDB();

String data = getParam("data");
String file = getParam("file");
File ff = new File(file);
if(!ff.exists()) {
throw new CommandException("File does not exist : "+ff.getAbsolutePath());
}

//Load it in..
byte[] txndata = MiniFile.readCompleteFile(ff);

//Convert to MiniData
MiniData minitxn = new MiniData(txndata);

//Convert this..
TxnRow txnrow = TxnRow.convertMiniDataVersion(new MiniData(data));
TxnRow txnrow = TxnRow.convertMiniDataVersion(minitxn);
if(existsParam("id")) {
txnrow.setID(getParam("id"));
}
Expand Down
7 changes: 6 additions & 1 deletion src/org/minima/system/commands/txn/txninput.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.minima.database.MinimaDB;
import org.minima.database.userprefs.txndb.TxnDB;
import org.minima.database.userprefs.txndb.TxnRow;
import org.minima.objects.Coin;
import org.minima.objects.Transaction;
import org.minima.objects.base.MiniData;
Expand Down Expand Up @@ -46,7 +47,11 @@ public JSONObject runCommand() throws Exception {
}

//Get the Transaction
Transaction trans = db.getTransactionRow(id).getTransaction();
TxnRow txnrow = db.getTransactionRow(getParam("id"));
if(txnrow == null) {
throw new CommandException("Transaction not found : "+id);
}
Transaction trans = txnrow.getTransaction();
trans.addInput(cc);

//Output the current trans..
Expand Down
7 changes: 6 additions & 1 deletion src/org/minima/system/commands/txn/txnoutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.minima.database.MinimaDB;
import org.minima.database.userprefs.txndb.TxnDB;
import org.minima.database.userprefs.txndb.TxnRow;
import org.minima.objects.Coin;
import org.minima.objects.Token;
import org.minima.objects.Transaction;
Expand Down Expand Up @@ -59,7 +60,11 @@ public JSONObject runCommand() throws Exception {
output.setFloating(floating);

//Get the Transaction
Transaction trans = db.getTransactionRow(id).getTransaction();
TxnRow txnrow = db.getTransactionRow(getParam("id"));
if(txnrow == null) {
throw new CommandException("Transaction not found : "+id);
}
Transaction trans = txnrow.getTransaction();
trans.addOutput(output);

//Output the current trans..
Expand Down
4 changes: 4 additions & 0 deletions src/org/minima/system/commands/txn/txnpost.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.minima.system.Main;
import org.minima.system.brains.TxPoWGenerator;
import org.minima.system.commands.Command;
import org.minima.system.commands.CommandException;
import org.minima.utils.json.JSONObject;

public class txnpost extends Command {
Expand All @@ -28,6 +29,9 @@ public JSONObject runCommand() throws Exception {

//Get the row..
TxnRow txnrow = db.getTransactionRow(id);
if(txnrow == null) {
throw new CommandException("Transaction not found : "+id);
}

//Get the Transaction
Transaction trans = txnrow.getTransaction();
Expand Down
4 changes: 4 additions & 0 deletions src/org/minima/system/commands/txn/txnsign.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public JSONObject runCommand() throws Exception {

//Get the Transaction..
TxnRow txnrow = db.getTransactionRow(getParam("id"));
if(txnrow == null) {
throw new CommandException("Transaction not found : "+id);
}

Transaction txn = txnrow.getTransaction();
Witness wit = txnrow.getWitness();

Expand Down
8 changes: 7 additions & 1 deletion src/org/minima/system/commands/txn/txnstate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.minima.database.MinimaDB;
import org.minima.database.userprefs.txndb.TxnDB;
import org.minima.database.userprefs.txndb.TxnRow;
import org.minima.objects.StateVariable;
import org.minima.objects.Transaction;
import org.minima.system.commands.Command;
import org.minima.system.commands.CommandException;
import org.minima.utils.json.JSONObject;

public class txnstate extends Command {
Expand All @@ -29,7 +31,11 @@ public JSONObject runCommand() throws Exception {
}

//Get the Transaction
Transaction trans = db.getTransactionRow(id).getTransaction();
TxnRow txnrow = db.getTransactionRow(getParam("id"));
if(txnrow == null) {
throw new CommandException("Transaction not found : "+id);
}
Transaction trans = txnrow.getTransaction();

//Create a state variable..
StateVariable sv = new StateVariable(Integer.parseInt(port),value,keeper);
Expand Down

0 comments on commit 0cdeef3

Please sign in to comment.