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

all code styled and new lint rule added #65

Merged
merged 3 commits into from
Sep 19, 2017
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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"sourceType": "module"
},
"rules": {
"object-curly-spacing" : [2, "always"]
}
}
92 changes: 43 additions & 49 deletions src/blockchain/block.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import {isObject} from '../helpers'
import { isObject } from '../helpers'
import * as primitive from '../types/primitive'
import {newType} from '../types/generic'
import {newMessage} from '../types/message'
import {validateHexadecimal} from '../types/validate'
import {hash, verifySignature} from '../crypto'
import { newType } from '../types/generic'
import { newMessage } from '../types/message'
import { validateHexadecimal } from '../types/validate'
import { hash, verifySignature } from '../crypto'

const PROTOCOL_VERSION = 0
const CORE_SERVICE_ID = 0
const PRECOMMIT_MESSAGE_ID = 4
const Block = newType({
size: 112,
fields: {
schema_version: {type: primitive.Uint16, size: 2, from: 0, to: 2},
proposer_id: {type: primitive.Uint16, size: 2, from: 2, to: 4},
height: {type: primitive.Uint64, size: 8, from: 4, to: 12},
tx_count: {type: primitive.Uint32, size: 4, from: 12, to: 16},
prev_hash: {type: primitive.Hash, size: 32, from: 16, to: 48},
tx_hash: {type: primitive.Hash, size: 32, from: 48, to: 80},
state_hash: {type: primitive.Hash, size: 32, from: 80, to: 112}
schema_version: { type: primitive.Uint16, size: 2, from: 0, to: 2 },
proposer_id: { type: primitive.Uint16, size: 2, from: 2, to: 4 },
height: { type: primitive.Uint64, size: 8, from: 4, to: 12 },
tx_count: { type: primitive.Uint32, size: 4, from: 12, to: 16 },
prev_hash: { type: primitive.Hash, size: 32, from: 16, to: 48 },
tx_hash: { type: primitive.Hash, size: 32, from: 48, to: 80 },
state_hash: { type: primitive.Hash, size: 32, from: 80, to: 112 }
}
})
const SystemTime = newType({
size: 12,
fields: {
secs: {type: primitive.Uint64, size: 8, from: 0, to: 8},
nanos: {type: primitive.Uint32, size: 4, from: 8, to: 12}
secs: { type: primitive.Uint64, size: 8, from: 0, to: 8 },
nanos: { type: primitive.Uint32, size: 4, from: 8, to: 12 }
}
})

Expand All @@ -36,24 +36,24 @@ const SystemTime = newType({
* @return {boolean}
*/
export function verifyBlock (data, validators, networkId) {
if (isObject(data) === false) {
if (!isObject(data)) {
return false
} else if (isObject(data.block) === false) {
} else if (!isObject(data.block)) {
return false
} else if (Array.isArray(data.precommits) === false) {
} else if (!Array.isArray(data.precommits)) {
return false
} else if (Array.isArray(validators) === false) {
} else if (!Array.isArray(validators)) {
return false
}

for (var i = 0; i < validators.length; i++) {
for (let i = 0; i < validators.length; i++) {
if (!validateHexadecimal(validators[i])) {
return false
}
}

let blockHash
try {
var blockHash = hash(data.block, Block)
blockHash = hash(data.block, Block)
} catch (error) {
return false
}
Expand All @@ -62,30 +62,29 @@ export function verifyBlock (data, validators, networkId) {
return false
}

var Precommit = newMessage({
const Precommit = newMessage({
size: 90,
network_id: networkId,
protocol_version: PROTOCOL_VERSION,
message_id: PRECOMMIT_MESSAGE_ID,
service_id: CORE_SERVICE_ID,
fields: {
validator: {type: primitive.Uint16, size: 2, from: 0, to: 2},
height: {type: primitive.Uint64, size: 8, from: 2, to: 10},
round: {type: primitive.Uint32, size: 4, from: 10, to: 14},
propose_hash: {type: primitive.Hash, size: 32, from: 14, to: 46},
block_hash: {type: primitive.Hash, size: 32, from: 46, to: 78},
time: {type: SystemTime, size: 12, from: 78, to: 90}
validator: { type: primitive.Uint16, size: 2, from: 0, to: 2 },
height: { type: primitive.Uint64, size: 8, from: 2, to: 10 },
round: { type: primitive.Uint32, size: 4, from: 10, to: 14 },
propose_hash: { type: primitive.Hash, size: 32, from: 14, to: 46 },
block_hash: { type: primitive.Hash, size: 32, from: 46, to: 78 },
time: { type: SystemTime, size: 12, from: 78, to: 90 }
}
})

var validatorsTotalNumber = validators.length
var uniqueValidators = []
var round

for (i = 0; i < data.precommits.length; i++) {
var precommit = data.precommits[i]
const validatorsTotalNumber = validators.length
const uniqueValidators = []
let round

if (isObject(precommit.body) === false) {
for (let i = 0; i < data.precommits.length; i++) {
const precommit = data.precommits[i]
if (!isObject(precommit.body)) {
return false
}

Expand All @@ -94,7 +93,7 @@ export function verifyBlock (data, validators, networkId) {
}

if (precommit.body.validator >= validatorsTotalNumber) {
// validator does not exist
// validator does not exist
return false
}

Expand All @@ -103,38 +102,33 @@ export function verifyBlock (data, validators, networkId) {
}

if (precommit.network_id !== networkId ||
precommit.protocol_version !== PROTOCOL_VERSION ||
precommit.service_id !== CORE_SERVICE_ID ||
precommit.message_id !== PRECOMMIT_MESSAGE_ID) {
precommit.protocol_version !== PROTOCOL_VERSION ||
precommit.service_id !== CORE_SERVICE_ID ||
precommit.message_id !== PRECOMMIT_MESSAGE_ID) {
return false
}

if (precommit.body.height !== data.block.height) {
// wrong height of block in precommit
// wrong height of block in precommit
return false
} else if (precommit.body.block_hash !== blockHash) {
// wrong hash of block in precommit
// wrong hash of block in precommit
return false
}

if (round === undefined) {
round = precommit.body.round
} else if (precommit.body.round !== round) {
// wrong round in precommit
// wrong round in precommit
return false
}

var publicKey = validators[precommit.body.validator]
const publicKey = validators[precommit.body.validator]

if (!verifySignature(precommit.signature, publicKey, precommit.body, Precommit)) {
return false
}
}

if (uniqueValidators.length <= validatorsTotalNumber * 2 / 3) {
// not enough precommits from unique validators
return false
}

return true
return uniqueValidators.length > validatorsTotalNumber * 2 / 3
Copy link
Contributor

@boguslavsky boguslavsky Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nitpick: this option is certainly shorter but previous one was a bit more understandable...

}
Loading