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
|
@ -1,23 +1,32 @@
|
|||
const ChainUtil = require('../chain-util');
|
||||
const SeedRandom = require('seedrandom');
|
||||
|
||||
const outputValidation = {
|
||||
publicKey: ChainUtil.validateIsPublicKey,
|
||||
sensor: ChainUtil.validateIsString,
|
||||
amount: ChainUtil.createValidateIsIntegerWithMin(1),
|
||||
counter: ChainUtil.createValidateIsIntegerWithMin(1)
|
||||
};
|
||||
|
||||
function validateOutputs(t) {
|
||||
if (!ChainUtil.validateArray(t, (output) => {
|
||||
return ChainUtil.validateObject(output, outputValidation).result;
|
||||
})) {
|
||||
return false;
|
||||
const validateArrayRes = ChainUtil.validateArray(t, (output) => {
|
||||
return ChainUtil.validateObject(output, outputValidation);
|
||||
});
|
||||
|
||||
if (!validateArrayRes.result) {
|
||||
return validateArrayRes;
|
||||
}
|
||||
|
||||
if (t.outputs.length <= 0) {
|
||||
return false;
|
||||
if (t.length <= 0) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Integration must have at least 1 output"
|
||||
};
|
||||
}
|
||||
|
||||
return true;
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
const baseValidation = {
|
||||
|
@ -25,17 +34,19 @@ const baseValidation = {
|
|||
counter: ChainUtil.createValidateIsIntegerWithMin(1),
|
||||
rewardAmount: ChainUtil.createValidateIsIntegerWithMin(0),
|
||||
outputs: validateOutputs,
|
||||
witnessCount: ChainUtil.createValidateIsIntegerWithMin(0),
|
||||
signature: ChainUtil.validateIsSignature
|
||||
};
|
||||
|
||||
class Integration {
|
||||
constructor(senderKeyPair, counter, outputs, rewardAmount) {
|
||||
constructor(senderKeyPair, counter, outputs, witnessCount, rewardAmount) {
|
||||
this.input = senderKeyPair.getPublic().encode('hex');
|
||||
this.counter = counter;
|
||||
this.rewardAmount = rewardAmount;
|
||||
this.outputs = outputs;
|
||||
this.signature = senderKeyPair.sign(Integration.hashToSign(this));
|
||||
this.witnessCount = witnessCount;
|
||||
|
||||
this.signature = senderKeyPair.sign(Integration.hashToSign(this));
|
||||
|
||||
const verification = Integration.verify(this);
|
||||
if (!verification.result) {
|
||||
|
@ -43,31 +54,33 @@ class Integration {
|
|||
}
|
||||
}
|
||||
|
||||
static createOutput(recipientPublicKey, sensorId, amount) {
|
||||
static createOutput(recipientPublicKey, sensorId, amount, counter) {
|
||||
return {
|
||||
publicKey: recipientPublicKey,
|
||||
sensor: sensorId,
|
||||
amount: amount
|
||||
amount: amount,
|
||||
counter: counter
|
||||
};
|
||||
}
|
||||
|
||||
static hashToSign(registration) {
|
||||
static hashToSign(integration) {
|
||||
return ChainUtil.hash([
|
||||
registration.counter,
|
||||
registration.rewardAmount,
|
||||
registration.outputs]);
|
||||
integration.counter,
|
||||
integration.rewardAmount,
|
||||
integration.witnesses,
|
||||
integration.outputs]);
|
||||
}
|
||||
|
||||
static verify(registration) {
|
||||
const validationRes = ChainUtil.validateObject(registration, baseValidation);
|
||||
static verify(integration) {
|
||||
const validationRes = ChainUtil.validateObject(integration, baseValidation);
|
||||
if (!validationRes.result) {
|
||||
return validationRes;
|
||||
}
|
||||
|
||||
const verifyRes = ChainUtil.verifySignature(
|
||||
registration.input,
|
||||
registration.signature,
|
||||
Integration.hashToSign(registration));
|
||||
integration.input,
|
||||
integration.signature,
|
||||
Integration.hashToSign(integration));
|
||||
if (!verifyRes.result) {
|
||||
return verifyRes;
|
||||
}
|
||||
|
@ -76,6 +89,45 @@ class Integration {
|
|||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
static chooseWitnesses(integration, brokerList) {
|
||||
const brokerListCopy = [...brokerList];
|
||||
brokerListCopy.sort();
|
||||
|
||||
const witnessCount = integration.witnessCount;
|
||||
|
||||
if (witnessCount > brokerList.length) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Not enough brokers for the number of witnesses requested"
|
||||
};
|
||||
}
|
||||
|
||||
if (witnessCount === brokerList.length) {
|
||||
return {
|
||||
result: true,
|
||||
witnesses: brokerListCopy
|
||||
};
|
||||
}
|
||||
|
||||
const rng = new SeedRandom.alea(integration.signature, Integration.hashToSign(integration));
|
||||
|
||||
const witnesses = [];
|
||||
|
||||
for (var i = 0; i < witnessCount; ++i) {
|
||||
const chosen = Math.floor(rng() * brokerListCopy.length);
|
||||
|
||||
witnesses.push(brokerListCopy[chosen]);
|
||||
brokerListCopy[chosen] = brokerListCopy[brokerListCopy.length - 1];
|
||||
brokerListCopy.pop();
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
result: true,
|
||||
witnesses: witnesses
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Integration;
|
Loading…
Add table
Add a link
Reference in a new issue