From 242fb759e63df2ccd98c823f911ae087a4af2f1a Mon Sep 17 00:00:00 2001 From: Sky Young Date: Thu, 10 May 2018 13:54:01 -0700 Subject: [PATCH] Split value into wholeCoin and Satoshis when serialized & recombine on deserialization --- lib/blockchain/chaindb.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 13117903..322af916 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -2150,11 +2150,17 @@ ChainState.prototype.commit = function commit(hash) { }; ChainState.prototype.toRaw = function toRaw() { - const bw = new StaticWriter(56); + const bw = new StaticWriter(64); bw.writeHash(this.tip); bw.writeU64(this.tx); bw.writeU64(this.coin); - bw.writeU64(this.value); + + var wholeCoinValue = Math.floor(this.value / Math.pow(10,8)) + + var satoshiCoinValue = this.value - (wholeCoinValue * Math.pow(10,8)); + + bw.writeU64BE(wholeCoinValue); + bw.writeU64BE(satoshiCoinValue); return bw.render(); }; @@ -2164,7 +2170,12 @@ ChainState.fromRaw = function fromRaw(data) { state.tip = br.readHash('hex'); state.tx = br.readU64(); state.coin = br.readU64(); - state.value = br.readU64(); + + var wholeCoinValue = br.readU64BE(); + var satoshiCoinValue = br.readU64BE(); + + state.value = (wholeCoinValue * Math.pow(10,8)) + satoshiCoinValue; + return state; };