Skip to content

chore: remove node buffers #174

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

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
15 changes: 2 additions & 13 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* set up a libp2p instance for browser nodes to relay through
* before tests start
*/
const path = require('path')

const Libp2p = require('libp2p')
const PeerId = require('peer-id')
Expand Down Expand Up @@ -45,28 +44,18 @@ const before = async () => {
}
}
})

await libp2p.start()
}

const after = async () => {
await libp2p.stop()
}

/** @type {import('aegir').Options["build"]["config"]} */
const esbuild = {
inject: [path.join(__dirname, './scripts/node-globals.js')]
}

/** @type {import('aegir').PartialOptions} */
module.exports = {
test: {
before,
after,
browser: {
config: {
buildConfig: esbuild
}
}
after
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@typescript-eslint/parser": "^3.0.2",
"aegir": "^36.0.2",
"benchmark": "^2.1.4",
"buffer": "^6.0.3",
"chai": "^4.2.0",
"chai-spies": "^1.0.0",
"delay": "^5.0.0",
Expand Down
1 change: 0 additions & 1 deletion scripts/node-globals.js

This file was deleted.

53 changes: 28 additions & 25 deletions test/go-gossipsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const errcode = require('err-code')
const sinon = require('sinon')
const pRetry = require('p-retry')
const { EventEmitter } = require('events')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { equals: uint8ArrayEquals } = require('uint8arrays/equals')

const Floodsub = require('libp2p-floodsub')
const Gossipsub = require('../src')
Expand Down Expand Up @@ -41,7 +43,7 @@ const checkReceivedMessage = (topic, data, senderIx, msgIx) =>
reject(new Error(`Message never received, sender ${senderIx}, receiver ${receiverIx}, index ${msgIx}`))
}, 20000)
cb = (msg) => {
if (data.equals(msg.data)) {
if (uint8ArrayEquals(data, msg.data)) {
clearTimeout(t)
psub.off(topic, cb)
resolve()
Expand Down Expand Up @@ -95,7 +97,8 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)

const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -128,7 +131,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -164,7 +167,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)

const owner = 0

Expand All @@ -186,7 +189,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

sendRecv = []
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)

const owner = 0

Expand Down Expand Up @@ -227,7 +230,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
let sendRecv = []
const sendMessages = () => {
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)

const owner = 0

Expand Down Expand Up @@ -286,7 +289,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 5; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)

const owner = 0

Expand Down Expand Up @@ -328,7 +331,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
await Promise.all(psubs.map(ps => awaitEvents(ps, 'gossipsub:heartbeat', 2)))

