Skip to content

Commit c178b1c

Browse files
committed
general bugfixes following integration testing
1 parent fb7089d commit c178b1c

File tree

9 files changed

+43
-28
lines changed

9 files changed

+43
-28
lines changed

src/ChluIPFS.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ class ChluIPFS {
124124
this.ipfsUtils = new IPFSUtils(this);
125125
this.orbitDb = new DB(this, options.OrbitDBIndex || options.OrbitDBIndexName, options.OrbitDBIndexOptions);
126126
this.pinning = new Pinning(this);
127-
this.room = new Room(this);
127+
this.room = new Room(this, options.ignoreOwnMessages);
128128
this.reviewRecords = new ReviewRecords(this);
129129
this.validator = new Validator(this);
130130
this.persistence = new Persistence(this);
131-
this.didIpfsHelper = new DIDIPFSHelper(this);
131+
this.didIpfsHelper = new DIDIPFSHelper(this, options.did);
132132
this.crypto = new Crypto(this);
133133
this.bitcoin = new Bitcoin(this, {
134134
apiKey: options.blockCypherApiKey,

src/modules/didIpfsHelper.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,27 @@ class ChluDIDIPFSHelper {
88
return typeof didId === 'string' && didId.indexOf('did:') === 0
99
}
1010

11-
constructor(chluIpfs) {
11+
constructor(chluIpfs, did = null) {
1212
this.chluIpfs = chluIpfs
1313
// ChluDID instance
1414
this.chluDID = new ChluDID()
1515
// Well Known DIDs
1616
// used for hardcoded DIDs in tests
1717
// and will also be used for stuff like official Chlu trusted DIDs
1818
this.wellKnownDIDs = {}
19+
this.didToImport = did
1920
this.didId = null
2021
this.publicDidDocument = null
2122
this.privateKeyBase58 = null
2223
}
2324

2425
async start() {
2526
if (!this.isPresent()) {
26-
// Generate a DID & Publish
27-
await this.generate();
28-
await this.chluIpfs.persistence.persistData();
27+
if (this.didToImport) {
28+
await this.import(this.didToImport)
29+
} else {
30+
await this.generate();
31+
}
2932
}
3033
}
3134

src/modules/orbitdb/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class DB {
114114
}
115115

116116
async getDID(didId, waitUntilPresent = false) {
117-
// TODO: move sig checking to orbit-db updateIndex
118117
let result = null, firstTry = true
119118
this.chluIpfs.logger.info(`getDID (OrbitDB) ${didId} => ...`)
120119
while(!get(result, 'multihash') && (firstTry || waitUntilPresent)) {

src/modules/orbitdb/indexes/abstract.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ChluAbstractIndex {
5959
subjectDidId
6060
});
6161
}
62+
this.chluIpfs.events.emit('discover/reviewrecord', item.payload.multihash);
6263
} catch (error) {
6364
this.chluIpfs.logger.error(`Error while updating ChluDB Index: ${error.message}`)
6465
console.log(error)
@@ -69,6 +70,7 @@ class ChluAbstractIndex {
6970
if (this.enableWrites) {
7071
await this.putDID(publicDidDocument, item.payload.multihash)
7172
}
73+
this.chluIpfs.events.emit('discover/did', publicDidDocument.id);
7274
} catch (error) {
7375
this.chluIpfs.logger.error(`Error while updating ChluDB Index: ${error.message}`)
7476
console.log(error)

src/modules/orbitdb/indexes/sql.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class ChluSQLIndex extends ChluAbstractIndex {
5252
})
5353
this.chluIpfs.logger.debug('ChluDB SQL Index: Syncing => ...')
5454
await this.sequelize.sync()
55-
this.chluIpfs.logger.debug('ChluDB SQL Index: Syncing => OK, READY')
55+
this.chluIpfs.logger.debug('ChluDB SQL Index: Syncing => OK')
56+
if (this.options.clearOnStart) await this.clear()
5657
}
5758

5859
async clear() {
@@ -176,10 +177,11 @@ class ChluSQLIndex extends ChluAbstractIndex {
176177
const metadata = Object.assign({
177178
bitcoinTransactionHash: entity.bitcoinTransactionHash
178179
}, entity.data.metadata)
179-
return formatReviewRecords([entity]).map(x => {
180+
const array = formatReviewRecords([entity]).map(x => {
180181
x.metadata = metadata
181182
return x
182183
})
184+
return array[0]
183185
}
184186
return { metadata: null, multihash }
185187
}
@@ -295,7 +297,7 @@ class ChluSQLIndex extends ChluAbstractIndex {
295297
function formatReviewRecords(list) {
296298
return list.map(v => ({
297299
multihash: get(v, 'data.multihash', null),
298-
reviewRecord: get(v, 'latestVersionData', get(v, 'data', null)),
300+
reviewRecord: get(v, 'latestVersionData', null) || get(v, 'data', null),
299301
reviewRecordOriginal: get(v, 'data', null)
300302
}))
301303
}

src/modules/reviewrecords.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ class ReviewRecords {
2424
[...this.watched].forEach(async item => {
2525
try {
2626
const { multihash, validate } = item;
27-
const update = await this.chluIpfs.orbitDb.getLatestReviewRecordUpdate(multihash);
27+
const result = await this.chluIpfs.orbitDb.getLatestReviewRecordUpdate(multihash);
28+
const update = result.multihash
29+
let reviewRecord = get(result, 'reviewRecord', null)
2830
if (update && update !== multihash) {
29-
const reviewRecord = await this.readReviewRecord(multihash, { validate });
30-
// TODO: validate update!!
31+
if (!reviewRecord) {
32+
reviewRecord = await this.readReviewRecord(multihash, { validate });
33+
}
3134
const i = findIndex(this.watched, o => o.multihash === multihash);
3235
this.watched.splice(i, 1, Object.assign(this.watched[i], {
3336
multihash: update
@@ -149,7 +152,10 @@ class ReviewRecords {
149152
}
150153
reviewRecord.history = await this.getHistory(reviewRecord)
151154
reviewRecord.history = await Promise.all(
152-
reviewRecord.history.map(async rr => await this.resolveReviewRecord(rr, useCache))
155+
reviewRecord.history.map(async h => {
156+
h.reviewRecord = await this.resolveReviewRecord(h.reviewRecord, useCache)
157+
return h
158+
})
153159
)
154160
if (reviewRecord.popr) {
155161
reviewRecord.popr = await this.resolvePoPR(reviewRecord.popr, useCache)
@@ -191,11 +197,7 @@ class ReviewRecords {
191197
}
192198

193199
isVerifiable(reviewRecord) {
194-
return (
195-
!isEmpty(reviewRecord.customer_address)
196-
&& !isEmpty(reviewRecord.currency_symbol)
197-
&& !isEmpty(reviewRecord.popr)
198-
)
200+
return Boolean(get(reviewRecord, 'verifiable', false))
199201
}
200202

201203
async prepareReviewRecord(reviewRecord, bitcoinTransactionHash = null, validate = true, signAsCustomer = true, signAsIssuer = true) {

src/modules/room.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const constants = require('../constants');
33

44
class Room {
55

6-
constructor(chluIpfs) {
6+
constructor(chluIpfs, ignoreOwnMessages = true) {
77
this.chluIpfs = chluIpfs;
88
this.subscription = null;
99
this.topic = null;
1010
this.peers = [];
11+
this.ignoreOwnMessages = ignoreOwnMessages
1112
}
1213

1314
async start() {
@@ -133,7 +134,7 @@ class Room {
133134
async handleMessage(message) {
134135
try {
135136
const myId = await this.chluIpfs.ipfsUtils.id();
136-
if (message.from !== myId) {
137+
if (!this.ignoreOwnMessages || message.from !== myId) {
137138
const str = message.data.toString();
138139
this.chluIpfs.logger.debug('Handling PubSub message from ' + message.from + ': ' + str);
139140
const obj = parseMessage(message);

src/modules/validator.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,17 @@ class Validator {
116116
});
117117
const history = reviewRecord.history || []
118118
if (history.length > 0) {
119-
const reviewRecords = [reviewRecord].concat(history);
120-
const validations = reviewRecords.map(async (reviewRecord, i) => {
119+
const reviewRecords = [{ reviewRecord }].concat(history);
120+
const validations = reviewRecords.map(async (data, i) => {
121+
const reviewRecord = data.reviewRecord
121122
if (!this.chluIpfs.reviewRecords.isVerifiable(reviewRecord)) {
122123
throw new Error('Cannot update an unverified review')
123124
}
124-
await this.validateReviewRecord(reviewRecord, v);
125+
if (i > 0) {
126+
await this.validateReviewRecord(reviewRecord, v);
127+
}
125128
if (i !== reviewRecords.length-1) {
126-
this.validatePrevious(reviewRecord, reviewRecords[i+1]);
129+
this.validatePrevious(reviewRecord, reviewRecords[i+1].reviewRecord);
127130
}
128131
});
129132
await Promise.all(validations);
@@ -237,9 +240,9 @@ class Validator {
237240

238241
validatePrevious(reviewRecord, previousVersion) {
239242
// Check that the PoPR was not changed
240-
const poprEqual = isEqual(reviewRecord.popr, previousVersion.popr);
243+
const poprEqual = reviewRecord.popr.hash === previousVersion.popr.hash
241244
if (!poprEqual) {
242-
throw new Error('PoPR was changed');
245+
throw new Error('PoPR hash was changed when updating the review');
243246
}
244247
// Check other fields
245248
assertFieldsEqual(previousVersion, reviewRecord, [

tests/Validator.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ describe('Validator Module', () => {
155155
reviewRecord3.rating = 3;
156156
reviewRecord3.previous_version_multihash = 'QmQ6vGTgqjec2thBj5skqfPUZcsSuPAbPS7XvkqaYNQVP2';
157157
reviewRecord3.multihash = 'QmQ6vGTgqjec2thBj5skqfPUZcsSuPAbPS7XvkqaYNQVP3';
158-
reviewRecord3.history = [reviewRecord2, reviewRecord]
158+
reviewRecord3.history = [
159+
{ multihash: reviewRecord2.multihash, reviewRecord: reviewRecord2 },
160+
{ multihash: reviewRecord.multihash, reviewRecord }
161+
]
159162
let map = {
160163
'QmQ6vGTgqjec2thBj5skqfPUZcsSuPAbPS7XvkqaYNQVP1': reviewRecord,
161164
'QmQ6vGTgqjec2thBj5skqfPUZcsSuPAbPS7XvkqaYNQVP2': reviewRecord2

0 commit comments

Comments
 (0)