Skip to content

Commit f8701b0

Browse files
committed
refactor: support sync on multiple networks
1 parent 516cfc6 commit f8701b0

19 files changed

+215
-110
lines changed

lib/actions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/** @module actions */
22

33
const EventEmitter = require('events').EventEmitter
4-
const pick = require('lodash/pick')
5-
const extend = require('lodash/extend')
64
const debug = require('debug')('tradle:actions')
75
const protocol = require('@tradle/protocol')
86
const typeforce = require('./typeforce')
@@ -76,7 +74,7 @@ module.exports = function (opts) {
7674
throw new Error('expected either both prevLink and sealPrevAddress or neither')
7775
}
7876

79-
const action = pick(data, WROTE_SEAL_COPY_PROPS)
77+
const action = utils.pick(data, WROTE_SEAL_COPY_PROPS)
8078
action.status = SealStatus.sealed
8179
action.txId = tx.txId
8280
action.topic = topics.wroteseal
@@ -170,6 +168,8 @@ module.exports = function (opts) {
170168

171169
function createWatch (watch, cb) {
172170
typeforce({
171+
blockchain: typeforce.String,
172+
networkName: typeforce.String,
173173
address: typeforce.String,
174174
link: typeforce.String,
175175
headerHash: typeforce.String,

lib/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @augments tradle/protocol/lib/constants
55
*/
66

7-
const extend = require('xtend/mutable')
7+
const extend = require('lodash/extend')
88
const constants = require('@tradle/constants')
99
const { TYPES } = constants
1010

lib/dbs/addressBook.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const subdown = require('subleveldown')
99
const debug = require('debug')('tradle:addressBook')
1010
const indexer = require('feed-indexer')
1111
const protocol = require('@tradle/protocol')
12-
const clone = require('clone')
1312
// const LiveStream = require('level-live-stream')
1413
const constants = require('../constants')
1514
const PERMALINK = constants.PERMALINK
@@ -166,7 +165,7 @@ module.exports = function createAddressBook (opts) {
166165
const cached = getCachedBy(prop, val)
167166
if (cached) {
168167
// return defensive copy
169-
return process.nextTick(() => cb(null, clone(cached)))
168+
return process.nextTick(() => cb(null, utils.cloneDeep(cached)))
170169
}
171170

172171
findOneByPropInDB(prop, val, cb)
@@ -223,7 +222,7 @@ module.exports = function createAddressBook (opts) {
223222
if (remove) cache.del(prop + val)
224223
else {
225224
// make defensive copy before storing
226-
cache.set(prop + val, clone(identityInfo))
225+
cache.set(prop + val, utils.cloneDeep(identityInfo))
227226
}
228227
}
229228
}

lib/dbs/watches.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* @module watchesDB
33
*/
44

5-
const extend = require('xtend')
65
const collect = require('stream-collector')
76
const typeforce = require('../typeforce')
87
const subdown = require('subleveldown')

lib/errors.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,10 @@ exports.WillNotSend = TypedError({
7676
property: null,
7777
value: null
7878
})
79+
80+
exports.NoBlockchainAdapter = TypedError({
81+
type: 'noblockchainadapter',
82+
message: `missing blockchain adapter for blockchain "{blockchain}:{networkName}"`,
83+
blockchain: null,
84+
networkName: null
85+
})

lib/node.js

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
const util = require('util')
44
const EventEmitter = require('events').EventEmitter
55
const path = require('path')
6-
const extend = require('xtend/mutable')
7-
const clone = require('xtend')
86
const reemit = require('re-emitter')
97
const map = require('map-stream')
108
const mutexify = require('mutexify')
@@ -93,7 +91,8 @@ function Tradle (opts) {
9391
typeforce({
9492
network: types.network,
9593
dir: typeforce.String,
96-
blockchain: typeforce.Object,
94+
blockchain: typeforce.maybe(types.blockchain),
95+
getBlockchainAdapter: typeforce.maybe(typeforce.Function),
9796
identity: types.identity,
9897
keeper: types.keeper,
9998
keys: typeforce.Array,
@@ -109,8 +108,8 @@ function Tradle (opts) {
109108
// }), opts.merkle)
110109
}, opts)
111110

112-
extend(this, DEFAULT_OPTS, opts)
113111
utils.bindFunctions(this)
112+
utils.extend(this, DEFAULT_OPTS, opts)
114113

115114
this.opts = opts
116115
this._levelOpts = { db: this.leveldown }
@@ -284,6 +283,21 @@ Tradle.prototype._init = function _init () {
284283
})
285284
}
286285

286+
/**
287+
* Overwritten by constructor opt getBlockchainAdapter
288+
* @param {{ blockchain, networkName }} opts [description]
289+
*/
290+
Tradle.prototype.getBlockchainAdapter = function (opts) {
291+
if (this.blockchain) {
292+
const { blockchain, networkName } = opts
293+
if (blockchain === this.network.blockchain && networkName === this.network.name) {
294+
return this.blockchain
295+
}
296+
297+
this._debug('missing blockchain adapter', { blockchain, networkName })
298+
}
299+
}
300+
287301
/**
288302
* Log with a prefix
289303
* @private
@@ -464,7 +478,7 @@ Tradle.prototype.updateIdentity = function (opts, cb) {
464478
// })
465479

466480
let { identity, keys } = opts
467-
identity = extend(
481+
identity = utils.extend(
468482
protocol.nextVersion(this.identity),
469483
identity,
470484
{ [VERSION]: (this.identity[VERSION] || 0) + 1 }
@@ -857,9 +871,10 @@ Tradle.prototype._queueSend = function _queueSend (opts, cb) {
857871

858872
self.seals.findOne('link', oLink, function (err, seal) {
859873
if (!err && seal.txId) {
874+
// formatted per tradle.Seal model
860875
done(null, {
861-
blockchain: self.network.blockchain,
862-
network: self.network.name,
876+
blockchain: seal.blockchain,
877+
network: seal.networkName,
863878
basePubKey: seal.basePubKey.pub,
864879
link: seal.link,
865880
headerHash: seal.headerHash,
@@ -906,7 +921,7 @@ Tradle.prototype._queueSend = function _queueSend (opts, cb) {
906921
}
907922
})
908923

909-
const msg = utils.clone(opts.other || {}, meta, base)
924+
const msg = utils.extend({}, opts.other || {}, meta, base)
910925
const seal = results.maybeGetSeal
911926
if (seal) msg.seal = seal
912927

@@ -1100,6 +1115,10 @@ Tradle.prototype._receive = function _receive (msg, from, cb) {
11001115
if (err) return cb(err)
11011116
if (seal) {
11021117
self.watchSeal({
1118+
chain: {
1119+
blockchain: seal.blockchain,
1120+
networkName: seal.network || seal.networkName
1121+
},
11031122
headerHash: seal.headerHash,
11041123
basePubKey: utils.getSealPubKey(seal)
11051124
})
@@ -1156,6 +1175,8 @@ Tradle.prototype.seal = function seal (opts, cb) {
11561175
// const sealPrevAddress = sealPrevPubKey &&
11571176
// utils.sealPrevAddress(basePubKey, link, self.networkName)
11581177

1178+
const blockchain = network.blockchain
1179+
const networkName = network.name
11591180
const sealProps = {
11601181
link,
11611182
prevLink: object[PREVLINK],
@@ -1167,8 +1188,8 @@ Tradle.prototype.seal = function seal (opts, cb) {
11671188
sealPubKey,
11681189
sealPrevPubKey,
11691190
amount: opts.amount,
1170-
blockchain: network.blockchain,
1171-
networkName: network.name
1191+
blockchain,
1192+
networkName
11721193
}
11731194

11741195
self.actions.writeSeal(sealProps, err => {
@@ -1177,7 +1198,14 @@ Tradle.prototype.seal = function seal (opts, cb) {
11771198
cb(null, sealProps)
11781199
})
11791200

1180-
self.watchSeal({ object, basePubKey })
1201+
self.watchSeal({
1202+
chain: {
1203+
blockchain,
1204+
networkName
1205+
},
1206+
object,
1207+
basePubKey
1208+
})
11811209
})
11821210
})
11831211
}
@@ -1191,21 +1219,22 @@ Tradle.prototype.seal = function seal (opts, cb) {
11911219
*/
11921220
Tradle.prototype.watchNextVersion = function (opts, cb) {
11931221
typeforce({
1222+
chain: types.blockchainIdentifier,
11941223
link: typeforce.maybe(typeforce.String),
11951224
object: typeforce.maybe(typeforce.Object),
11961225
headerHash: typeforce.maybe(typeforce.String),
11971226
basePubKey: types.chainPubKey
11981227
}, opts)
11991228

1200-
let { link, object, headerHash, basePubKey } = opts
1229+
let { chain, link, object, headerHash, basePubKey } = opts
12011230
if (!headerHash) headerHash = protocol.headerHash(object)
12021231
if (!link) link = utils.hexLink(object)
12031232

1204-
const { network } = this
1205-
const address = utils.sealPrevAddress({ network, basePubKey, headerHash })
1233+
const address = utils.sealPrevAddress({ network: this.network, basePubKey, headerHash })
12061234
this._watch({
12071235
// the next version's previous is the current version
12081236
// the tx for next version will have a predictable seal based on the current version's link
1237+
chain,
12091238
link,
12101239
address,
12111240
basePubKey,
@@ -1224,19 +1253,21 @@ Tradle.prototype.watchNextVersion = function (opts, cb) {
12241253
*/
12251254
Tradle.prototype.watchSeal = function (opts, cb) {
12261255
typeforce({
1256+
chain: types.blockchainIdentifier,
12271257
link: typeforce.maybe(typeforce.String),
12281258
object: typeforce.maybe(typeforce.Object),
12291259
headerHash: typeforce.maybe(typeforce.String),
12301260
basePubKey: types.chainPubKey
12311261
}, opts)
12321262

1233-
let { link, object, headerHash, basePubKey } = opts
1263+
let { chain, link, object, headerHash, basePubKey } = opts
12341264
if (!headerHash) headerHash = protocol.headerHash(object)
12351265
if (!link) link = utils.hexLink(object)
12361266

12371267
const { network } = this
12381268
const address = utils.sealAddress({ network, basePubKey, headerHash })
12391269
this._watch({
1270+
chain,
12401271
link,
12411272
address,
12421273
basePubKey,
@@ -1249,6 +1280,8 @@ Tradle.prototype.watchSeal = function (opts, cb) {
12491280
* watch an address for a seal for an object's current or next version
12501281
* @private
12511282
* @param {Object} opts
1283+
* @param {string} opts.blockchain blockchain to monitor
1284+
* @param {string} opts.networkName blockchain network to monitor
12521285
* @param {string} opts.headerHash headerHash to monitor
12531286
* @param {Object} opts.basePubKey [sealer's blockchain pubKey]{@link types#chainPubKey}
12541287
* @param {string} opts.watchType [watch type]{@link constants#watchType}
@@ -1259,6 +1292,7 @@ Tradle.prototype._watch = function (opts, cb) {
12591292
const self = this
12601293

12611294
typeforce({
1295+
chain: types.blockchainIdentifier,
12621296
link: typeforce.String,
12631297
headerHash: typeforce.String,
12641298
basePubKey: types.chainPubKey,
@@ -1295,6 +1329,7 @@ Tradle.prototype._watch = function (opts, cb) {
12951329
], function (err) {
12961330
if (err) return cb(err)
12971331

1332+
opts = inlineChain(opts)
12981333
self.actions.createWatch(opts, cb)
12991334
})
13001335
}
@@ -1460,3 +1495,10 @@ function getLockId (identifier) {
14601495

14611496
throw new Error('invalid lock id')
14621497
}
1498+
1499+
function inlineChain (opts) {
1500+
opts = utils.clone(opts)
1501+
utils.extend(opts, opts.chain)
1502+
delete opts.chain
1503+
return opts
1504+
}

lib/partial.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ exports.from = function (object) {
5757

5858
exports.verify = function (partial) {
5959
const proof = partial.proof.concat(partial.root).map(bufferizeMerkleNode)
60-
const opts = utils.clone({ proof }, protocol.DEFAULT_MERKLE_OPTS)
60+
const opts = utils.extend({ proof }, protocol.DEFAULT_MERKLE_OPTS)
6161
const verify = merkleProofs.verifier(opts)
6262
return partial.leaves.every(node => verify(node))
6363
}

0 commit comments

Comments
 (0)