Updates
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const b4a = require('b4a')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'credit-ledger/v1'
|
||||
|
||||
@@ -10,50 +11,105 @@ class HyperP2PCreditLedger extends EventEmitter {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.peerHex = b4a.toString(this.keyPair.publicKey, 'hex')
|
||||
this._accounts = new Map()
|
||||
this._stats = { credits: 0, debits: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'credit-ledger-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
openAccount (accountId, initial = 0) {
|
||||
assertNonEmpty(accountId, 'accountId')
|
||||
if (this._accounts.has(accountId)) throw new Error('account exists')
|
||||
const acct = { id: accountId, balance: initial, updatedAt: Date.now() }
|
||||
this._accounts.set(accountId, acct)
|
||||
this._sync(accountId, 'open', acct.balance)
|
||||
this.emit('account', acct)
|
||||
return acct
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'credit-ledger-sync', key, value: null })
|
||||
return ok
|
||||
credit (accountId, amount, reason = '') {
|
||||
return this._apply(accountId, Math.abs(amount), 'credit', reason)
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
debit (accountId, amount, reason = '') {
|
||||
return this._apply(accountId, -Math.abs(amount), 'debit', reason)
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'credit-ledger-sync') return
|
||||
transfer (fromId, toId, amount) {
|
||||
assertNonEmpty(fromId, 'fromId')
|
||||
assertNonEmpty(toId, 'toId')
|
||||
if (amount <= 0) throw new Error('amount must be positive')
|
||||
const from = this._accounts.get(fromId)
|
||||
const to = this._accounts.get(toId)
|
||||
if (!from || !to) throw new Error('unknown account')
|
||||
if (from.balance < amount) throw new Error('insufficient balance')
|
||||
this.debit(fromId, amount, `transfer to ${toId}`)
|
||||
this.credit(toId, amount, `transfer from ${fromId}`)
|
||||
return { from: fromId, to: toId, amount, at: Date.now() }
|
||||
}
|
||||
|
||||
balance (accountId) {
|
||||
const a = this._accounts.get(accountId)
|
||||
return a ? a.balance : null
|
||||
}
|
||||
|
||||
_apply (accountId, delta, kind, reason) {
|
||||
assertNonEmpty(accountId, 'accountId')
|
||||
const acct = this._accounts.get(accountId)
|
||||
if (!acct) throw new Error('unknown account')
|
||||
acct.balance += delta
|
||||
acct.updatedAt = Date.now()
|
||||
if (kind === 'credit') this._stats.credits++
|
||||
else this._stats.debits++
|
||||
this._sync(accountId, kind, acct.balance, { delta, reason, peer: this.peerHex })
|
||||
this.emit(kind, { accountId, balance: acct.balance, delta, reason })
|
||||
return acct
|
||||
}
|
||||
|
||||
_sync (accountId, kind, balance, extra = {}) {
|
||||
if (!this._peerMsgs) return
|
||||
gossipSend(this, { type: 'ledger-entry', accountId, kind, balance, at: Date.now(), ...extra })
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
|
||||
_onGossip (data) {
|
||||
if (!data || data.type !== 'ledger-entry') return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
let acct = this._accounts.get(data.accountId)
|
||||
if (!acct && data.kind === 'open') {
|
||||
acct = { id: data.accountId, balance: data.balance, updatedAt: data.at }
|
||||
this._accounts.set(data.accountId, acct)
|
||||
} else if (acct) {
|
||||
acct.balance = data.balance
|
||||
acct.updatedAt = data.at
|
||||
}
|
||||
this.emit('remote-entry', data)
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return {
|
||||
...this._stats,
|
||||
accounts: this._accounts.size,
|
||||
protocol: PROTOCOL
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (d) => this._onGossip(d)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this._accounts.clear()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user