-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(swingset): hash kernel state changes into 'crankhash'
The multiple members of a consensus machine are supposed to perform identical exection of every crank. To detect any possible divergence as quickly as possible, the kernel maintains the "crankhash": a constantly-updated string which incorporates (by SHA256 hash) a copy of every DB write and delete. `controller.getCrankHash()` can be run after one or more cranks have finished, and the resulting hex string can be e.g. stored in a host application consensus state vector. If two members diverge in a way that causes their swingset state to differ, they will have different crankhashes, and the consensus state vectors will diverge. This should cause at least one of them to fall out of consensus. Some keys are excluded from consensus: currently just those involving vat snapshots and the truncation (non-initial starting point) of the transcript. refs #3442
- Loading branch information
Showing
13 changed files
with
324 additions
and
29 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,32 @@ | ||
import { assert } from '@agoric/assert'; | ||
|
||
import { createHash } from 'crypto'; | ||
|
||
/** | ||
* @typedef { (initial: string?) => { | ||
* add: (more: string) => void, | ||
* finish: () => string, | ||
* } | ||
* } CreateSHA256 | ||
*/ | ||
|
||
/** @type { CreateSHA256 } */ | ||
function createSHA256(initial = undefined) { | ||
const hash = createHash('sha256'); | ||
let done = false; | ||
function add(more) { | ||
assert(!done); | ||
hash.update(more); | ||
} | ||
function finish() { | ||
assert(!done); | ||
done = true; | ||
return hash.digest('hex'); | ||
} | ||
if (initial) { | ||
add(initial); | ||
} | ||
return harden({ add, finish }); | ||
} | ||
harden(createSHA256); | ||
export { createSHA256 }; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { test } from '../tools/prepare-test-env-ava.js'; | ||
|
||
// eslint-disable-next-line import/order | ||
import { createSHA256 } from '../src/hasher.js'; | ||
|
||
test('createSHA256', t => { | ||
t.is( | ||
createSHA256().finish(), | ||
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', | ||
); | ||
|
||
const h1 = createSHA256('a'); | ||
t.is( | ||
h1.finish(), | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
); | ||
|
||
const h2 = createSHA256(); | ||
h2.add('a'); | ||
t.is( | ||
h2.finish(), | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
); | ||
|
||
const h3 = createSHA256('a'); | ||
h3.add('b'); | ||
t.is( | ||
h3.finish(), | ||
'fb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603', | ||
); | ||
|
||
const h4 = createSHA256(); | ||
h4.finish(); | ||
t.throws(h4.add); | ||
t.throws(h4.finish); | ||
}); |
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
Oops, something went wrong.