-
-
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.
* Impelement feature flags module * Make flags serializable * Print spend_cond * Simplify flag value func * Flag for stricter spending conditions rules that will allow to sync with testnet * Updated readme * Update src/flags/index.js Co-Authored-By: Kosta Korenkov <[email protected]> * Typo
- Loading branch information
Showing
9 changed files
with
185 additions
and
17 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
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const flagsFactory = require('./flagsFactory'); | ||
|
||
const makeFlags = flagsFactory(['test_flag', 'test_flag_2']); | ||
|
||
describe('Feature flags', () => { | ||
it('should be true by default', () => { | ||
const bridgeState = { blockHeight: 0 }; | ||
const flags = makeFlags(bridgeState, {}); | ||
|
||
expect(flags.test_flag).toBe(true); | ||
}); | ||
|
||
it('should be false if configured height not reached yet', () => { | ||
const bridgeState = { blockHeight: 5 }; | ||
const flags = makeFlags(bridgeState, { | ||
test_flag: 10, | ||
}); | ||
|
||
expect(flags.test_flag).toBe(false); | ||
}); | ||
|
||
it('should be true if configured height reached', () => { | ||
const bridgeState = { blockHeight: 10 }; | ||
const flags = makeFlags(bridgeState, { | ||
test_flag: 10, | ||
}); | ||
|
||
expect(flags.test_flag).toBe(true); | ||
}); | ||
|
||
it('should change value after height change', () => { | ||
const bridgeState = { blockHeight: 0 }; | ||
const flags = makeFlags(bridgeState, { | ||
test_flag: 10, | ||
}); | ||
|
||
expect(flags.test_flag).toBe(false); | ||
bridgeState.blockHeight = 10; | ||
expect(flags.test_flag).toBe(true); | ||
}); | ||
|
||
it('should throw on unknown flag', () => { | ||
const bridgeState = { blockHeight: 0 }; | ||
const flags = makeFlags(bridgeState); | ||
expect(() => { | ||
const value = flags.unknown_flag; // eslint-disable-line no-unused-vars | ||
}).toThrow('Unknown feature flag unknown_flag'); | ||
}); | ||
|
||
it('should throw on attempt to set a flag value', () => { | ||
const bridgeState = { blockHeight: 0 }; | ||
const flags = makeFlags(bridgeState); | ||
expect(() => { | ||
flags.test_flag = true; | ||
}).toThrow('Flags are read-only: test_flag'); | ||
}); | ||
|
||
it('should serialize flag values', () => { | ||
const bridgeState = { blockHeight: 0 }; | ||
const flags = makeFlags(bridgeState, { | ||
test_flag: 10, | ||
}); | ||
|
||
bridgeState.blockHeight = 0; | ||
expect(JSON.parse(JSON.stringify(flags))).toEqual({ | ||
test_flag: false, | ||
test_flag_2: true, | ||
}); | ||
|
||
bridgeState.blockHeight = 10; | ||
expect(JSON.parse(JSON.stringify(flags))).toEqual({ | ||
test_flag: true, | ||
test_flag_2: true, | ||
}); | ||
}); | ||
}); |
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,31 @@ | ||
/* eslint-disable no-prototype-builtins */ | ||
|
||
function flagValue(bridgeState, flagHeights, flag) { | ||
const targetHeight = flagHeights[flag] || 0; | ||
return bridgeState.blockHeight >= targetHeight; | ||
} | ||
|
||
module.exports = (flags = []) => (bridgeState, flagHeights = {}) => { | ||
const proxy = new Proxy(flagHeights, { | ||
get(target, key) { | ||
if (key === 'toJSON') { | ||
return () => | ||
flags.reduce((flagValues, flag) => { | ||
flagValues[flag] = flagValue(bridgeState, target, flag); | ||
return flagValues; | ||
}, {}); | ||
} | ||
|
||
if (!flags.includes(key)) { | ||
throw new Error(`Unknown feature flag ${key}`); | ||
} | ||
|
||
return flagValue(bridgeState, target, key); | ||
}, | ||
set(_, key) { | ||
throw new Error(`Flags are read-only: ${key}`); | ||
}, | ||
}); | ||
|
||
return proxy; | ||
}; |
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,9 @@ | ||
const flagsFactory = require('./flagsFactory'); | ||
|
||
// when adding a flag, add a link to the issue/PR describing the reason for the change | ||
const FLAGS = [ | ||
'spend_cond_stricter_rules', // https://github.com/leapdao/leap-node/pull/303 | ||
'spend_cond_new_bytecode' // https://github.com/leapdao/leap-node/pull/292 | ||
]; | ||
|
||
module.exports = flagsFactory(FLAGS); |
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
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