Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn-cacao-near-exp #3164

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/common/src/utils/stream-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cloneDeep from 'lodash.clonedeep'
import * as uint8arrays from 'uint8arrays'
import { toCID } from './cid-utils.js'
import type { DiagnosticsLogger } from '@ceramicnetwork/common'
import * as dagCBOR from '@ipld/dag-cbor'
import {
AnchorCommit,
Expand Down Expand Up @@ -354,7 +355,7 @@ export class StreamUtils {
/**
* Takes a StreamState and validates that none of the commits in its log are based on expired CACAOs.
*/
static checkForCacaoExpiration(state: StreamState): void {
static checkForCacaoExpiration(state: StreamState, logger: DiagnosticsLogger): void {
const now = Math.floor(Date.now() / 1000) // convert millis to seconds
for (const logEntry of state.log) {
const timestamp = logEntry.timestamp ?? now
Expand All @@ -366,6 +367,12 @@ export class StreamUtils {
logEntry.expirationTime
}. Loading the stream with 'sync: SyncOptions.ALWAYS_SYNC' will restore the stream to a usable state, by discarding the invalid commits (this means losing the data from those invalid writes!)`
)
} else if (logEntry.expirationTime && logEntry.expirationTime - timestamp < 12 * 60 * 60) {
logger.warn(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since timestamp can come from the anchor commit time OR from "now" if the stream isn't actually anchored yet, this log message is a bit misleading, since the message implies the stream is already anchored in all cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anchorTime=${logEntry.timestamp} will be null if there is no anchor but I see what you mean about the wording.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking about this more, I'm actually not sure how useful this log message is, and I'm afraid it might be quite noisy. This is because there's nothing that prevents us from doing a write with a CACAO right before it's about to expire. A standard CACAO session is valid for a week by default, but if the user logs in once, then logs in again 6 days, 23 hours, and 59 minutes later, the app will still let them re-use their old session without signing in again. So the mere fact that the CACAO is close to expiring doesn't actually tell us that the stream took longer than expected to be anchored.

I think this log message really needs to be based on the timestamp of when the write was performed, which we only have in the ComposeDB index database.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we have the 24 hour grace period logging the fact that it is expired tells us basicly the same thing but it is more clear that a client is not refreshing it CACAO sessions aggressively enugh.

`CACAO anchored with less then 12 hours left. ${logEntry.cid.toString()} of ${StreamUtils.streamIdFromState(
state
).toString()}: anchorTime=${logEntry.timestamp} expirationTime=${logEntry.expirationTime}`
)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/state-management/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export class Repository {
})

if (checkCacaoExpiration) {
StreamUtils.checkForCacaoExpiration(state$.state)
StreamUtils.checkForCacaoExpiration(state$.state, this.logger)
}

if (syncStatus != SyncStatus.ALREADY_SYNCED) {
Expand Down Expand Up @@ -485,7 +485,7 @@ export class Repository {
const stateAtCommit = await this.streamLoader.stateAtCommit(existingState$.state, commitId)

// Since we skipped CACAO expiration checking earlier we need to make sure to do it here.
StreamUtils.checkForCacaoExpiration(stateAtCommit)
StreamUtils.checkForCacaoExpiration(stateAtCommit, this.logger)

// If the provided CommitID is ahead of what we have in the cache and state store, then we
// should update them to include it.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/stream-loading/state-manipulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export class StateManipulator {
// The initial state may have included commits that were valid previously but have since had
// their CACAOs expire. Before returning the state back to the caller we should double-check
// that it is based all on valid commits without expired CACAOs.
StreamUtils.checkForCacaoExpiration(state)
StreamUtils.checkForCacaoExpiration(state, this.logger)

return state
}
Expand Down