-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cosmic-swingset): add board, use board in mailboxAdmin (#1167)
* feat: add board, use board in mailboxAdmin
- Loading branch information
1 parent
9cb3050
commit 1381ba6
Showing
6 changed files
with
186 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import harden from '@agoric/harden'; | ||
import { generateSparseInts } from '@agoric/sparse-ints'; | ||
import { assert, details } from '@agoric/assert'; | ||
import makeStore from '@agoric/store'; | ||
|
||
function makeBoard(seed = 0) { | ||
const idToVal = makeStore(); | ||
const valToId = makeStore(); | ||
const sparseInts = generateSparseInts(seed); | ||
|
||
const board = harden({ | ||
// Add if not already present | ||
getId: value => { | ||
if (!valToId.has(value)) { | ||
// Retry until we have a unique id. | ||
let id; | ||
do { | ||
id = sparseInts.next().value.toString(); | ||
} while (idToVal.has(id)); | ||
|
||
valToId.init(value, id); | ||
idToVal.init(id, value); | ||
} | ||
return valToId.get(value); | ||
}, | ||
getValue: id => { | ||
assert.equal(typeof id, 'string', details`id must be string ${id}`); | ||
assert(idToVal.has(id), details`board does not have id: ${id}`); | ||
return idToVal.get(id); | ||
}, | ||
has: valToId.has, | ||
ids: () => harden([...idToVal.keys()]), | ||
}); | ||
|
||
return board; | ||
} | ||
|
||
export { makeBoard }; |
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,16 @@ | ||
import harden from '@agoric/harden'; | ||
import { makeBoard } from './lib-board'; | ||
|
||
function build(_E, _log) { | ||
const board = makeBoard(); | ||
return harden({ getBoard: () => board }); | ||
} | ||
|
||
export default function setup(syscall, state, helpers) { | ||
return helpers.makeLiveSlots( | ||
syscall, | ||
state, | ||
E => build(E, helpers.log), | ||
helpers.vatID, | ||
); | ||
} |
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,32 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { test } from 'tape-promise/tape'; | ||
import harden from '@agoric/harden'; | ||
|
||
import { makeBoard } from '../../lib/ag-solo/vats/lib-board'; | ||
|
||
test('makeBoard', async t => { | ||
try { | ||
const board = makeBoard(); | ||
|
||
const obj1 = harden({}); | ||
const obj2 = harden({}); | ||
|
||
t.deepEquals(board.ids(), [], `board is empty to start`); | ||
|
||
const idObj1 = board.getId(obj1); | ||
t.deepEquals(board.ids(), [idObj1], `board has one id`); | ||
|
||
const idObj2 = board.getId(obj2); | ||
t.deepEquals(board.ids().length, 2, `board has two ids`); | ||
|
||
t.deepEquals(board.getValue(idObj1), obj1, `id matches value obj1`); | ||
t.deepEquals(board.getValue(idObj2), obj2, `id matches value obj2`); | ||
|
||
t.deepEquals(board.getId(obj1), idObj1, `value matches id obj1`); | ||
t.deepEquals(board.getId(obj2), idObj2, `value matches id obj2`); | ||
} catch (e) { | ||
t.isNot(e, e, 'unexpected exception'); | ||
} finally { | ||
t.end(); | ||
} | ||
}); |