Skip to content

Commit 06c80ca

Browse files
authored
fix: use dht selectors and validators from interfaces (#74)
1 parent 69dbd29 commit 06c80ca

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"it-pair": "^1.0.0",
5353
"libp2p": "^0.30.9",
5454
"libp2p-gossipsub": "^0.8.0",
55-
"libp2p-interfaces": "^0.8.3",
55+
"libp2p-interfaces": "^0.10.3",
5656
"libp2p-record": "^0.10.0",
5757
"p-wait-for": "^3.1.0",
5858
"peer-id": "^0.14.2",

src/index.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const log = Object.assign(debug('datastore-pubsub:publisher'), {
1414
* @typedef {import('peer-id')} PeerId
1515
* @typedef {import('./types').Validator} Validator
1616
* @typedef {import('./types').SubscriptionKeyFn} SubscriptionKeyFn
17-
* @typedef {import('libp2p-interfaces/src/pubsub/message').Message} PubSubMessage
17+
* @typedef {import('libp2p-interfaces/src/pubsub').InMessage} PubSubMessage
1818
*/
1919

2020
// DatastorePubsub is responsible for providing an api for pubsub to be used as a datastore with
@@ -253,11 +253,11 @@ class DatastorePubsub extends Adapter {
253253
/**
254254
* Select the best record according to the received select function
255255
*
256-
* @param {Uint8Array} receivedRecord
257-
* @param {Uint8Array} currentRecord
256+
* @param {Uint8Array} key
257+
* @param {Uint8Array[]} records
258258
*/
259-
async _selectRecord (receivedRecord, currentRecord) {
260-
const res = await this._validator.select(receivedRecord, currentRecord)
259+
async _selectRecord (key, records) {
260+
const res = await this._validator.select(key, records)
261261

262262
// If the selected was the first (0), it should be stored (true)
263263
return res === 0
@@ -270,17 +270,10 @@ class DatastorePubsub extends Adapter {
270270
* @param {Uint8Array} val
271271
*/
272272
async _isBetter (key, val) {
273-
// validate received record
274-
let error, valid
275-
276273
try {
277-
valid = await this._validateRecord(val, key)
274+
await this._validateRecord(val, key)
278275
} catch (err) {
279-
error = err
280-
}
281-
282-
// If not valid, it is not better than the one currently available
283-
if (error || !valid) {
276+
// If not valid, it is not better than the one currently available
284277
const errMsg = 'record received through pubsub is not valid'
285278

286279
log.error(errMsg)
@@ -304,7 +297,7 @@ class DatastorePubsub extends Adapter {
304297
}
305298

306299
// verify if the received record should replace the current one
307-
return this._selectRecord(val, currentRecord)
300+
return this._selectRecord(key, [currentRecord, val])
308301
}
309302

310303
/**

src/types.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1+
import { ValidateFn, SelectFn } from 'libp2p-interfaces/src/types'
12

2-
type ValidateFn = (record: Uint8Array, peerId: Uint8Array) => Promise<boolean> | boolean
3-
type CompareFn = (received: Uint8Array, current: Uint8Array) => number
43
export type SubscriptionKeyFn = (key: Uint8Array) => Promise<Uint8Array> | Uint8Array
5-
64
export interface Validator {
75
validate: ValidateFn,
8-
select: CompareFn
6+
select: SelectFn
97
}

test/index.spec.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const { keyToTopic, topicToKey } = require('../src/utils')
3535
// Valid record and select the new one
3636
const smoothValidator = {
3737
validate: () => {
38-
return true
38+
return Promise.resolve()
3939
},
4040
select: () => {
4141
return 0
@@ -160,7 +160,10 @@ describe('datastore-pubsub', function () {
160160

161161
expect(receivedRecord.value.toString()).to.equal(value) // validator should deserialize correctly
162162

163-
return receivedRecord.value.toString() === value
163+
if (receivedRecord.value.toString() === value) {
164+
return Promise.resolve()
165+
}
166+
return Promise.reject(new Error('invalid record'))
164167
},
165168
select: () => {
166169
return 0
@@ -287,7 +290,7 @@ describe('datastore-pubsub', function () {
287290
it('should fail if it fails getTopics to validate the record', async () => {
288291
const customValidator = {
289292
validate: () => {
290-
return false // return false validation
293+
throw new Error()
291294
},
292295
select: () => {
293296
return 0
@@ -332,7 +335,7 @@ describe('datastore-pubsub', function () {
332335
it('should get the second record if the selector selects it as the newest one', async () => {
333336
const customValidator = {
334337
validate: () => {
335-
return true
338+
return Promise.resolve()
336339
},
337340
select: () => {
338341
return 1 // current record is the newer
@@ -383,7 +386,7 @@ describe('datastore-pubsub', function () {
383386
it('should get the new record if the selector selects it as the newest one', async () => {
384387
const customValidator = {
385388
validate: () => {
386-
return true
389+
return Promise.resolve()
387390
},
388391
select: () => {
389392
return 0 // received record is the newer

0 commit comments

Comments
 (0)