Skip to content
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

Add method to verify table proof #138

Merged
merged 2 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.13.0 (Oct 5, 2018)

* Add a new `verifyTable` method to verify table existence in the root tree. ([#138][pr-138])

## 0.12.5 (Oct 5, 2018)

* Change package used to replace library version via Grunt. ([#137][pr-137])
Expand Down Expand Up @@ -145,6 +149,7 @@ matching [release 0.1][release-0.1] of the Exonum core repository.
[release-0.7]: https://github.com/exonum/exonum/blob/master/CHANGELOG.md#07---2018-04-11
[release-0.5]: https://github.com/exonum/exonum/blob/master/CHANGELOG.md#05---2018-01-30
[release-0.1]: https://github.com/exonum/exonum/releases/tag/v0.1
[pr-138]: https://github.com/exonum/exonum-client/pull/138
[pr-137]: https://github.com/exonum/exonum-client/pull/137
[pr-133]: https://github.com/exonum/exonum-client/pull/133
[pr-126]: https://github.com/exonum/exonum-client/pull/126
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Library compatibility with Exonum core:

| JavaScript light client | Exonum core |
|---|---|
| 0.12.4 (latest) | 0.9.* |
| 0.13.0 | 0.9.* |
| 0.10.2 | 0.8.* |
| 0.9.0 | 0.7.* |
| 0.6.1 | 0.6.* |
Expand Down Expand Up @@ -56,6 +56,7 @@ Library compatibility with Exonum core:
* [Map proof](#map-proof)
* [Integrity checks](#integrity-checks)
* [Verify block](#verify-block)
* [Verify table](#verify-table)
* [Helpers](#helpers)
* [Generate key pair](#generate-key-pair)
* [Get random number](#get-random-number)
Expand Down Expand Up @@ -697,6 +698,23 @@ Returns `true` if verification is succeeded or `false` if it is failed.

An example of [block verification](examples/block.md).

### Verify table

```javascript
Exonum.verifyTable(proof, stateHash, serviceId, tableIndex)
```

Verify table existence in the root tree.

Returns root hash for the table as hexadecimal `String`.

| Argument | Description | Type |
|---|---|---|
| **proof** | The JSON presentation of the proof obtained from a full node. | `Object` |
| **stateHash** | Hash of current blockchain state stored in each block. | `String` |
| **serviceId** | [Service ID][docs:architecture:serialization:service-id]. | `Number` |
| **tableIndex** | Table index. | `Number` |

## Helpers

### Generate key pair
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exonum-client",
"version": "0.12.5",
"version": "0.13.0",
"description": "Light Client for Exonum Blockchain",
"main": "./lib/index.js",
"engines": {
Expand Down
1 change: 1 addition & 0 deletions src/blockchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './merkle'
export * from './merkle-patricia'
export * from './block'
export * from './transport'
export * from './table'
43 changes: 43 additions & 0 deletions src/blockchain/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Uint16 } from '../types/primitive'
import { newType } from '../types/generic'
import { Hash } from '../types/hexadecimal'
import { MapProof } from './merkle-patricia'

const TableKey = newType({
fields: [
{ name: 'service_id', type: Uint16 },
{ name: 'table_index', type: Uint16 }
]
})

/**
* Validate path from tree root to some table
* @param {Object} proof
* @param {string} stateHash
* @param {number} serviceId
* @param {number} tableIndex
* @returns {string}
*/
export function verifyTable (proof, stateHash, serviceId, tableIndex) {
// calculate key of table in the root tree
const key = TableKey.hash({
service_id: serviceId,
table_index: tableIndex
})

// validate proof of table existence in root tree
const tableProof = new MapProof(proof, Hash, Hash)

if (tableProof.merkleRoot !== stateHash) {
throw new Error('Table proof is corrupted')
}

// get root hash of the table
const rootHash = tableProof.entries.get(key)

if (typeof rootHash === 'undefined') {
throw new Error('Table not found in the root tree')
}

return rootHash
}
63 changes: 63 additions & 0 deletions test/sources/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,69 @@ describe('Verify block of precommits', function () {
})
})

describe('Verify table existence', function () {
const proof = {
entries: [
{
key: '775be457774803ff0221f0d18f407c9718a2f4c635445a691f6061bd5d651581',
value: '540c35c61837aa819986e137b6ae0091faa781419e8234802094ef77a2cdc293'
}
],
proof: [
{
path: '0000101010101110110000001010110110011000000001100011001110110111000101011001101100100100000010011111001000011101110010101110111001111111101111101110100011111110000111011111101111110011011010100100110101110010101000101110101000100110011100100010101101100001',
hash: '0000000000000000000000000000000000000000000000000000000000000000'
},
{
path: '1101',
hash: '9d57d84328c5fce997b0dd0cf806c114a37ebf32bd1b966154244548206acd27'
},
{
path: '1110011011010101101110110100111000001000001001000000111111111111011100101101000011111100001100101111010010000011110111001010001101011101001010111011010011010000000111101000101000101011011010100001101110110001000001001011110010101000010101010010010100001010',
hash: '0000000000000000000000000000000000000000000000000000000000000000'
},
{
path: '1111101111111100100001100001100100100000100101011111010011011011000000101110101010011000101101000010001110111100111010110001001001010111111011100101000100111011010010100011110110010010001100010001011110100000001001000000001100101000000111011000100010011000',
hash: '15ca2aa05d8f054125ee327ac35805c5a20d9305aae10880dc56373219126a47'
}
]
}
const serviceId = 130
const tableIndex = 0
const stateHash = 'dd09c5ec10fe7eb5d82aeabf362772440880394dfabb0a4c4cfbfdecc2d6bad7'
const rootHash = '540c35c61837aa819986e137b6ae0091faa781419e8234802094ef77a2cdc293'

it('should return root hash when valid proof', function () {
expect(Exonum.verifyTable(proof, stateHash, serviceId, tableIndex)).to.equals(rootHash)
})

it('should throw error when wrong service ID is passed', function () {
const endpoints = [null, false, 'Hello world', new Date(), {}, []]

endpoints.forEach(function (value) {
expect(() => Exonum.verifyTable(proof, stateHash, value, tableIndex))
.to.throw(Error, `Uint16 of wrong type is passed: ${value}`)
})
})

it('should throw error when wrong table index is passed', function () {
const endpoints = [null, false, 'Hello world', new Date(), {}, []]

endpoints.forEach(function (value) {
expect(() => Exonum.verifyTable(proof, stateHash, serviceId, value))
.to.throw(Error, `Uint16 of wrong type is passed: ${value}`)
})
})

it('should throw error when wrong proof is passed', function () {
const endpoints = [null, false, 42, 'Hello world', new Date(), {}, []]

endpoints.forEach(function (value) {
expect(() => Exonum.verifyTable(value, stateHash, serviceId, tableIndex)).to.throw(Error)
})
})
})

describe('Send transaction to the blockchain', function () {
let sendFunds = Exonum.newMessage({
protocol_version: 0,
Expand Down