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

version each core and reserve flags #7

Open
wants to merge 1 commit into
base: main
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
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const m = require('./lib/messages')

const INF = Buffer.from([0xff])

const STORAGE_INFO = Buffer.from([0x00])
const DKEYS = Buffer.from([0x01])
const TOP_LEVEL_STORAGE_INFO = Buffer.from([0x00])
const TOP_LEVEL_CORE_INFO = Buffer.from([0x01])

const CORE_META = 0
const CORE_TREE = 1
Expand Down Expand Up @@ -150,8 +150,8 @@ module.exports = class CoreStorage {

list () {
const s = this.db.iterator({
gt: DKEYS,
lt: Buffer.from([DKEYS[0] + 1])
gt: TOP_LEVEL_CORE_INFO,
lt: Buffer.from([TOP_LEVEL_CORE_INFO[0] + 1])
})

s._readableState.map = mapOnlyDiscoveryKey
Expand All @@ -168,7 +168,7 @@ module.exports = class CoreStorage {

async clear () {
const b = this.db.write()
b.tryDeleteRange(STORAGE_INFO, INF)
b.tryDeleteRange(TOP_LEVEL_STORAGE_INFO, INF)
await b.flush()
}

Expand All @@ -187,9 +187,9 @@ class HypercoreStorage {
}

async open () {
const val = await this.db.get(Buffer.concat([DKEYS, this.discoveryKey]))
const val = await this.db.get(Buffer.concat([TOP_LEVEL_CORE_INFO, this.discoveryKey]))
if (val === null) return false
this._onopen(c.decode(m.DiscoveryKey, val))
this._onopen(c.decode(m.CoreInfo, val))
return true
}

Expand All @@ -204,8 +204,8 @@ class HypercoreStorage {

info.total++

write.tryPut(Buffer.concat([DKEYS, this.discoveryKey]), c.encode(m.DiscoveryKey, { auth, data }))
write.tryPut(STORAGE_INFO, c.encode(m.StorageInfo, info))
write.tryPut(Buffer.concat([TOP_LEVEL_CORE_INFO, this.discoveryKey]), c.encode(m.CoreInfo, { auth, data }))
write.tryPut(TOP_LEVEL_STORAGE_INFO, c.encode(m.StorageInfo, info))

await write.flush()

Expand Down Expand Up @@ -276,7 +276,7 @@ function mapOnlyDiscoveryKey (data) {
}

async function getStorageInfo (db) {
const value = await db.get(STORAGE_INFO)
const value = await db.get(TOP_LEVEL_STORAGE_INFO)
if (value === null) return null
return c.decode(m.StorageInfo, value)
}
Expand Down
16 changes: 13 additions & 3 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ exports.StorageInfo = {
c.uint.encode(state, m.total)
},
decode (state, m) {
const v = c.uint.decode(state)
if (v !== 0) throw new Error('Invalid version: ' + v)
const version = c.uint.decode(state)
if (version !== 0) throw new Error('Unsupported storage version: ' + version)

c.uint.decode(state) // flags, ignore

Expand All @@ -43,17 +43,27 @@ exports.StorageInfo = {
}
}

exports.DiscoveryKey = {
exports.CoreInfo = {
preencode (state, m) {
c.uint.preencode(state, 0) // core version
c.uint.preencode(state, 0) // flags, reserved
c.uint.preencode(state, m.auth)
c.uint.preencode(state, m.data)
},
encode (state, m) {
c.uint.encode(state, 0) // core version
c.uint.encode(state, 0) // flags, reserved
c.uint.encode(state, m.auth)
c.uint.encode(state, m.data)
},
decode (state) {
const version = c.uint.decode(state)
if (version !== 0) throw new Error('Unsupported core version: ' + version)

c.uint.decode(state, 0) // flags, ignore

return {
version,
auth: c.uint.decode(state),
data: c.uint.decode(state)
}
Expand Down