Skip to content
This repository has been archived by the owner on Jun 6, 2019. It is now read-only.

Commit

Permalink
Fixes some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed May 23, 2018
1 parent 03c93af commit 7e50391
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 18 deletions.
62 changes: 44 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const uuid = require('uuid')
const batPublisher = require('bat-publisher')

const SEED_LENGTH = 32
const BATCH_SIZE = 10
const HKDF_SALT = new Uint8Array([ 126, 244, 99, 158, 51, 68, 253, 80, 133, 183, 51, 180, 77, 62, 74, 252, 62, 106, 96, 125, 241, 110, 134, 87, 190, 208, 158, 84, 125, 69, 246, 207, 162, 247, 107, 172, 37, 34, 53, 246, 105, 20, 215, 5, 248, 154, 179, 191, 46, 17, 6, 72, 210, 91, 10, 169, 145, 248, 22, 147, 117, 24, 105, 12 ])
const LEDGER_SERVERS = {
'staging': {
Expand Down Expand Up @@ -198,19 +199,19 @@ Client.prototype.sync = function (callback) {
transaction = underscore.find(self.state.transactions, function (transaction) {
return ((transaction.credential) &&
(ballot.viewingId === transaction.viewingId) &&
((!ballot.prepareBallot) || (!ballot.delayStamp) || (ballot.delayStamp <= now)))
((!ballot.delayStamp) || (ballot.delayStamp <= now)))
})
if (!transaction) continue

if (!ballot.prepareBallot) return self._prepareBallot(ballot, transaction, callback)
if (!ballot.proofBallot) return self._proofBallot(ballot, transaction, callback)
}

if (ballots && ballots.length > 0 && (!self.state.batch || self.state.batch.length === 0)) {
self._prepareBatch()
if (ballots && ballots.length > 0 && (!self.state.batch || Object.keys(self.state.batch).length === 0)) {
return self._prepareBatch(callback)
}

if (self.state.batch && self.state.batch.length > 0) {
if (self.state.batch && Object.keys(self.state.batch).length > 0) {
return self._voteBatch(callback)
}

Expand Down Expand Up @@ -1095,20 +1096,41 @@ Client.prototype._proofBallot = function (ballot, transaction, callback) {
})
}

Client.prototype._prepareBatch = function () {
Client.prototype._prepareBatch = function (callback) {
let batch = {}
const transactions = this.state.transactions
const self = this
const now = underscore.now()
const transactions = self.state.transactions

if (!Array.isArray(self.state.ballots)) {
return callback(new Error('Ballots are not an array'))
}

this.state.ballots.forEach((ballot) => {
for (let i = self.state.ballots.length - 1; i >= 0; i--) {
const ballot = self.state.ballots[i]
let transaction = underscore.find(transactions, function (transaction) {
return ((transaction.credential) &&
(ballot.viewingId === transaction.viewingId) &&
((!ballot.prepareBallot) || (!ballot.delayStamp) || (ballot.delayStamp <= now)))
return transaction.credential &&
ballot.viewingId === transaction.viewingId &&
(
!ballot.delayStamp ||
ballot.delayStamp <= now
)
})

if (!transaction.ballots) transaction.ballots = {}
if (!transaction.ballots[ballot.publisher]) transaction.ballots[ballot.publisher] = 0
if (!transaction) continue

if (!ballot.prepareBallot || !ballot.proofBallot) {
return callback(new Error('Ballot is not ready'))
}

if (!transaction.ballots) {
transaction.ballots = {}
}

if (!transaction.ballots[ballot.publisher]) {
transaction.ballots[ballot.publisher] = 0
}

transaction.ballots[ballot.publisher]++

if (!batch[ballot.publisher]) batch[ballot.publisher] = []
Expand All @@ -1117,10 +1139,14 @@ Client.prototype._prepareBatch = function () {
surveyorId: ballot.prepareBallot.surveyorId,
proof: ballot.proofBallot.proof
})
})

this.state.ballots = []
this.state.batch = batch
self.state.ballots.splice(i, 1)
}

self.state.batch = batch

const delayTime = random.randomInt({ min: 10 * msecs.second, max: 1 * msecs.minute })
callback(null, self.state, delayTime)
}

Client.prototype._voteBatch = function (callback) {
Expand All @@ -1139,14 +1165,14 @@ Client.prototype._voteBatch = function (callback) {
const publisher = self.state.batch[keys[0]]
let payload

if (publisher.length > 10) {
payload = publisher.splice(0, 10)
if (publisher.length > BATCH_SIZE) {
payload = publisher.splice(0, BATCH_SIZE)
} else {
payload = publisher
}

self._retryTrip(self, { path: path, method: 'POST', useProxy: true, payload: payload }, function (err, response, body) {
self._log('_voteBatch', { method: 'PUT', path: path + '...', errP: !!err })
self._log('_voteBatch', { method: 'POST', path: path + '...', errP: !!err })
// TODO add error to the specific transaction
if (err || !body) return callback(err)

Expand Down
102 changes: 102 additions & 0 deletions test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,105 @@ test('_fuzzing', (t) => {
client._fuzzing(synopsisTime, () => {})
t.equal(client.state.reconcileStamp, tomorrow, 'Should not change if stamp is in tomorrow and browsing time is above 30min')
})

test('_prepareBatch', (t) => {
t.plan(9)
const client = new Ledger(null, options)
const callback = () => {}

client._prepareBatch(callback)
t.deepEqual(client.state.batch, {}, 'Should be empty for null case')

client.state = {
batch: {},
ballots: null
}
client._prepareBatch(callback)
t.deepEqual(client.state.batch, {}, 'Should be empty when we do not have ballots')

client.state = {
batch: {},
ballots: null
}
client._prepareBatch(callback)
t.deepEqual(client.state.batch, {}, 'Should be empty for null case')

client.state = {
batch: {},
ballots: [{
publisher: 'clifton.io',
viewingId: '123a',
prepareBallot: {
surveyorId: '12323'
},
proofBallot: {
proof: 'dfdsfsd'
}
}],
transactions: []
}
client._prepareBatch(callback)
t.deepEqual(client.state.batch, {}, 'Should be empty when there is no transaction')

client.state = {
batch: {},
ballots: [{
publisher: 'clifton.io',
viewingId: '123a'
}],
transactions: [{
viewingId: '123a',
credential: '12'
}]
}
client._prepareBatch(callback)
t.deepEqual(client.state.batch, {}, 'Should be empty when ballot is missing proof and prepare')

client.state = {
batch: {},
ballots: [{
publisher: 'clifton.io',
viewingId: '123a',
prepareBallot: {
surveyorId: '12323'
},
proofBallot: {
proof: 'dfdsfsd'
}
}],
transactions: [{
viewingId: '123a',
credential: '12'
}]
}
client._prepareBatch(callback)
t.deepEqual(client.state.batch, {
'clifton.io': [{
surveyorId: '12323',
proof: 'dfdsfsd'
}]
}, 'Should have one batch')
t.deepEqual(client.state.ballots, [], 'Should not have any ballots when we move it into the batch')

client.state = {
batch: {},
ballots: [{
publisher: 'clifton.io',
viewingId: '123a',
prepareBallot: {
surveyorId: '12323'
},
proofBallot: {
proof: 'dfdsfsd'
},
delayStamp: new Date().getTime() + 10000000
}],
transactions: [{
viewingId: '123a',
credential: '12'
}]
}
client._prepareBatch(callback)
t.deepEqual(client.state.batch, {}, 'Should not have any batches when delay is provided')
t.equal(client.state.ballots.length, 1, 'Should not delete ballots when delay is provided')
})

0 comments on commit 7e50391

Please sign in to comment.