/** * Method calls from {@link SlotContract#fromDslFile(String)} and initialize contract from given binder. * @param root id binder with initialized data * @return created and ready {@link SlotContract} contract. * @throws EncryptionError if something went wrong */ protected SlotContract initializeWithDsl(Binder root) throws EncryptionError { super.initializeWithDsl(root); int numRevisions = root.getBinder("state").getBinder("data").getInt(KEEP_REVISIONS_FIELD_NAME, -1); if(numRevisions > 0) keepRevisions = numRevisions; return this; }
return true; int UAmount = contract.getStateData().getInt("transaction_units",0); uBalance.addAndGet(UAmount); int testUAmount = contract.getStateData().getInt("test_transaction_units",0); int minBalance = config.getBinder("auto_payment").getInt("min_balance", -1); if (minBalance > 0 && uBalance.get() < minBalance) { System.out.println("U balance is less than threshold of " + minBalance + ". Trying to get more Us"); int min = answer.data.getBinder("limits").getBinder("U").getIntOrThrow("min"); int max = answer.data.getBinder("limits").getBinder("U").getIntOrThrow("max"); int amount = config.getBinder("auto_payment").getInt("amount", min); if (amount < min) { amount = min;
private void deserializeForUns(BiDeserializer deserializer) { storedNames = deserializer.deserialize(getStateData().getList(NAMES_FIELD_NAME, null)); paidU = getStateData().getInt(PAID_U_FIELD_NAME, 0); prepaidNamesForDays = getStateData().getDouble(PREPAID_ND_FIELD_NAME); long prepaidFromSeconds = getStateData().getLong(PREPAID_ND_FROM_TIME_FIELD_NAME, 0); prepaidFrom = ZonedDateTime.ofInstant(Instant.ofEpochSecond(prepaidFromSeconds), ZoneId.systemDefault()); }
@Test public void getInt() throws Exception { Binder b = Binder.fromKeysValues( "i1", 100, "i2", "101", "l1", "1505774997427", "l2", 1505774997427L ); assertEquals(100, (int) b.getInt("i1",222)); assertEquals(101, (int) b.getInt("i2",222)); assertEquals(100, b.getIntOrThrow("i1")); assertEquals(101, b.getIntOrThrow("i2")); assertEquals(1505774997427L, b.getLongOrThrow("l1")); assertEquals(1505774997427L, b.getLongOrThrow("l2")); }
@Override public void deserialize(Binder data, BiDeserializer deserializer) { super.deserialize(data, deserializer); this.quorumSize = data.getInt("quorumSize", 0); Object mode = data.getOrDefault("mode", null); if (mode != null) { this.mode = Mode.valueOf((String) mode); } List<Binder> roles = data.getList("roles", null); if (roles != null) { this.roles.clear(); roles.forEach(role -> addRole(deserializer.deserialize(role))); } }
@Override public boolean beforeUpdate(ImmutableEnvironment c) { boolean checkResult = false; // recalculate prepaid origins*days without saving to state calculatePrepaidOriginDays(false); // check that payment was not hacked checkResult = prepaidOriginDays == getStateData().getInt(PREPAID_OD_FIELD_NAME, 0); if(!checkResult) { addError(Errors.FAILED_CHECK, "Wrong [state.data." + PREPAID_OD_FIELD_NAME + "] value. " + "Should be sum of early paid U and paid U by current revision."); return checkResult; } // and call common follower check checkResult = additionallyFollowerCheck(c); return checkResult; }
private Binder getStats(Binder params, Session session) throws CommandFailedException { checkNode(session, true); if (config == null || node == null || !( config.getNetworkAdminKeyAddress().isMatchingKey(session.getPublicKey()) || node.getNodeKey().equals(session.getPublicKey()) || config.getKeysWhiteList().contains(session.getPublicKey()) || config.getAddressesWhiteList().stream().anyMatch(addr -> addr.isMatchingKey(session.getPublicKey())) )) { System.out.println("command needs admin key"); return Binder.of( "itemResult", itemResultOfError(Errors.BAD_CLIENT_KEY,"getStats", "command needs admin key")); } return node.provideStats(params.getInt("showDays",null)); }
this.name = data.getString("name", null); this.type = data.getInt("type", null); this.comment = data.getString("comment", null);
int u = getStateData().getInt("transaction_units", -1); int test_u = getStateData().getInt("test_transaction_units", -1); if (u < 0) { res = false; parent = getRevokingItem(getParent()); int was_u = parent.getStateData().getInt("transaction_units", -1); int was_test_u = parent.getStateData().getInt("test_transaction_units", -1);
@Override public boolean beforeUpdate(ImmutableEnvironment c) { boolean checkResult = false; // recalculate storing info without saving to state to get valid storing data calculatePrepaidKilobytesForDays(false); // check that payment was not hacked checkResult = prepaidKilobytesForDays == getStateData().getInt(PREPAID_KD_FIELD_NAME, 0); if(!checkResult) { addError(Errors.FAILED_CHECK, "Wrong [state.data." + PREPAID_KD_FIELD_NAME + "] value. " + "Should be sum of early paid U and paid U by current revision."); return checkResult; } if(getTrackingContract() != null) { // check for that last revision of tracking contract has same owner as creator of slot checkResult = getTrackingContract().getOwner().isAllowedForKeys(getEffectiveKeys()); if (!checkResult) { addError(Errors.FAILED_CHECK, "Slot-contract signing keys must has allowed keys for owner of tracking contract"); return checkResult; } } // and call common slot check checkResult = additionallySlotCheck(c); return checkResult; }
@Override public boolean beforeCreate(ImmutableEnvironment c) { boolean checkResult = true; // recalculate prepaid origins*days without saving to state calculatePrepaidOriginDays(false); int paidU = getPaidU(); if(paidU == 0) { if(getPaidU(true) > 0) { addError(Errors.FAILED_CHECK, "Test payment is not allowed for follower contracts"); } checkResult = false; } else if(paidU < getMinPayment()) { addError(Errors.FAILED_CHECK, "Payment for follower contract is below minimum level of " + getMinPayment() + "U"); checkResult = false; } if(!checkResult) { addError(Errors.FAILED_CHECK, "Follower contract hasn't valid payment"); return checkResult; } // check that payment was not hacked checkResult = prepaidOriginDays == getStateData().getInt(PREPAID_OD_FIELD_NAME, 0); if(!checkResult) { addError(Errors.FAILED_CHECK, "Wrong [state.data." + PREPAID_OD_FIELD_NAME + "] value. " + "Should be sum of early paid U and paid U by current revision."); return checkResult; } // and call common follower check checkResult = additionallyFollowerCheck(c); return checkResult; }
private Binder getContract(Binder params, Session session) throws IOException { checkNode(session, true); Binder res = new Binder(); if (!node.getConfig().isPermanetMode()) return res; HashId origin = (HashId) params.get("origin"); int limit = params.getInt("limit", 100); if (limit > node.getConfig().getQueryContractsLimit()) limit = node.getConfig().getQueryContractsLimit(); if (limit < 1) limit = 1; Object keeping = node.getLedger().getKeepingByOrigin(origin, limit); if (keeping == null) return res; if (keeping instanceof byte[]) res.put("packedContract", keeping); else if (keeping instanceof List) res.put("contractIds", keeping); return res; }
assertEquals(followerInfo.size(),9); assertEquals(followerInfo.getInt("paid_U", 0),followerContract.getStateData().get("paid_U")); assertTrue(followerInfo.getDouble("prepaid_OD") == followerContract.getPrepaidOriginsDays().doubleValue()); assertEquals(followerInfo.getLong("prepaid_from", 0), followerContract.getStateData().get("prepaid_from")); assertEquals(followerInfo.getInt("followed_origins", 0), followerContract.getStateData().get("followed_origins")); assertEquals(followerInfo.getDouble("spent_OD"), followerContract.getStateData().get("spent_OD")); assertEquals(followerInfo.getLong("spent_OD_time", 0), followerContract.getStateData().get("spent_OD_time"));
int paidU = payload.getStateData().getInt(NSmartContract.PAID_U_FIELD_NAME, 0); if (paidU > 0) ledger.savePayment(paidU, payloadProcessor != null ? payloadProcessor.record.getCreatedAt() : ledger.getRecord(payload.getId()).getCreatedAt());
checkResult = prepaidKilobytesForDays == getStateData().getInt(PREPAID_KD_FIELD_NAME, 0); if(!checkResult) { addError(Errors.FAILED_CHECK, "Wrong [state.data." + PREPAID_KD_FIELD_NAME + "] value. " +