@Override public void addFollowerCallback(HashId id, long environmentId, ZonedDateTime expiresAt, ZonedDateTime storedUntil) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "INSERT INTO follower_callbacks (id, state, environment_id, expires_at, stored_until) VALUES (?,?,?,?,?)" ) ) { statement.setBytes(1, id.getDigest()); statement.setInt(2, CallbackService.FollowerCallbackState.STARTED.ordinal()); statement.setLong(3, environmentId); statement.setLong(4, StateRecord.unixTime(expiresAt)); statement.setLong(5, StateRecord.unixTime(storedUntil)); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("follower callback save failed:" + se); } catch (Exception e) { e.printStackTrace(); } }
@Override public void saveFollowerEnvironment(long environmentId, ZonedDateTime expiresAt, ZonedDateTime mutedAt, double spent, int startedCallbacks) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "INSERT INTO follower_environments (environment_id, expires_at, muted_at, spent_for_callbacks, started_callbacks) " + "VALUES (?,?,?,?,?) ON CONFLICT (environment_id) DO UPDATE SET expires_at = EXCLUDED.expires_at, " + "muted_at = EXCLUDED.muted_at, spent_for_callbacks = EXCLUDED.spent_for_callbacks, started_callbacks = EXCLUDED.started_callbacks" ) ) { statement.setLong(1, environmentId); statement.setLong(2, StateRecord.unixTime(expiresAt)); statement.setLong(3, StateRecord.unixTime(mutedAt)); statement.setDouble(4, spent); statement.setInt(5, startedCallbacks); statement.closeOnCompletion(); statement.executeUpdate(); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("saveFollowerEnvironment failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("saveFollowerEnvironment failed: " + e); } }
@Override public void clearExpiredSubscriptions() { try (PooledDb db = dbPool.db()) { ZonedDateTime now = ZonedDateTime.now(); try ( PreparedStatement statement = db.statement( "DELETE FROM contract_subscription WHERE expires_at < ?" ) ) { statement.setLong(1, StateRecord.unixTime(now)); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("clearExpiredSubscriptions failed: " + se); } catch (Exception e) { e.printStackTrace(); } }
@Override public void clearExpiredStorages() { try (PooledDb db = dbPool.db()) { ZonedDateTime now = ZonedDateTime.now(); try ( PreparedStatement statement = db.statement( "DELETE FROM contract_storage WHERE expires_at < ?" ) ) { statement.setLong(1, StateRecord.unixTime(now)); db.updateWithStatement(statement); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("clearExpiredStorages failed: " + se); } catch (Exception e) { e.printStackTrace(); } }
@Override public void updateStorageExpiresAt(long storageId, ZonedDateTime expiresAt) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "UPDATE contract_storage SET expires_at = ? WHERE id = ?" ) ) { statement.setLong(1, StateRecord.unixTime(expiresAt)); statement.setLong(2, storageId); statement.closeOnCompletion(); statement.executeUpdate(); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("updateStorageExpiresAt failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("updateStorageExpiresAt failed: " + e); } }
@Override public void updateSubscriptionInStorage(long subscriptionId, ZonedDateTime expiresAt) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "UPDATE contract_subscription SET expires_at = ? WHERE id = ?" ) ) { statement.setLong(1, StateRecord.unixTime(expiresAt)); statement.setLong(2, subscriptionId); statement.closeOnCompletion(); statement.executeUpdate(); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("updateSubscriptionInStorage failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("updateSubscriptionInStorage failed: " + e); } }
@Override public void updateNameRecord(long nameRecordId, ZonedDateTime expiresAt) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement( "UPDATE name_storage SET expires_at = ? WHERE id = ?" ) ) { statement.setLong(1, StateRecord.unixTime(expiresAt)); statement.setLong(2, nameRecordId); statement.closeOnCompletion(); statement.executeUpdate(); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("updateNameRecord failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("updateNameRecord failed: " + e); } }
@Override public long saveSubscriptionInStorage(HashId hashId, boolean subscriptionOnChain, ZonedDateTime expiresAt, long environmentId) { try (PooledDb db = dbPool.db()) { try ( PreparedStatement statement = db.statement("INSERT INTO contract_subscription (hash_id, subscription_on_chain, expires_at, environment_id) VALUES(?,?,?,?) RETURNING id") ) { statement.setBytes(1, hashId.getDigest()); statement.setBoolean(2, subscriptionOnChain); statement.setLong(3, StateRecord.unixTime(expiresAt)); statement.setLong(4, environmentId); statement.closeOnCompletion(); ResultSet rs = statement.executeQuery(); if (rs == null) throw new Failure("saveSubscriptionInStorage failed: returning null"); rs.next(); long resId = rs.getLong(1); rs.close(); return resId; } } catch (SQLException se) { se.printStackTrace(); throw new Failure("saveSubscriptionInStorage failed: " + se); } catch (Exception e) { e.printStackTrace(); return 0; } }
public void clearExpiredNameRecords(Duration holdDuration) { try (PooledDb db = dbPool.db()) { ZonedDateTime before = ZonedDateTime.now().minus(holdDuration); try ( PreparedStatement statement = db.statement( "DELETE FROM name_storage WHERE expires_at < ? " ) ) { statement.setLong(1, StateRecord.unixTime(before)); statement.closeOnCompletion(); statement.executeUpdate(); } } catch (SQLException se) { se.printStackTrace(); throw new Failure("clearExpiredNameRecords failed: " + se); } catch (Exception e) { e.printStackTrace(); throw new Failure("clearExpiredNameRecords failed: " + e); } }
statement.setBytes(1, contractId.getDigest()); statement.setBytes(2, origin.getDigest()); statement.setLong(3, StateRecord.unixTime(expiresAt)); statement.setLong(4, environmentId); statement.closeOnCompletion();
statement.setString(3, nameRecord.getDescription()); statement.setString(4, nameRecord.getUrl()); statement.setLong(5, StateRecord.unixTime(nameRecord.expiresAt())); statement.setLong(6, nameRecord.getEnvironmentId()); statement.closeOnCompletion();
statement.setBytes(1, stateRecord.getId().getDigest()); statement.setInt(2, stateRecord.getState().ordinal()); statement.setLong(3, StateRecord.unixTime(stateRecord.getCreatedAt())); statement.setLong(4, StateRecord.unixTime(stateRecord.getExpiresAt())); statement.setLong(5, stateRecord.getLockedByRecordId()); statement.executeUpdate(); db.update("update ledger set state=?, expires_at=?, locked_by_id=? where id=?", stateRecord.getState().ordinal(), StateRecord.unixTime(stateRecord.getExpiresAt()), stateRecord.getLockedByRecordId(), stateRecord.getRecordId()
statement.setBytes(1, stateRecord.getId().getDigest()); statement.setInt(2, stateRecord.getState().ordinal()); statement.setLong(3, StateRecord.unixTime(stateRecord.getCreatedAt())); statement.setLong(4, StateRecord.unixTime(stateRecord.getExpiresAt())); statement.setLong(5, stateRecord.getLockedByRecordId()); db.updateWithStatement(statement); db.update("update ledger set state=?, expires_at=?, locked_by_id=? where id=?", stateRecord.getState().ordinal(), StateRecord.unixTime(stateRecord.getExpiresAt()), stateRecord.getLockedByRecordId(), stateRecord.getRecordId()
@Test public void checkPaymentContract() throws Exception { // to register manually, execute from deploy project: // bin/sql_all pro "insert into ledger(hash,state,created_at, expires_at, locked_by_id) values(decode('9186C0A9E9471E4559E74B5DAC3DBBB8445807DF80CAE4CE06FDB6588FAEBA1CE004AD378BEF3C445DECF3375E3CA5FD16227DBE5831A21207BB1BD21C85F30D0CED014E152F77E62082E0442FBD9FD2458C20778F7501B5D425AF9984062E54','hex'),'4','1520977039','1552513039','0');" // to erase all ledgers, execute: // bin/sql_all pro "truncate table ledger" // (after erasing ledgers, items still stay in cache -> need to restart (or redeploy) nodes) Contract contract = paymentContract; contract.check(); System.out.println("uno bin: " + Base64.encodeString(contract.getPackedTransaction())); System.out.println("uno hashId: " + Bytes.toHex(contract.getId().getDigest()).replace(" ", "")); System.out.println("approved ord: " + ItemState.APPROVED.ordinal()); System.out.println("getCreatedAt: " + StateRecord.unixTime(contract.getCreatedAt())); System.out.println("getExpiresAt: " + StateRecord.unixTime(contract.getExpiresAt())); ItemResult itemResult = normalClient.getState(contract.getId()); System.out.println("getState... done! itemResult: " + itemResult.state); }