Skip to content

Commit

Permalink
Fix #2128
Browse files Browse the repository at this point in the history
* Add check for OP_CONTRACT on handleEntry for efficiency
* Add existing contracts to channels on startup
  • Loading branch information
corrideat committed Jun 28, 2024
1 parent 5da85fd commit dc72e0c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 21 additions & 12 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
'use strict'

import sbp from '@sbp/sbp'
import Hapi from '@hapi/hapi'
import sbp from '@sbp/sbp'
import chalk from 'chalk'
import '~/shared/domains/chelonia/chelonia.js'
import { SERVER } from '~/shared/domains/chelonia/presets.js'
import initDB from './database.js'
import { SERVER_RUNNING } from './events.js'
import { SERVER_INSTANCE, PUBSUB_INSTANCE } from './instance-keys.js'
import { PUBSUB_INSTANCE, SERVER_INSTANCE } from './instance-keys.js'
import {
NOTIFICATION_TYPE,
REQUEST_TYPE,
createKvMessage,
createMessage,
createPushErrorResponse,
createNotification,
createKvMessage,
createServer,
NOTIFICATION_TYPE,
REQUEST_TYPE
createPushErrorResponse,
createServer
} from './pubsub.js'
import { pushServerActionhandlers } from './push.js'
import chalk from 'chalk'
import '~/shared/domains/chelonia/chelonia.js'
import { SERVER } from '~/shared/domains/chelonia/presets.js'
// $FlowFixMe[cannot-resolve-module]
import { webcrypto } from 'node:crypto'
import { GIMessage } from '~/shared/domains/chelonia/GIMessage.js'

const { CONTRACTS_VERSION, GI_VERSION } = process.env

Expand Down Expand Up @@ -136,7 +137,9 @@ sbp('sbp/selectors/register', {
},
'backend/server/handleEntry': async function (deserializedHEAD: Object, entry: string) {
const contractID = deserializedHEAD.contractID
sbp('okTurtles.data/get', PUBSUB_INSTANCE).channels.add(contractID)
if (deserializedHEAD.head.op === GIMessage.OP_CONTRACT) {
sbp('okTurtles.data/get', PUBSUB_INSTANCE).channels.add(contractID)
}
await sbp('chelonia/private/in/enqueueHandleEvent', contractID, entry)
// Persist the Chelonia state after processing a message
await sbp('backend/server/persistState', deserializedHEAD, entry)
Expand Down Expand Up @@ -241,12 +244,18 @@ sbp('okTurtles.data/set', PUBSUB_INSTANCE, createServer(hapi.listener, {
// partition
const recoveredState = Object.create(null)
recoveredState.contracts = Object.create(null)
const channels = sbp('okTurtles.data/get', PUBSUB_INSTANCE).channels
await Promise.all(savedStateIndex.split('\x00').map(async (contractID) => {
const cpSerialized = await sbp('chelonia/db/get', `_private_cheloniaState_${contractID}`)
if (!cpSerialized) return
if (!cpSerialized) {
console.warn('[chelonia] Contract in index but missing state', contractID)
return
}
const cp = JSON.parse(cpSerialized)
recoveredState[contractID] = cp.contractState
recoveredState.contracts[contractID] = cp.cheloniaContractInfo
// Add existing contract IDs to the list of channels
channels.add(contractID)
}))
Object.assign(sbp('chelonia/rootState'), recoveredState)
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc72e0c

Please sign in to comment.