Commit some new networking code, adding integration, broker still not 100%, hasn't been committed
This commit is contained in:
parent
1af6d56e2d
commit
050e69e23f
18 changed files with 1829 additions and 359 deletions
|
@ -20,8 +20,21 @@ function getData(block, key) {
|
|||
}
|
||||
}
|
||||
|
||||
const acceptableMembers = new Set();
|
||||
acceptableMembers.add("timestamp");
|
||||
acceptableMembers.add("lastHash");
|
||||
acceptableMembers.add("hash");
|
||||
acceptableMembers.add("reward");
|
||||
acceptableMembers.add("payments");
|
||||
acceptableMembers.add("sensorRegistrations");
|
||||
acceptableMembers.add("brokerRegistrations");
|
||||
acceptableMembers.add("integrations");
|
||||
acceptableMembers.add("compensations");
|
||||
acceptableMembers.add("nonce");
|
||||
acceptableMembers.add("difficulty");
|
||||
|
||||
class Block {
|
||||
constructor(timestamp, lastHash, hash, reward, payments, sensorRegistrations, brokerRegistrations, integrations, nonce, difficulty) {
|
||||
constructor(timestamp, lastHash, hash, reward, payments, sensorRegistrations, brokerRegistrations, integrations, compensations, nonce, difficulty) {
|
||||
this.timestamp = timestamp;
|
||||
this.lastHash = lastHash;
|
||||
this.hash = hash;
|
||||
|
@ -38,6 +51,9 @@ class Block {
|
|||
if (integrations !== null && integrations.length !== 0) {
|
||||
this.integrations = integrations;
|
||||
}
|
||||
if (compensations !== null && compensations.length !== 0) {
|
||||
this.compensations = compensations;
|
||||
}
|
||||
this.nonce = nonce;
|
||||
if (difficulty === undefined) {
|
||||
this.difficulty = DIFFICULTY;
|
||||
|
@ -62,6 +78,10 @@ class Block {
|
|||
return getData(block, "integrations");
|
||||
}
|
||||
|
||||
static getCompensations(block) {
|
||||
return getData(block, "compensations");
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `Block -
|
||||
Timestamp : ${this.timestamp}
|
||||
|
@ -78,7 +98,7 @@ class Block {
|
|||
return new this('Genesis time', '-----', 'f1r57-h45h', null, null, null, null, null, 0, DIFFICULTY);
|
||||
}
|
||||
|
||||
static hash(timestamp, lastHash, reward, payments, sensorRegistrations, brokerRegistrations, integrations, nonce, difficulty) {
|
||||
static hash(timestamp, lastHash, reward, payments, sensorRegistrations, brokerRegistrations, integrations, compensations, nonce, difficulty) {
|
||||
//backwards compatible hashing:
|
||||
//if we add a new type of thing to the chain, the hash of previous blocks won't change as it will be undefined
|
||||
let hashing = `${timestamp}${lastHash}${nonce}${difficulty}${reward}`;
|
||||
|
@ -86,6 +106,7 @@ class Block {
|
|||
hashing = concatIfNotUndefined(hashing, 'sensorRegistrations', sensorRegistrations);
|
||||
hashing = concatIfNotUndefined(hashing, 'brokerRegistrations', brokerRegistrations);
|
||||
hashing = concatIfNotUndefined(hashing, 'integrations', integrations);
|
||||
hashing = concatIfNotUndefined(hashing, 'compensations', compensations);
|
||||
|
||||
return ChainUtil.hash(hashing).toString();
|
||||
}
|
||||
|
@ -99,6 +120,7 @@ class Block {
|
|||
block.sensorRegistrations,
|
||||
block.brokerRegistrations,
|
||||
block.integrations,
|
||||
block.compensations,
|
||||
block.nonce,
|
||||
block.difficulty);
|
||||
}
|
||||
|
@ -127,7 +149,7 @@ class Block {
|
|||
}
|
||||
}
|
||||
|
||||
static debugMine(lastBlock, reward, payments, sensorRegistrations,brokerRegistrations,integrations) {
|
||||
static debugMine(lastBlock, reward, payments, sensorRegistrations,brokerRegistrations,integrations,compensations) {
|
||||
const timestamp = Date.now();
|
||||
const difficulty = Block.adjustDifficulty(lastBlock, timestamp);
|
||||
|
||||
|
@ -144,6 +166,7 @@ class Block {
|
|||
sensorRegistrations,
|
||||
brokerRegistrations,
|
||||
integrations,
|
||||
compensations,
|
||||
nonce,
|
||||
difficulty);
|
||||
} while (hash.substring(0, difficulty) !== '0'.repeat(difficulty));
|
||||
|
@ -157,9 +180,116 @@ class Block {
|
|||
sensorRegistrations,
|
||||
brokerRegistrations,
|
||||
integrations,
|
||||
compensations,
|
||||
nonce,
|
||||
difficulty);
|
||||
}
|
||||
|
||||
static validateIsBlock(block) {
|
||||
if (!(block instanceof Object)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not an object"
|
||||
};
|
||||
}
|
||||
|
||||
for (const key in block) {
|
||||
if (!acceptableMembers.has(key)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: `Block has key not in acceptable members`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (!("timestamp" in block)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Block doesn't have a timestamp"
|
||||
};
|
||||
}
|
||||
const timestampRes = ChainUtil.validateIsIntegerWithMin(block.timestamp, 0);
|
||||
if (!timestampRes.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Timestamp validation failed: " + timestampRes.reason
|
||||
};
|
||||
}
|
||||
|
||||
if (!("lastHash" in block)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Block doesn't have lastHash"
|
||||
};
|
||||
}
|
||||
const lastHashRes = ChainUtil.validateIsString(block.lastHash);
|
||||
if (!lastHashRes.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "lastHash validation failed: " + lastHashRes.reason
|
||||
};
|
||||
}
|
||||
|
||||
if (!("hash" in block)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Block doesn't have hash"
|
||||
};
|
||||
}
|
||||
const hashRes = ChainUtil.validateIsString(block.hash);
|
||||
if (!hashRes.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "hash validation failed: " + hashRes.reason
|
||||
};
|
||||
}
|
||||
|
||||
if (!("reward" in block)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Block doesn't have reward"
|
||||
};
|
||||
}
|
||||
const rewardRes = ChainUtil.validateIsPublicKey(block.reward);
|
||||
if (!rewardRes.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "reward validation failed: " + rewardRes.reason
|
||||
};
|
||||
}
|
||||
|
||||
if (!("nonce" in block)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Block doesn't have nonce"
|
||||
};
|
||||
}
|
||||
const nonceRes = ChainUtil.validateIsIntegerWithMin(block.nonce);
|
||||
if (!nonceRes.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "nonce validation failed: " + nonceRes.reason
|
||||
};
|
||||
}
|
||||
|
||||
if (!("difficulty" in block)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Block doesn't have difficulty"
|
||||
};
|
||||
}
|
||||
const difficultyRes = ChainUtil.validateIsIntegerWithMin(block.difficulty);
|
||||
if (!difficultyRes.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "difficulty validation failed: " + difficultyRes.reason
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Block;
|
Loading…
Add table
Add a link
Reference in a new issue