Skip to content

Commit

Permalink
Various updates (#468)
Browse files Browse the repository at this point in the history
* Switch slot used in updates to current slot

* Remove fallback provider

* Remove buffer nonsense

* Suppress dep warning
  • Loading branch information
acolytec3 authored Sep 26, 2023
1 parent 553af1b commit cf8b546
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/browser-client/src/Components/ContentManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function ContentManager() {
dispatch({
type: StateChange.SETBLOCK,
payload: await ethJsBlockToEthersBlockWithTxs(
Block.fromRLPSerializedBlock(Buffer.from(fromHexString(last[1].rlp)), {
Block.fromRLPSerializedBlock(fromHexString(last[1].rlp), {
setHardfork: true,
}),
),
Expand Down
27 changes: 10 additions & 17 deletions packages/browser-client/src/Components/DisplayBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import React, { useContext, useEffect, useState } from 'react'
import { AppContext, AppContextType, StateChange } from '../globalReducer'
import SelectTx from './SelectTx'
import { bytesToBigInt } from '@ethereumjs/util'

const DisplayBlock = () => {
const { state, dispatch } = useContext(AppContext as React.Context<AppContextType>)
Expand Down Expand Up @@ -98,7 +99,7 @@ const DisplayBlock = () => {
) : k === 'extraData' ? (
<HStack width="100%">
<Text paddingY={0} fontSize={'x-small'} width="50%">
{Buffer.from(fromHexString(v)).toString()}
{v}
</Text>
<Text paddingY={0} fontSize={'x-small'}>
{v}
Expand Down Expand Up @@ -181,13 +182,13 @@ const DisplayBlock = () => {
? 'Undefineded for unknown reasons'
: typeof value
: key === 'coinbase'
? toHexString((value as any).buf as Buffer)
? toHexString((value as any).buf as Uint8Array)
: typeof value === 'string'
? value
: typeof value === 'bigint'
? value
: key === 'nonce'
? BigInt('0x' + (value as Buffer).toString('hex'))
? bytesToBigInt(value as Uint8Array)
: '0x' + (value as any).toString('hex')
return key === 'cache' ? (
<Box key={key}></Box>
Expand Down Expand Up @@ -220,12 +221,10 @@ const DisplayBlock = () => {
async function init() {
try {
const receipts = decodeReceipts(
Buffer.from(
fromHexString(
await state.provider!.historyProtocol.get(
ProtocolId.HistoryNetwork,
getContentKey(HistoryNetworkContentType.Receipt, fromHexString(state.block!.hash)),
),
fromHexString(
await state.provider!.historyProtocol.get(
ProtocolId.HistoryNetwork,
getContentKey(HistoryNetworkContentType.Receipt, fromHexString(state.block!.hash)),
),
),
)
Expand All @@ -245,15 +244,9 @@ const DisplayBlock = () => {
typeof (state.block as any).hash === 'string'
? (state.block as any).hash
: toHexString((state.block as any).hash())
const header = getContentKey(
HistoryNetworkContentType.BlockHeader,
Buffer.from(fromHexString(hash)),
)
const header = getContentKey(HistoryNetworkContentType.BlockHeader, fromHexString(hash))

const body = getContentKey(
HistoryNetworkContentType.BlockBody,
Buffer.from(fromHexString(hash)),
)
const body = getContentKey(HistoryNetworkContentType.BlockBody, fromHexString(hash))
setKeys({
header,
body,
Expand Down
2 changes: 1 addition & 1 deletion packages/browser-client/src/Components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function Layout() {
}
async function setSample() {
const sampleBlock = await ethJsBlockToEthersBlockWithTxs(
Block.fromRLPSerializedBlock(Buffer.from(fromHexString(bigblock[0].rlp)), {
Block.fromRLPSerializedBlock(fromHexString(bigblock[0].rlp), {
setHardfork: true,
}),
)
Expand Down
2 changes: 1 addition & 1 deletion packages/browser-client/src/Components/PortalButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function PortalButton(props: IPortalButton) {
}, [blockHash])

const addToOffer = async (type: HistoryNetworkContentType) => {
const contentKey = getContentKey(type, Buffer.from(fromHexString(blockHash)))
const contentKey = getContentKey(type, fromHexString(blockHash))
const contentId = getContentId(type, blockHash)
if (await state.provider?.historyProtocol.get(ProtocolId.HistoryNetwork, contentKey)) {
setOffer([...offer, contentId])
Expand Down
14 changes: 9 additions & 5 deletions packages/browser-client/src/portalClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Capacitor } from '@capacitor/core'
import { ethers } from 'ethers'
import {
ENR,
log2Distance,
Expand All @@ -8,6 +7,7 @@ import {
fromHexString,
TransportLayer,
UltralightProvider,
ProtocolId,
} from 'portalnetwork'
import { AppState } from './globalReducer'
import bns from './bootnodes.json'
Expand Down Expand Up @@ -64,35 +64,39 @@ export const startUp = async (provider: UltralightProvider) => {
}
export async function createNodeFromScratch(state: AppState): Promise<UltralightProvider> {
const provider = Capacitor.isNativePlatform()
? await UltralightProvider.create(new ethers.providers.CloudflareProvider(), 1, {
? await UltralightProvider.create('', 1, {
bootnodes: bns,
db: state.LDB as any,
transport: TransportLayer.MOBILE,
supportedProtocols: [ProtocolId.HistoryNetwork, ProtocolId.BeaconLightClientNetwork],
})
: await UltralightProvider.create(new ethers.providers.CloudflareProvider(), 1, {
: await UltralightProvider.create('', 1, {
proxyAddress: state.proxy,
bootnodes: bns,
db: state.LDB as any,
transport: TransportLayer.WEB,
supportedProtocols: [ProtocolId.HistoryNetwork, ProtocolId.BeaconLightClientNetwork],
})
await startUp(provider)
return provider
}

export async function createNodeFromStorage(state: AppState): Promise<UltralightProvider> {
const provider = Capacitor.isNativePlatform()
? await UltralightProvider.create(new ethers.providers.CloudflareProvider(), 1, {
? await UltralightProvider.create('', 1, {
bootnodes: bns,
db: state.LDB as any,
rebuildFromMemory: true,
transport: TransportLayer.MOBILE,
supportedProtocols: [ProtocolId.HistoryNetwork, ProtocolId.BeaconLightClientNetwork],
})
: await UltralightProvider.create(new ethers.providers.CloudflareProvider(), 1, {
: await UltralightProvider.create('', 1, {
proxyAddress: state.proxy,
bootnodes: bns,
db: state.LDB as any,
rebuildFromMemory: true,
transport: TransportLayer.WEB,
supportedProtocols: [ProtocolId.HistoryNetwork, ProtocolId.BeaconLightClientNetwork],
})
await startUp(provider)
return provider
Expand Down
1 change: 1 addition & 0 deletions packages/browser-client/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
},
},
module: {
exprContextCritical: false,
rules: [
{
test: /\.m?js/,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Debugger } from 'debug'
import { concatBytes, hexToBytes } from '@ethereumjs/util'
import { genesisData } from '@lodestar/config/networks'
import { getBeaconContentKey } from './util.js'
import { toHexString } from '@chainsafe/ssz'

export class UltralightTransport implements LightClientTransport {
protocol: BeaconLightClientNetwork
Expand Down Expand Up @@ -84,7 +83,7 @@ export class UltralightTransport implements LightClientTransport {
concatBytes(
new Uint8Array([BeaconLightClientNetworkContentType.LightClientOptimisticUpdate]),
LightClientOptimisticUpdateKey.serialize({
optimisticSlot: currentSlot - 1n,
optimisticSlot: currentSlot,
}),
),
)
Expand All @@ -111,13 +110,16 @@ export class UltralightTransport implements LightClientTransport {
}> {
let finalityUpdate, forkname

this.logger('requesting latest LightClientFinalityUpdate')
const currentSlot = BigInt(
getCurrentSlot(this.protocol.beaconConfig, genesisData.mainnet.genesisTime),
)
this.logger(`requesting LightClientFinalityUpdate for ${currentSlot.toString(10)}`)
// Try to get finality update from Portal Network
const decoded = await this.protocol.sendFindContent(
this.protocol.routingTable.random()!.nodeId,
concatBytes(
new Uint8Array([BeaconLightClientNetworkContentType.LightClientFinalityUpdate]),
LightClientFinalityUpdateKey.serialize({ finalizedSlot: 0n }),
LightClientFinalityUpdateKey.serialize({ finalizedSlot: currentSlot }),
),
)
if (decoded !== undefined) {
Expand Down

0 comments on commit cf8b546

Please sign in to comment.