Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Fix multi-transactions per block gas usage & cumulative gas usage (#191)
Browse files Browse the repository at this point in the history
When multiple transactions occur in a block, each transaction's `.gasUsed` should reflect the gas used in that transaction alone and the `cumulativeGas` should equal the `totalGasUsed` at the time of the transaction instead of total `gasUsed` in the block. This PR should fix the `gasUsed` and `cumulativeGasUsed` values to accurately reflect their respective values.


Resolves #445.
  • Loading branch information
nicholasjpaterno authored and davidmurdoch committed Oct 17, 2018
1 parent cf723a0 commit 72efbd7
Show file tree
Hide file tree
Showing 7 changed files with 660 additions and 530 deletions.
3 changes: 2 additions & 1 deletion lib/blockchain_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ BlockchainDouble.prototype.processBlock = function(block, commit, callback) {
}
}

receipts.push(new Receipt(tx, block, tx_logs, receipt.gasUsed, result.createdAddress, receipt.status, to.hex(result.bloom.bitvector)));
let rcpt = new Receipt(tx, block, tx_logs, result.gasUsed.toArrayLike(Buffer), receipt.gasUsed, result.createdAddress, receipt.status, to.hex(result.bloom.bitvector));
receipts.push(rcpt);
}

function commmitIfNeeded(cb) {
Expand Down
2 changes: 1 addition & 1 deletion lib/database/receiptserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ReceiptSerializer.prototype.decode = function(json, done) {
}, function(err, result) {
if (err) return done(err);

done(null, new Receipt(tx, result.block, result.logs, json.gasUsed, json.contractAddress, json.status, json.logsBloom));
done(null, new Receipt(tx, result.block, result.logs, json.gasUsed, json.cumulativeGasUsed, json.contractAddress, json.status, json.logsBloom));
});
});
});
Expand Down
3 changes: 1 addition & 2 deletions lib/utils/forkedblockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ ForkedBlockchain.prototype.getTransactionReceipt = function(hash, callback) {
var logs = receipt_json.logs.map(function(log) {
return new Log(log);
});

var receipt = new Receipt(result.tx, result.block, logs, receipt_json.cumulativeGasUsed,
var receipt = new Receipt(result.tx, result.block, logs, receipt_json.gasUsed, receipt_json.cumulativeGasUsed,
receipt_json.contractAddress, receipt_json.status, to.hex(receipt_json.logsBloom));

callback(null, receipt);
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/receipt.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
var to = require("./to");

function Receipt(tx, block, logs, gasUsed, contractAddress, status, logsBloom) {
function Receipt(tx, block, logs, gasUsed, cumulativeGasUsed, contractAddress, status, logsBloom) {
this.tx = tx;
this.block = block;
this.logs = logs;
this.gasUsed = gasUsed;
this.cumulativeGasUsed = cumulativeGasUsed;
this.contractAddress = contractAddress;
this.status = status;
this.logsBloom = logsBloom;


this.transactionIndex = 0;

Expand All @@ -30,7 +32,7 @@ Receipt.prototype.toJSON = function() {
blockHash: to.rpcDataHexString(this.block.hash()),
blockNumber: to.rpcQuantityHexString(this.block.header.number),
gasUsed: to.rpcQuantityHexString(this.gasUsed),
cumulativeGasUsed: to.rpcQuantityHexString(this.block.header.gasUsed),
cumulativeGasUsed: to.rpcQuantityHexString(this.cumulativeGasUsed),
contractAddress: this.contractAddress != null ? to.rpcDataHexString(this.contractAddress) : null,
logs: this.logs.map(function(log) {return log.toJSON()}),
status: to.rpcQuantityHexString(this.status),
Expand Down
5 changes: 4 additions & 1 deletion lib/webSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ConnectionManager.prototype.manageConnection = function (connection) {
});

connection.on('close', function () {
console.log(`handling connection close: id ${connection.id}`)
// remove subscriptions
Object.keys(self.connections[connection.id].subscriptions).forEach((subscriptionId) => {
self.provider.send({
Expand All @@ -68,6 +69,8 @@ ConnectionManager.prototype.manageConnection = function (connection) {

delete self.connections[ connection.id ];
});

console.log(`opened connection: id ${connection.id}`)
};


Expand All @@ -86,6 +89,7 @@ ConnectionManager.prototype._handleRequest = function (connection, payload) {
});
break;
case 'eth_unsubscribe':
console.log(`handling unsubscribe: id ${connection.id}, ${payload.params[0]}`)
self.provider.send(payload, function (err, result) {
if (err || result.error) {
if (connection && connection.send) {
Expand All @@ -94,7 +98,6 @@ ConnectionManager.prototype._handleRequest = function (connection, payload) {
return
}

connection = self.connectionsBySubscriptionId[payload.params[0]];
delete self.connections[connection.id].subscriptions[payload.params[0]]
delete self.connectionsBySubscriptionId[payload.params[0]];

Expand Down

0 comments on commit 72efbd7

Please sign in to comment.