Skip to content

Commit

Permalink
feat: add amendments status backtracking (#223)
Browse files Browse the repository at this point in the history
## High Level Overview of Change

<!--
Please include a summary/list of the changes.
If too broad, please consider splitting into multiple PRs.
If a relevant Asana task, please link it here.
-->

Currently we are using `p2p.livenet.ripple.com` to retrieve amendments
enablement via ledger + 1 for mainnet. However, its connection is very
inconsistent, led to missing information.

We should backtrack every 30 minutes using clio for mainnet to retrieve
this information in case the connections get lost. We would also
backtrack major networks like testnet and devnet to ensure we have
adequate data for these networks.

### Type of Change

<!--
Please check relevant options, delete irrelevant ones.
-->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Refactor (non-breaking change that only restructures code)
- [ ] Tests (You added tests for code that already exists, or your new
feature included in this PR)
- [ ] Documentation Updates
- [ ] Release

## Test Plan

<!--
Please describe the tests that you ran to verify your changes and
provide instructions so that others can reproduce.
-->

Ran without breaking on staging
  • Loading branch information
pdp2121 authored Apr 2, 2024
1 parent 5a1f60e commit baaf0f8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/connection-manager/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { FeeVote, LedgerResponseCorrected } from '../shared/types'
import logger from '../shared/utils/logger'

import {
backtrackAmendmentStatus,
getAmendmentLedgerEntry,
handleWsMessageLedgerEnableAmendments,
handleWsMessageLedgerEntryAmendments,
Expand All @@ -28,6 +29,7 @@ const networkFee: Map<string, FeeVote> = new Map()
const CM_INTERVAL = 60 * 60 * 1000
const WS_TIMEOUT = 10000
const REPORTING_INTERVAL = 15 * 60 * 1000
const BACKTRACK_INTERVAL = 30 * 60 * 1000

// The frequent closing codes seen so far after connections established include:
// 1008: Policy error: client is too slow. (Most frequent)
Expand Down Expand Up @@ -243,8 +245,14 @@ export default async function startConnections(): Promise<void> {
await fetchAmendmentInfo()
await clearConnectionsDb()
await createConnections()
await backtrackAmendmentStatus()
setInterval(() => {
void fetchAmendmentInfo()
void createConnections()
}, CM_INTERVAL)

setInterval(() => {
void backtrackAmendmentStatus()
}, BACKTRACK_INTERVAL)
}
}
78 changes: 77 additions & 1 deletion src/connection-manager/wsHandling.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* eslint-disable import/max-dependencies -- Disbale for this file which uses a lot of types. */
import WebSocket from 'ws'
import { LedgerEntryResponse, rippleTimeToUnixTime } from 'xrpl'
import {
Client,
LedgerEntryResponse,
LedgerResponse,
rippleTimeToUnixTime,
} from 'xrpl'
import { AMENDMENTS_ID } from 'xrpl/dist/npm/models/ledger'

import {
Expand All @@ -27,6 +32,11 @@ const LEDGER_HASHES_SIZE = 10
const GOT_MAJORITY_FLAG = 65536
const LOST_MAJORITY_FLAG = 131072
const FOURTEEN_DAYS_IN_MILLISECONDS = 14 * 24 * 60 * 60 * 1000
const NETWORKS_HOSTS = new Map([
['main', 'ws://s2.ripple.com:51233'],
['test', 'wss://s.altnet.rippletest.net:51233'],
['dev', 'wss://s.devnet.rippletest.net:51233'],
])

const log = logger({ name: 'connections' })

Expand Down Expand Up @@ -225,3 +235,69 @@ export async function handleWsMessageLedgerEnableAmendments(
}),
)
}

/**
* Backtracking a network to update amendment status in case of Websocket disconnection.
*
* @param network - The network being tracked.
* @param url - The Faucet URL of the network.
* @returns Void.
*/
async function backtrackNetworkAmendmentStatus(
network: string,
url: string,
): Promise<void> {
try {
log.info(`Backtracking to update amendment status for ${network}...`)
const client = new Client(url)
await client.connect()
const ledgerResponse: LedgerResponse = await client.request({
command: 'ledger',
ledger_index: 'validated',
})
const currentLedger = ledgerResponse.result.ledger_index

// a flag + 1 ledger typically comes in every 10 to 15 minutes.
const fourFlagPlusOneLedgerBefore =
(Math.floor(currentLedger / 256) - 3) * 256 + 1

for (
let index = fourFlagPlusOneLedgerBefore;
index < currentLedger;
index += 256
) {
const ledger: LedgerResponse = await client.request({
command: 'ledger',
transactions: true,
ledger_index: index,
expand: true,
})

await handleWsMessageLedgerEnableAmendments(
ledger as LedgerResponseCorrected,
network,
)
}

await client.disconnect()

log.info(`Finished backtracked amendment status for ${network}...`)
} catch (error) {
log.error(
`Failed to backtrack amendment status for ${network} due to error: ${String(
error,
)}`,
)
}
}

/**
* Backtrack amendment status periodically to ensure changes are captured.
*
* @returns Void.
*/
export async function backtrackAmendmentStatus(): Promise<void> {
for (const [networks, url] of NETWORKS_HOSTS) {
await backtrackNetworkAmendmentStatus(networks, url)
}
}

0 comments on commit baaf0f8

Please sign in to comment.