use of com.iota.iri.model.Hash in project iri by iotaledger.
the class TipsManagerTest method updateRatings2TestWorks.
@Test
public void updateRatings2TestWorks() throws Exception {
TransactionViewModel transaction, transaction1, transaction2, transaction3, transaction4;
transaction = new TransactionViewModel(getRandomTransactionTrits(), getRandomTransactionHash());
transaction1 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction.getHash(), transaction.getHash()), getRandomTransactionHash());
transaction2 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction1.getHash(), transaction1.getHash()), getRandomTransactionHash());
transaction3 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction2.getHash(), transaction2.getHash()), getRandomTransactionHash());
transaction4 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction3.getHash(), transaction3.getHash()), getRandomTransactionHash());
transaction.store(tangle);
transaction1.store(tangle);
transaction2.store(tangle);
transaction3.store(tangle);
transaction4.store(tangle);
Map<Hash, Long> ratings = new HashMap<>();
tipsManager.recursiveUpdateRatings(transaction.getHash(), ratings, new HashSet<>());
Assert.assertTrue(ratings.get(transaction.getHash()).equals(5L));
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class TipsManagerTest method updateRatingsTestWorks.
@Test
public void updateRatingsTestWorks() throws Exception {
TransactionViewModel transaction, transaction1, transaction2, transaction3, transaction4;
transaction = new TransactionViewModel(getRandomTransactionTrits(), getRandomTransactionHash());
transaction1 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction.getHash(), transaction.getHash()), getRandomTransactionHash());
transaction2 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction1.getHash(), transaction1.getHash()), getRandomTransactionHash());
transaction3 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction2.getHash(), transaction1.getHash()), getRandomTransactionHash());
transaction4 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction2.getHash(), transaction3.getHash()), getRandomTransactionHash());
transaction.store(tangle);
transaction1.store(tangle);
transaction2.store(tangle);
transaction3.store(tangle);
transaction4.store(tangle);
Map<Hash, Set<Hash>> ratings = new HashMap<>();
tipsManager.updateHashRatings(transaction.getHash(), ratings, new HashSet<>());
Assert.assertEquals(ratings.get(transaction.getHash()).size(), 5);
Assert.assertEquals(ratings.get(transaction1.getHash()).size(), 4);
Assert.assertEquals(ratings.get(transaction2.getHash()).size(), 3);
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class API method process.
private AbstractResponse process(final String requestString, InetSocketAddress sourceAddress) throws UnsupportedEncodingException {
try {
final Map<String, Object> request = gson.fromJson(requestString, Map.class);
if (request == null) {
return ExceptionResponse.create("Invalid request payload: '" + requestString + "'");
}
final String command = (String) request.get("command");
if (command == null) {
return ErrorResponse.create("COMMAND parameter has not been specified in the request.");
}
if (instance.configuration.string(DefaultConfSettings.REMOTE_LIMIT_API).contains(command) && !sourceAddress.getAddress().isLoopbackAddress()) {
return AccessLimitedResponse.create("COMMAND " + command + " is not available on this node");
}
log.debug("# {} -> Requesting command '{}'", counter.incrementAndGet(), command);
switch(command) {
case "addNeighbors":
{
List<String> uris = getParameterAsList(request, "uris", 0);
log.debug("Invoking 'addNeighbors' with {}", uris);
return addNeighborsStatement(uris);
}
case "attachToTangle":
{
final Hash trunkTransaction = new Hash(getParameterAsStringAndValidate(request, "trunkTransaction", HASH_SIZE));
final Hash branchTransaction = new Hash(getParameterAsStringAndValidate(request, "branchTransaction", HASH_SIZE));
final int minWeightMagnitude = getParameterAsInt(request, "minWeightMagnitude");
final List<String> trytes = getParameterAsList(request, "trytes", TRYTES_SIZE);
List<String> elements = attachToTangleStatement(trunkTransaction, branchTransaction, minWeightMagnitude, trytes);
return AttachToTangleResponse.create(elements);
}
case "broadcastTransactions":
{
final List<String> trytes = getParameterAsList(request, "trytes", TRYTES_SIZE);
broadcastTransactionStatement(trytes);
return AbstractResponse.createEmptyResponse();
}
case "findTransactions":
{
return findTransactionStatement(request);
}
case "getBalances":
{
final List<String> addresses = getParameterAsList(request, "addresses", HASH_SIZE);
final List<String> tips = request.containsKey("tips") ? getParameterAsList(request, "tips ", HASH_SIZE) : null;
final int threshold = getParameterAsInt(request, "threshold");
return getBalancesStatement(addresses, tips, threshold);
}
case "getInclusionStates":
{
if (invalidSubtangleStatus()) {
return ErrorResponse.create("This operations cannot be executed: The subtangle has not been updated yet.");
}
final List<String> transactions = getParameterAsList(request, "transactions", HASH_SIZE);
final List<String> tips = getParameterAsList(request, "tips", HASH_SIZE);
return getNewInclusionStateStatement(transactions, tips);
}
case "getNeighbors":
{
return getNeighborsStatement();
}
case "getNodeInfo":
{
String name = instance.configuration.booling(Configuration.DefaultConfSettings.TESTNET) ? IRI.TESTNET_NAME : IRI.MAINNET_NAME;
return GetNodeInfoResponse.create(name, IRI.VERSION, Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().freeMemory(), System.getProperty("java.version"), Runtime.getRuntime().maxMemory(), Runtime.getRuntime().totalMemory(), instance.milestone.latestMilestone, instance.milestone.latestMilestoneIndex, instance.milestone.latestSolidSubtangleMilestone, instance.milestone.latestSolidSubtangleMilestoneIndex, instance.node.howManyNeighbors(), instance.node.queuedTransactionsSize(), System.currentTimeMillis(), instance.tipsViewModel.size(), instance.transactionRequester.numberOfTransactionsToRequest());
}
case "getTips":
{
return getTipsStatement();
}
case "getTransactionsToApprove":
{
if (invalidSubtangleStatus()) {
return ErrorResponse.create("This operations cannot be executed: The subtangle has not been updated yet.");
}
final String reference = request.containsKey("reference") ? getParameterAsStringAndValidate(request, "reference", HASH_SIZE) : null;
final int depth = getParameterAsInt(request, "depth");
if (depth < 0 || (reference == null && depth == 0)) {
return ErrorResponse.create("Invalid depth input");
}
int numWalks = request.containsKey("numWalks") ? getParameterAsInt(request, "numWalks") : 1;
if (numWalks < minRandomWalks) {
numWalks = minRandomWalks;
}
try {
final Hash[] tips = getTransactionToApproveStatement(depth, reference, numWalks);
if (tips == null) {
return ErrorResponse.create("The subtangle is not solid");
}
return GetTransactionsToApproveResponse.create(tips[0], tips[1]);
} catch (RuntimeException e) {
log.info("Tip selection failed: " + e.getLocalizedMessage());
return ErrorResponse.create(e.getLocalizedMessage());
}
}
case "getTrytes":
{
final List<String> hashes = getParameterAsList(request, "hashes", HASH_SIZE);
return getTrytesStatement(hashes);
}
case "interruptAttachingToTangle":
{
pearlDiver.cancel();
return AbstractResponse.createEmptyResponse();
}
case "removeNeighbors":
{
List<String> uris = getParameterAsList(request, "uris", 0);
log.debug("Invoking 'removeNeighbors' with {}", uris);
return removeNeighborsStatement(uris);
}
case "storeTransactions":
{
try {
final List<String> trytes = getParameterAsList(request, "trytes", TRYTES_SIZE);
storeTransactionStatement(trytes);
return AbstractResponse.createEmptyResponse();
} catch (RuntimeException e) {
// transaction not valid
return ErrorResponse.create("Invalid trytes input");
}
}
case "getMissingTransactions":
{
// TransactionRequester.instance().rescanTransactionsToRequest();
synchronized (instance.transactionRequester) {
List<String> missingTx = Arrays.stream(instance.transactionRequester.getRequestedTransactions()).map(Hash::toString).collect(Collectors.toList());
return GetTipsResponse.create(missingTx);
}
}
case "checkConsistency":
{
if (invalidSubtangleStatus()) {
return ErrorResponse.create("This operations cannot be executed: The subtangle has not been updated yet.");
}
final List<String> transactions = getParameterAsList(request, "tails", HASH_SIZE);
return checkConsistencyStatement(transactions);
}
case "wereAddressesSpentFrom":
{
final List<String> addresses = getParameterAsList(request, "addresses", HASH_SIZE);
return wereAddressesSpentFromStatement(addresses);
}
default:
{
AbstractResponse response = ixi.processCommand(command, request);
return response == null ? ErrorResponse.create("Command [" + command + "] is unknown") : response;
}
}
} catch (final ValidationException e) {
log.info("API Validation failed: " + e.getLocalizedMessage());
return ErrorResponse.create(e.getLocalizedMessage());
} catch (final Exception e) {
log.error("API Exception: ", e);
return ExceptionResponse.create(e.getLocalizedMessage());
}
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class API method attachToTangleStatement.
public synchronized List<String> attachToTangleStatement(final Hash trunkTransaction, final Hash branchTransaction, final int minWeightMagnitude, final List<String> trytes) {
final List<TransactionViewModel> transactionViewModels = new LinkedList<>();
Hash prevTransaction = null;
pearlDiver = new PearlDiver();
int[] transactionTrits = Converter.allocateTritsForTrytes(TRYTES_SIZE);
for (final String tryte : trytes) {
long startTime = System.nanoTime();
long timestamp = System.currentTimeMillis();
try {
Converter.trits(tryte, transactionTrits, 0);
// branch and trunk
System.arraycopy((prevTransaction == null ? trunkTransaction : prevTransaction).trits(), 0, transactionTrits, TransactionViewModel.TRUNK_TRANSACTION_TRINARY_OFFSET, TransactionViewModel.TRUNK_TRANSACTION_TRINARY_SIZE);
System.arraycopy((prevTransaction == null ? branchTransaction : trunkTransaction).trits(), 0, transactionTrits, TransactionViewModel.BRANCH_TRANSACTION_TRINARY_OFFSET, TransactionViewModel.BRANCH_TRANSACTION_TRINARY_SIZE);
// tag - copy the obsolete tag to the attachment tag field only if tag isn't set.
if (Arrays.stream(transactionTrits, TransactionViewModel.TAG_TRINARY_OFFSET, TransactionViewModel.TAG_TRINARY_OFFSET + TransactionViewModel.TAG_TRINARY_SIZE).allMatch(s -> s == 0)) {
System.arraycopy(transactionTrits, TransactionViewModel.OBSOLETE_TAG_TRINARY_OFFSET, transactionTrits, TransactionViewModel.TAG_TRINARY_OFFSET, TransactionViewModel.TAG_TRINARY_SIZE);
}
Converter.copyTrits(timestamp, transactionTrits, TransactionViewModel.ATTACHMENT_TIMESTAMP_TRINARY_OFFSET, TransactionViewModel.ATTACHMENT_TIMESTAMP_TRINARY_SIZE);
Converter.copyTrits(0, transactionTrits, TransactionViewModel.ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_OFFSET, TransactionViewModel.ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_SIZE);
Converter.copyTrits(MAX_TIMESTAMP_VALUE, transactionTrits, TransactionViewModel.ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_OFFSET, TransactionViewModel.ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_SIZE);
if (!pearlDiver.search(transactionTrits, minWeightMagnitude, 0)) {
transactionViewModels.clear();
break;
}
// validate PoW - throws exception if invalid
final TransactionViewModel transactionViewModel = instance.transactionValidator.validate(transactionTrits, instance.transactionValidator.getMinWeightMagnitude());
transactionViewModels.add(transactionViewModel);
prevTransaction = transactionViewModel.getHash();
} finally {
API.incEllapsedTime_PoW(System.nanoTime() - startTime);
API.incCounter_PoW();
if ((API.getCounter_PoW() % 100) == 0) {
String sb = "Last 100 PoW consumed " + API.getEllapsedTime_PoW() / 1000000000L + " seconds processing time.";
log.info(sb);
counter_PoW = 0;
ellapsedTime_PoW = 0L;
}
}
}
final List<String> elements = new LinkedList<>();
for (int i = transactionViewModels.size(); i-- > 0; ) {
elements.add(Converter.trytes(transactionViewModels.get(i).trits()));
}
return elements;
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class API method findTail.
private Hash findTail(Hash hash) throws Exception {
TransactionViewModel tx = TransactionViewModel.fromHash(instance.tangle, hash);
final Hash bundleHash = tx.getBundleHash();
long index = tx.getCurrentIndex();
boolean foundApprovee = false;
while (index-- > 0 && tx.getBundleHash().equals(bundleHash)) {
Set<Hash> approvees = tx.getApprovers(instance.tangle).getHashes();
for (Hash approvee : approvees) {
TransactionViewModel nextTx = TransactionViewModel.fromHash(instance.tangle, approvee);
if (nextTx.getBundleHash().equals(bundleHash)) {
tx = nextTx;
foundApprovee = true;
break;
}
}
if (!foundApprovee) {
break;
}
}
if (tx.getCurrentIndex() == 0) {
return tx.getHash();
}
return null;
}
Aggregations