-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLA-2013] Add config and LRU cache (#41)
- Loading branch information
1 parent
dfd6f7f
commit fc5e802
Showing
8 changed files
with
106 additions
and
44 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,4 @@ | ||
NUMBER_OF_ISOLATES= | ||
DEFAULT_BIND_PORT= | ||
SPEC_PER_ISOLATE= | ||
SPEC_EXPIRE_DURATION= |
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,4 +1,6 @@ | ||
.idea | ||
.env | ||
bin/**.exe | ||
|
||
### Dart template | ||
# See https://www.dartlang.org/guides/libraries/private-files | ||
|
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
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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,56 +1,60 @@ | ||
// 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:dotenv/dotenv.dart'; | ||
import 'package:logging/logging.dart'; | ||
import 'package:lru_memory_cache/lru_memory_cache.dart'; | ||
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 = {}; | ||
final logger = Logger('Decoder'); | ||
|
||
ChainInfo getChainInfo(network, specVersion) { | ||
final chainInfoMetadata = chainInfos[network]?[specVersion]; | ||
var env = DotEnv(includePlatformEnvironment: true)..load(); | ||
int specPerIsolate = | ||
int.tryParse(env.getOrElse('SPEC_PER_ISOLATE', () => '4')) ?? 4; | ||
int specExpireDuration = | ||
int.tryParse(env.getOrElse('SPEC_EXPIRE_DURATION', () => '300')) ?? 300; | ||
|
||
LRUMemoryCache<String, ChainInfo> cache = LRUMemoryCache( | ||
generateKey: (k) => | ||
"${k.constants['System']!['Version']!.value['spec_name']}-${k.constants['System']!['Version']!.value['spec_version']}", | ||
capacity: specPerIsolate, | ||
expireMode: ExpireMode.onInteraction, | ||
); | ||
|
||
if (chainInfoMetadata != null) { | ||
return chainInfoMetadata; | ||
String getSpecName(String network, int specVersion) { | ||
if (network == 'canary-matrixchain') { | ||
return 'matrix-$specVersion'; | ||
} | ||
if (network == 'enjin-matrixchain') { | ||
return 'matrix-enjin-$specVersion'; | ||
} | ||
if (network == 'canary-relaychain') { | ||
return 'canary-$specVersion'; | ||
} | ||
|
||
// 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); | ||
return 'enjin-$specVersion'; | ||
} | ||
|
||
if (chainInfos[network] == null) { | ||
chainInfos[network] = {specVersion: chain}; | ||
} else { | ||
chainInfos[network]![specVersion] = chain; | ||
} | ||
DecodedMetadata getDecodedMetadata(String network, int specVersion) { | ||
if (network == 'canary-relaychain' || network == 'enjin-relaychain') { | ||
return MetadataDecoder.instance | ||
.decode(enjin.metadata(network, specVersion)); | ||
} | ||
|
||
print("Instantiated new metadata for $network $specVersion"); | ||
return MetadataDecoder.instance.decode(matrix.metadata(network, specVersion)); | ||
} | ||
|
||
return chain; | ||
} | ||
ChainInfo getChainInfo(network, specVersion) { | ||
ChainInfo? chainInfo = cache.get(getSpecName(network, specVersion)); | ||
|
||
// Instantiate a new metadata for matrixchains | ||
final metadata = | ||
MetadataDecoder.instance.decode(matrix.metadata(network, specVersion)); | ||
final ChainInfo chain = ChainInfo.fromMetadata(metadata); | ||
if (chainInfo == null) { | ||
logger.info("Metadata $network v$specVersion instantiated"); | ||
|
||
if (chainInfos[network] == null) { | ||
chainInfos[network] = {specVersion: chain}; | ||
} else { | ||
chainInfos[network]![specVersion] = chain; | ||
final metadata = getDecodedMetadata(network, specVersion); | ||
chainInfo = ChainInfo.fromMetadata(metadata); | ||
} | ||
|
||
print("Instantiated new metadata for $network $specVersion"); | ||
cache.add(chainInfo, expiryDuration: Duration(seconds: specExpireDuration)); | ||
|
||
return chain; | ||
return chainInfo; | ||
} |
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 |
---|---|---|
@@ -1,19 +1,25 @@ | ||
name: platform_decoder | ||
description: Enjin Platform Decoder | ||
version: 1.10.1 | ||
version: 2.1.0 | ||
publish_to: none | ||
|
||
environment: | ||
sdk: ">=3.3.0 <4.0.0" | ||
|
||
dependencies: | ||
shelf: ^1.4.2 | ||
shelf: ^1.4.1 | ||
shelf_plus: ^1.9.2 | ||
polkadart: ^0.4.7 | ||
substrate_metadata: ^1.2.2 | ||
polkadart_scale_codec: ^1.2.1 | ||
compute: ^1.0.2 | ||
lru_memory_cache: ^1.0.3 | ||
dotenv: ^4.2.0 | ||
logging: ^1.2.0 | ||
|
||
dev_dependencies: | ||
lints: ^4.0.0 | ||
test: ^1.25.8 | ||
|
||
assets: | ||
- .env |