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

Proof of Reserves batch requests & burst limiting #449

Closed
wants to merge 13 commits into from

Conversation

justinkaseman
Copy link
Collaborator

@justinkaseman justinkaseman commented Apr 21, 2021

Description

Replaying the previous #276 as a hotfix PR into master due to priority.

This will need to be engineered into an actual burst rate limit feature on EAEE.

Next steps:
API_RATE_LIMIT will need to be added to Blockcypher's environment variables.
API_KEY and API_SECRET need to be added for BTC.com
BLOCKCYPHER_API_RATE_LIMIT will need to be added to blockcypher PoR adapter
BTC_COM_API_SECRET and BTC_COM_API_KEY will need to be added to BTC.com PoR adapter

Story

https://app.clubhouse.io/chainlinklabs/story/7356/wbtc-por-adapter-fix

@github-actions
Copy link
Contributor

I see that you haven't updated any CHANGELOG files. Would it make sense to do so?


const getBalanceURI = (address: string, confirmations: number) =>
`/q/addressbalance/${address}?confirmations=${confirmations}`
const getBalanceURI = (addresses: string[]) => `balance?active=${addresses.join(',')}`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const getBalanceURI = (addresses: string[]) => `balance?active=${addresses.join(',')}`
const getBalanceURL = (addresses: string[]) => `balance?active=${addresses.join(',')}`
Suggested change
const getBalanceURI = (addresses: string[]) => `balance?active=${addresses.join(',')}`
const getBalanceURI = (addresses: string[]) => `balance?active=${addresses.join(',')}`

*
* @returns 2d array
*/
export const chunk = (amount: number, data: any[]) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use rxjs for this

return from(data).pipe(bufferCount(amount), toArray())

* @returns array of response data
*/
export const throttle = async (amount: number, data: any[], exec: any) => {
const chunks = chunk(amount, data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be expressed via rxjs

const output = from(data).pipe(
bufferCount(amount),
delay(1000),  
mergeMap(
 datas => forkJoin(datas.map(d =>exec(d)))
),
toArray()
).toPromise()

return output.flat()

}

const response = await Requester.request(reqConfig)
const response: any = await new Promise((resolve, reject) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use https://nodejs.org/api/util.html#util_util_promisify_original here, or https://rxjs-dev.firebaseapp.com/api/index/function/bindNodeCallback

Comment on lines +104 to +106
const requests = groups.flatMap((group) => {
return getBalances(group as Account[], config)
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about one-lining this?

Suggested change
const requests = groups.flatMap((group) => {
return getBalances(group as Account[], config)
})
const requests = groups.flatMap((group) =>
getBalances(group as Account[], config)
)
Suggested change
const requests = groups.flatMap((group) => {
return getBalances(group as Account[], config)
})
const requests = groups.flatMap((group) => {
return getBalances(group as Account[], config)
})

api.getAddrBal(account.address, params, (error: Error, body: AddressBalance) =>
error ? reject(error) : resolve(body),

const _getAddrBal = (addrs: string[]) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Underscores mean unused in JS-land (by linters and tsc too), we shouldnt be using it as an internal function naming convention. Best to keep regular camel casing.

? await util.throttle(config.ratelimit, addresses, _getAddrBal)
: await _getAddrBal(addresses)

const addrLookup: { [key: string]: AddressBalance } = {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const addrLookup: { [key: string]: AddressBalance } = {}
const addrLookup: Record<string,AddressBalance> = {}
Suggested change
const addrLookup: { [key: string]: AddressBalance } = {}
const addrLookup: { [key: string]: AddressBalance } = {}

const api = new bcypher(account.coin, chainId, config.apiKey)
const getBalances: balance.GetBalances<Config> = async (accounts, config) => {
const addresses = accounts.map((a) => a.address)
const { coin, chain } = accounts[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it always the same coin/chain for all accounts? We should note that if so

export const makeConfig = (prefix = ''): Config => {
const config = Requester.getDefaultConfig(prefix)
const ratelimit = util.getEnv(ENV_RATE_LIMIT, prefix)
if (ratelimit) config.ratelimit = Number(ratelimit)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (ratelimit) config.ratelimit = Number(ratelimit)
config.ratelimit &&= Number(ratelimit)
Suggested change
if (ratelimit) config.ratelimit = Number(ratelimit)
if (ratelimit) config.ratelimit = Number(ratelimit)

@justinkaseman
Copy link
Collaborator Author

Closing. This PR is very outdated. The burst limiter middle-ware and batch warmer have made this change obsolete.

@austinborn austinborn deleted the feature/PoR-burst-rate-limit branch November 4, 2021 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants