public final boolean isApproved() { return state.isApproved(); }
private void voteItself() { if (resyncingItem.getItemState().isConsensusFound()) resyncingItem.resyncVote(myInfo, resyncingItem.getItemState()); else resyncingItem.resyncVote(myInfo, ItemState.UNDEFINED); }
public final boolean isPending() { return state.isPending(); }
public Binder toBinder() { return Binder.fromKeysValues( "state", state.name(), "haveCopy", haveCopy, "createdAt", createdAt, "expiresAt", expiresAt, "errors", DefaultBiMapper.serialize(errors), "isTestnet", isTestnet, "lockedById", lockedById, "extra", extraDataBinder ); }
@Override public Object getKeepingByOrigin(HashId origin, int limit) { return protect(() -> { try (ResultSet rs = inPool(db -> db.queryRow( "select keeping_items.packed, keeping_items.hash from keeping_items, ledger where ledger.hash = keeping_items.hash and origin = ? and state = ? order by keeping_items.id desc limit ?", origin.getDigest(), ItemState.APPROVED.ordinal(), limit))) { if (rs == null) return null; byte[] packed = rs.getBytes("packed"); List<byte[]> contractIds = new ArrayList<>(); contractIds.add(rs.getBytes("hash")); while (rs.next()) contractIds.add(rs.getBytes("hash")); if (contractIds.size() > 1) return contractIds; else return packed; } catch (Exception e) { e.printStackTrace(); throw e; } }); }
public ItemResult(Boss.Reader br) throws IOException { state = ItemState.values()[br.readInt()]; createdAt = ZonedDateTime.ofInstant(Instant.ofEpochSecond(br.readLong()), ZoneId.systemDefault()); expiresAt = ZonedDateTime.ofInstant(Instant.ofEpochSecond(br.readLong()), ZoneId.systemDefault()); haveCopy = br.read(); }
if((state.isPositive() && positiveNodes.contains(node)) || (!state.isPositive() && negativeNodes.contains(node))) { return; if (state.isPositive()) { add = positiveNodes; remove = negativeNodes;
/** * Construct from serialized parameters, presented in the binder instance * * @param fields binder with named parameters (case sensitive field names, like haveCopy or createdAt) */ public ItemResult(Binder fields) { state = ItemState.valueOf(fields.getStringOrThrow("state")); haveCopy = fields.getBooleanOrThrow("haveCopy"); createdAt = fields.getZonedDateTime("createdAt", null); expiresAt = fields.getZonedDateTime("expiresAt", null); errors = new ArrayList<>(); fields.getList("errors", Collections.EMPTY_LIST).forEach(x -> { errors.add( x instanceof Binder ? new ErrorRecord((Binder)x) : (ErrorRecord) x); }); isTestnet = fields.getBoolean("isTestnet",false); lockedById = (HashId) fields.get("lockedById"); extraDataBinder = fields.getBinder("extra", new Binder()); }
private ItemResult itemResultOfError(Errors error, String object, String message) { Binder binder = new Binder(); binder.put("state",ItemState.UNDEFINED.name()); binder.put("haveCopy",false); binder.put("createdAt", new Date()); binder.put("expiresAt", new Date()); ArrayList<ErrorRecord> errorRecords = new ArrayList<>(); errorRecords.add(new ErrorRecord(error,object,message)); binder.put("errors",errorRecords); return new ItemResult(binder); }
" hash " + "FROM ledger " + "WHERE hash IN ("+queryPart+") AND state = " + ItemState.APPROVED.ordinal()
@Override public Map<ItemState, Integer> getLedgerSize(ZonedDateTime createdAfter) { return protect(() -> { try (ResultSet rs = inPool(db -> db.queryRow("select count(id), state from ledger where created_at >= ? group by state", createdAfter != null ? createdAfter.toEpochSecond() : 0))) { Map<ItemState, Integer> result = new HashMap<>(); if (rs != null) { do { int count = rs.getInt(1); ItemState state = ItemState.values()[rs.getInt(2)]; result.put(state, count); } while (rs.next()); } return result; } catch (Exception e) { e.printStackTrace(); throw e; } }); }
/** * true if we need to get vote from a node * * @param node we might need vote from * * @return */ private final boolean needsVoteFrom(NodeInfo node) { return record.getState().isPending() && !positiveNodes.contains(node) && !negativeNodes.contains(node); }
@Test public void createAndRegisterTokenFromDSL() throws Exception { // You have a token dsl and want to release own tokens String uContract = getApprovedUContract(); callMain2("-create", rootPath + "TokenDSLTemplate.yml", "--output", basePath + "realToken.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey"); assertTrue (new File(basePath + "realToken.unicon").exists()); callMain("-register", basePath + "realToken.unicon", "-u", uContract, "-k", rootPath + "keys/stepan_mamontov.private.unikey", "-wait", "2000"); System.out.println(output); assertTrue (output.indexOf(ItemState.APPROVED.name()) >= 0); }
/** * Shortcut method: check that record exists and its state returns {@link ItemState#isConsensusFound()}}. Check it to * ensure its meaning. * * @param id is {@link HashId} for checking item * @return true if it is. */ default boolean isConsensusFound(HashId id) { StateRecord r = getRecord(id); return r != null && r.getState().isConsensusFound(); }
@Override protected void writeTo(Boss.Writer bw) throws IOException { super.writeTo(bw); if (!answerIsRequested()) { bw.write(itemState.ordinal()); bw.write(hasEnvironment); } }
public boolean isPositive() { return isApproved() || this == PENDING_POSITIVE; } }
public void initFrom(ResultSet rs) throws SQLException { // the processing mught be already fininshed by now: if( rs == null || rs.isClosed() ) throw new SQLException("resultset or connection is closed"); recordId = rs.getLong("id"); try { id = HashId.withDigest(Do.read(rs.getBinaryStream("hash"))); } catch (IOException e) { throw new SQLException("failed to read hash from the recordset"); } state = ItemState.values()[rs.getInt("state")]; createdAt = getTime(rs.getLong("created_at")); expiresAt = getTime(rs.getLong("expires_at")); if(expiresAt == null) { // todo: what we should do with items without expiresAt? expiresAt = createdAt.plusMonths(3); } lockedByRecordId = rs.getInt("locked_by_id"); }
/** * Set record to the approved state and saves it. Note that this process does not resolve any dependencies as for * now and does not revoke any records locked for revocation. This could be changed though. */ public void approve() { checkLedgerExists(); // check sanity if (state.isPending()) { setState(ItemState.APPROVED); save(); } else throw new IllegalStateException("attempt to approve record that is not pending: " + state); }
@Test public void createAndRegisterNotaryContractWithFileFromDSL() throws Exception { String uContract = getApprovedUContract(); callMain2("-create", rootPath + "notaryContractWithFile.yaml", "--output", basePath + "realNotaryContractWithFile.unicon", "-k", rootPath + "keys/marty_mcfly.private.unikey"); assertTrue (new File(basePath + "realNotaryContractWithFile.unicon").exists()); callMain("--register", basePath + "realNotaryContractWithFile.unicon", "--u", uContract, "-k", rootPath + "keys/stepan_mamontov.private.unikey", "--wait", "1000"); System.out.println(output); assertTrue (output.indexOf(ItemState.APPROVED.name()) >= 0); }
if(committingState.isConsensusFound()) { Set<NodeInfo> rNodes = new HashSet<>(); Set<NodeInfo> nowNodes = resyncNodes.get(committingState);