Skip to content

Commit

Permalink
implement a fallback to EXPERIMENTAL_genesis_config for `experimental…
Browse files Browse the repository at this point in the history
…_protocolConfig` method implementation
  • Loading branch information
frol committed Feb 26, 2021
1 parent 766dd9d commit c6dd9fe
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
22 changes: 13 additions & 9 deletions lib/providers/json-rpc-provider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ export class JsonRpcProvider extends Provider {
* @returns {Promise<GenesisConfig>}
*/
async experimental_genesisConfig(): Promise<NearProtocolConfig> {
const deprecate = depd('JsonRpcProvider.experimental_protocolConfig({ sync_checkpoint: \'genesis\' })');
deprecate('use `experimental_protocolConfig` to fetch the up-to-date or genesis protocol config explicitly');
// TODO: Once EXPERIMENTAL_protocol_config (https://github.com/near/nearcore/pull/3919)
// is released to mainnet, the following line should be used:
//
//return await this.sendJsonRpc('EXPERIMENTAL_protocol_config', { sync_checkpoint: 'genesis' });
//
const deprecate = depd('JsonRpcProvider.experimental_protocolConfig({ sync_checkpoint: \'genesis\' })');
deprecate('use `experimental_protocolConfig` to fetch the up-to-date or genesis protocol config explicitly');
return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []);
}

Expand All @@ -139,13 +139,16 @@ export class JsonRpcProvider extends Provider {
* @returns {Promise<ProtocolConfig>}
*/
async experimental_protocolConfig(blockReference: BlockReference): Promise<NearProtocolConfig> {
// TODO: Once EXPERIMENTAL_protocol_config (https://github.com/near/nearcore/pull/3919)
// is released to mainnet, the following line should be used:
//
//return await this.sendJsonRpc('EXPERIMENTAL_protocol_config', blockReference);
//
// Meanwhile, we use the old genesis config method in order to initiate migration
return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []);
try {
return await this.sendJsonRpc('EXPERIMENTAL_protocol_config', blockReference);
} catch (error) {
// TODO: Once EXPERIMENTAL_protocol_config (https://github.com/near/nearcore/pull/3919)
// is released to mainnet, remove this fallback, and the wrapping try/catch
if (error.message === 'Method not found') {
return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []);
}
throw error;
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,24 @@ test('json rpc light client proof', async() => {

await expect(provider.lightClientProof(lightClientRequest)).rejects.toThrow(/.+ block .+ is ahead of head block .+/);
});

test('json rpc fetch genesis protocol config', withProvider(async (provider) => {
const response = await provider.experimental_genesisConfig();
expect('chain_id' in response).toBe(true);
expect('genesis_height' in response).toBe(true);
expect('runtime_config' in response).toBe(true);
expect('storage_amount_per_byte' in response.runtime_config).toBe(true);
}));

test('json rpc fetch protocol config', withProvider(async (provider) => {
const status = await provider.status();
const blockHeight = status.sync_info.latest_block_height;
const blockHash = status.sync_info.latest_block_hash;
for (const blockReference of [{ sync_checkpoint: 'genesis' }, { block_id: blockHeight }, { block_id: blockHash }, { finality: 'final' }, { finality: 'optimistic' }]) {
const response = await provider.experimental_protocolConfig(blockReference);
expect('chain_id' in response).toBe(true);
expect('genesis_height' in response).toBe(true);
expect('runtime_config' in response).toBe(true);
expect('storage_amount_per_byte' in response.runtime_config).toBe(true);
}
}));

0 comments on commit c6dd9fe

Please sign in to comment.