Skip to content

Commit

Permalink
feat: CoreReplicationState (#288)
Browse files Browse the repository at this point in the history
* feat: CoreReplicationState

Add a new CoreReplicationState implementation, see
#287

* cleanup and simplify

* derive state

* Add missing and connected & cleanup

* Move exported class to top

* cache derived state

* Add code comments

* add benchmarks

* entries -> for index, 4,150ms -> 2,500ms

Use `for(let j = 0; j < peers.length ...` instead of `peers.entries()`

* re-used haves/wants arrays, 2500ms -> 2300ms

* Remove wants array, 2300ms -> 2180ms

* deriveState unit tests

* bitwise operations, 1500ms -> ~90ms

* increase benchmark iterations for better accuracy

* Update src/core-manager/core-replication-state.js

Co-authored-by: Andrew Chou <andrewchou@fastmail.com>

* fixes

* wip tests

* fix prehave test

* fix failing test

* bitcount test

* add some additional code comments

---------

Co-authored-by: Andrew Chou <andrewchou@fastmail.com>
  • Loading branch information
gmaclennan and achou11 committed Oct 3, 2023
1 parent ead493a commit 7b96e0c
Show file tree
Hide file tree
Showing 6 changed files with 919 additions and 2 deletions.
46 changes: 46 additions & 0 deletions benchmarks/core-replication-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import bench from 'nanobench'
import {
PeerState,
deriveState,
} from '../src/core-manager/core-replication-state.js'
import RemoteBitfield from '../src/core-manager/remote-bitfield.js'
import createRandom from 'math-random-seed'

bench('deriveState x 10,000; 10 peers; 10,000 blocks', function (b) {
const length = 10000
const remotePeers = new Map()
for (let i = 0; i < 10; i++) {
remotePeers.set(i, createPeer(length, i + ''))
}
const localPeer = createPeer(length, 'local')

const state = {
length,
localState: localPeer,
remoteStates: remotePeers,
}

b.start()
for (let i = 0; i < 10000; i++) {
deriveState(state)
}
b.end()
})

function createPeer(length, seed) {
const peer = new PeerState()
peer.setHavesBitfield(createBitfield(length, seed))
return peer
}

function createBitfield(length, seed) {
const random = createRandom(seed)
const b = new RemoteBitfield()
let i = random() < 0.5 ? 0 : Math.ceil(random() * 200)
while (i < length) {
const length = Math.ceil(random() * 200)
b.setRange(i, length, true)
i += length + Math.ceil(random() * 200)
}
return b
}
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"eslint": "^8.39.0",
"fastify": "^4.20.0",
"light-my-request": "^5.10.0",
"math-random-seed": "^2.0.0",
"nanobench": "^3.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
Expand Down
Loading

0 comments on commit 7b96e0c

Please sign in to comment.