-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
heartbeat + enhanced tests + refactor #57
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d449518
added post heartbeat message route
samparsky 8f6a94d
fix: rename vars
samparsky 71bc28b
added: heartbeat
samparsky 25e5906
added: validator worker heartbeat
samparsky 44b6ec8
format code
samparsky c28db9c
fix: heartbeat implementation
samparsky ed7bef5
test: heartbeat notification
samparsky 470367a
modify: timestamp
samparsky d2fcb79
fix: timestamp
samparsky 083e03f
fix: signature test case
samparsky f1bdb74
fix: timestmap
samparsky e980a2d
fix: remove hardcoded vals
samparsky 1a50e41
fix: tests, added index.js
samparsky 3e834cf
fix: heartbeat test case, timestamp
samparsky a64406b
fix: test case
samparsky 51db940
test/integration updated
ca057b8
services/validatorWorker/follower: fix invalid sig check
5d24220
Added a short doc page for validator message types
simzzz 2871ba4
Fixed formatting
simzzz adb3835
Added timestamp property
simzzz 2ddc59c
services/validatorWorker/follower: rename result of verify
1ec4b60
fix: test case
samparsky e433260
Merge branch 'master' into heartbeat-message
samparsky b800fe1
fix: return heartbeat
samparsky 9eeb934
Merge branch 'heartbeat-message' of https://github.com/samparsky/adex…
samparsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,35 @@ | ||
const assert = require('assert') | ||
const { persistAndPropagate } = require('./lib/propagation') | ||
|
||
function heartbeat(adapter, channel){ | ||
const whoami = adapter.whoami() | ||
const validatorIdx = channel.validators.indexOf(whoami) | ||
assert.ok(validatorIdx !== -1, 'validatorTick: sending heartbeat for a channel where we are not validating') | ||
const otherValidators = channel.spec.validators.filter(v => v.id != whoami) | ||
|
||
let timestamp = Buffer.alloc(32); | ||
timestamp.writeUIntBE(Date.now(), 26, 6); | ||
|
||
// in the future, we can add more information to this tree, | ||
// such as the validator node capacity/status, | ||
// or a proof of 'no earlier than' (hash of the latest blockchain block) | ||
const tree = new adapter.MerkleTree([ timestamp ]) | ||
const infoRootRaw = tree.getRoot() | ||
|
||
const stateRootRaw = adapter.getSignableStateRoot(Buffer.from(channel.id), infoRootRaw) | ||
|
||
return adapter.sign(stateRootRaw) | ||
.then(function(signature) { | ||
const stateRoot = stateRootRaw.toString('hex') | ||
timestamp = timestamp.toString('hex') | ||
|
||
return persistAndPropagate(adapter, otherValidators, channel, { | ||
type: 'HeartBeat', | ||
timestamp, | ||
signature, | ||
stateRoot, | ||
}); | ||
}) | ||
} | ||
|
||
module.exports = heartbeat |
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,28 @@ | ||
const assert = require('assert') | ||
const BN = require('bn.js') | ||
|
||
function getStateRootHash(channel, balances, adapter){ | ||
// Note: MerkleTree takes care of deduplicating and sorting | ||
const elems = Object.keys(balances).map( | ||
acc => adapter.getBalanceLeaf(acc, balances[acc]) | ||
) | ||
const tree = new adapter.MerkleTree(elems) | ||
const balanceRoot = tree.getRoot() | ||
// keccak256(channelId, balanceRoot) | ||
const stateRoot = adapter.getSignableStateRoot(Buffer.from(channel.id), balanceRoot).toString('hex') | ||
return stateRoot | ||
} | ||
|
||
function isValidRootHash(leaderRootHash, { channel, balances, adapter }) { | ||
return getStateRootHash(channel, balances, adapter) === leaderRootHash | ||
} | ||
|
||
function toBNMap(raw) { | ||
assert.ok(raw && typeof(raw) === 'object', 'raw map is a valid object') | ||
const balances = {} | ||
Object.entries(raw).forEach(([acc, bal]) => balances[acc] = new BN(bal, 10)) | ||
return balances | ||
} | ||
|
||
|
||
module.exports = { getStateRootHash, isValidRootHash, toBNMap } |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also the res here from
verify()
should be renamed toisValid