Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle components refactor #26

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion DEMO.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,15 @@ container for listening GET requests
- `ADDRESS` - Ethereum address of a validator.
- After enough votes are collected, validator is added into
the next validators list for the next epoch.
- http://localhost:5001/vote/addValidator/ADDRESS
- http://localhost:5001/vote/removeValidator/ADDRESS
- `ADDRESS` - Ethereum address of a validator.
- After enough votes are collected, validator is removed from
the next validators list for the next epoch.
- http://localhost:5001/vote/changeThreshold/THRESHOLD
- `THRESHOLD` - Number. New threshold value.
- After enough votes are collected, new threshold is set for next epoch.
- http://localhost:5001/vote/changeCloseEpoch/CLOSE_EPOCH
- `CLOSE_EPOCH` - Boolean. Next epoch close epoch policy
(If true, next validators set will first disable binance account for previous
epoch, before moving onto a new one).
- After enough votes are collected, new close policy is set for the next epoch.
7 changes: 4 additions & 3 deletions src/oracle/bncWatcher/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ FROM node:10.16.0-alpine
WORKDIR /watcher

RUN apk update && \
apk add libssl1.1 libressl-dev curl
apk add libressl-dev

COPY ./bncWatcher/package.json /watcher/

RUN npm install

COPY ./bncWatcher/bncWatcher.js ./shared/db.js ./shared/logger.js ./shared/crypto.js ./shared/amqp.js ./shared/wait.js /watcher/
COPY ./bncWatcher/bncWatcher.js /watcher/src/
COPY ./shared/db.js ./shared/logger.js ./shared/crypto.js ./shared/amqp.js ./shared/wait.js ./shared/binanceClient.js /watcher/shared/

ENTRYPOINT ["node", "bncWatcher.js"]
ENTRYPOINT ["node", "src/bncWatcher.js"]
53 changes: 10 additions & 43 deletions src/oracle/bncWatcher/bncWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ const BN = require('bignumber.js')
const fs = require('fs')
const { computeAddress } = require('ethers').utils

const logger = require('./logger')
const redis = require('./db')
const { publicKeyToAddress } = require('./crypto')
const { delay, retry } = require('./wait')
const { connectRabbit, assertQueue } = require('./amqp')
const logger = require('../shared/logger')
const redis = require('../shared/db')
const { publicKeyToAddress } = require('../shared/crypto')
const { delay } = require('../shared/wait')
const { connectRabbit, assertQueue } = require('../shared/amqp')
const { getTx, getBlockTime, fetchNewTransactions } = require('../shared/binanceClient')

const {
FOREIGN_URL, PROXY_URL, FOREIGN_ASSET, RABBITMQ_URL
PROXY_URL, RABBITMQ_URL
} = process.env

const FOREIGN_FETCH_INTERVAL = parseInt(process.env.FOREIGN_FETCH_INTERVAL, 10)
const FOREIGN_FETCH_BLOCK_TIME_OFFSET = parseInt(process.env.FOREIGN_FETCH_BLOCK_TIME_OFFSET, 10)
const FOREIGN_FETCH_MAX_TIME_INTERVAL = parseInt(process.env.FOREIGN_FETCH_MAX_TIME_INTERVAL, 10)

const foreignHttpClient = axios.create({ baseURL: FOREIGN_URL })
const proxyHttpClient = axios.create({ baseURL: PROXY_URL })

let channel
Expand All @@ -33,45 +33,11 @@ function getForeignAddress(epoch) {
}
}

async function getTx(hash) {
const response = await retry(() => foreignHttpClient.get(
`/api/v1/tx/${hash}`,
{
params: {
format: 'json'
}
}
))
return response.data.tx.value
}

async function getBlockTime() {
const response = await retry(() => foreignHttpClient.get('/api/v1/time'))
return Date.parse(response.data.block_time) - FOREIGN_FETCH_BLOCK_TIME_OFFSET
}

async function fetchNewTransactions(address, startTime, endTime) {
logger.debug('Fetching new transactions')
const params = {
address,
side: 'RECEIVE',
txAsset: FOREIGN_ASSET,
txType: 'TRANSFER',
startTime,
endTime
}

logger.trace('Transactions fetch params %o', params)
return (
await retry(() => foreignHttpClient.get('/api/v1/transactions', { params }))
).data.tx
}

async function fetchTimeIntervalsQueue() {
let epoch = null
let startTime = null
let endTime = null
const lastBncBlockTime = await getBlockTime()
const lastBncBlockTime = await getBlockTime() - FOREIGN_FETCH_BLOCK_TIME_OFFSET
logger.trace(`Binance last block timestamp ${lastBncBlockTime}`)
while (true) {
const msg = await epochTimeIntervalsQueue.get()
Expand Down Expand Up @@ -160,7 +126,8 @@ async function loop() {
const publicKeyEncoded = (await getTx(tx.txHash)).signatures[0].pub_key.value
await proxyHttpClient.post('/transfer', {
to: computeAddress(Buffer.from(publicKeyEncoded, 'base64')),
value: new BN(tx.value).multipliedBy(10 ** 18).toString(16),
value: new BN(tx.value).multipliedBy(10 ** 18)
.toString(16),
hash: tx.txHash,
epoch
})
Expand Down
5 changes: 4 additions & 1 deletion src/oracle/bncWatcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
},
"engines": {
"node": ">=10.6.0"
}
},
"files": [
"../shared"
]
}


