public static List<File> findFiles(String path, Boolean recursively) { List<File> foundContractFiles = new ArrayList<>(); File pathFile = new File(path); if (pathFile.exists()) { fillWithContractsFiles(foundContractFiles, path, recursively); } else { addError(Errors.NOT_FOUND.name(), path, "Path " + path + " does not exist"); // usage("Path " + path + " does not exist"); } return foundContractFiles; }
private static void doSetLogLevels() { List<String> sources = new ArrayList<String>((List) options.valuesOf("set-log-levels")); if(sources.size() != 3) { addError(Errors.COMMAND_FAILED.name(),"levels", "specify 3 log levels: node,network,udp"); } try { ItemResult ir = getClientNetwork().client.setVerboseLevel(DatagramAdapter.VerboseLevel.stringToInt(sources.get(0)), DatagramAdapter.VerboseLevel.stringToInt(sources.get(1)), DatagramAdapter.VerboseLevel.stringToInt(sources.get(2))); addErrors(ir.errors); } catch (ClientError clientError) { if (options.has("verbose")) clientError.printStackTrace(); addError(Errors.COMMAND_FAILED.name(),"levels",clientError.getMessage()); } catch (IOException e) { if (options.has("verbose")) e.printStackTrace(); addError(Errors.COMMAND_FAILED.name(),"levels",e.getMessage()); } finish(); }
public static synchronized Map<String, PrivateKey> keysMapContract() throws IOException { if (keyFilesContract == null) { keyFilesContract = new HashMap<>(); for (String fileName : keyFileNamesContract) { try { PrivateKey pk = PrivateKey.fromPath(Paths.get(fileName)); keyFilesContract.put(fileName, pk); } catch (IOException e) { addError(Errors.NOT_FOUND.name(), fileName.toString(), "failed to load key file: " + e.getMessage()); } } } return keyFilesContract; }
/** * Save specified parcel to file. * * @param parcel - parcel to save. * @param fileName - name of file to save to. * */ public static boolean saveParcel(Parcel parcel, String fileName) throws IOException { if (fileName == null) { fileName = "Universa_" + DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss").format(parcel.getPayloadContract().getCreatedAt()) + ".uniparcel"; } byte[] data = parcel.pack(); String newFileName = FileTool.writeFileContentsWithRenaming(fileName, data); report("Parcel is saved to: " + newFileName); report("Parcel size: " + data.length); try { if (parcel.getPaymentContract().check() && parcel.getPayloadContract().check()) { report("Parcel has no errors"); } else { addErrors(parcel.getPaymentContract().getErrors()); addErrors(parcel.getPayloadContract().getErrors()); } } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", parcel.toString(), e.getMessage()); } return (newFileName!=null); }
/** * Load parcel from specified path. * * @param fileName * * @return loaded Parcel. */ public static Parcel loadParcel(String fileName) throws IOException { Parcel parcel = null; File pathFile = new File(fileName); if (pathFile.exists()) { Path path = Paths.get(fileName); byte[] data = Files.readAllBytes(path); parcel = Parcel.unpack(data); } else { addError(Errors.NOT_FOUND.name(), fileName, "Path " + fileName + " does not exist"); } return parcel; }
addErrors(contract.getErrors()); } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage());
private static boolean checkAnswer(BasicHttpClient.Answer answer) { if(answer.code != 200) { addError(Errors.FAILURE.name(),"HTTP","http code " + answer.code); return false; } if(answer.data.getString("status").equals("error")) { addError(Errors.COMMAND_FAILED.name(),answer.data.getString("code"),answer.data.getString("text","")); return false; } if(!answer.data.getString("status").equalsIgnoreCase("ok")) { addError(Errors.COMMAND_FAILED.name(),"response_status","unknown_response status"); } return true; }
foundContracts.put(file.getAbsolutePath(), contract); } catch (RuntimeException e) { addError(Errors.FAILURE.name(), file.getAbsolutePath(), e.getMessage()); } catch (IOException e) { addError(Errors.FAILURE.name(), file.getAbsolutePath(), e.getMessage()); addError(Errors.NOT_FOUND.name(), path, "Path " + path + " does not exist");
private static void checkFile(File f) { try { TransactionPack tp = TransactionPack.unpack(Do.read(f), true); if (tp.isReconstructed()) { report("file " + f + " is a single contract"); } else { report("file " + f + " is a transaction pack"); } System.out.println(); checkContract(tp.getContract()); } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", f.getPath(), e.toString()); } catch (IOException e) { addError("READ_ERROR", f.getPath(), e.toString()); } catch (Exception e) { addError("UNKNOWN_ERROR", f.getPath(), e.toString()); } }
private static void addErrors(List<ErrorRecord> errors) { errors.forEach(e -> addError(e.getError().name(), e.getObjectName(), e.getMessage())); }
addError(Errors.NOT_FOUND.name(), roleName, "role doesn't exist");
private static String getUFromWallet(UUTNWallet wallet, int amount,boolean isTest) throws IOException { if(wallet == null) { System.out.println("UUTN wallet not found"); return null; } String fieldName = isTest ? "test_transaction_units" : "transaction_units"; int minAcceptableBalance = 0; HashId acceptableContractId = null; for(HashId uIds : wallet.us.keySet()) { Contract ucontract = wallet.us.get(uIds); int unitsCount = ucontract.getStateData().getIntOrThrow(fieldName); if(unitsCount >= amount && (minAcceptableBalance > unitsCount || acceptableContractId == null)) { minAcceptableBalance = unitsCount; acceptableContractId =uIds; } } String acceptableContract = acceptableContractId == null ? null : wallet.uPathes.get(acceptableContractId); if(acceptableContract == null) { addError(Errors.NOT_FOUND.name(),"U contract", "U contract is not found in UUTN wallet."); return null; } else { System.out.println("U contract is found in UUTN wallet: " + acceptableContract); System.out.println("Checking status..."); if(getClientNetwork().check(wallet.us.get(acceptableContractId).getId()).state != ItemState.APPROVED) { return getUFromWallet(fixUUTNWallet(wallet.path),amount,isTest); } } return wallet.path + File.separator + acceptableContract; } private static String getUFromWallet(int amount,boolean isTest) throws IOException {
/** * Check bytes is contract. And if bytes is, check contract for errors. Print errors if found. * * @param data - data to check. * * @return true if bytes is Contract and Contract is valid. */ private static Boolean checkBytesIsValidContract(byte[] data) { try { Contract contract = new Contract(data); if (!contract.isOk()) { reporter.message("The capsule is not sealed"); contract.getErrors().forEach(e -> reporter.error(e.getError().toString(), e.getObjectName(), e.getMessage())); } checkContract(contract); } catch (RuntimeException e) { addError(Errors.BAD_VALUE.name(), "byte[] data", e.getMessage()); return false; } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", "", e.toString()); } catch (IOException e) { addError(Errors.BAD_VALUE.name(), "byte[] data", e.getMessage()); return false; } return true; }
private static void saveContractSubitems(String source, String suffix, Contract contract) throws IOException { try { report("unpack contract from " + source); int i = 1; if (contract.getNewItems() != null) { for (Approvable newItem : contract.getNewItems()) { String newItemFileName = new FilenameTool(source).addSuffixToBase(suffix+"_new_item_" + i).toString(); report("save newItem to " + newItemFileName); // ((Contract) newItem).seal(); saveContract((Contract) newItem, newItemFileName); i++; } } i = 1; if (contract.getRevokingItems() != null) { for (Approvable revokeItem : contract.getRevokingItems()) { String revokeItemFileName = new FilenameTool(source).addSuffixToBase(suffix+"_revoke_" + i).setExtension("unicon").toString(); report("save revokeItem to " + revokeItemFileName); saveContract((Contract) revokeItem, revokeItemFileName); i++; } } } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage()); } }