-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PartialRegistry class and merge method (#33)
### Description - Add PartialRegistry class - Add merge() method to IRegistry ### Backward compatibility Yes ### Testing New unit test coverage
- Loading branch information
Showing
10 changed files
with
215 additions
and
52 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,6 @@ | ||
--- | ||
'@hyperlane-xyz/registry': minor | ||
--- | ||
|
||
Add PartialRegistry class | ||
Add merge() method to IRegistry |
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
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
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
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
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
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
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,74 @@ | ||
import type { Logger } from 'pino'; | ||
|
||
import type { ChainMap, ChainMetadata, ChainName, WarpCoreConfig } from '@hyperlane-xyz/sdk'; | ||
import { ChainAddresses } from '../types.js'; | ||
import { ChainFiles, IRegistry, RegistryContent, RegistryType } from './IRegistry.js'; | ||
import { SynchronousRegistry } from './SynchronousRegistry.js'; | ||
|
||
const PARTIAL_URI_PLACEHOLDER = '__partial_registry__'; | ||
|
||
/** | ||
* A registry that accepts partial data, such as incomplete chain metadata or addresses. | ||
* Useful for merging with other registries force overrides of subsets of data. | ||
*/ | ||
export interface PartialRegistryOptions { | ||
chainMetadata?: ChainMap<Partial<ChainMetadata>>; | ||
chainAddresses?: ChainMap<Partial<ChainAddresses>>; | ||
// TODO add more fields here as needed | ||
logger?: Logger; | ||
} | ||
|
||
export class PartialRegistry extends SynchronousRegistry implements IRegistry { | ||
public readonly type = RegistryType.Partial; | ||
public chainMetadata: ChainMap<Partial<ChainMetadata>>; | ||
public chainAddresses: ChainMap<Partial<ChainAddresses>>; | ||
|
||
constructor({ chainMetadata, chainAddresses, logger }: PartialRegistryOptions) { | ||
super({ uri: PARTIAL_URI_PLACEHOLDER, logger }); | ||
this.chainMetadata = chainMetadata || {}; | ||
this.chainAddresses = chainAddresses || {}; | ||
} | ||
|
||
listRegistryContent(): RegistryContent { | ||
const chains: ChainMap<ChainFiles> = {}; | ||
Object.keys(this.chainMetadata).forEach((c) => { | ||
chains[c] ||= {}; | ||
chains[c].metadata = PARTIAL_URI_PLACEHOLDER; | ||
}); | ||
Object.keys(this.chainAddresses).forEach((c) => { | ||
chains[c] ||= {}; | ||
chains[c].addresses = PARTIAL_URI_PLACEHOLDER; | ||
}); | ||
return { | ||
chains, | ||
deployments: {}, | ||
}; | ||
} | ||
|
||
getMetadata(): ChainMap<ChainMetadata> { | ||
return this.chainMetadata as ChainMap<ChainMetadata>; | ||
} | ||
|
||
getAddresses(): ChainMap<ChainAddresses> { | ||
return this.chainAddresses as ChainMap<ChainAddresses>; | ||
} | ||
|
||
removeChain(chainName: ChainName): void { | ||
super.removeChain(chainName); | ||
if (this.chainMetadata?.[chainName]) delete this.chainMetadata[chainName]; | ||
if (this.chainAddresses?.[chainName]) delete this.chainAddresses[chainName]; | ||
} | ||
|
||
addWarpRoute(_config: WarpCoreConfig): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
protected createOrUpdateChain(chain: { | ||
chainName: ChainName; | ||
metadata?: ChainMetadata; | ||
addresses?: ChainAddresses; | ||
}): void { | ||
if (chain.metadata) this.chainMetadata[chain.chainName] = chain.metadata; | ||
if (chain.addresses) this.chainAddresses[chain.chainName] = chain.addresses; | ||
} | ||
} |
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,71 @@ | ||
import type { ChainMap, ChainMetadata, ChainName, WarpCoreConfig } from '@hyperlane-xyz/sdk'; | ||
|
||
import { ChainAddresses } from '../types.js'; | ||
import { BaseRegistry } from './BaseRegistry.js'; | ||
import { IRegistry, RegistryContent } from './IRegistry.js'; | ||
|
||
/** | ||
* Shared code for sync registries like the FileSystem and Partial registries. | ||
* This is required because of the inconsistent sync/async methods across registries. | ||
* If the Infra package can be updated to work with async-only methods, this code can be moved to the BaseRegistry class. | ||
*/ | ||
export abstract class SynchronousRegistry extends BaseRegistry implements IRegistry { | ||
abstract listRegistryContent(): RegistryContent; | ||
|
||
getChains(): Array<ChainName> { | ||
return Object.keys(this.listRegistryContent().chains); | ||
} | ||
|
||
abstract getMetadata(): ChainMap<ChainMetadata>; | ||
|
||
getChainMetadata(chainName: ChainName): ChainMetadata | null { | ||
return this.getMetadata()[chainName] || null; | ||
} | ||
|
||
abstract getAddresses(): ChainMap<ChainAddresses>; | ||
|
||
getChainAddresses(chainName: ChainName): ChainAddresses | null { | ||
return this.getAddresses()[chainName] || null; | ||
} | ||
|
||
addChain(chain: { | ||
chainName: ChainName; | ||
metadata?: ChainMetadata; | ||
addresses?: ChainAddresses; | ||
}): void { | ||
const currentChains = this.listRegistryContent().chains; | ||
if (currentChains[chain.chainName]) | ||
throw new Error(`Chain ${chain.chainName} already exists in registry`); | ||
|
||
this.createOrUpdateChain(chain); | ||
} | ||
|
||
updateChain(chain: { | ||
chainName: ChainName; | ||
metadata?: ChainMetadata; | ||
addresses?: ChainAddresses; | ||
}): void { | ||
const currentChains = this.listRegistryContent(); | ||
if (!currentChains.chains[chain.chainName]) { | ||
this.logger.debug(`Chain ${chain.chainName} not found in registry, adding it now`); | ||
} | ||
this.createOrUpdateChain(chain); | ||
} | ||
|
||
removeChain(chainName: ChainName): void { | ||
const currentChains = this.listRegistryContent().chains; | ||
if (!currentChains[chainName]) throw new Error(`Chain ${chainName} does not exist in registry`); | ||
|
||
if (this.listContentCache?.chains[chainName]) delete this.listContentCache.chains[chainName]; | ||
if (this.metadataCache?.[chainName]) delete this.metadataCache[chainName]; | ||
if (this.addressCache?.[chainName]) delete this.addressCache[chainName]; | ||
} | ||
|
||
abstract addWarpRoute(config: WarpCoreConfig): void; | ||
|
||
protected abstract createOrUpdateChain(chain: { | ||
chainName: ChainName; | ||
metadata?: ChainMetadata; | ||
addresses?: ChainAddresses; | ||
}): void; | ||
} |
Oops, something went wrong.