-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
228 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"parser": "@typescript-eslint/parser", | ||
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], | ||
"parserOptions": { | ||
"ecmaVersion": 2020, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
// Add custom rules here | ||
} | ||
} |
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,36 +1,36 @@ | ||
name: coretime-utils CI | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
build-and-test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v2 | ||
|
||
# Setup Node.js environment | ||
- name: Use Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' # You can specify your Node.js version here | ||
# Setup Node.js environment | ||
- name: Use Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' # You can specify your Node.js version here | ||
|
||
# Install dependencies | ||
- name: Install dependencies | ||
run: npm install | ||
# Install dependencies | ||
- name: Install dependencies | ||
run: npm install | ||
|
||
# Run linter | ||
- name: Run Linter | ||
run: npm run lint | ||
# Run linter | ||
- name: Run Linter | ||
run: npm run lint | ||
|
||
# Check Prettier | ||
- name: Check Prettier | ||
run: npm run prettier-check | ||
# Check Prettier | ||
- name: Check Prettier | ||
run: npm run prettier-check | ||
|
||
# Run tests | ||
- name: Run Tests | ||
run: npm test | ||
# Run tests | ||
- name: Run Tests | ||
run: npm test |
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 +1,2 @@ | ||
node_modules | ||
dist |
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 +1,3 @@ | ||
# coretime-utils | ||
|
||
An npm package containing types and constants that are commonly reused in regarding to Coretime |
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,52 @@ | ||
import { CoreMask, COMPLETE_MASK, VOID_MASK } from '../src'; | ||
|
||
describe('CoreMask utils work', () => { | ||
test('constructor works', () => { | ||
expect(() => new CoreMask(COMPLETE_MASK)).not.toThrow(); | ||
expect(() => new CoreMask(VOID_MASK)).not.toThrow(); | ||
expect(() => new CoreMask('foo')).toThrow(); | ||
expect(() => new CoreMask(VOID_MASK + '0')).toThrow(); | ||
}); | ||
|
||
test('fromChunk works', () => { | ||
expect(CoreMask.fromChunk(40, 60).getMask()).toEqual('0x0000000000fffff00000'); | ||
}); | ||
|
||
test('fromBin works', () => { | ||
expect( | ||
CoreMask.fromBin( | ||
'11111111111111111111111111111111111111111111111111111111111111111111111111111111', | ||
).getMask(), | ||
).toEqual(COMPLETE_MASK); | ||
|
||
expect( | ||
CoreMask.fromBin( | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000', | ||
).getMask(), | ||
).toEqual(VOID_MASK); | ||
}); | ||
|
||
test('countOnes works', () => { | ||
expect(CoreMask.voidMask().countOnes()).toEqual(0); | ||
expect(CoreMask.completeMask().countOnes()).toEqual(80); | ||
expect(CoreMask.fromChunk(0, 20).countOnes()).toEqual(20); | ||
}); | ||
|
||
test('countZeros works', () => { | ||
expect(CoreMask.voidMask().countZeros()).toEqual(80); | ||
expect(CoreMask.completeMask().countZeros()).toEqual(0); | ||
expect(CoreMask.fromChunk(0, 20).countZeros()).toEqual(60); | ||
}); | ||
|
||
test('toBin works', () => { | ||
expect(CoreMask.voidMask().toBin()).toEqual( | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000', | ||
); | ||
expect(CoreMask.completeMask().toBin()).toEqual( | ||
'11111111111111111111111111111111111111111111111111111111111111111111111111111111', | ||
); | ||
expect(CoreMask.fromChunk(0, 5).toBin()).toEqual( | ||
'11111000000000000000000000000000000000000000000000000000000000000000000000000000', | ||
); | ||
}); | ||
}); |
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,89 @@ | ||
export const VOID_MASK = '0x00000000000000000000'; // hex encoded 80 bit bitmap. | ||
export const COMPLETE_MASK = '0xffffffffffffffffffff'; // hex encoded 80 bit bitmap. | ||
|
||
export class CoreMask { | ||
private mask: string; | ||
|
||
getMask(): string { | ||
return this.mask; | ||
} | ||
|
||
constructor(mask: string) { | ||
// 10 hex characters plus the '0x' at the start. | ||
if (isHex(mask) && mask.length === 22) { | ||
this.mask = mask; | ||
} else { | ||
throw new Error('Invalid mask'); | ||
} | ||
} | ||
|
||
static completeMask(): CoreMask { | ||
return new CoreMask(COMPLETE_MASK); | ||
} | ||
|
||
static voidMask(): CoreMask { | ||
return new CoreMask(VOID_MASK); | ||
} | ||
|
||
static fromChunk(from: number, to: number): CoreMask { | ||
if (from < 0 || to >= 80 || from > to) { | ||
throw new Error('Invalid bit range'); | ||
} | ||
|
||
let mask = 0n; | ||
for (let i = from; i < to; i++) { | ||
mask |= 1n << BigInt(79 - i); | ||
} | ||
|
||
return new CoreMask('0x' + mask.toString(16).padStart(20, '0')); | ||
} | ||
|
||
static fromBin(bin: string): CoreMask { | ||
let hexMask = ''; | ||
for (let i = 0; i < bin.length; i += 4) { | ||
const v = parseInt(bin.slice(i, i + 4), 2); | ||
hexMask += v.toString(16); | ||
} | ||
return new CoreMask(`0x${hexMask}`); | ||
} | ||
|
||
countZeros(): number { | ||
let count = 0; | ||
for (let i = 2; i < this.mask.length; ++i) { | ||
let v = parseInt(this.mask.slice(i, i + 1), 16); | ||
for (let j = 0; j < 4; ++j) { | ||
if ((v & 1) === 0) ++count; | ||
v >>= 1; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
countOnes(): number { | ||
let count = 0; | ||
for (let i = 2; i < this.mask.length; ++i) { | ||
let v = parseInt(this.mask.slice(i, i + 1), 16); | ||
while (v > 0) { | ||
if (v & 1) ++count; | ||
v >>= 1; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
toBin(): string { | ||
let bin = ''; | ||
for (let i = 2; i < this.mask.length; ++i) { | ||
const v = parseInt(this.mask.slice(i, i + 1), 16); | ||
for (let j = 3; j >= 0; --j) { | ||
bin += v & (1 << j) ? '1' : '0'; | ||
} | ||
} | ||
return bin; | ||
} | ||
} | ||
|
||
function isHex(str: string) { | ||
const regex = /^0x[0-9a-fA-F]+$/; | ||
return regex.test(str); | ||
} |
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,2 @@ | ||
export * from './coreMask'; | ||
export * from './types'; |
Empty file.
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,33 @@ | ||
import { CoreMask } from '.'; | ||
|
||
export type Timeslice = number; | ||
export type CoreIndex = number; | ||
|
||
export type Balance = number; | ||
|
||
export type RegionId = { | ||
begin: Timeslice; | ||
core: CoreIndex; | ||
mask: CoreMask; | ||
}; | ||
|
||
export type RegionRecord = { | ||
end: Timeslice; | ||
owner: string; | ||
paid: null | Balance; | ||
}; | ||
|
||
export type Region = { | ||
regionId: RegionId; | ||
regionRecord: RegionRecord; | ||
}; | ||
|
||
export const Id = { | ||
_enum: { | ||
U8: 'u8', | ||
U16: 'u16', | ||
U32: 'u32', | ||
U64: 'u64', | ||
U128: 'u128', | ||
}, | ||
}; |
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"declaration": true, | ||
"target": "ES2020" | ||
} | ||
} |