for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -372,7 +375,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 10; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = 0
const results = Promise.all(
group1.slice(1)
Expand Down Expand Up @@ -435,7 +438,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -473,7 +476,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -514,7 +517,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = Math.floor(Math.random() * (psubs.length - 5))
const results = Promise.all(
psubs.slice(5)
Expand Down Expand Up @@ -549,7 +552,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
await Promise.all(psubs.map(ps => awaitEvents(ps, 'gossipsub:heartbeat', 9)))

for (let i = 0; i < 35; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -589,7 +592,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

// create a background flood of messages that overloads the queues
const floodOwner = Math.floor(Math.random() * psubs.length)
const floodMsg = Buffer.from('background flooooood')
const floodMsg = uint8ArrayFromString('background flooooood')
const backgroundFlood = new Promise(async resolve => {
for (let i = 0; i < 10000; i++) {
await psubs[floodOwner].publish(floodTopic, floodMsg)
Expand All @@ -612,7 +615,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
// and test that we have functional overlays
let sendRecv = []
for (let i = 0; i < 5; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -657,7 +660,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 100; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -691,7 +694,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
// wait for heartbeats to build mesh
await Promise.all(psubs.map(ps => awaitEvents(ps, 'gossipsub:heartbeat', 2)))

const msg = Buffer.from(`${0} its not a flooooood ${0}`)
const msg = uint8ArrayFromString(`${0} its not a flooooood ${0}`)
const owner = 0
const results = checkReceivedMessage(topic, msg, owner, 0)(psubs[5], 5)
await psubs[owner].publish(topic, msg)
Expand Down Expand Up @@ -742,7 +745,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (const owner of [9, 3]) {
const msg = Buffer.from(`${owner} its not a flooooood ${owner}`)
const msg = uint8ArrayFromString(`${owner} its not a flooooood ${owner}`)
const results = Promise.all(
psubs
.filter((psub, j) => j !== owner)
Expand Down Expand Up @@ -798,7 +801,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
// send a message from each peer and assert it was propagated
let sendRecv = []
for (let i = 0; i < psubs.length; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = i
const results = Promise.all(
psubs
Expand Down Expand Up @@ -867,7 +870,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 3; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = i
const results = Promise.all(
psubs
Expand All @@ -888,7 +891,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

sendRecv = []
for (let i = 0; i < 3; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = i
const results = Promise.all(
psubs
Expand Down Expand Up @@ -925,7 +928,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
// send messages from the star and assert they were received
let sendRecv = []
for (let i = 0; i < 20; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = 0
const results = Promise.all(
psubs
Expand Down Expand Up @@ -978,7 +981,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

let sendRecv = []
for (let i = 0; i < 20; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = i
sendRecv.push(psubs[owner].publish(topic, msg))
}
Expand Down Expand Up @@ -1030,7 +1033,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

psubs[0].on(topic, () => expect.fail('node 0 should not receive any messages'))

const msg = Buffer.from('its not a flooooood')
const msg = uint8ArrayFromString('its not a flooooood')
await psubs[1].publish(topic, msg)
await psubs[2].publish(topic, msg)

Expand Down Expand Up @@ -1139,7 +1142,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
psubs.forEach(ps => ps.subscribe(topic))

for (let i = 0; i < 300; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = i % 10
await psubs[owner].publish(topic, msg)
await delay(20)
Expand Down
6 changes: 3 additions & 3 deletions test/gossip.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'
/* eslint-env mocha */

const { Buffer } = require('buffer')
const { expect } = require('chai')
const sinon = require('sinon')
const delay = require('delay')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')

const { GossipsubDhi } = require('../src/constants')
const {
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('gossip', () => {
// set spy
sinon.spy(nodeA, '_pushGossip')

await nodeA.publish(topic, Buffer.from('hey'))
await nodeA.publish(topic, uint8ArrayFromString('hey'))

await new Promise((resolve) => nodeA.once('gossipsub:heartbeat', resolve))

Expand Down Expand Up @@ -84,7 +84,7 @@ describe('gossip', () => {
const graft = { graft: [{ topicID: topic }] }
nodeA.control.set(peerB, graft)

await nodeA.publish(topic, Buffer.from('hey'))
await nodeA.publish(topic, uint8ArrayFromString('hey'))

expect(nodeA._piggybackControl.callCount).to.be.equal(1)
// expect control message to be sent alongside published message
Expand Down
6 changes: 3 additions & 3 deletions test/tracer.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { expect } = require('chai')
const delay = require('delay')
const { utils } = require('libp2p-interfaces/src/pubsub')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')

const { IWantTracer } = require('../src/tracer')
const constants = require('../src/constants')
Expand All @@ -17,7 +17,7 @@ describe('IWantTracer', () => {
const msgIds = []
for (let i = 0; i < 100; i++) {
const m = makeTestMessage(i)
m.from = Buffer.from(peerA)
m.from = uint8ArrayFromString(peerA)
msgIds.push(getMsgId(m))
}

Expand Down Expand Up @@ -47,7 +47,7 @@ describe('IWantTracer', () => {
const msgIds = []
for (let i = 0; i < 100; i++) {
const m = makeTestMessage(i)
m.from = Buffer.from(peerA)
m.from = uint8ArrayFromString(peerA)
msgs.push(m)
msgIds.push(getMsgId(m))
}
Expand Down
5 changes: 3 additions & 2 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { expect } = require('chai')
const FloodSub = require('libp2p-floodsub')
const PeerId = require('peer-id')
const delay = require('delay')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')

exports.first = (map) => map.values().next().value

Expand Down Expand Up @@ -46,8 +47,8 @@ for (const [k, v] of Object.entries({
}

exports.getMsgId = (msg) => {
const from = Buffer.from(msg.from)
const seqno = Buffer.from(msg.seqno)
const from = uint8ArrayFromString(msg.from)
const seqno = uint8ArrayFromString(msg.seqno)
const result = new Uint8Array(from.length + seqno.length)
result.set(from, 0)
result.set(seqno, from.length)
Expand Down
4 changes: 2 additions & 2 deletions test/utils/make-test-message.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const makeTestMessage = (i, topicIDs = []) => {
return {
seqno: Buffer.alloc(8, i),
data: Buffer.from([i]),
seqno: Uint8Array.from(new Array(8).fill(i)),
data: Uint8Array.from([i]),
from: "test",
topicIDs
}
Expand Down