Skip to content

Commit

Permalink
Adds handling for content in beacon network (#428)
Browse files Browse the repository at this point in the history
* Add content handling to network scaffolding

* Add tests for storing and retrieving beacon content

* Integration test scaffolding

* Add UTP handling in sendFindContent

* WIP

* WIP

* Finish test for finding content

* Turn off logging

* Fix method names

* Add TODO
  • Loading branch information
acolytec3 authored Aug 15, 2023
1 parent eb29512 commit 99fb319
Show file tree
Hide file tree
Showing 41 changed files with 649 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ExtendedEthersBlockWithTransactions,
fromHexString,
getContentKey,
ContentType,
HistoryNetworkContentType,
toHexString,
TxReceiptWithType,
decodeReceipts,
Expand Down Expand Up @@ -222,7 +222,7 @@ const DisplayBlock = () => {
Buffer.from(
fromHexString(
await state.provider!.historyProtocol.get(
getContentKey(ContentType.Receipt, fromHexString(state.block!.hash))
getContentKey(HistoryNetworkContentType.Receipt, fromHexString(state.block!.hash))
)
)
)
Expand All @@ -243,9 +243,9 @@ const DisplayBlock = () => {
typeof (state.block as any).hash === 'string'
? (state.block as any).hash
: toHexString((state.block as any).hash())
const header = getContentKey(ContentType.BlockHeader, Buffer.from(fromHexString(hash)))
const header = getContentKey(HistoryNetworkContentType.BlockHeader, Buffer.from(fromHexString(hash)))

const body = getContentKey(ContentType.BlockBody, Buffer.from(fromHexString(hash)))
const body = getContentKey(HistoryNetworkContentType.BlockBody, Buffer.from(fromHexString(hash)))
setKeys({
header,
body,
Expand Down
4 changes: 2 additions & 2 deletions packages/archived-browser-client/src/Components/GetEpoch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SearchIcon } from '@chakra-ui/icons'
import { HStack, Input, useToast, IconButton } from '@chakra-ui/react'
import {
ContentLookup,
ContentType,
HistoryNetworkContentType,
EpochAccumulator,
epochRootByBlocknumber,
epochRootByIndex,
Expand All @@ -29,7 +29,7 @@ export default function GetEpoch() {
) as HistoryProtocol
const lookup = new ContentLookup(
protocol,
fromHexString(getContentKey(ContentType.EpochAccumulator, epochRootHash))
fromHexString(getContentKey(HistoryNetworkContentType.EpochAccumulator, epochRootHash))
)
const epoch = await lookup.startLookup()
if (epoch !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { ChevronDownIcon, SearchIcon } from '@chakra-ui/icons'
import { AppContext, AppContextType, StateChange } from '../globalReducer'
import { PeerActions } from '../peerActions'
import { ENR, fromHexString, getContentId, getContentKey, ContentType } from 'portalnetwork'
import { ENR, fromHexString, getContentId, getContentKey, HistoryNetworkContentType } from 'portalnetwork'
import { PeerContext, PeerContextType, PeerStateChange } from '../peerReducer'

enum GetBy {
Expand Down Expand Up @@ -61,7 +61,7 @@ export function PortalButton(props: IPortalButton) {
setInput(blockHash)
}, [blockHash])

const addToOffer = async (type: ContentType) => {
const addToOffer = async (type: HistoryNetworkContentType) => {
const contentKey = getContentKey(type, Buffer.from(fromHexString(blockHash)))
const contentId = getContentId(type, blockHash)
if (await state.provider?.historyProtocol.get(contentKey)) {
Expand Down Expand Up @@ -231,7 +231,7 @@ export function PortalButton(props: IPortalButton) {
width={'50%'}
title="Add content to offer"
onClick={() => {
addToOffer(ContentType.BlockHeader)
addToOffer(HistoryNetworkContentType.BlockHeader)
}}
>
add_header_to_offer
Expand All @@ -241,7 +241,7 @@ export function PortalButton(props: IPortalButton) {
width={'50%'}
title="Add content to offer"
onClick={() => {
addToOffer(ContentType.BlockBody)
addToOffer(HistoryNetworkContentType.BlockBody)
}}
>
add_block_body_to_offer
Expand All @@ -251,7 +251,7 @@ export function PortalButton(props: IPortalButton) {
width={'50%'}
title="Add content to offer"
onClick={() => {
addToOffer(ContentType.EpochAccumulator)
addToOffer(HistoryNetworkContentType.EpochAccumulator)
}}
>
add_epoch_to_offer
Expand Down
12 changes: 6 additions & 6 deletions packages/archived-browser-client/src/peerActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
fromHexString,
getContentId,
getContentKey,
ContentType,
HistoryNetworkContentType,
HistoryProtocol,
reassembleBlock,
epochRootByIndex,
Expand All @@ -20,7 +20,7 @@ export class PeerActions {
this.historyProtocol = protocol
}

addToOffer = (type: ContentType): void => {
addToOffer = (type: HistoryNetworkContentType): void => {
this.dispatch({
type: PeerStateChange.SETOFFER,
payload: [...this.state.offer, getContentId(type, this.state.blockHash)],
Expand Down Expand Up @@ -56,7 +56,7 @@ export class PeerActions {
sendFindContent = async (type: string, enr: string) => {
if (type === 'header') {
const headerContentId = fromHexString(
getContentKey(ContentType.BlockHeader, fromHexString(this.state.blockHash)),
getContentKey(HistoryNetworkContentType.BlockHeader, fromHexString(this.state.blockHash)),
)
const header = await this.historyProtocol.sendFindContent(
ENR.decodeTxt(enr).nodeId,
Expand All @@ -66,16 +66,16 @@ export class PeerActions {
return block //
} else if (type === 'body') {
const headerContentKey = fromHexString(
getContentKey(ContentType.BlockHeader, fromHexString(this.state.blockHash)),
getContentKey(HistoryNetworkContentType.BlockHeader, fromHexString(this.state.blockHash)),
)
this.historyProtocol!.sendFindContent(ENR.decodeTxt(enr).nodeId, headerContentKey)
const bodyContentKey = fromHexString(
getContentKey(ContentType.BlockBody, fromHexString(this.state.blockHash)),
getContentKey(HistoryNetworkContentType.BlockBody, fromHexString(this.state.blockHash)),
)
this.historyProtocol!.sendFindContent(ENR.decodeTxt(enr).nodeId, bodyContentKey)
} else if (type === 'epoch') {
const epochContentKey = fromHexString(
getContentKey(ContentType.EpochAccumulator, epochRootByIndex(this.state.epoch)),
getContentKey(HistoryNetworkContentType.EpochAccumulator, epochRootByIndex(this.state.epoch)),
)
this.historyProtocol!.sendFindContent(ENR.decodeTxt(enr).nodeId, epochContentKey)
}
Expand Down
14 changes: 10 additions & 4 deletions packages/browser-client/src/Components/DisplayBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ExtendedEthersBlockWithTransactions,
fromHexString,
getContentKey,
ContentType,
HistoryNetworkContentType,
toHexString,
TxReceiptWithType,
decodeReceipts,
Expand Down Expand Up @@ -224,7 +224,7 @@ const DisplayBlock = () => {
fromHexString(
await state.provider!.historyProtocol.get(
ProtocolId.HistoryNetwork,
getContentKey(ContentType.Receipt, fromHexString(state.block!.hash)),
getContentKey(HistoryNetworkContentType.Receipt, fromHexString(state.block!.hash)),
),
),
),
Expand All @@ -245,9 +245,15 @@ const DisplayBlock = () => {
typeof (state.block as any).hash === 'string'
? (state.block as any).hash
: toHexString((state.block as any).hash())
const header = getContentKey(ContentType.BlockHeader, Buffer.from(fromHexString(hash)))
const header = getContentKey(
HistoryNetworkContentType.BlockHeader,
Buffer.from(fromHexString(hash)),
)

const body = getContentKey(ContentType.BlockBody, Buffer.from(fromHexString(hash)))
const body = getContentKey(
HistoryNetworkContentType.BlockBody,
Buffer.from(fromHexString(hash)),
)
setKeys({
header,
body,
Expand Down
4 changes: 2 additions & 2 deletions packages/browser-client/src/Components/GetEpoch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SearchIcon } from '@chakra-ui/icons'
import { HStack, Input, useToast, IconButton } from '@chakra-ui/react'
import {
ContentLookup,
ContentType,
HistoryNetworkContentType,
EpochAccumulator,
epochRootByIndex,
fromHexString,
Expand All @@ -27,7 +27,7 @@ export default function GetEpoch() {
) as HistoryProtocol
const lookup = new ContentLookup(
protocol,
fromHexString(getContentKey(ContentType.EpochAccumulator, epochRootHash)),
fromHexString(getContentKey(HistoryNetworkContentType.EpochAccumulator, epochRootHash)),
)
const epoch = await lookup.startLookup()
if (epoch !== undefined) {
Expand Down
10 changes: 5 additions & 5 deletions packages/browser-client/src/Components/PortalButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
fromHexString,
getContentId,
getContentKey,
ContentType,
HistoryNetworkContentType,
ProtocolId,
} from 'portalnetwork'
import { PeerContext, PeerContextType, PeerStateChange } from '../peerReducer'
Expand Down Expand Up @@ -68,7 +68,7 @@ export function PortalButton(props: IPortalButton) {
setInput(blockHash)
}, [blockHash])

const addToOffer = async (type: ContentType) => {
const addToOffer = async (type: HistoryNetworkContentType) => {
const contentKey = getContentKey(type, Buffer.from(fromHexString(blockHash)))
const contentId = getContentId(type, blockHash)
if (await state.provider?.historyProtocol.get(ProtocolId.HistoryNetwork, contentKey)) {
Expand Down Expand Up @@ -238,7 +238,7 @@ export function PortalButton(props: IPortalButton) {
width={'50%'}
title="Add content to offer"
onClick={() => {
addToOffer(ContentType.BlockHeader)
addToOffer(HistoryNetworkContentType.BlockHeader)
}}
>
add_header_to_offer
Expand All @@ -248,7 +248,7 @@ export function PortalButton(props: IPortalButton) {
width={'50%'}
title="Add content to offer"
onClick={() => {
addToOffer(ContentType.BlockBody)
addToOffer(HistoryNetworkContentType.BlockBody)
}}
>
add_block_body_to_offer
Expand All @@ -258,7 +258,7 @@ export function PortalButton(props: IPortalButton) {
width={'50%'}
title="Add content to offer"
onClick={() => {
addToOffer(ContentType.EpochAccumulator)
addToOffer(HistoryNetworkContentType.EpochAccumulator)
}}
>
add_epoch_to_offer
Expand Down
12 changes: 6 additions & 6 deletions packages/browser-client/src/peerActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
fromHexString,
getContentId,
getContentKey,
ContentType,
HistoryNetworkContentType,
HistoryProtocol,
reassembleBlock,
epochRootByIndex,
Expand All @@ -20,7 +20,7 @@ export class PeerActions {
this.historyProtocol = protocol
}

addToOffer = (type: ContentType): void => {
addToOffer = (type: HistoryNetworkContentType): void => {
this.dispatch({
type: PeerStateChange.SETOFFER,
payload: [...this.state.offer, getContentId(type, this.state.blockHash)],
Expand Down Expand Up @@ -56,7 +56,7 @@ export class PeerActions {
sendFindContent = async (type: string, enr: string) => {
if (type === 'header') {
const headerContentId = fromHexString(
getContentKey(ContentType.BlockHeader, fromHexString(this.state.blockHash)),
getContentKey(HistoryNetworkContentType.BlockHeader, fromHexString(this.state.blockHash)),
)
const header = await this.historyProtocol.sendFindContent(
ENR.decodeTxt(enr).nodeId,
Expand All @@ -66,16 +66,16 @@ export class PeerActions {
return block //
} else if (type === 'body') {
const headerContentKey = fromHexString(
getContentKey(ContentType.BlockHeader, fromHexString(this.state.blockHash)),
getContentKey(HistoryNetworkContentType.BlockHeader, fromHexString(this.state.blockHash)),
)
this.historyProtocol!.sendFindContent(ENR.decodeTxt(enr).nodeId, headerContentKey)
const bodyContentKey = fromHexString(
getContentKey(ContentType.BlockBody, fromHexString(this.state.blockHash)),
getContentKey(HistoryNetworkContentType.BlockBody, fromHexString(this.state.blockHash)),
)
this.historyProtocol!.sendFindContent(ENR.decodeTxt(enr).nodeId, bodyContentKey)
} else if (type === 'epoch') {
const epochContentKey = fromHexString(
getContentKey(ContentType.EpochAccumulator, epochRootByIndex(this.state.epoch)),
getContentKey(HistoryNetworkContentType.EpochAccumulator, epochRootByIndex(this.state.epoch)),
)
this.historyProtocol!.sendFindContent(ENR.decodeTxt(enr).nodeId, epochContentKey)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/scripts/gossipTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
fromHexString,
getContentId,
getContentKey,
ContentType,
HistoryNetworkContentType,
ProtocolId,
toHexString,
} from 'portalnetwork'
Expand Down Expand Up @@ -46,7 +46,7 @@ const gossip = async () => {
// GossipTest

const headerKey = getContentKey(
ContentType.BlockHeader,
HistoryNetworkContentType.BlockHeader,
testBlocks[0].hash()
)
const header = testBlocks[0].header.serialize()
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/scripts/recursiveFindContentTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
fromHexString,
getContentId,
getContentKey,
ContentType,
HistoryNetworkContentType,
ProtocolId,
toHexString,
} from 'portalnetwork'
Expand Down Expand Up @@ -46,7 +46,7 @@ const recursiveFindContent = async () => {
// GossipTest

const headerKey = getContentKey(
ContentType.BlockHeader,
HistoryNetworkContentType.BlockHeader,
testBlocks[0].hash()
)
const header = testBlocks[0].header.serialize()
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/scripts/seeder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jayson, { HttpClient } from 'jayson/promise/index.js'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { fromHexString, getContentKey, ContentType, ProtocolId, toHexString } from 'portalnetwork'
import { fromHexString, getContentKey, HistoryNetworkContentType, ProtocolId, toHexString } from 'portalnetwork'
import { createRequire } from 'module'
import { readFileSync } from 'fs'
import { Block } from '@ethereumjs/block'
Expand Down Expand Up @@ -84,7 +84,7 @@ const main = async () => {
}
console.log(`ok ${method} test`)
}
const epochKey = getContentKey(ContentType.EpochAccumulator, fromHexString(epoch.hash))
const epochKey = getContentKey(HistoryNetworkContentType.EpochAccumulator, fromHexString(epoch.hash))
let res = await clientInfo.ultralight.client.request('ultralight_addContentToDB', [
epochKey,
epoch.serialized,
Expand Down Expand Up @@ -165,7 +165,7 @@ const main = async () => {
await testRes([clients[0]], 'portal_historyOffer', [
[
clientInfo.peer1.enr,
getContentKey(ContentType.BlockHeader, fromHexString(block[0])),
getContentKey(HistoryNetworkContentType.BlockHeader, fromHexString(block[0])),
toHexString(
Block.fromRLPSerializedBlock(hexToBytes((block[1] as any).rlp), {
setHardfork: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/scripts/sendPingTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
fromHexString,
getContentId,
getContentKey,
ContentType,
HistoryNetworkContentType,
ProtocolId,
toHexString,
} from 'portalnetwork'
Expand Down
Loading

0 comments on commit 99fb319

Please sign in to comment.