chores: log epoch

This commit is contained in:
fryorcraken.eth 2022-10-10 07:25:38 -05:00
parent ebf147969d
commit bb93a34e30
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -1,21 +1,30 @@
import debug from "debug";
const DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds
const log = debug("waku:rln:epoch");
export function dateToEpoch(
timestamp: Date,
epochUnitSeconds: number = DefaultEpochUnitSeconds
): number {
const time = timestamp.getTime();
return Math.floor(time / 1000 / epochUnitSeconds);
const epoch = Math.floor(time / 1000 / epochUnitSeconds);
log("generated epoch", epoch);
return epoch;
}
export function epochIntToBytes(epoch: number): Uint8Array {
const bytes = new Uint8Array(32);
const db = new DataView(bytes.buffer);
db.setUint32(0, epoch, true);
log("encoded epoch", epoch, bytes);
return bytes;
}
export function epochBytesToInt(bytes: Uint8Array): number {
const dv = new DataView(bytes.buffer);
return dv.getUint32(0, true);
const epoch = dv.getUint32(0, true);
log("decoded epoch", epoch, bytes);
return epoch;
}