Skip to content

Commit

Permalink
OG-134 Not ready yet is not informative (#498)
Browse files Browse the repository at this point in the history
* OG-134 Not ready yet is not informative

Also, some progress on OG-139 as logs are improved throughout the
relaying code (WIP)

1. Remove 'StateError' as a way to bail out when not ready.
2. Create 'AmountRequired' to maybe save on suplicate logic piece
(meeting some ETH value criteria)
  • Loading branch information
forshtat authored Sep 27, 2020
1 parent 0ecdccc commit 1fbbf47
Show file tree
Hide file tree
Showing 21 changed files with 1,808 additions and 1,262 deletions.
5 changes: 2 additions & 3 deletions dockers/jsrelay/config/gsn-relay-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"pctRelayFee": 0,
"port": 8090,
"relayHubAddress": "0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B",
"gasPricePercent": 10,
"gasPriceFactor": 1.1,
"ethereumNodeUrl": "http://host.docker.internal:8545",
"devMode": true,
"debug": false
"devMode": false
}
5 changes: 2 additions & 3 deletions dockers/relaydc/config-sample/gsn-relay-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"baseRelayFee": 0,
"pctRelayFee": 70,
"versionRegistryAddress": "<deployed version registry>",
"gasPricePercent": 1,
"ethereumNodeUrl": "https://kovan.infura.io/v3/<INFURA-ID>",
"debug": false
"gasPriceFactor": 1,
"ethereumNodeUrl": "https://kovan.infura.io/v3/<INFURA-ID>"
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"bn.js": "5.1.2",
"body-parser": "1.19.0",
"chai": "4.2.0",
"chalk" : "4.1.0",
"commander": "5.1.0",
"console-read-write": "^0.1.1",
"date-format": "^3.0.0",
Expand All @@ -81,9 +82,11 @@
"ethereumjs-tx": "2.1.2",
"ethereumjs-util": "6.2.0",
"ethereumjs-wallet": "0.6.3",
"ethval" : "2.1.1",
"express": "4.17.1",
"jsonrpc-lite": "2.1.0",
"lodash": "4.17.19",
"loglevel": "1.7.0",
"minimist": "1.2.5",
"nedb-async": "0.1.3",
"ow": "0.17.0",
Expand Down
71 changes: 71 additions & 0 deletions src/common/AmountRequired.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// @ts-ignore
import EthVal from 'ethval'
import log from 'loglevel'
import { toBN } from 'web3-utils'
import { boolString } from './Utils'

export class AmountRequired {
_name: string
_currentValue = toBN(0)
_requiredValue = toBN(0)
_listener?: () => void

constructor (name: string, requiredValue: BN, listener?: () => void) {
this._name = name
this._requiredValue = requiredValue
this._listener = listener
}

get currentValue (): BN {
return this._currentValue
}

set currentValue (newValue: BN) {
const didChange = !this._currentValue.eq(newValue)
const wasSatisfied = this.isSatisfied
this._currentValue = newValue
if (didChange) {
this._onChange(wasSatisfied)
}
}

get requiredValue (): BN {
return this._requiredValue
}

set requiredValue (newValue: BN) {
const didChange = !this._requiredValue.eq(newValue)
const wasSatisfied = this.isSatisfied
this._requiredValue = newValue
if (didChange) {
this._onChange(wasSatisfied)
}
}

_onChange (wasSatisfied: boolean): void {
let changeString
if (wasSatisfied === this.isSatisfied) {
changeString = `still${this.isSatisfied ? '' : ' not'}`
} else if (this.isSatisfied) {
changeString = 'now'
} else {
changeString = 'no longer'
}
const message = `${this._name} requirement is ${changeString} satisfied\n${this.description}`
log.warn(message)
if (this._listener != null) {
this._listener()
}
}

get isSatisfied (): boolean {
return this._currentValue.gte(this._requiredValue)
}

get description (): string {
const status = boolString(this.isSatisfied)
const actual: string = new EthVal(this._currentValue).toEth().toFixed(4)
const required: string = new EthVal(this._requiredValue).toEth().toFixed(4)
return `${this._name.padEnd(14)} | ${status.padEnd(14)} | actual: ${actual.padStart(12)} ETH | required: ${required.padStart(12)} ETH`
}
}
12 changes: 12 additions & 0 deletions src/common/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Address } from '../relayclient/types/Aliases'
import { ServerConfigParams } from '../relayserver/ServerConfigParams'

import TypedRequestData from './EIP712/TypedRequestData'
import chalk from 'chalk'

export function removeHexPrefix (hex: string): string {
if (hex == null || typeof hex.replace !== 'function') {
Expand Down Expand Up @@ -166,6 +167,13 @@ export function randomInRange (min: number, max: number): number {
return Math.floor(Math.random() * (max - min) + min)
}

export function isSecondEventLater (a: EventData, b: EventData): boolean {
if (a.blockNumber === b.blockNumber) {
return b.transactionIndex > a.transactionIndex
}
return b.blockNumber > a.blockNumber
}

export function getLatestEventData (events: EventData[]): EventData | undefined {
if (events.length === 0) {
return
Expand Down Expand Up @@ -212,3 +220,7 @@ interface Signature {
r: number[]
s: number[]
}

export function boolString (bool: boolean): string {
return bool ? chalk.green('good'.padEnd(14)) : chalk.red('wrong'.padEnd(14))
}
4 changes: 2 additions & 2 deletions src/relayclient/GsnTestEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ class GsnTestEnvironmentClass {
gasPriceFactor: 1,
baseRelayFee: '0',
pctRelayFee: 0,
devMode: true,
logLevel: 1
checkInterval: 10,
logLevel: 5
}
const backend = new RelayServer(relayServerParams, relayServerDependencies)
await backend.init()
Expand Down
9 changes: 6 additions & 3 deletions src/relayserver/HttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import bodyParser from 'body-parser'
import cors from 'cors'
import { RelayServer } from './RelayServer'
import { Server } from 'http'
import log from 'loglevel'

export class HttpServer {
app: Express
Expand All @@ -16,7 +17,6 @@ export class HttpServer {
this.app.use(bodyParser.urlencoded({ extended: false }))
this.app.use(bodyParser.json())

console.log('setting handlers')
this.app.post('/', this.rootHandler.bind(this))
// TODO change all to jsonrpc
this.app.post('/getaddr', this.pingHandler.bind(this))
Expand All @@ -31,13 +31,16 @@ export class HttpServer {
if (this.serverInstance === undefined) {
this.serverInstance = this.app.listen(this.port, () => {
console.log('Listening on port', this.port)
this.startBackend()
})
}
}

startBackend (): void {
try {
this.backend.start()
console.log('Relay worker started.')
} catch (e) {
console.log('relay task error', e)
log.error('relay task error', e)
}
}

Expand Down
Loading

0 comments on commit 1fbbf47

Please sign in to comment.