|
| 1 | +const {expect} = require('chai') |
| 2 | +const sinon = require('sinon') |
| 3 | +const {PeerScore} = require('../src/score') |
| 4 | +const Gossipsub = require('../src') |
| 5 | +const { |
| 6 | + createPeer, |
| 7 | +} = require('./utils') |
| 8 | + |
| 9 | +describe('Gossipsub acceptFrom', () => { |
| 10 | + let gossipsub |
| 11 | + const sandbox = sinon.createSandbox() |
| 12 | + let scoreStub |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + sandbox.useFakeTimers() |
| 16 | + gossipsub = new Gossipsub(await createPeer({ started: false }), { emitSelf: true }) |
| 17 | + scoreStub = sandbox.createStubInstance(PeerScore) |
| 18 | + gossipsub.score = scoreStub |
| 19 | + }) |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + sandbox.restore() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should only white list peer with positive score', () => { |
| 26 | + scoreStub.score.withArgs("peerA").returns(1000) |
| 27 | + gossipsub._acceptFrom("peerA") |
| 28 | + // 1st time, we have to compute score |
| 29 | + expect(scoreStub.score.withArgs("peerA").calledOnce).to.be.true |
| 30 | + // 2nd time, use a cached score since it's white listed |
| 31 | + gossipsub._acceptFrom("peerA") |
| 32 | + expect(scoreStub.score.withArgs("peerA").calledOnce).to.be.true |
| 33 | + }) |
| 34 | + |
| 35 | + it('should recompute score after 1s', () => { |
| 36 | + scoreStub.score.returns(1000) |
| 37 | + gossipsub._acceptFrom("peerA") |
| 38 | + // 1st time, we have to compute score |
| 39 | + expect(scoreStub.score.withArgs("peerA").calledOnce).to.be.true |
| 40 | + gossipsub._acceptFrom("peerA") |
| 41 | + expect(scoreStub.score.withArgs("peerA").calledOnce).to.be.true |
| 42 | + |
| 43 | + // after 1s |
| 44 | + sandbox.clock.tick(1001) |
| 45 | + |
| 46 | + gossipsub._acceptFrom("peerA") |
| 47 | + expect(scoreStub.score.withArgs("peerA").calledTwice).to.be.true |
| 48 | + }) |
| 49 | + |
| 50 | + it('should recompute score after max messages accepted', () => { |
| 51 | + scoreStub.score.returns(1000) |
| 52 | + gossipsub._acceptFrom("peerA") |
| 53 | + // 1st time, we have to compute score |
| 54 | + expect(scoreStub.score.withArgs("peerA").calledOnce).to.be.true |
| 55 | + |
| 56 | + for (let i = 0; i < 128; i++) { |
| 57 | + gossipsub._acceptFrom("peerA") |
| 58 | + } |
| 59 | + expect(scoreStub.score.withArgs("peerA").calledOnce).to.be.true |
| 60 | + |
| 61 | + // max messages reached |
| 62 | + gossipsub._acceptFrom("peerA") |
| 63 | + expect(scoreStub.score.withArgs("peerA").calledTwice).to.be.true |
| 64 | + }) |
| 65 | + |
| 66 | + it('should NOT white list peer with negative score', () => { |
| 67 | + // peerB is not white listed since score is negative |
| 68 | + scoreStub.score.withArgs("peerB").returns(-1) |
| 69 | + gossipsub._acceptFrom("peerB") |
| 70 | + // 1st time, we have to compute score |
| 71 | + expect(scoreStub.score.withArgs("peerB").calledOnce).to.be.true |
| 72 | + // 2nd time, still have to compute score since it's NOT white listed |
| 73 | + gossipsub._acceptFrom("peerB") |
| 74 | + expect(scoreStub.score.withArgs("peerB").calledTwice).to.be.true |
| 75 | + }) |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +}) |
0 commit comments