Skip to content

Commit

Permalink
StateNetwork: naive implement stateLocalContent
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Oct 25, 2023
1 parent fd65fc4 commit f2ce866
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/cli/src/rpc/modules/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { Multiaddr } from '@multiformats/multiaddr'
const methods = [
'portal_stateRoutingTableInfo',
'portal_stateStore',
'portal_stateLocalContent',
// history
'portal_historyRoutingTableInfo',
'portal_historyAddEnr',
Expand Down Expand Up @@ -123,6 +124,7 @@ export class portal {
this.historyLocalContent = middleware(this.historyLocalContent.bind(this), 1, [
[validators.hex],
])
this.stateLocalContent = middleware(this.stateLocalContent.bind(this), 1, [[validators.hex]])
this.historyStore = middleware(this.historyStore.bind(this), 2, [
[validators.contentKey],
[validators.hex],
Expand Down Expand Up @@ -441,6 +443,15 @@ export class portal {
this.logger.extend(`historyLocalContent`)(`${toHexString(res)}`)
return res.length > 0 ? toHexString(res) : '0x'
}
async stateLocalContent(params: [string]): Promise<string | undefined> {
const [contentKey] = params
this.logger(`Received stateLocalContent request for ${contentKey}`)

const res = await this._state.findContentLocally(fromHexString(contentKey))
this.logger.extend(`stateLocalContent`)(`request returned ${res.length} bytes`)
this.logger.extend(`stateLocalContent`)(`${toHexString(res)}`)
return res.length > 0 ? toHexString(res) : '0x'
}
async historyFindContent(params: [string, string]) {
const [enr, contentKey] = params
const nodeId = ENR.decodeTxt(enr).nodeId
Expand Down
6 changes: 6 additions & 0 deletions packages/portalnetwork/src/subprotocols/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import debug, { Debugger } from 'debug'
import { PortalNetwork } from '../../client/client.js'
import { BaseProtocol } from '../protocol.js'
import { ProtocolId } from '../types.js'
import { toHexString } from '@chainsafe/ssz'
import { hexToBytes } from '@ethereumjs/util'

export class StateProtocol extends BaseProtocol {
protocolId: ProtocolId.StateNetwork
Expand All @@ -18,6 +20,10 @@ export class StateProtocol extends BaseProtocol {
return undefined
}

public findContentLocally = async (contentKey: Uint8Array): Promise<Uint8Array> => {
const value = await this.retrieve(toHexString(contentKey))
return value ? hexToBytes(value) : hexToBytes('0x')
}

public routingTableInfo = async () => {
return {
Expand Down

0 comments on commit f2ce866

Please sign in to comment.