Skip to content

Commit

Permalink
Support multiple archive nodes
Browse files Browse the repository at this point in the history
This adds support for multiple archive nodes, which is necessary to support a chain over multiple hardforks.
  • Loading branch information
peachbits committed Aug 5, 2024
1 parent e1e3839 commit 163a0bc
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- added: (Cosmos) Add chain ID updater
- added: (Cosmos) Support multiple archive nodes

## 4.17.1 (2024-07-30)

Expand Down
77 changes: 45 additions & 32 deletions src/cosmos/CosmosEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,38 +520,50 @@ export class CosmosEngine extends CurrencyEngine<
]

for (const query of txQueryStrings) {
let startingPage = 1
let clients = this.getClients()

const clientsList: CosmosClients[] = []
if (
this.networkInfo.archiveNode != null &&
this.networkInfo.archiveNodes != null &&
Date.now() - TWO_WEEKS > this.otherData.archivedTxLastCheckTime
) {
startingPage = this.otherData[query].lastPage
clients = await createCosmosClients(
this.fetchCors,
rpcWithApiKey(this.networkInfo.archiveNode, this.tools.initOptions)
const sortedArchiveNodes = this.networkInfo.archiveNodes.sort(
(a, b) =>
a.blockTimeRangeSeconds.start - b.blockTimeRangeSeconds.start
)
for (const node of sortedArchiveNodes) {
if (
node.blockTimeRangeSeconds.end == null ||
node.blockTimeRangeSeconds.end >
this.otherData.archivedTxLastCheckTime
) {
const archiveClients = await createCosmosClients(
this.fetchCors,
rpcWithApiKey(node.endpoint, this.tools.initOptions)
)
clientsList.push(archiveClients)
}
}
}
clientsList.push(this.getClients())

const { newestTxid, lastPage } = await this.queryTransactionsInner(
query,
clients,
startingPage
)
if (
(newestTxid != null &&
this.otherData[query]?.newestTxid !== newestTxid) ||
lastPage !== this.otherData[query]?.lastPage
) {
this.otherData[query] = { newestTxid, lastPage }
this.walletLocalDataDirty = true
for (const clients of clientsList) {
const { newestTxid, lastTimestamp } = await this.queryTransactionsInner(
query,
clients
)
if (
newestTxid != null &&
this.otherData[query]?.newestTxid !== newestTxid
) {
this.otherData[query] = { newestTxid }
this.otherData.archivedTxLastCheckTime = lastTimestamp
this.walletLocalDataDirty = true
}
progress += 0.5 / clientsList.length
allCurrencyCodes.forEach(
code => (this.tokenCheckTransactionsStatus[code] = progress)
)
this.updateOnAddressesChecked()
}
progress += 0.5
allCurrencyCodes.forEach(
code => (this.tokenCheckTransactionsStatus[code] = progress)
)
this.updateOnAddressesChecked()
}
this.otherData.archivedTxLastCheckTime = Date.now()

Expand All @@ -565,16 +577,16 @@ export class CosmosEngine extends CurrencyEngine<

async queryTransactionsInner(
queryString: typeof txQueryStrings[number],
clients: CosmosClients,
startingPage: number
): Promise<{ newestTxid: string | undefined; lastPage: number }> {
clients: CosmosClients
): Promise<{ newestTxid: string | undefined; lastTimestamp: number }> {
const txSearchParams = {
query: `${queryString}='${this.walletInfo.keys.bech32Address}'`,
per_page: TXS_PER_PAGE, // sdk default 50
order_by: 'asc'
}
let newestTxid: string | undefined
let page = startingPage
let lastTimestamp = 0
let page = 1
do {
try {
const { totalCount, txs } = await clients.cometClient.txSearch({
Expand Down Expand Up @@ -637,7 +649,8 @@ export class CosmosEngine extends CurrencyEngine<
})

newestTxid = txidHex
this.otherData[queryString] = { newestTxid: txidHex, lastPage: page }
this.otherData[queryString] = { newestTxid: txidHex }
lastTimestamp = date * 1000
this.walletLocalDataDirty = true
}

Expand All @@ -657,11 +670,11 @@ export class CosmosEngine extends CurrencyEngine<
}

page++
this.otherData[queryString] = { newestTxid, lastPage: page }
this.otherData[queryString] = { newestTxid }
this.walletLocalDataDirty = true
} while (true)

return { newestTxid, lastPage: page }
return { newestTxid, lastTimestamp }
}

processCosmosTransaction(
Expand Down
20 changes: 12 additions & 8 deletions src/cosmos/cosmosTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ export interface CosmosNetworkInfo {
nativeDenom: string
pluginMnemonicKeyName: string
rpcNode: HttpEndpoint
archiveNode?: HttpEndpoint // If no archive node, the rpc node will be used and only grab transaction in a limited range
// If no archive node, the rpc node will be used and only grab transaction in a limited range
archiveNodes?: Array<{
blockTimeRangeSeconds: {
start: number
end?: number
}
endpoint: HttpEndpoint
}>
}
const asHttpEndpoint = asObject({
url: asString,
Expand All @@ -92,19 +99,16 @@ export const txQueryStrings = [
] as const

const asLocalTxQueryParams = asObject({
newestTxid: asMaybe(asString),
lastPage: asMaybe(asNumber, 1)
newestTxid: asMaybe(asString)
})

export const asCosmosWalletOtherData = asObject({
archivedTxLastCheckTime: asMaybe(asNumber, 0),
'coin_spent.spender': asMaybe(asLocalTxQueryParams, () => ({
newestTxid: undefined,
lastPage: 1
newestTxid: undefined
})),
'coin_received.receiver': asMaybe(asLocalTxQueryParams, () => ({
newestTxid: undefined,
lastPage: 1
newestTxid: undefined
}))
})
export type CosmosWalletOtherData = ReturnType<typeof asCosmosWalletOtherData>
Expand Down Expand Up @@ -274,7 +278,7 @@ export interface IbcChannel {

export const asCosmosInfoPayload = asObject({
rpcNode: asOptional(asHttpEndpoint),
archiveNode: asOptional(asHttpEndpoint)
archiveNodes: asOptional(asHttpEndpoint)
})
export type CosmosInfoPayload = ReturnType<typeof asCosmosInfoPayload>

Expand Down
15 changes: 11 additions & 4 deletions src/cosmos/info/axelarInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ const networkInfo: CosmosNetworkInfo = {
url: 'https://axelar-rpc.publicnode.com:443',
headers: {}
},
archiveNode: {
url: 'https://axelararchive-rpc.quickapi.com:443',
headers: {}
}
archiveNodes: [
{
blockTimeRangeSeconds: {
start: 0
},
endpoint: {
url: 'https://axelararchive-rpc.quickapi.com:443',
headers: {}
}
}
]
}

const currencyInfo: EdgeCurrencyInfo = {
Expand Down
15 changes: 11 additions & 4 deletions src/cosmos/info/coreumInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,17 @@ const networkInfo: CosmosNetworkInfo = {
url: 'https://full-node.mainnet-1.coreum.dev:26657',
headers: {}
},
archiveNode: {
url: 'https://full-node.mainnet-1.coreum.dev:26657',
headers: {}
}
archiveNodes: [
{
blockTimeRangeSeconds: {
start: 0
},
endpoint: {
url: 'https://full-node.mainnet-1.coreum.dev:26657',
headers: {}
}
}
]
}

const currencyInfo: EdgeCurrencyInfo = {
Expand Down
15 changes: 11 additions & 4 deletions src/cosmos/info/osmosisInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ const networkInfo: CosmosNetworkInfo = {
url: 'https://rpc.osmosis.zone:443',
headers: {}
},
archiveNode: {
url: 'https://osmosisarchive-rpc.quickapi.com:443',
headers: {}
}
archiveNodes: [
{
blockTimeRangeSeconds: {
start: 0
},
endpoint: {
url: 'https://osmosisarchive-rpc.quickapi.com:443',
headers: {}
}
}
]
}

const currencyInfo: EdgeCurrencyInfo = {
Expand Down
25 changes: 21 additions & 4 deletions src/cosmos/info/thorchainruneInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,27 @@ const networkInfo: CosmosNetworkInfo = {
url: 'https://rpc.ninerealms.com',
headers: { 'x-client-id': '{{ninerealmsClientId}}' }
},
archiveNode: {
url: 'https://rpc-v1.ninerealms.com',
headers: { 'x-client-id': '{{ninerealmsClientId}}' }
}
archiveNodes: [
{
blockTimeRangeSeconds: {
start: 1647912564649 // 2022-03-22T01:29:24.649Z
// end: TBD
},
endpoint: {
url: 'https://rpc-v1.ninerealms.com',
headers: { 'x-client-id': '{{ninerealmsClientId}}' }
}
}
// {
// blockTimeRangeSeconds: {
// start: TBD
// },
// endpoint: {
// url: 'https://rpc-v2.ninerealms.com',
// headers: { 'x-client-id': '{{ninerealmsClientId}}' }
// }
// }
]
}

const currencyInfo: EdgeCurrencyInfo = {
Expand Down

0 comments on commit 163a0bc

Please sign in to comment.