feat: state.settings as object for Evolve

This commit is contained in:
ppedziwiatr
2021-09-07 08:40:23 +02:00
committed by Piotr Pędziwiatr
parent 843582cd9a
commit 8a0754b459
4 changed files with 297 additions and 319 deletions

View File

@@ -36,8 +36,11 @@ This also makes it easier to audit given contract - as you keep all its versions
*/
function isEvolveCompatible(state: any): state is EvolveCompatibleState {
const settings =
state.settings && isIterable(state.settings) ? new Map<string, any>(state.settings) : new Map<string, any>();
if (!state) {
return false;
}
const settings = evalSettings(state);
return state.evolve !== undefined || settings.has('evolve');
}
@@ -62,8 +65,7 @@ export class Evolve implements ExecutionContextModifier {
}
const currentSrcTxId = executionContext.contractDefinition.srcTxId;
const settings =
state.settings && isIterable(state.settings) ? new Map<string, any>(state.settings) : new Map<string, any>();
const settings = evalSettings(state);
// note: from my understanding - this variable holds the id of the transaction with updated source code.
const evolve: string = state.evolve || settings.get('evolve');
@@ -112,6 +114,22 @@ export class Evolve implements ExecutionContextModifier {
}
}
function evalSettings(state: any): Map<string, any> {
// default - empty
let settings = new Map<string, any>();
if (state.settings) {
// for Iterable format
if (isIterable(state.settings)) {
settings = new Map<string, any>(state.settings);
// for Object format
} else if (isObject(state.settings)) {
settings = new Map<string, any>(Object.entries(state.settings));
}
}
return settings;
}
function isIterable(obj: unknown): boolean {
// checks for null and undefined
if (obj == null) {
@@ -119,3 +137,7 @@ function isIterable(obj: unknown): boolean {
}
return typeof obj[Symbol.iterator] === 'function';
}
function isObject(obj: unknown): boolean {
return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
}