Skip to content

Commit

Permalink
add isBlockTimestamp field to TxStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Nov 27, 2014
1 parent befc5b0 commit a0992a5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cc-wallet-core",
"version": "0.2.3",
"version": "0.2.4",
"description": "",
"main": "./src/index.js",
"keywords": [],
Expand Down
10 changes: 10 additions & 0 deletions src/history/HistoryEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var historyEntryType = require('../const').historyEntryType
* @param {bitcoinjs-lib.Transaction} data.tx
* @param {number} data.height
* @param {number} data.timestamp
* @param {boolean} data.isBlockTimestamp
* @param {AssetValue[]} data.values
* @param {HistoryTarget[]} data.targets
* @param {number} data.entryType
Expand All @@ -18,6 +19,7 @@ function HistoryEntry(data) {
verify.Transaction(data.tx)
verify.number(data.height)
verify.number(data.timestamp)
verify.boolean(data.isBlockTimestamp)
verify.array(data.values)
data.values.forEach(verify.AssetValue)
verify.array(data.targets)
Expand All @@ -27,6 +29,7 @@ function HistoryEntry(data) {
this.txId = data.tx.getId()
this.height = data.height
this.timestamp = data.timestamp
this.isBlockTimestamp = data.isBlockTimestamp
this.values = data.values
this.targets = data.targets
this.entryType = data.entryType
Expand All @@ -53,6 +56,13 @@ HistoryEntry.prototype.getTimestamp = function() {
return this.timestamp
}

/**
* @return {boolean}
*/
HistoryEntry.prototype.isBlockTimestamp = function () {
return this.isBlockTimestamp
}

/**
* @return {AssetValue[]}
*/
Expand Down
15 changes: 9 additions & 6 deletions src/history/HistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ HistoryManager.prototype._updateEntries = function () {
return [tx.getId(), {
tx: tx,
height: txDb.getTxHeight(txId),
timestamp: txDb.getTxTimestamp(txId)
timestamp: txDb.getTxTimestamp(txId),
isBlockTimestamp: txDb.isBlockTimestamp(txId)
}]
})
.sortBy(function (entry) {
Expand All @@ -73,6 +74,7 @@ HistoryManager.prototype._updateEntries = function () {
.value()

toposort(transactions).forEach(function(tx) {
var txId = tx.getId()
var colorValues = {}
var myColorTargets = []

Expand All @@ -98,7 +100,7 @@ HistoryManager.prototype._updateEntries = function () {
})

var outs = _.filter(tx.outs.map(function (output, index) {
return coins[tx.getId()+index]
return coins[txId+index]
}))
outs.forEach(function(coin) {
promise = promise.then(function() {
Expand All @@ -121,11 +123,11 @@ HistoryManager.prototype._updateEntries = function () {
var colorTargets = []
tx.outs.forEach(function (output, index) {
promise = promise.then(function () {
if (!_.isUndefined(coins[tx.getId()+index])) { return }
if (!_.isUndefined(coins[txId+index])) { return }

var address = bitcoin.getAddressesFromOutputScript(output.script, self._wallet.getBitcoinNetwork())[0]
var coin = new Coin(self._wallet.getCoinManager(), {
txId: tx.getId(),
txId: txId,
outIndex: index,
value: output.value,
script: output.script.toHex(),
Expand Down Expand Up @@ -186,8 +188,9 @@ HistoryManager.prototype._updateEntries = function () {

self._entries.push(new HistoryEntry({
tx: tx,
height: txEntries[tx.getId()].height,
timestamp: txEntries[tx.getId()].timestamp,
height: txEntries[txId].height,
timestamp: txEntries[txId].timestamp,
isBlockTimestamp: txEntries[txId].isBlockTimestamp,
values: _.values(assetValues),
targets: historyTargets,
entryType: entryType
Expand Down
11 changes: 11 additions & 0 deletions src/tx/TxDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ TxDb.prototype._addTx = function(txId, data, cb) {

return Q.ninvoke(self._wallet.getBlockchain(), 'getBlockTime', data.height).then(function(ts) {
data.timestamp = ts + TimezoneOffset
data.isBlockTimestamp = true
})
}

Expand All @@ -194,6 +195,7 @@ TxDb.prototype._addTx = function(txId, data, cb) {
status: data.status,
height: data.height,
timestamp: data.timestamp,
isBlockTimestamp: data.isBlockTimestamp,
tAddresses: data.tAddresses
}

Expand Down Expand Up @@ -359,5 +361,14 @@ TxDb.prototype.getTxTimestamp = function(txId) {
return record === null ? null : record.timestamp
}

/**
* @param {string} txId
* @return {?boolean}
*/
TxDb.prototype.isBlockTimestamp = function(txId) {
var record = this._txStorage.get(txId)
return record === null ? null : record.isBlockTimestamp
}


module.exports = TxDb
24 changes: 19 additions & 5 deletions src/tx/TxStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ var SyncStorage = require('../SyncStorage')
var verify = require('../verify')


// Todo: add shrotcut names for save storage's size
/**
* @todo Add shrotcut names for save storage's size
*/

/**
* @typedef {Object} TxStorageRecord
* @property {string} rawTx
* @property {number} status
* @property {number} [height=null]
* @property {number} [timestamp=null]
* @property {string[]} [tAddresses=[]]
* @property {string[]} [rAddresses=[]]
* @property {number} height
* @property {number} timestamp
* @property {boolean} isBlockTimestamp
* @property {string[]} tAddresses
* @property {string[]} rAddresses
*/

/**
Expand Down Expand Up @@ -68,6 +72,13 @@ function TxStorage(opts) {
this._saveRecords({})
this.store.set(this.txDbKey + '_version', 5)
}

if (this.store.get(this.txDbKey + '_version') === 5) {
var records = this._getRecords()
_.forEach(records, function (record) { record.isBlockTimestamp = true })
this._saveRecords(records)
this.store.set(this.txDbKey + '_version', 6)
}
}

inherits(TxStorage, SyncStorage)
Expand Down Expand Up @@ -100,6 +111,7 @@ TxStorage.prototype._save2store = function() {
* @param {number} opts.status
* @param {number} opts.height
* @param {number} opts.timestamp
* @param {boolean} [opts.isBlockTimestamp=false]
* @param {string} opts.tAddresses
* @return {TxStorageRecord}
* @throws {Error} If txId exists
Expand All @@ -111,6 +123,7 @@ TxStorage.prototype.add = function(txId, rawTx, opts) {
verify.number(opts.status)
verify.number(opts.height)
verify.number(opts.timestamp)
if (!_.isUndefined(opts.isBlockTimestamp)) verify.boolean(opts.isBlockTimestamp)
verify.array(opts.tAddresses)
opts.tAddresses.forEach(verify.string)

Expand All @@ -123,6 +136,7 @@ TxStorage.prototype.add = function(txId, rawTx, opts) {
status: opts.status,
height: opts.height,
timestamp: opts.timestamp,
isBlockTimestamp: opts.isBlockTimestamp || false,
tAddresses: _.sortBy(opts.tAddresses)
}
this._saveRecords(records)
Expand Down

0 comments on commit a0992a5

Please sign in to comment.