-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
425e2ed
commit d418451
Showing
2 changed files
with
296 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { Fail, makeError } from '@endo/errors'; | ||
import { E } from '@endo/far'; | ||
import { M } from '@endo/patterns'; | ||
|
||
import { VowShape } from '@agoric/vow'; | ||
// eslint-disable-next-line no-restricted-syntax | ||
import { heapVowTools } from '@agoric/vow/vat.js'; | ||
import { makeHeapZone } from '@agoric/zone'; | ||
import { CosmosChainInfoShape, IBCConnectionInfoShape } from '../typeGuards.js'; | ||
|
||
// FIXME test thoroughly whether heap suffices for ChainHub | ||
// eslint-disable-next-line no-restricted-syntax | ||
const { allVows, watch } = heapVowTools; | ||
|
||
/** | ||
* @import {NameHub} from '@agoric/vats'; | ||
* @import {Vow} from '@agoric/vow'; | ||
* @import {CosmosChainInfo, IBCConnectionInfo} from '../cosmos-api.js'; | ||
* @import {ChainInfo, KnownChains} from '../chain-info.js'; | ||
* @import {Remote} from '@agoric/internal'; | ||
* @import {Zone} from '@agoric/zone'; | ||
*/ | ||
|
||
/** | ||
* @template {string} K | ||
* @typedef {K extends keyof KnownChains | ||
* ? Omit<KnownChains[K], 'connections'> | ||
* : ChainInfo} ActualChainInfo | ||
*/ | ||
|
||
/** agoricNames key for ChainInfo hub */ | ||
export const CHAIN_KEY = 'chain'; | ||
/** namehub for connection info */ | ||
export const CONNECTIONS_KEY = 'chainConnection'; | ||
|
||
/** | ||
* Character used in a connection tuple key to separate the two chain ids. Valid | ||
* because a chainId can contain only alphanumerics and dash. | ||
* | ||
* Vstorage keys can be only alphanumerics, dash or underscore. That leaves | ||
* underscore as the only valid separator. | ||
* | ||
* @see {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md} | ||
*/ | ||
const CHAIN_ID_SEPARATOR = '_'; | ||
|
||
/** | ||
* The entries of the top-level namehubs in agoricNames are reflected to | ||
* vstorage. But only the top level. So we combine the 2 chain ids into 1 key. | ||
* Connections are directionless, so we sort the ids. | ||
* | ||
* @param {string} chainId1 | ||
* @param {string} chainId2 | ||
*/ | ||
export const connectionKey = (chainId1, chainId2) => { | ||
if ( | ||
chainId1.includes(CHAIN_ID_SEPARATOR) || | ||
chainId2.includes(CHAIN_ID_SEPARATOR) | ||
) { | ||
Fail`invalid chain id ${chainId1} or ${chainId2}`; | ||
} | ||
return [chainId1, chainId2].sort().join(CHAIN_ID_SEPARATOR); | ||
}; | ||
|
||
const ChainIdArgShape = M.or( | ||
M.string(), | ||
M.splitRecord( | ||
{ | ||
chainId: M.string(), | ||
}, | ||
undefined, | ||
M.any(), | ||
), | ||
); | ||
|
||
const ChainHubI = M.interface('ChainHub', { | ||
registerChain: M.call(M.string(), CosmosChainInfoShape).returns(), | ||
getChainInfo: M.call(M.string()).returns(VowShape), | ||
registerConnection: M.call( | ||
M.string(), | ||
M.string(), | ||
IBCConnectionInfoShape, | ||
).returns(), | ||
getConnectionInfo: M.call(ChainIdArgShape, ChainIdArgShape).returns(VowShape), | ||
getChainsAndConnection: M.call(M.string(), M.string()).returns(VowShape), | ||
}); | ||
|
||
/** | ||
* Make a new ChainHub in the zone (or in the heap if no zone is provided). | ||
* | ||
* The resulting object is an Exo singleton. It has no precious state. It's only | ||
* state is a cache of queries to agoricNames and whatever info was provided in | ||
* registration calls. When you need a newer version you can simply make a hub | ||
* hub and repeat the registrations. | ||
* | ||
* @param {Remote<NameHub>} agoricNames | ||
* @param {Zone} [zone] | ||
*/ | ||
export const makeChainHub = (agoricNames, zone = makeHeapZone()) => { | ||
/** @type {MapStore<string, CosmosChainInfo>} */ | ||
const chainInfos = zone.mapStore('chainInfos', { | ||
keyShape: M.string(), | ||
valueShape: CosmosChainInfoShape, | ||
}); | ||
/** @type {MapStore<string, IBCConnectionInfo>} */ | ||
const connectionInfos = zone.mapStore('connectionInfos', { | ||
keyShape: M.string(), | ||
valueShape: IBCConnectionInfoShape, | ||
}); | ||
|
||
const chainHub = zone.exo('ChainHub', ChainHubI, { | ||
/** | ||
* Register a new chain. The name will override a name in well known chain | ||
* names. | ||
* | ||
* If a durable zone was not provided, registration will not survive a | ||
* reincarnation of the vat. Then if the chain is not yet in the well known | ||
* names at that point, it will have to be registered again. In an unchanged | ||
* contract `start` the call will happen again naturally. | ||
* | ||
* @param {string} name | ||
* @param {CosmosChainInfo} chainInfo | ||
*/ | ||
registerChain(name, chainInfo) { | ||
chainInfos.init(name, chainInfo); | ||
}, | ||
/** | ||
* @template {string} K | ||
* @param {K} chainName | ||
* @returns {Vow<ActualChainInfo<K>>} | ||
*/ | ||
getChainInfo(chainName) { | ||
// Either from registerChain or memoized remote lookup() | ||
if (chainInfos.has(chainName)) { | ||
return /** @type {Vow<ActualChainInfo<K>>} */ ( | ||
watch(chainInfos.get(chainName)) | ||
); | ||
} | ||
|
||
return watch(E(agoricNames).lookup(CHAIN_KEY, chainName), { | ||
onFulfilled: chainInfo => { | ||
chainInfos.init(chainName, chainInfo); | ||
return chainInfo; | ||
}, | ||
onRejected: _cause => { | ||
throw makeError(`chain not found:${chainName}`); | ||
}, | ||
}); | ||
}, | ||
/** | ||
* @param {string} chainId1 | ||
* @param {string} chainId2 | ||
* @param {IBCConnectionInfo} connectionInfo | ||
*/ | ||
registerConnection(chainId1, chainId2, connectionInfo) { | ||
const key = connectionKey(chainId1, chainId2); | ||
connectionInfos.init(key, connectionInfo); | ||
}, | ||
|
||
/** | ||
* @param {string | { chainId: string }} chain1 | ||
* @param {string | { chainId: string }} chain2 | ||
* @returns {Vow<IBCConnectionInfo>} | ||
*/ | ||
cgetConnectionInfo(chain1, chain2) { | ||
const chainId1 = typeof chain1 === 'string' ? chain1 : chain1.chainId; | ||
const chainId2 = typeof chain2 === 'string' ? chain2 : chain2.chainId; | ||
const key = connectionKey(chainId1, chainId2); | ||
if (connectionInfos.has(key)) { | ||
return watch(connectionInfos.get(key)); | ||
} | ||
|
||
return watch(E(agoricNames).lookup(CONNECTIONS_KEY, key), { | ||
onFulfilled: connectionInfo => { | ||
connectionInfos.init(key, connectionInfo); | ||
return connectionInfo; | ||
}, | ||
onRejected: _cause => { | ||
throw makeError(`connection not found: ${chainId1}<->${chainId2}`); | ||
}, | ||
}); | ||
}, | ||
|
||
/** | ||
* @template {string} C1 | ||
* @template {string} C2 | ||
* @param {C1} chainName1 | ||
* @param {C2} chainName2 | ||
* @returns {Vow< | ||
* [ActualChainInfo<C1>, ActualChainInfo<C2>, IBCConnectionInfo] | ||
* >} | ||
*/ | ||
getChainsAndConnection(chainName1, chainName2) { | ||
return watch( | ||
allVows([ | ||
chainHub.getChainInfo(chainName1), | ||
chainHub.getChainInfo(chainName2), | ||
]), | ||
{ | ||
onFulfilled: ([chain1, chain2]) => { | ||
return watch(chainHub.getConnectionInfo(chain2, chain1), { | ||
onFulfilled: connectionInfo => { | ||
return [chain1, chain2, connectionInfo]; | ||
}, | ||
}); | ||
}, | ||
}, | ||
); | ||
}, | ||
}); | ||
|
||
return chainHub; | ||
}; | ||
/** @typedef {ReturnType<typeof makeChainHub>} ChainHub */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,100 @@ | ||
# Chain Hub | ||
# ChainHub | ||
|
||
This [code module](https://github.com/Agoric/agoric-sdk/blob/859d8c0d151ff6f686583db1eaf72efb89cc7648/packages/orchestration/src/exos/chain-hub.js#L99) is designed for managing chain and IBC connection information. It facilitate the registration and retrieval of chain and connection data. Let's break down the components and functionalities of this code. | ||
The [ChainHub API](https://github.com/Agoric/agoric-sdk/blob/859d8c0d151ff6f686583db1eaf72efb89cc7648/packages/orchestration/src/exos/chain-hub.js#L99) is responsible managing chain and IBC connection information. It facilitate the registration and retrieval of chain and connection data. | ||
|
||
The `makeChainHub` function accepts a NameHub reference (`agoricNames`) and an optional Zone for managing data durability. Inside the function two `MapStores` are created: | ||
```js | ||
const zone = makeDurableZone(baggage); | ||
const { agoricNames } = remotePowers; | ||
const chainHub = makeChainHub(agoricNames, zone); | ||
``` | ||
|
||
The `makeChainHub` function accepts a `Remote<NameHub>` reference (`agoricNames`) and an optional `Zone` for managing data durability. The `makeChainHub` fuction creates a new `ChainHub` either in the specified zone or in the heap if no zone is provided. The resulting object is an Exo singleton, which means it has no previous state. Its state consists only of a cache of queries to `agoricNames` and the information provided in registration calls. | ||
|
||
The `ChainHub` objects maintains two `MapStores`: | ||
|
||
- `chainInfos`: for storing `CosmosChainInfo` objects. | ||
- `connectionInfos`: for storing `IBCConnectionInfo` objects. | ||
|
||
These `MapStores` are not exposed directly. They are abstracted and used internally by the methods provided by the ChainHub. | ||
|
||
# ChainHub Interface | ||
|
||
The core functionality is encapsulated within the `makeChainHub` function, which sets up a new ChainHub in the given zone. The ChainHub is responsible for: | ||
|
||
- **Registering Chain Information (`registerChain`)**: Stores information about a blockchain inside the `chainInfos` mapstore, which can be used for quickly looking up details without querying a remote source. | ||
- **Registering Chain Information (`registerChain`)**: Stores information about a chain inside the `chainInfos` mapstore, which can be used for quickly looking up details without querying a remote source. | ||
|
||
```js | ||
const chainInfo = harden({ | ||
chainId: 'agoric-3', | ||
icaEnabled: false, | ||
icqEnabled: false, | ||
pfmEnabled: false, | ||
ibcHooksEnabled: false, | ||
stakingTokens: [{ denom: 'uist' }], | ||
}); | ||
let nonce = 0n; | ||
|
||
const chainKey = `${chainInfo.chainId}-${(nonce += 1n)}`; | ||
|
||
chainHub.registerChain(chainKey, chainInfo); | ||
``` | ||
|
||
The function takes two parameters: `name`, which is a `string` representing the unique identifier of the chain, and `chainInfo`, which is an object structured according to the `CosmosChainInfo` format. | ||
|
||
- **Retrieving Chain Information (`getChainInfo`)**: Retrieves stored chain information from the `chainInfos` mapstore or fetches it from a remote source if not available locally. | ||
|
||
```js | ||
chainHub.getChainInfo('agoric-3'); | ||
``` | ||
|
||
The function takes a single parameter, `chainName`, which is a `string` template type `K`, and returns a promise (`Vow`) that resolves to `ActualChainInfo<K>`, providing detailed information about the specified chain based on its name. | ||
|
||
- **Registering Connection Information (`registerConnection`)**: Stores information about a connection between two chains in `connectionInfos` mapstore, such as IBC connection details. | ||
|
||
```js | ||
const chainConnection = { | ||
id: 'connection-0', | ||
client_id: '07-tendermint-2', | ||
counterparty: { | ||
client_id: '07-tendermint-2', | ||
connection_id: 'connection-1', | ||
prefix: { | ||
key_prefix: '', | ||
}, | ||
}, | ||
state: 3 /* IBCConnectionState.STATE_OPEN */, | ||
transferChannel: { | ||
portId: 'transfer', | ||
channelId: 'channel-1', | ||
counterPartyChannelId: 'channel-1', | ||
counterPartyPortId: 'transfer', | ||
ordering: 1 /* Order.ORDER_UNORDERED */, | ||
state: 3 /* IBCConnectionState.STATE_OPEN */, | ||
version: 'ics20-1', | ||
}, | ||
}; | ||
|
||
chainHub.registerConnection('agoric-3', 'cosmoshub', chainConnection); | ||
``` | ||
|
||
The function accepts three parameters: `chainId1` and `chainId2`, both of which are `strings` representing the identifiers of the two chains being connected, and `connectionInfo`, which is an object containing the details of the IBC connection as specified by the `IBCConnectionInfo` format | ||
|
||
- **Retrieving Connection Information (`getConnectionInfo`)**: Retrieves stored connection information from `connectionInfos` mapstore or fetches it from a remote source if not available locally. | ||
|
||
```js | ||
const chainConnection = await E.when( | ||
chainHub.getConnectionInfo('agoric-3', 'cosmoshub'), | ||
); | ||
``` | ||
|
||
The function takes two parameters, `chain1` and `chain2`, each of which can be either a `string` representing a chain identifier or an `object` with a `chainId` property, and it returns a promise (`Vow`) that resolves with an `IBCConnectionInfo` object detailing the connection between the two chains. | ||
|
||
- **Retrieving Combined Chain and Connection Information (`getChainsAndConnection`)**: A composite function that fetches information about two chains and their connection simultaneously. | ||
|
||
```js | ||
const [agoric3, cosmoshub, connectionInfo] = await E.when( | ||
chainHub.getChainsAndConnection('agoric-3', 'cosmoshub'), | ||
); | ||
``` | ||
|
||
The function accepts two parameters, `chainName1` and `chainName2`, both of which are strings but defined as template types `C1` and `C2` respectively. It returns a promise (`Vow`) that resolves to a tuple containing the detailed information of both chains, `ActualChainInfo<C1>` and `ActualChainInfo<C2>`, along with their IBC connection information (`IBCConnectionInfo`). |