Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Haimerl authored and Nikolas Haimerl committed Jan 16, 2025
1 parent a00e61b commit c2ed0ad
Show file tree
Hide file tree
Showing 9 changed files with 31,587 additions and 31,611 deletions.
6 changes: 3 additions & 3 deletions backend/lib/deal-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class DealObserver {
#rpcProviderURL
#cache

constructor(pgPool = null, rpcProviderURL = GLIF_RPC, chainHead = null) {
constructor (pgPool = null, rpcProviderURL = GLIF_RPC, chainHead = null) {
// TODO: Store events in pgPool
this.#pgPool = pgPool
this.#rpcProviderURL = rpcProviderURL
this.#cache = new Map()
this.#cache.set('chainHead', chainHead)
}

async build() {
async build () {
this.#lotusService = await (new LotusService(this.#rpcProviderURL)).build()

Check failure on line 22 in backend/lib/deal-observer.js

View workflow job for this annotation

GitHub Actions / lint-all

Argument of type 'string' is not assignable to parameter of type '(method: any, params: any) => Promise<any>'.
if (!this.#cache.get('chainHead')) {
const chainHead = await this.#lotusService.getChainHead()
Expand All @@ -27,7 +27,7 @@ class DealObserver {
return this
}

async observeBuiltinActorEvents(fromHeight = this.#cache.get('chainHead').Height, toHeight = this.#cache.get('chainHead').Height, eventTypes = EVENT_TYPES) {
async observeBuiltinActorEvents (fromHeight = this.#cache.get('chainHead').Height, toHeight = this.#cache.get('chainHead').Height, eventTypes = EVENT_TYPES) {
return this.#lotusService.getActorEvents(new ActorEventFilter(fromHeight, toHeight, eventTypes))
}
}
Expand Down
8 changes: 3 additions & 5 deletions backend/lib/rpc-service/ipld-schema.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { create } from '@ipld/schema/typed.js'
import { schemaDmt } from './builtin-actor-events-schemas.js'
import { base64pad } from 'multiformats/bases/base64'
import { encode as cborEncode, decode as cborDecode } from '@ipld/dag-cbor'

class IpldSchema {
// A transformer which takes in a json object and returns a typed ClaimEvent object
#claimEventSchema
#rawActorEventSchema

async build() {
async build () {
// TODO: Catch and log errors
this.#claimEventSchema = this.#createType('ClaimEvent')
this.#rawActorEventSchema = this.#createType('RawActorEvent')
return this
}

#createType(name) {
#createType (name) {
// TODO: Catch and log errors
return create(schemaDmt, name)
}

applyType(typeName, data) {
applyType (typeName, data) {
switch (typeName.toLowerCase()) {
case 'claimevent':
// TODO: Catch and log errors
Expand Down
23 changes: 10 additions & 13 deletions backend/lib/rpc-service/service.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { EVENT_TYPES, GLIF_RPC } from '../config.js'
import { GLIF_RPC } from '../config.js'
import { base64pad } from 'multiformats/bases/base64'
import { encode as cborEncode } from '@ipld/dag-cbor'
import { decode as jsonDecode } from '@ipld/dag-json'
import { request } from 'undici'
import fs from 'fs'
import { IpldSchema } from './ipld-schema.js'
import { rawEventEntriesToEvent } from './utils.js'
let counter = 0

const make_rpc_request = async (method, params) => {
const reqBody = JSON.stringify({ method, params, id: counter++, jsonrpc: '2.0' })
const makeRpcRequest = async (method, params) => {
const reqBody = JSON.stringify({ method, params, id: 1, jsonrpc: '2.0' })
const response = await request(GLIF_RPC, {
bodyTimeout: 1000 * 60,
headersTimeout: 1000 * 60,
Expand All @@ -27,12 +25,11 @@ class LotusService {
#ipldSchema
#make_rpc_request


constructor(rpc_request = make_rpc_request) {
this.#make_rpc_request = rpc_request
constructor (rpcRequest = makeRpcRequest) {
this.#make_rpc_request = rpcRequest
}

async build() {
async build () {
this.#ipldSchema = await (new IpldSchema()).build()
return this
}
Expand All @@ -42,7 +39,7 @@ class LotusService {
* Returns actor events filtered by the given actorEventFilter
* @returns {Promise<object>}
*/
async getActorEvents(actorEventFilter) {
async getActorEvents (actorEventFilter) {
// TODO: Handle multiple events, currently we are expecting a single event to exist in the filter
const rawEvents = (await this.#make_rpc_request('Filecoin.GetActorEventsRaw', [actorEventFilter]))
const typedRawEventEntries = rawEvents.map((rawEvent) => this.#ipldSchema.applyType(
Expand All @@ -52,7 +49,7 @@ class LotusService {
const emittedEvents = new Set()
for (const typedEventEntries of typedRawEventEntries) {
const { event, eventType } = rawEventEntriesToEvent(typedEventEntries.entries)
// Verify the returned event matches the expected event schema
// Verify the returned event matches the expected event schema
const typedEvent = this.#ipldSchema.applyType(eventType, event)
emittedEvents.add(
{
Expand All @@ -64,7 +61,7 @@ class LotusService {
return emittedEvents
}

async getChainHead() {
async getChainHead () {
return await this.#make_rpc_request('Filecoin.ChainHead', [])
}
}
Expand All @@ -75,7 +72,7 @@ class ActorEventFilter {
* @param {number} toHeight
* @param {string[]} eventTypes
*/
constructor(fromHeight, toHeight, eventTypes) {
constructor (fromHeight, toHeight, eventTypes) {
this.fromHeight = fromHeight
this.toHeight = toHeight
this.fields = {
Expand Down
62 changes: 30 additions & 32 deletions backend/lib/rpc-service/utils.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import { create } from '@ipld/schema/typed.js'
import { schemaDmt } from './builtin-actor-events-schemas.js'
import { base64pad } from 'multiformats/bases/base64'
import { encode as cborEncode, decode as cborDecode } from '@ipld/dag-cbor'

const decodeCborInBase64 = (data) => {
return cborDecode(base64pad.baseDecode(data))
}

const encodeCborInBase64 = (data) => {
return base64pad.baseEncode(cborEncode(data))
}

const rawEventEntriesToEvent = (rawEventEntries) => {
// Each event is defined by a list of even entries which will will transform into a typed event
const event = {}
// TODO handle if there is no type entry
let eventType
for (const entry of rawEventEntries) {
// The key returned by the Lotus API is kebab-case, we convert it to camelCase
const key = entry.Key.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
const value = decodeCborInBase64(entry.Value)
// In each entry exists an event type declaration which we need to extract
if (key === '$type') {
eventType = value.concat('event')
// The type entry is not part of the event itself
continue
}
event[key] = value
}
return {event, eventType}
return cborDecode(base64pad.baseDecode(data))
}

const encodeCborInBase64 = (data) => {
return base64pad.baseEncode(cborEncode(data))
}

const rawEventEntriesToEvent = (rawEventEntries) => {
// Each event is defined by a list of even entries which will will transform into a typed event
const event = {}
// TODO handle if there is no type entry
let eventType
for (const entry of rawEventEntries) {
// The key returned by the Lotus API is kebab-case, we convert it to camelCase
const key = entry.Key.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
const value = decodeCborInBase64(entry.Value)
// In each entry exists an event type declaration which we need to extract
if (key === '$type') {
eventType = value.concat('event')
// The type entry is not part of the event itself
continue
}
event[key] = value
}
return { event, eventType }
}

export {
decodeCborInBase64,
encodeCborInBase64,
rawEventEntriesToEvent
}
export {
decodeCborInBase64,
encodeCborInBase64,
rawEventEntriesToEvent
}
31 changes: 9 additions & 22 deletions backend/test/observer.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { after, before, beforeEach, describe, it, mock } from 'node:test'
import { after, before, beforeEach, describe, it } from 'node:test'
import { createPgPool, migrateWithPgClient } from '@filecoin-station/deal-observer-db'
import { IpldSchema } from '../lib/rpc-service/ipld-schema.js'
import assert from 'assert'
import { claimTestEvent } from './test_data/claimEvent.js'
import { ActorEventFilter, LotusService } from '../lib/rpc-service/service.js'
import { GLIF_RPC } from '../lib/config.js'
import { CID } from 'multiformats'
import { chainHeadTestData } from './test_data/chainHead.js'
import { rawActorEventTestData } from './test_data/rawActorEvent.js'
import { base32 } from 'multiformats/bases/base32'
import { parseCIDs } from './utils.js'

describe('deal-observer-backend', () => {
Expand All @@ -24,14 +21,9 @@ describe('deal-observer-backend', () => {
})

describe('observeBuiltinActorEvents', () => {
let providerMock
beforeEach(async () => {
// TODO: reset DB
// await pgPool.query('DELETE FROM daily_reward_transfers')

providerMock = {
getBlockNumber: async () => 2000
}
})

// TODO - remove this placeholder and implement proper tests
Expand All @@ -54,25 +46,21 @@ describe('deal-observer-backend', () => {
})

describe('LotusService', () => {
let lotusService
let lotusService

before(async () => {
const make_rpc_request = async (method,params) => {
const makeRpcRequest = async (method, params) => {
switch (method) {
case 'Filecoin.ChainHead':
let result = parseCIDs(chainHeadTestData)
return parseCIDs(chainHeadTestData)
return parseCIDs(chainHeadTestData)
case 'Filecoin.GetActorEventsRaw':
let testData = parseCIDs(rawActorEventTestData)
let from = params[0].fromHeight
let to = params[0].toHeight
return testData.filter(e => e.height >= params[0].fromHeight && e.height <= params[0].toHeight)
return parseCIDs(rawActorEventTestData).filter(e => e.height >= params[0].fromHeight && e.height <= params[0].toHeight)
default:
console.error('Unknown method')
}
}
}

lotusService = await(new LotusService(make_rpc_request)).build()
lotusService = await (new LotusService(makeRpcRequest)).build()
})
it('test the retrieval of the chainHead', async () => {
const chainHead = await lotusService.getChainHead()
Expand All @@ -81,11 +69,10 @@ describe('deal-observer-backend', () => {
})

it('test the retrieval of rawActorEvents', async () => {
let schema = await (new IpldSchema().build())
const actorEvents = await lotusService.getActorEvents(new ActorEventFilter(4622129, 4622139, ["claim"]))
const actorEvents = await lotusService.getActorEvents(new ActorEventFilter(4622129, 4622139, ['claim']))
assert(actorEvents)
actorEvents.forEach(e => {
assert(e.height >= 4622129 && e.height <= 4622139)
assert(e.height >= 4622129 && e.height <= 4622139)
})
})
})
Expand Down
Loading

0 comments on commit c2ed0ad

Please sign in to comment.