Skip to content

Commit

Permalink
Merge pull request #87 from Zondax/dev
Browse files Browse the repository at this point in the history
New Release
  • Loading branch information
emmanuelm41 authored Jul 4, 2024
2 parents d79fc5b + fadeb78 commit bf5daab
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 32 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@types/body-parser": "^1",
"@types/express": "^4",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"@types/node": "^20.14.9",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^7.9.0",
"axios": "^1.6.8",
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.lock

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

4 changes: 2 additions & 2 deletions src/handlers/nodeMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { ChainConfig } from '../utils/types'

export const nodeMetadata = async (req: Request, res: Response) => {
const chains = getChains()
const { id: chainId }: ChainConfig = req.body
const { id: chainId = '' }: ChainConfig = req.body

const chain = chains.find((b: Chain) => b.id === chainId)
const chain = chains.find((b: Chain) => b.id.toLowerCase() === chainId.toLowerCase())
if (!chain) {
renderChainNotFoundError(res)
return
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/nodeMetadataFlush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { ChainConfig } from '../utils/types'

export const nodeMetadataFlush = (req: Request, res: Response) => {
const chains = getChains()
const { id: chainId }: ChainConfig = req.body
const { id: chainId = '' }: ChainConfig = req.body

const chain = chains.find((b: Chain) => b.id === chainId)
const chain = chains.find((b: Chain) => b.id.toLowerCase() === chainId.toLowerCase())
if (!chain) {
renderChainNotFoundError(res)
return
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/nodeMetadataHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { ChainConfig } from '../utils/types'

export const nodeMetadataHash = async (req: Request, res: Response) => {
const chains = getChains()
const { id: chainId }: ChainConfig = req.body
const { id: chainId = '' }: ChainConfig = req.body

const chain = chains.find((b: Chain) => b.id === chainId)
const chain = chains.find((b: Chain) => b.id.toLowerCase() === chainId.toLowerCase())
if (!chain) {
renderChainNotFoundError(res)
return
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/nodeProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { ChainConfig } from '../utils/types'
export const nodeProps = async (req: Request, res: Response) => {
const chains = getChains()

const { id: chainId }: ChainConfig = req.body
const { id: chainId = '' }: ChainConfig = req.body

const chain = chains.find((b: Chain) => b.id === chainId)
const chain = chains.find((b: Chain) => b.id.toLowerCase() === chainId.toLowerCase())
if (!chain) {
renderChainNotFoundError(res)
return
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/transactionMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
renderGetMetadataFirstError,
renderInternalError,
renderMissingTxBlobError,
renderShortenerMetadataError
renderShortenerMetadataError,
} from '../utils/errors'
import { cacheMetadata } from '../utils/metadata'
import { getShortMetadataFromTxBlob } from '../../rust'
Expand All @@ -17,11 +17,11 @@ export const transactionMetadata = async (req: Request, res: Response) => {
const chains = getChains()

const {
chain: { id: chainId },
chain: { id: chainId = '' },
}: TxToSign = req.body
let { txBlob }: TxToSign = req.body

const chain = chains.find((b: Chain) => b.id === chainId)
const chain = chains.find((b: Chain) => b.id.toLowerCase() === chainId.toLowerCase())
if (!chain) {
renderChainNotFoundError(res)
return
Expand Down Expand Up @@ -55,7 +55,7 @@ export const transactionMetadata = async (req: Request, res: Response) => {

const result = getShortMetadataFromTxBlob({ txBlob, metadata: metadataHex, props })

if(!validHex.test(result)){
if (!validHex.test(result)) {
renderShortenerMetadataError(res, result)
return
}
Expand Down
7 changes: 6 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express'
import bodyParser from 'body-parser'
import http from 'http'

import { getChains } from './utils/chains'
import { getChains, reloadChainsRegularly } from './utils/chains'
import { transactionMetadata } from './handlers/transactionMetadata'
import { nodeProps } from './handlers/nodeProps'
import { nodeMetadataHash } from './handlers/nodeMetadataHash'
Expand All @@ -12,6 +12,7 @@ import { chains } from './handlers/chains'

export function createAndServe() {
getChains()
const reloadChainsTimeout = reloadChainsRegularly()

// Create a new express application instance
const app: express.Application = express()
Expand All @@ -31,5 +32,9 @@ export function createAndServe() {
console.log('Server running on http://0.0.0.0:3001/')
})

httpServer.on('close', () => {
clearInterval(reloadChainsTimeout)
})

return httpServer
}
35 changes: 35 additions & 0 deletions src/utils/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs'
import type { MetadataV15 } from '@polkadot/types/interfaces/metadata'

import type { ChainProps } from '../types'
import { cacheMetadata } from './metadata'

export type Chain = {
id: string
Expand All @@ -15,6 +16,10 @@ export type Chain = {

export type ChainsFile = { chains: Chain[] }

const reloadChainMetadataInterval = 60 * 60 * 1000
let reloadChainTimer: NodeJS.Timeout | undefined
let reloadChainsInProgress = false

let chainsFile: ChainsFile | undefined

export const getChains = (): Chain[] => {
Expand All @@ -28,6 +33,36 @@ export const getChains = (): Chain[] => {
return chainsFile.chains
}

export function reloadChainsRegularly() {
const fn = async () => {
if (reloadChainsInProgress) {
return
}

reloadChainsInProgress = true
await reloadChains(getChains())
reloadChainsInProgress = false
}

if (!reloadChainTimer) {
reloadChainTimer = setInterval(fn, reloadChainMetadataInterval)
}

return reloadChainTimer
}

async function reloadChains(chains: Chain[]): Promise<void> {
for (const chain of chains) {
try {
console.log('reloading chain metadata from chain: ', chain.id)
await cacheMetadata(chain)
console.log('chain metadata reloaded from chain: ', chain.id)
} catch (e) {
console.log('error reloading chain metadata from chain: ', chain.id)
}
}
}

function loadChains(filePath: string) {
const file = fs.readFileSync(filePath, 'utf8')
const yamlFile = yaml.parse(file)
Expand Down
Loading

0 comments on commit bf5daab

Please sign in to comment.