/** * Quantize given permission (add cost for that permission). * Use for permissions that will be applicated, but before checking. * @param permission that will be quantized * @throws Quantiser.QuantiserException if processing cost limit is got */ public void checkApplicablePermissionQuantized(Permission permission) throws Quantiser.QuantiserException { // Add check an applicable permission quanta quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_APPLICABLE_PERM); // Add check a splitjoin permission in addition to the permission check quanta if(permission instanceof SplitJoinPermission) { quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_SPLITJOIN_PERM); } }
protected void checkSubItemQuantized(Contract contract, String prefix, Map<HashId,Contract> neighbourContracts) throws Quantiser.QuantiserException { // Add checks from subItem quanta contract.quantiser.reset(quantiser.getQuantaLimit() - quantiser.getQuantaSum()); contract.check(prefix, neighbourContracts); quantiser.addWorkCostFrom(contract.quantiser); }
@Test public void fullSum() throws Exception { int wantedSum = Quantiser.QuantiserProcesses.PRICE_APPLICABLE_PERM.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_2048_SIG.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_4096_SIG.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_REFERENCED_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_REVOKE_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_SPLITJOIN_PERM.getCost(); try { byte[] hashBytes = new byte[128]; new Random().nextBytes(hashBytes); HashId hashId = new HashId(hashBytes); Quantiser q = new Quantiser(); q.reset(wantedSum); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_APPLICABLE_PERM); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_2048_SIG); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_4096_SIG); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_REFERENCED_VERSION); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REVOKE_VERSION); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_SPLITJOIN_PERM); assertEquals(wantedSum, q.getQuantaSum()); } catch (Quantiser.QuantiserException e) { fail(); } }
@Test public void limit() throws Exception { byte[] hashBytes = new byte[128]; new Random().nextBytes(hashBytes); try { Quantiser q = new Quantiser(); q.reset(1000); for (int i = 0; i < 1000000; ++i) q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION); fail(); } catch(Quantiser.QuantiserException e) { return; } }
@Test public void noLimit() throws Exception { try { byte[] hashBytes = new byte[128]; new Random().nextBytes(hashBytes); Quantiser q = new Quantiser(); q.resetNoLimit(); for (int i = 0; i < 1000000; ++i) q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION); } catch(Quantiser.QuantiserException e) { fail(); } }
Quantiser quantiser = new Quantiser(); quantiser.reset(Contract.getTestQuantaLimit()); for (Bytes b : foreignReferenceBytesList) { Contract frc = new Contract(b.toArray(), this); quantiser.addWorkCostFrom(frc.getQuantiser()); referencedItems.put(frc.getId(), frc); contract = new Contract(bb, this); quantiser.addWorkCostFrom(contract.getQuantiser());
private boolean check(String prefix, Map<HashId,Contract> contractsTree) throws Quantiser.QuantiserException { quantiser.reset(quantiser.getQuantaLimit()); quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION); quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REVOKE_VERSION); quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_REFERENCED_VERSION);
public void resetNoLimit() { reset(-1); }
/** * Get the cost used for contract processing. * You must first call {@link #check()} method to check contract and calculate cost. * @return cost in quantas */ public int getProcessedCost() { return quantiser.getQuantaSum(); }
quantiser.addWorkCostFrom(c.getQuantiser()); subItems.put(c.getId(), c);
contract.addNewItems(c_2); c_1.getQuantiser().resetNoLimit(); c_2.getQuantiser().resetNoLimit(); c_3.getQuantiser().resetNoLimit();
/** * Create an empty new contract */ public Contract() { this.quantiser.reset(testQuantaLimit); // debug const. need to get quantaLimit from TransactionPack here definition = new Definition(); state = new State(); }
/** * Get the cost used for contract processing. * You must first call {@link #check()} method to check contract and calculate cost. * @return cost in "U" */ public int getProcessedCostU() { return (int) Math.ceil( (double) quantiser.getQuantaSum() / Quantiser.quantaPerU); }
/** * Verify signatures in contract and all sub-items (if needed). * Errors found can be accessed in certain contract with {@link #getErrors()} * * @throws Quantiser.QuantiserException when quantas limit was reached during check */ private void verifySignatures() throws Quantiser.QuantiserException { verifySealedKeys(true); // verify signatures of new items for (Contract c: newItems) { // Add verification signatures from new item quanta c.quantiser.reset(quantiser.getQuantaLimit() - quantiser.getQuantaSum()); c.verifySignatures(); quantiser.addWorkCostFrom(c.quantiser); } // verify signatures of revoking items for (Contract c: revokingItems) { // Add verification signatures from revoking item quanta c.quantiser.reset(quantiser.getQuantaLimit() - quantiser.getQuantaSum()); c.verifySealedKeys(true); quantiser.addWorkCostFrom(c.quantiser); } }
this.quantiser.reset(testQuantaLimit); // debug const. need to get quantaLimit from TransactionPack here
public void addWorkCostFrom(Quantiser quantiser) throws QuantiserException { quantaSum_ += quantiser.getQuantaSum(); // System.out.println("Add processing cost from " + quantiser.getClass().getSimpleName() + " (" + quantiser.getQuantaSum() + "), now cost is " + quantaSum_ + ", limit is " + quantaLimit_); if (quantaLimit_ >= 0) if (quantaSum_ > quantaLimit_) { // System.out.println("Limit, break "); throw new QuantiserException(); } }
/** * Verify signature, but before quantize this operation. * @param key that will be quantized * @throws Quantiser.QuantiserException if processing cost limit is got */ protected void verifySignatureQuantized(PublicKey key) throws Quantiser.QuantiserException { // Add check signature quanta if(key.getBitStrength() == 2048) { quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_2048_SIG); } else { quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_4096_SIG); } }
this.quantiser.reset(testQuantaLimit); // debug const. need to get quantaLimit from TransactionPack here
assertEquals(stepaCoins.getQuantiser().getQuantaSum(), 50); assertEquals(stepaCoins.getSealedByKeys().size(), 2); assertEquals(stepaCoins.getNew().get(0).getSealedByKeys().size(), 1);
payload.getQuantiser().reset(parcel.getQuantasLimit());