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

fix: use dht selectors and validators from interfaces #74

Merged
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"it-pair": "^1.0.0",
"libp2p": "^0.30.9",
"libp2p-gossipsub": "^0.8.0",
"libp2p-interfaces": "^0.8.3",
"libp2p-interfaces": "^0.10.3",
"libp2p-record": "^0.10.0",
"p-wait-for": "^3.1.0",
"peer-id": "^0.14.2",
Expand Down
23 changes: 8 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const log = Object.assign(debug('datastore-pubsub:publisher'), {
* @typedef {import('peer-id')} PeerId
* @typedef {import('./types').Validator} Validator
* @typedef {import('./types').SubscriptionKeyFn} SubscriptionKeyFn
* @typedef {import('libp2p-interfaces/src/pubsub/message').Message} PubSubMessage
* @typedef {import('libp2p-interfaces/src/pubsub').InMessage} PubSubMessage
*/

// DatastorePubsub is responsible for providing an api for pubsub to be used as a datastore with
Expand Down Expand Up @@ -253,11 +253,11 @@ class DatastorePubsub extends Adapter {
/**
* Select the best record according to the received select function
*
* @param {Uint8Array} receivedRecord
* @param {Uint8Array} currentRecord
* @param {Uint8Array} key
* @param {Uint8Array[]} records
*/
async _selectRecord (receivedRecord, currentRecord) {
const res = await this._validator.select(receivedRecord, currentRecord)
async _selectRecord (key, records) {
const res = await this._validator.select(key, records)
Copy link
Member Author

Choose a reason for hiding this comment

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


// If the selected was the first (0), it should be stored (true)
return res === 0
Expand All @@ -270,17 +270,10 @@ class DatastorePubsub extends Adapter {
* @param {Uint8Array} val
*/
async _isBetter (key, val) {
// validate received record
let error, valid

try {
valid = await this._validateRecord(val, key)
await this._validateRecord(val, key)
} catch (err) {
error = err
}

// If not valid, it is not better than the one currently available
if (error || !valid) {
// If not valid, it is not better than the one currently available
const errMsg = 'record received through pubsub is not valid'

log.error(errMsg)
Expand All @@ -304,7 +297,7 @@ class DatastorePubsub extends Adapter {
}

// verify if the received record should replace the current one
return this._selectRecord(val, currentRecord)
return this._selectRecord(key, [currentRecord, val])
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ValidateFn, SelectFn } from 'libp2p-interfaces/src/types'

type ValidateFn = (record: Uint8Array, peerId: Uint8Array) => Promise<boolean> | boolean
type CompareFn = (received: Uint8Array, current: Uint8Array) => number
export type SubscriptionKeyFn = (key: Uint8Array) => Promise<Uint8Array> | Uint8Array

export interface Validator {
validate: ValidateFn,
select: CompareFn
select: SelectFn
}
13 changes: 8 additions & 5 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const { keyToTopic, topicToKey } = require('../src/utils')
// Valid record and select the new one
const smoothValidator = {
validate: () => {
return true
return Promise.resolve()
},
select: () => {
return 0
Expand Down Expand Up @@ -160,7 +160,10 @@ describe('datastore-pubsub', function () {

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

return receivedRecord.value.toString() === value
if (receivedRecord.value.toString() === value) {
return Promise.resolve()
}
return Promise.reject(new Error('invalid record'))
},
select: () => {
return 0
Expand Down Expand Up @@ -287,7 +290,7 @@ describe('datastore-pubsub', function () {
it('should fail if it fails getTopics to validate the record', async () => {
const customValidator = {
validate: () => {
return false // return false validation
throw new Error()
},
select: () => {
return 0
Expand Down Expand Up @@ -332,7 +335,7 @@ describe('datastore-pubsub', function () {
it('should get the second record if the selector selects it as the newest one', async () => {
const customValidator = {
validate: () => {
return true
return Promise.resolve()
},
select: () => {
return 1 // current record is the newer
Expand Down Expand Up @@ -383,7 +386,7 @@ describe('datastore-pubsub', function () {
it('should get the new record if the selector selects it as the newest one', async () => {
const customValidator = {
validate: () => {
return true
return Promise.resolve()
},
select: () => {
return 0 // received record is the newer
Expand Down