7 changes: 4 additions & 3 deletions src/oracle/ethWatcher/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ FROM node:10.16.0-alpine
WORKDIR /watcher

RUN apk update && \
apk add libssl1.1 libressl-dev curl
apk add libressl-dev

COPY ./ethWatcher/package.json /watcher/

RUN npm install

COPY ./ethWatcher/ethWatcher.js ./shared/db.js ./shared/logger.js ./shared/amqp.js ./shared/crypto.js ./shared/wait.js /watcher/
COPY ./ethWatcher/ethWatcher.js /watcher/src/
COPY ./shared/db.js ./shared/logger.js ./shared/amqp.js ./shared/crypto.js ./shared/wait.js /watcher/shared/

ENTRYPOINT ["node", "ethWatcher.js"]
ENTRYPOINT ["node", "src/ethWatcher.js"]
10 changes: 5 additions & 5 deletions src/oracle/ethWatcher/ethWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const ethers = require('ethers')
const BN = require('bignumber.js')
const axios = require('axios')

const logger = require('./logger')
const redis = require('./db')
const { connectRabbit, assertQueue } = require('./amqp')
const { publicKeyToAddress } = require('./crypto')
const { delay, retry } = require('./wait')
const logger = require('../shared/logger')
const redis = require('../shared/db')
const { connectRabbit, assertQueue } = require('../shared/amqp')
const { publicKeyToAddress } = require('../shared/crypto')
const { delay, retry } = require('../shared/wait')

const {
HOME_RPC_URL, HOME_BRIDGE_ADDRESS, RABBITMQ_URL, HOME_START_BLOCK, VALIDATOR_PRIVATE_KEY
Expand Down
5 changes: 4 additions & 1 deletion src/oracle/ethWatcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
},
"engines": {
"node": ">=10.6.0"
}
},
"files": [
"../shared"
]
}
5 changes: 3 additions & 2 deletions src/oracle/proxy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ COPY ./proxy/package.json /proxy/

RUN npm install

COPY ./proxy/index.js ./proxy/encode.js ./proxy/decode.js ./proxy/sendTx.js ./proxy/contractsAbi.js ./proxy/utils.js ./shared/logger.js ./shared/crypto.js ./shared/wait.js /proxy/
COPY ./proxy/index.js ./proxy/encode.js ./proxy/decode.js ./proxy/sendTx.js ./proxy/contractsAbi.js ./proxy/utils.js ./proxy/expressUtils.js /proxy/src/
COPY ./shared/logger.js ./shared/crypto.js ./shared/wait.js ./shared/binanceClient.js /proxy/shared/

ENTRYPOINT ["node", "index.js"]
ENTRYPOINT ["node", "src/index.js"]
2 changes: 1 addition & 1 deletion src/oracle/proxy/encode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const BN = require('bignumber.js')

const { padZeros } = require('./crypto')
const { padZeros } = require('../shared/crypto')

function makeBuffer(value, length = 32, base = 16) {
return Buffer.from(padZeros(new BN(value, base).toString(16), length * 2), 'hex')
Expand Down
63 changes: 63 additions & 0 deletions src/oracle/proxy/expressUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const ethers = require('ethers')

const logger = require('../shared/logger')

function parseNumber(fromQuery, field, defaultValue = null) {
return (req, res, next) => {
const source = fromQuery ? req.query : req.params
if (/^[0-9]+$/.test(source[field])) {
req[field] = parseInt(source[field], 10)
logger.trace(`Set req.${field} to ${req[field]}`)
next()
} else if (!source[field] && defaultValue !== null) {
req[field] = defaultValue
logger.trace(`Set req.${field} to ${defaultValue}`)
next()
} else {
res.status(400).end()
}
}
}

function parseAddress(field) {
return (req, res, next) => {
logger.debug(`${field} %o`, req.params)
if (ethers.utils.isHexString(req.params[field], 20)) {
req[field] = req.params[field]
logger.trace(`Set req.${field} to ${req[field]}`)
next()
} else {
res.status(400).end()
}
}
}

function parseBool(field) {
return (req, res, next) => {
if (req.params[field] === 'true' || req.params[field] === 'false') {
req[field] = req.params[field] === 'true'
logger.trace(`Set req.${field} to ${req[field]}`)
next()
} else {
res.status(400).end()
}
}
}

function logRequest(req, res, next) {
logger.debug(`${req.method} request to ${req.originalUrl}`)
if (req.query && Object.keys(req.query).length > 0) {
logger.trace('Requst query: %o', req.query)
}
if (req.body && Object.keys(req.body).length > 0) {
logger.trace('Requst body: %o', req.body)
}
next()
}

module.exports = {
parseNumber,
parseAddress,
parseBool,
logRequest
}
Loading