This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #647 from thehubbleproject/pubkey2states
added mapping from pubkey hash to states
- Loading branch information
Showing
10 changed files
with
571 additions
and
347 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
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
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,70 @@ | ||
import chai, { assert } from "chai"; | ||
import chaiAsPromised from "chai-as-promised"; | ||
import { StateDatabaseEngine } from "../../ts/client/database"; | ||
import { PRODUCTION_PARAMS } from "../../ts/constants"; | ||
import { State } from "../../ts/state"; | ||
import del from "del"; | ||
import { PubkeyLeaf } from "../../ts/tree/leaves/PubkeyLeaf"; | ||
import { init } from "../../ts/mcl"; | ||
import { BlsSigner } from "../../ts/blsSigner"; | ||
import { Pubkey2StatesDB } from "../../ts/client/database/pubkey2states"; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
const { | ||
MAX_DEPTH: maxDepth, | ||
MAX_DEPOSIT_SUBTREE_DEPTH: maxSubtreeDepth | ||
} = PRODUCTION_PARAMS; | ||
|
||
describe("StateDBEngine", () => { | ||
let engine: StateDatabaseEngine; | ||
let p0: PubkeyLeaf; | ||
let p1: PubkeyLeaf; | ||
let statesP0: State[]; | ||
let statesP1: State[]; | ||
|
||
before(async function() { | ||
await del("./leveldb/*"); | ||
}); | ||
|
||
after(async function() { | ||
await del("./leveldb/*"); | ||
}); | ||
|
||
beforeEach(async function() { | ||
engine = new StateDatabaseEngine(maxDepth); | ||
statesP0 = []; | ||
statesP1 = []; | ||
|
||
await init(); | ||
|
||
p0 = PubkeyLeaf.fromSolG2(BlsSigner.new().pubkey, 0); | ||
await p0.toDB(); | ||
|
||
p1 = PubkeyLeaf.fromSolG2(BlsSigner.new().pubkey, 1); | ||
await p1.toDB(); | ||
|
||
for (let i = 0; i < 4; i++) { | ||
statesP0.push(State.new(p0.itemID, i, i, i)); | ||
} | ||
|
||
for (let i = 0; i < 2; i++) { | ||
statesP1.push(State.new(p1.itemID, i, i, i)); | ||
} | ||
}); | ||
it("update pubkey with states", async function() { | ||
await engine.updateBatch(0, maxSubtreeDepth, statesP0); | ||
await engine.updateBatch(1, maxSubtreeDepth, statesP1); | ||
|
||
assert.deepEqual(await Pubkey2StatesDB.getStates(p0.item.hash()), [ | ||
0, | ||
1, | ||
2, | ||
3 | ||
]); | ||
assert.deepEqual(await Pubkey2StatesDB.getStates(p1.item.hash()), [ | ||
4, | ||
5 | ||
]); | ||
}); | ||
}); |
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,29 @@ | ||
import _ from "lodash"; | ||
import { PubkeyLeafFactory } from "../../tree/leaves/PubkeyLeaf"; | ||
import { pubkey2statesDB } from "./connection"; | ||
|
||
export class Pubkey2StatesDB { | ||
static async getStates(pubkeyHash: string): Promise<number[]> { | ||
return JSON.parse(await pubkey2statesDB.get(pubkeyHash)); | ||
} | ||
|
||
static async update(pubkeyID: number, stateID: number): Promise<void> { | ||
const pubkeyLeaf = await PubkeyLeafFactory().fromDB(pubkeyID); | ||
const pubkeyHash = pubkeyLeaf.item.hash(); | ||
|
||
try { | ||
const states: string = await pubkey2statesDB.get(pubkeyHash); | ||
const appended = _.union<number>(JSON.parse(states), [stateID]); | ||
await pubkey2statesDB.put(pubkeyHash, JSON.stringify(appended)); | ||
} catch (error) { | ||
if (error.name === "NotFoundError") { | ||
await pubkey2statesDB.put( | ||
pubkeyHash, | ||
JSON.stringify([stateID]) | ||
); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
} |
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