Skip to content

Commit

Permalink
fix: do not republish self key twice (ipfs#3634)
Browse files Browse the repository at this point in the history
* fix: do not republish self key twice

* chore: decrease brodcast initial delay

* chore: decrease other test timers
  • Loading branch information
vasco-santos authored Apr 28, 2021
1 parent 4a14d20 commit 8545a76
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/ipfs-core/src/ipns/republisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class IpnsRepublisher {
const keys = await this._keychain.listKeys()

for (const key of keys) {
if (key.name === 'self') {
continue
}
const pem = await this._keychain.exportKey(key.name, pass)
const privKey = await crypto.keys.import(pem, pass)

Expand Down
32 changes: 26 additions & 6 deletions packages/ipfs-core/test/name.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,44 @@ describe('name', function () {

it('should republish entries', async function () {
republisher = new IpnsRepublisher(sinon.stub(), sinon.stub(), sinon.stub(), sinon.stub(), {
initialBroadcastInterval: 500,
broadcastInterval: 1000
initialBroadcastInterval: 200,
broadcastInterval: 500
})
republisher._republishEntries = sinon.stub()

await republisher.start()

expect(republisher._republishEntries.calledOnce).to.equal(false)

// Initial republish should happen after ~500ms
await delay(750)
// Initial republish should happen after ~200ms
await delay(300)
expect(republisher._republishEntries.calledOnce).to.equal(true)

// Subsequent republishes should happen after ~1500ms
await delay(1000)
// Subsequent republishes should happen after ~700
await delay(600)
expect(republisher._republishEntries.calledTwice).to.equal(true)
})

it('should not republish self key twice', async function () {
const mockKeychain = {
listKeys: () => Promise.resolve([{ name: 'self' }])
}
republisher = new IpnsRepublisher(sinon.stub(), sinon.stub(), sinon.stub(), mockKeychain, {
initialBroadcastInterval: 100,
broadcastInterval: 1000,
pass: 'pass'
})
republisher._republishEntry = sinon.stub()

await republisher.start()

expect(republisher._republishEntry.calledOnce).to.equal(false)

// Initial republish should happen after ~100ms
await delay(200)
expect(republisher._republishEntry.calledOnce).to.equal(true)
})

it('should error if run republish again', async () => {
republisher = new IpnsRepublisher(sinon.stub(), sinon.stub(), sinon.stub(), sinon.stub(), {
initialBroadcastInterval: 50,
Expand Down

0 comments on commit 8545a76

Please sign in to comment.