-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add trace-offer #795
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
974f988
004a97f
2e5054b
cb97fa5
b587fa1
721f294
49bb49c
a9f8eba
0ce39a0
522eb43
fbc3a1d
6ba3931
2476bd0
29f2ba0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,36 @@ import type { Debugger } from 'debug' | |
| import type { BeaconNetwork, HistoryNetwork, PortalNetwork, StateNetwork } from 'portalnetwork' | ||
| import type { GetEnrResult } from '../schema/types.js' | ||
|
|
||
| /** | ||
| * Converts a BitArray response to a boolean array indicating which content keys were accepted | ||
| * @param res The response from sendOffer (can be BitArray, undefined, or empty array) | ||
| * @param contentKeysLength The number of content keys that were offered | ||
| * @returns Object with success array, declined flag, or failed flag | ||
| */ | ||
| function bitToBooleanArray(res: any, contentKeysLength: number): { success?: boolean[]; declined?: boolean; failed?: boolean } { | ||
foufrix marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (res === undefined) { | ||
| return { declined: true } | ||
| } | ||
|
|
||
| if (Array.isArray(res) && res.length === 0) { | ||
| return { declined: true } | ||
| } | ||
|
|
||
| // If res is a BitArray, convert it to boolean array | ||
| if (res !== undefined && res !== null && typeof res === 'object' && 'getTrueBitIndexes' in res) { | ||
| const acceptedBits = (res as any).getTrueBitIndexes() | ||
| const successArray = new Array(contentKeysLength).fill(false) | ||
| acceptedBits.forEach((index: number) => { | ||
| if (index < contentKeysLength) { | ||
| successArray[index] = true | ||
| } | ||
| }) | ||
| return { success: successArray } | ||
| } | ||
|
|
||
| return { success: new Array(contentKeysLength).fill(true) } | ||
| } | ||
|
|
||
| const methods = [ | ||
| // state | ||
| 'portal_stateAddEnr', | ||
|
|
@@ -46,6 +76,7 @@ const methods = [ | |
| 'portal_stateGetContent', | ||
| 'portal_stateTraceGetContent', | ||
| 'portal_stateOffer', | ||
| 'portal_stateTraceOffer', | ||
| // history | ||
| 'portal_historyRoutingTableInfo', | ||
| 'portal_historyAddEnr', | ||
|
|
@@ -56,6 +87,7 @@ const methods = [ | |
| 'portal_historyFindNodes', | ||
| 'portal_historyFindContent', | ||
| 'portal_historyOffer', | ||
| 'portal_historyTraceOffer', | ||
| 'portal_historyRecursiveFindNodes', | ||
| 'portal_historyGetContent', | ||
| 'portal_historyTraceGetContent', | ||
|
|
@@ -79,6 +111,7 @@ const methods = [ | |
| 'portal_beaconDeleteEnr', | ||
| 'portal_beaconLookupEnr', | ||
| 'portal_beaconOffer', | ||
| 'portal_beaconTraceOffer', | ||
| 'portal_beaconOptimisticStateRoot', | ||
| 'portal_beaconFinalizedStateRoot', | ||
|
|
||
|
|
@@ -275,6 +308,20 @@ export class portal { | |
| [content_params.ContentItems], | ||
| ]) | ||
|
|
||
| // portal_*TraceOffer | ||
| this.historyTraceOffer = middleware(this.historyTraceOffer.bind(this), 2, [ | ||
| [validators.enr], | ||
| [content_params.ContentItems], | ||
| ]) | ||
| this.stateTraceOffer = middleware(this.stateTraceOffer.bind(this), 2, [ | ||
| [validators.enr], | ||
| [content_params.ContentItems], | ||
| ]) | ||
| this.beaconTraceOffer = middleware(this.beaconTraceOffer.bind(this), 2, [ | ||
| [validators.enr], | ||
| [content_params.ContentItems], | ||
| ]) | ||
|
|
||
| // portal_*Gossip | ||
| this.historyGossip = middleware(this.historyGossip.bind(this), 2, [ | ||
| [validators.contentKey], | ||
|
|
@@ -1223,6 +1270,79 @@ export class portal { | |
| return res instanceof BitArray ? bytesToHex(res.uint8Array) : bytesToHex(res) | ||
| } | ||
|
|
||
| // portal_*TraceOffer | ||
| async historyTraceOffer( | ||
| params: [string, [string, string][]], | ||
| ): Promise<{ success?: boolean[]; declined?: boolean; failed?: boolean }> { | ||
| const [enrHex, contentItems] = params | ||
| const contentKeys = contentItems.map((item) => hexToBytes(item[0] as PrefixedHexString)) | ||
| const contentValues = contentItems.map((item) => hexToBytes(item[1] as PrefixedHexString)) | ||
| const enr = ENR.decodeTxt(enrHex) | ||
|
|
||
| try { | ||
| if (this._history.routingTable.getWithPending(enr.nodeId)?.value === undefined) { | ||
| const res = await this._history.sendPing(enr) | ||
| if (res === undefined) { | ||
| return { failed: true } | ||
| } | ||
| } | ||
|
|
||
| const res = await this._history.sendOffer(enr, contentKeys, contentValues) | ||
| return bitToBooleanArray(res, contentKeys.length) | ||
|
||
| } catch (error) { | ||
| this.logger(`historyTraceOffer failed: ${error}`) | ||
| return { failed: true } | ||
| } | ||
| } | ||
|
|
||
| async stateTraceOffer( | ||
| params: [string, [string, string][]], | ||
| ): Promise<{ success?: boolean[]; declined?: boolean; failed?: boolean }> { | ||
| const [enrHex, contentItems] = params | ||
| const contentKeys = contentItems.map((item) => hexToBytes(item[0] as PrefixedHexString)) | ||
| const contentValues = contentItems.map((item) => hexToBytes(item[1] as PrefixedHexString)) | ||
| const enr = ENR.decodeTxt(enrHex) | ||
|
|
||
| try { | ||
| if (this._state.routingTable.getWithPending(enr.nodeId)?.value === undefined) { | ||
| const res = await this._state.sendPing(enr) | ||
| if (res === undefined) { | ||
| return { failed: true } | ||
| } | ||
| } | ||
|
|
||
| const res = await this._state.sendOffer(enr, contentKeys, contentValues) | ||
| return bitToBooleanArray(res, contentKeys.length) | ||
|
||
| } catch (error) { | ||
| this.logger(`stateTraceOffer failed: ${error}`) | ||
| return { failed: true } | ||
| } | ||
| } | ||
|
|
||
| async beaconTraceOffer( | ||
| params: [string, [string, string][]], | ||
| ): Promise<{ success?: boolean[]; declined?: boolean; failed?: boolean }> { | ||
| const [enrHex, contentItems] = params | ||
| const contentKeys = contentItems.map((item) => hexToBytes(item[0] as PrefixedHexString)) | ||
| const contentValues = contentItems.map((item) => hexToBytes(item[1] as PrefixedHexString)) | ||
| const enr = ENR.decodeTxt(enrHex) | ||
|
|
||
| try { | ||
| if (this._beacon.routingTable.getWithPending(enr.nodeId)?.value === undefined) { | ||
| const res = await this._beacon.sendPing(enr) | ||
| if (res === undefined) { | ||
| return { failed: true } | ||
| } | ||
| } | ||
|
|
||
| const res = await this._beacon.sendOffer(enr, contentKeys, contentValues) | ||
| return bitToBooleanArray(res, contentKeys.length) | ||
|
||
| } catch (error) { | ||
| this.logger(`beaconTraceOffer failed: ${error}`) | ||
| return { failed: true } | ||
| } | ||
| } | ||
|
|
||
| // portal_*Gossip | ||
| async historyGossip(params: [string, string]) { | ||
| const [contentKey, content] = params | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { afterAll, assert, beforeAll, describe, it } from 'vitest' | ||
|
|
||
| import { startRpc } from '../util.js' | ||
|
|
||
| const method = 'portal_historyTraceOffer' | ||
| describe(`${method} tests`, () => { | ||
| let ul | ||
| let ul2 | ||
| let rp | ||
| let rp2 | ||
| beforeAll(async () => { | ||
| const { ultralight, rpc } = await startRpc({ networks: ['history'], rpcPort: 8545 }) | ||
| const { ultralight: ultralight2, rpc: rpc2 } = await startRpc({ | ||
| port: 9001, | ||
| rpcPort: 8546, | ||
| networks: ['history'], | ||
| }) | ||
| ul = ultralight | ||
| ul2 = ultralight2 | ||
| rp = rpc | ||
| rp2 = rpc2 | ||
| }) | ||
|
|
||
| it('should return declined when peer is not interested', async () => { | ||
| const enr = (await rp2.request('portal_historyNodeInfo', [])).result.enr | ||
| assert.exists(enr) | ||
|
|
||
| // Test with content keys that are unlikely to be accepted | ||
| const contentItems = [ | ||
| ['0x00' + '1'.repeat(62), '0x' + '2'.repeat(64)], | ||
| ['0x00' + '3'.repeat(62), '0x' + '4'.repeat(64)], | ||
| ] | ||
|
|
||
| try { | ||
| const res = await rp.request(method, [enr, contentItems]) | ||
| assert.ok(res.result.declined === true || res.result.failed === true, 'should be declined or failed') | ||
| } catch (error) { | ||
| assert.ok(true, 'error is acceptable for far content') | ||
| } | ||
| }, 20000) | ||
|
|
||
| it('should return failed when peer is unreachable', async () => { | ||
| const invalidEnr = 'enr:-invalid' | ||
| const contentItems = [ | ||
| ['0x00' + '1'.repeat(62), '0x' + '2'.repeat(64)], | ||
| ] | ||
|
|
||
| try { | ||
| const res = await rp.request(method, [invalidEnr, contentItems]) | ||
| assert.equal(res.result?.failed, true, 'should be failed for invalid ENR') | ||
| } catch (error) { | ||
| assert.ok(true, 'error is acceptable for invalid ENR') | ||
| } | ||
| }, 10000) | ||
|
|
||
| it('should return success when content keys are accepted', async () => { | ||
| const enr = (await rp2.request('portal_historyNodeInfo', [])).result.enr | ||
| assert.exists(enr) | ||
|
|
||
| // Generate content keys that are close to node2's ID to increase acceptance probability | ||
| const contentKey1 = '0x00' + '5'.repeat(64) | ||
| const content1 = '0x' + '1'.repeat(128) | ||
| const contentKey2 = '0x00' + '6'.repeat(64) | ||
| const content2 = '0x' + '2'.repeat(128) | ||
|
|
||
| // Store content in node2 | ||
| await rp2.request('portal_historyStore', [contentKey1, content1]) | ||
| await rp2.request('portal_historyStore', [contentKey2, content2]) | ||
|
Comment on lines
+66
to
+68
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If node2 stores these contents first, it will reject them in the OFFER. This test is giving you a false positive because of the bug in the |
||
|
|
||
| // Make an offer from node1 to node2 with content that node2 might accept | ||
| const contentItems = [ | ||
| [contentKey1, content1], | ||
| [contentKey2, content2], | ||
| ] | ||
|
|
||
| try { | ||
| const res = await rp.request(method, [enr, contentItems]) | ||
|
|
||
| // Should return success with boolean array indicating which keys were accepted | ||
| assert.ok(res.result.success !== undefined, 'should have success array') | ||
| if (res.result.success === true) { | ||
|
||
| assert.ok(Array.isArray(res.result.success), 'success should be an array') | ||
| const acceptedCount = res.result.success.filter((x: boolean) => x).length | ||
| assert.ok(acceptedCount > 0, 'at least one content key should be accepted') | ||
| } | ||
| } catch (error) { | ||
| console.log('Trace offer success error:', error) | ||
| assert.ok(false, 'error is unacceptable for success test') | ||
| } | ||
| }, 20000) | ||
|
|
||
| afterAll(() => { | ||
| ul.kill() | ||
| ul2.kill() | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move this to
rpc/utils.ts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks that's the info I was looking for 🙏