Skip to content

Commit

Permalink
refactor: destruct blocks into separate service
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKostroma committed Sep 20, 2024
1 parent 970a6de commit bf512f0
Show file tree
Hide file tree
Showing 15 changed files with 821 additions and 733 deletions.
83 changes: 0 additions & 83 deletions background/lib/gas.ts

This file was deleted.

25 changes: 16 additions & 9 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
SigningService,
TelemetryService,
} from "./services"
import { HexString, KeyringTypes } from "./types"
import { HexString } from "./types"
import { ChainIdWithError } from "./networks"
import {
AccountBalance,
Expand Down Expand Up @@ -156,6 +156,7 @@ import {
import ProviderFactory from "./services/provider-factory/provider-factory"
import { LocalNodeNetworkStatusEventTypes } from "./services/provider-factory/events"
import NotificationsManager from "./services/notifications"
import BlockService from "./services/block"

// This sanitizer runs on store and action data before serializing for remote
// redux devtools. The goal is to end up with an object that is directly
Expand Down Expand Up @@ -282,15 +283,18 @@ export default class Main extends BaseService<never> {
preferenceService,
keyringService
)
const blockService = BlockService.create(chainService, preferenceService)
const indexingService = IndexingService.create(
preferenceService,
chainService
chainService,
blockService
)
const nameService = NameService.create(chainService, preferenceService)
const enrichmentService = EnrichmentService.create(
chainService,
indexingService,
nameService
nameService,
blockService
)
const internalQuaiProviderService = InternalQuaiProviderService.create(
chainService,
Expand Down Expand Up @@ -345,7 +349,8 @@ export default class Main extends BaseService<never> {
await providerBridgeService,
await telemetryService,
await signingService,
await analyticsService
await analyticsService,
await blockService
)
}

Expand Down Expand Up @@ -410,7 +415,9 @@ export default class Main extends BaseService<never> {
* A promise to the analytics service which will be responsible for listening
* to events and dispatching to our analytics backend
*/
private analyticsService: AnalyticsService
private analyticsService: AnalyticsService,

private blockService: BlockService
) {
super({
initialLoadWaitExpired: {
Expand Down Expand Up @@ -880,7 +887,7 @@ export default class Main extends BaseService<never> {
this.store.dispatch(setEVMNetworks(supportedNetworks))
})

this.chainService.emitter.on("block", (block) => {
this.blockService.emitter.on("block", (block) => {
this.store.dispatch(blockSeen(block))
})

Expand Down Expand Up @@ -972,7 +979,7 @@ export default class Main extends BaseService<never> {
}
)

this.chainService.emitter.on(
this.blockService.emitter.on(
"blockPrices",
async ({ blockPrices, network }) => {
this.store.dispatch(
Expand Down Expand Up @@ -1301,7 +1308,7 @@ export default class Main extends BaseService<never> {
resolver: (result: string | PromiseLike<string>) => void
rejecter: () => void
}) => {
this.chainService.pollBlockPricesForNetwork(
await this.blockService.pollBlockPricesForNetwork(
payload.account.network.chainID
)
this.store.dispatch(signDataRequest(payload))
Expand Down Expand Up @@ -1360,7 +1367,7 @@ export default class Main extends BaseService<never> {
[{ chainId: network.chainID }],
PELAGUS_INTERNAL_ORIGIN
)
this.chainService.pollBlockPricesForNetwork(network.chainID)
this.blockService.pollBlockPricesForNetwork(network.chainID)
this.chainService.switchNetwork(network)
this.store.dispatch(clearCustomGas())
})
Expand Down
56 changes: 56 additions & 0 deletions background/services/block/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Dexie, { DexieOptions } from "dexie"

import { AnyEVMBlock } from "../../networks"
import { NetworkInterface } from "../../constants/networks/networkTypes"
import { ChainDatabase } from "../chain/db"

export class BlockDatabase extends Dexie {
private blocks!: Dexie.Table<AnyEVMBlock, [string, string]>

constructor(options?: DexieOptions) {
super("pelagus/blocks", options)
this.version(1).stores({
migrations: null,
blocks:
"&[hash+network.baseAsset.name],[network.baseAsset.name+timestamp],hash,network.baseAsset.name,timestamp,parentHash,blockHeight,[blockHeight+network.baseAsset.name]",
})
}

async getLatestBlock(network: NetworkInterface): Promise<AnyEVMBlock | null> {
return (
(
await this.blocks
.where("[network.baseAsset.name+timestamp]")
// Only query blocks from the last 86 seconds
.aboveOrEqual([network.baseAsset.name, Date.now() - 60 * 60 * 24])
.and(
(block) => block.network.baseAsset.name === network.baseAsset.name
)
.reverse()
.sortBy("timestamp")
)[0] || null
)
}

async getBlock(
network: NetworkInterface,
blockHash: string
): Promise<AnyEVMBlock | null> {
return (
(
await this.blocks
.where("[hash+network.baseAsset.name]")
.equals([blockHash, network.baseAsset.name])
.toArray()
)[0] || null
)
}

async addBlock(block: AnyEVMBlock): Promise<void> {
await this.blocks.put(block)
}
}

export function createDB(options?: DexieOptions): BlockDatabase {
return new BlockDatabase(options)
}
Loading

0 comments on commit bf512f0

Please sign in to comment.