Skip to content

Commit

Permalink
Return txVersion with artifacts (#46)
Browse files Browse the repository at this point in the history
* return txVersion with artifacts

* update txVersion when specVersion doesnt match

* remove double if block

* on second thought, an if-else
  • Loading branch information
joepetrowski authored May 16, 2020
1 parent b1d6161 commit f0f777e
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/ApiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ export default class ApiHandler {
// private wsUrl: string,
private api: ApiPromise;
private specVersion: u32;
private txVersion: u32;

constructor(api: ApiPromise) {
this.api = api;
// this.wsUrl = wsUrl;
this.specVersion = api.createType('u32', -1);
this.txVersion = api.createType('u32', -1);
}

async fetchBlock(hash: BlockHash) {
Expand Down Expand Up @@ -254,6 +256,7 @@ export default class ApiHandler {
at,
genesisHash,
specVersion: version.specVersion,
txVersion: version.transactionVersion,
metadata: metadata.toHex(),
};
}
Expand Down Expand Up @@ -328,20 +331,34 @@ export default class ApiHandler {
try {
const version = await api.rpc.state.getRuntimeVersion(hash);
const blockSpecVersion = version.specVersion;

// swap metadata if spec version is different
if (!this.specVersion.eq(blockSpecVersion)) {
const blockTxVersion = version.transactionVersion;

// Swap metadata if tx version is different. This block of code should never execute, as
// specVersion always increases when txVersion increases, but specVersion may increase
// without an increase in txVersion. Defensive only.
if (!this.txVersion.eq(blockTxVersion) && this.specVersion.eq(blockSpecVersion)) {
console.warn('txVersion bumped without specVersion bumping');
this.txVersion = blockTxVersion;
const meta = await api.rpc.state.getMetadata(hash);
api.registry.setMetadata(meta);
}
// Swap metadata and confirm txVersion if specVersion is different.
else if (!this.specVersion.eq(blockSpecVersion)) {
this.specVersion = blockSpecVersion;
this.txVersion = blockTxVersion;
const meta = await api.rpc.state.getMetadata(hash);
const chain = await api.rpc.system.chain();

api.registry.register(getSpecTypes(api.registry, chain, version.specName, blockSpecVersion));
api.registry.register(
getSpecTypes(api.registry, chain, version.specName, blockSpecVersion)
);
api.registry.setMetadata(meta);
}
} catch (err) {
console.error(`Failed to get Metadata for block ${hash}, using latest.`);
console.error(err);
this.specVersion = api.createType('u32', -1);
this.txVersion = api.createType('u32', -1);
}

return api;
Expand Down

0 comments on commit f0f777e

Please sign in to comment.