Skip to content

Commit

Permalink
[PLA-1977] Parses any specVersion of metadata (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Sep 4, 2024
1 parent 98f9286 commit b7fe8c0
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 84 deletions.
15 changes: 8 additions & 7 deletions lib/consts/enjin/canary/canary.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
export 'v100.dart';
export 'v101.dart';
export 'v102.dart';
export 'v103.dart';
export 'v104.dart';
export 'v105.dart';
export 'v106.dart';
export 'v107.dart';
export 'v110.dart';
export 'v120.dart';
export 'v1021.dart';
export 'v1022.dart';
export 'v1023.dart';
export 'v1024.dart';
export 'v1025.dart';
export 'v1026.dart';
export 'v103.dart';
export 'v1030.dart';
export 'v1031.dart';
export 'v1032.dart';
export 'v104.dart';
export 'v105.dart';
export 'v106.dart';
export 'v107.dart';
export 'v110.dart';
export 'v120.dart';
3 changes: 2 additions & 1 deletion lib/consts/enjin/canary/v1030.dart

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/consts/enjin/canary/v1031.dart

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/consts/enjin/canary/v1032.dart

Large diffs are not rendered by default.

86 changes: 84 additions & 2 deletions lib/consts/enjin/enjin.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,92 @@
import 'canary/canary.dart' as c;
import 'production/production.dart' as p;

String canary() {
String canaryLatest() {
return c.v1032;
}

String production() {
String productionLatest() {
return p.v1026;
}

String metadata(network, specVersion) {
if (network == 'canary-relaychain') {
return canarySpec(specVersion);
}

return productionSpec(specVersion);
}

String canarySpec(specVersion) {
switch (specVersion) {
case 100:
return c.v100;
case 101:
return c.v101;
case 102:
return c.v102;
case 103:
return c.v103;
case 104:
return c.v104;
case 105:
return c.v105;
case 106:
return c.v106;
case 107:
return c.v107;
case 110:
return c.v110;
case 120:
return c.v120;
case 1021:
return c.v1021;
case 1022:
return c.v1022;
case 1023:
return c.v1023;
case 1024:
return c.v1024;
case 1025:
return c.v1025;
case 1026:
return c.v1026;
case 1030:
return c.v1030;
case 1031:
return c.v1031;
case 1032:
return c.v1032;
default:
return canaryLatest();
}
}

String productionSpec(specVersion) {
switch (specVersion) {
case 100:
return p.v100;
case 101:
return p.v101;
case 102:
return p.v102;
case 110:
return p.v110;
case 120:
return p.v120;
case 1021:
return p.v1021;
case 1022:
return p.v1022;
case 1023:
return p.v1023;
case 1024:
return p.v1024;
case 1025:
return p.v1025;
case 1026:
return p.v1026;
default:
return productionLatest();
}
}
3 changes: 2 additions & 1 deletion lib/consts/matrix/canary/v1006.dart

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/consts/matrix/canary/v1010.dart

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/consts/matrix/canary/v1011.dart

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/consts/matrix/canary/v1012.dart

Large diffs are not rendered by default.

76 changes: 74 additions & 2 deletions lib/consts/matrix/matrix.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,82 @@
import 'canary/canary.dart' as c;
import 'production/production.dart' as p;

String canary() {
String canaryLatest() {
return c.v1012;
}

String production() {
String productionLatest() {
return p.v1006;
}

String metadata(network, specVersion) {
if (network == 'canary-matrixchain' || network == 'canary') {
return canarySpec(specVersion);
}

return productionSpec(specVersion);
}

String canarySpec(specVersion) {
switch (specVersion) {
case 500:
return c.v500;
case 600:
return c.v600;
case 601:
return c.v601;
case 602:
return c.v602;
case 604:
return c.v604;
case 605:
return c.v605;
case 1000:
return c.v1000;
case 1001:
return c.v1001;
case 1002:
return c.v1002;
case 1003:
return c.v1003;
case 1004:
return c.v1004;
case 1005:
return c.v1005;
case 1006:
return c.v1006;
case 1010:
return c.v1010;
case 1011:
return c.v1011;
case 1012:
return c.v1012;
default:
return canaryLatest();
}
}

String productionSpec(specVersion) {
switch (specVersion) {
case 603:
return p.v603;
case 604:
return p.v604;
case 605:
return p.v605;
case 1000:
return p.v1000;
case 1002:
return p.v1002;
case 1003:
return p.v1003;
case 1004:
return p.v1004;
case 1005:
return p.v1005;
case 1006:
return p.v1006;
default:
return productionLatest();
}
}
3 changes: 2 additions & 1 deletion lib/consts/matrix/production/v1006.dart

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions lib/decoder/chain_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// The first time a metadata is requested we instantiate it and keep it on memory to speed up the decoder
// Otherwise we would have to decode the metadata every time we decode an extrinsic or event
// TODO: Later we can make some kind of LRU cache so it doesn't stays there forever

import 'package:platform_decoder/consts/enjin/enjin.dart' as enjin;
import 'package:platform_decoder/consts/matrix/matrix.dart' as matrix;
import 'package:substrate_metadata/core/metadata_decoder.dart';
import 'package:substrate_metadata/models/models.dart';

// {
// "enjin-matrixchain": {
// 1000: ChainInfo,
// 1001: ChainInfo,
// }
// }
final Map<String, Map<int, ChainInfo>> chainInfos = {};

ChainInfo getChainInfo(network, specVersion) {
final chainInfoMetadata = chainInfos[network]?[specVersion];

if (chainInfoMetadata != null) {
return chainInfoMetadata;
}

// Instantiate a new metadata for relaychains
if (network == 'canary-relaychain' || network == 'enjin-relaychain') {
final metadata =
MetadataDecoder.instance.decode(enjin.metadata(network, specVersion));
final ChainInfo chain = ChainInfo.fromMetadata(metadata);

if (chainInfos[network] == null) {
chainInfos[network] = {specVersion: chain};
} else {
chainInfos[network]![specVersion] = chain;
}

print("Instantiated new metadata for $network $specVersion");

return chain;
}

// Instantiate a new metadata for matrixchains
final metadata =
MetadataDecoder.instance.decode(matrix.metadata(network, specVersion));
final ChainInfo chain = ChainInfo.fromMetadata(metadata);

if (chainInfos[network] == null) {
chainInfos[network] = {specVersion: chain};
} else {
chainInfos[network]![specVersion] = chain;
}

print("Instantiated new metadata for $network $specVersion");

return chain;
}
69 changes: 7 additions & 62 deletions lib/decoder/general.dart
Original file line number Diff line number Diff line change
@@ -1,73 +1,18 @@
import 'package:platform_decoder/consts/enjin/enjin.dart' as enjin;
import 'package:platform_decoder/consts/matrix/matrix.dart' as matrix;
import 'package:polkadart_scale_codec/io/io.dart';
import 'package:substrate_metadata/core/metadata_decoder.dart';
import 'package:substrate_metadata/models/models.dart';
import 'package:substrate_metadata/types/metadata_types.dart';

final DecodedMetadata matrixMetadata =
MetadataDecoder.instance.decode(matrix.production());
final ChainInfo matrixChain = ChainInfo.fromMetadata(matrixMetadata);
import 'chain_info.dart';

final DecodedMetadata matrixCanaryMetadata =
MetadataDecoder.instance.decode(matrix.canary());
final ChainInfo matrixCanaryChain =
ChainInfo.fromMetadata(matrixCanaryMetadata);

final DecodedMetadata enjinMetadata =
MetadataDecoder.instance.decode(enjin.production());
final ChainInfo enjinChain = ChainInfo.fromMetadata(enjinMetadata);

final DecodedMetadata enjinCanaryMetadata =
MetadataDecoder.instance.decode(enjin.canary());
final ChainInfo enjinCanaryChain = ChainInfo.fromMetadata(enjinCanaryMetadata);

dynamic decodeExtrinsic(raw, network) {
dynamic decodeExtrinsic(raw, network, specVersion) {
final input = Input.fromHex(raw);
final chainInfo = getChainInfo(network, specVersion);

if (network == 'canary' || network == 'canary-matrixchain') {
final dynamic decoded =
ExtrinsicsCodec(chainInfo: matrixCanaryChain).decode(input);
return decoded;
}

if (network == 'enjin-relaychain') {
final dynamic decoded =
ExtrinsicsCodec(chainInfo: enjinChain).decode(input);
return decoded;
}

if (network == 'canary-relaychain') {
final dynamic decoded =
ExtrinsicsCodec(chainInfo: enjinCanaryChain).decode(input);
return decoded;
}

final dynamic decoded = ExtrinsicsCodec(chainInfo: matrixChain).decode(input);
return decoded;
return ExtrinsicsCodec(chainInfo: chainInfo).decode(input);
}

dynamic decodeEvents(raw, network) {
dynamic decodeEvents(raw, network, specVersion) {
final input = Input.fromHex(raw);
final chainInfo = getChainInfo(network, specVersion);

if (network == 'canary' || network == 'canary-matrixchain') {
final List<dynamic> decoded =
matrixCanaryChain.scaleCodec.decode('EventCodec', input);
return decoded;
}

if (network == 'enjin-relaychain') {
final List<dynamic> decoded =
enjinChain.scaleCodec.decode('EventCodec', input);
return decoded;
}

if (network == 'canary-relaychain') {
final List<dynamic> decoded =
enjinCanaryChain.scaleCodec.decode('EventCodec', input);
return decoded;
}

final decoded = matrixChain.scaleCodec.decode('EventCodec', input);
return decoded;
return chainInfo.scaleCodec.decode('EventCodec', input);
}
24 changes: 20 additions & 4 deletions lib/handler/general.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@ import 'package:platform_decoder/decoder/decoder.dart';
import 'package:shelf/shelf.dart';
import 'package:substrate_metadata/utils/utils.dart';

int getLatestSpecVersion(network) {
if (network == 'enjin-relaychain') {
return 1026;
}
if (network == 'canary-relaychain') {
return 1032;
}
if (network == 'canary' || network == 'canary-matrixchain') {
return 1012;
}
return 1006;
}

Future<dynamic> handleRequest(Request request) async {
String content = await utf8.decoder.bind(request.read()).join();
final Map<String, dynamic> body = jsonDecode(content);
final network = body['network'] ?? 'enjin';
final network = body['network'] ?? 'enjin-matrixchain';
final int specVersion = body['spec_version'] ?? getLatestSpecVersion(network);

if (body['extrinsic'] != null) {
try {
final Map<String, dynamic> decoded =
decodeExtrinsic(body['extrinsic'], network);
decodeExtrinsic(body['extrinsic'], network, specVersion);
final extrinsic = decoded.toJson();
extrinsic['extrinsic_hash'] = extrinsic['hash'];

Expand All @@ -26,7 +40,8 @@ Future<dynamic> handleRequest(Request request) async {
if (body['extrinsics'] != null) {
try {
final extrinsics = (body['extrinsics'] as List).map((e) {
final Map<String, dynamic> decoded = decodeExtrinsic(e, network);
final Map<String, dynamic> decoded =
decodeExtrinsic(e, network, specVersion);
final extrinsic = decoded.toJson();
extrinsic['extrinsic_hash'] = extrinsic['hash'];

Expand All @@ -41,7 +56,8 @@ Future<dynamic> handleRequest(Request request) async {

if (body['events'] != null) {
try {
final decoded = (decodeEvents(body['events'], network) as List).map((e) {
final decoded =
(decodeEvents(body['events'], network, specVersion) as List).map((e) {
final event = e as Map<String, dynamic>;
return event.toJson();
});
Expand Down

0 comments on commit b7fe8c0

Please sign in to comment.