Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return proper transaction receipt #293

Merged
merged 3 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api/methods/getBlockByHash.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { Block } = require('leap-core');
const { EMPTY_ADDRESS } = require('../../utils/constants');
const txResponse = require('./txResponse');

const NULL_HASH =
'0x0000000000000000000000000000000000000000000000000000000000000000';
const NULL_ADDR = '0x0000000000000000000000000000000000000000';
const NA = 0;

module.exports = async (db, hash, showFullTxs = false) => {
Expand All @@ -27,7 +27,7 @@ module.exports = async (db, hash, showFullTxs = false) => {
difficulty: NA,
gasLimit: NA,
gasUsed: NA,
miner: NULL_ADDR,
miner: EMPTY_ADDRESS,
extraData: NA,
size: `0x${block.hex().length.toString(16)}`,
timestamp: `0x${block.timestamp.toString(16)}`,
Expand Down
34 changes: 34 additions & 0 deletions src/api/methods/getTransactionReceipt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2019-present, Leap DAO (leapdao.org)
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/
const { Tx } = require('leap-core');
const getPrevTx = require('./utils/getPrevTx');

module.exports = async (db, hash) => {
const txDoc = await db.getTransaction(hash);
if (!txDoc) return null;

const { txData, blockHash, height, txPos } = txDoc;

const tx = Tx.fromJSON(txData);

const prevTx = await getPrevTx(db, tx);

return {
transactionHash: hash,
transactionIndex: txPos,
blockHash,
blockNumber: `0x${height.toString(16)}`,
from: tx.from(prevTx),
to: tx.to(),
cumulativeGasUsed: '0x0',
gasUsed: '0x0',
contractAddress: null,
logs: [],
logsBloom: '0x',
status: '0x1',
};
};
75 changes: 75 additions & 0 deletions src/api/methods/getTransactionReceipt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { Tx, Input, Outpoint, Output } = require('leap-core');
const getTransactionReceipt = require('./getTransactionReceipt');

const PRIV1 =
'0x9b63fe8147edb8d251a6a66fd18c0ed73873da9fff3f08ea202e1c0a8ead7311';
const PRIV2 =
'0xea3a59a673a9f7e74ad65e92ee04c2330fc5b905d0fa47bb2ae36c0b94af61cd';
const A1 = '0xb8205608d54cb81f44f263be086027d8610f3c94';
const A2 = '0xD56F7dFCd2BaFfBC1d885F0266b21C7F2912020c';

describe('getTransactionReceipt', () => {
test('Non-existent tx', async () => {
const db = {
async getTransaction() {
return null;
},
};

expect(await getTransactionReceipt(db, '0x00')).toBe(null);
});

test('Existing tx', async () => {
const prevTx = Tx.transfer(
[
new Input(
new Outpoint(
'0x7777777777777777777777777777777777777777777777777777777777777777',
0
)
),
],
[new Output(120, A1, 0)]
).signAll(PRIV2);
const tx = Tx.transfer(
[new Input(new Outpoint(prevTx.hash(), 0))],
[new Output(100, A2, 0)]
).signAll(PRIV1);

const data = {
[tx.hash()]: {
txData: tx.toJSON(),
blockHash: '0x2',
height: 2,
txPos: 0,
},
[prevTx.hash()]: {
txData: prevTx.toJSON(),
blockHash: '0x1',
height: 1,
txPos: 0,
},
};
const db = {
async getTransaction(hash) {
return data[hash];
},
};

const response = await getTransactionReceipt(db, tx.hash());
expect(response).toEqual({
transactionHash: tx.hash(),
transactionIndex: 0,
blockHash: '0x2',
blockNumber: '0x2',
from: A1,
to: A2,
cumulativeGasUsed: '0x0',
gasUsed: '0x0',
contractAddress: null,
logs: [],
logsBloom: '0x',
status: '0x1',
});
});
});
5 changes: 4 additions & 1 deletion src/api/methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ module.exports = (bridgeState, db, app, tendermintPort) => {
tendermintPort
),
eth_getTransactionByHash: require('./getTransactionByHash').bind(null, db),
eth_getTransactionReceipt: require('./getTransactionByHash').bind(null, db),
eth_getTransactionReceipt: require('./getTransactionReceipt').bind(
null,
db
),
eth_getBlockByHash: require('./getBlockByHash').bind(null, db),
eth_getBlockByNumber: require('./getBlockByNumber').bind(
null,
Expand Down
46 changes: 11 additions & 35 deletions src/api/methods/txResponse.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,26 @@
const { Tx, Type, Util } = require('leap-core');

const txValue = (tx, prevTx) => {
// assuming first output is transfer, second one is change
if (tx.outputs && tx.outputs.length > 0) {
return {
value: tx.outputs[0].value,
color: tx.outputs[0].color,
};
}

if (tx.type === Type.EXIT && prevTx) {
const output = prevTx.outputs[tx.inputs[0].prevout.index];
return {
value: output.value,
color: output.color,
};
}

return { value: 0, color: 0 };
};
/**
* Copyright (c) 2019-present, Leap DAO (leapdao.org)
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/
const getPrevTx = require('./utils/getPrevTx');

module.exports = async (db, tx, blockHash, height, txPos) => {
let prevTx = null;
let from = '';
if (tx.inputs && tx.inputs.length > 0 && tx.inputs[0].prevout) {
const prevTxHash = tx.inputs[0].prevout.hash;
const outputIndex = tx.inputs[0].prevout.index;
const txDoc = await db.getTransaction(Util.toHexString(prevTxHash));
if (txDoc) {
prevTx = Tx.fromJSON(txDoc.txData);
from = prevTx.outputs[outputIndex].address;
}
}
const prevTx = await getPrevTx(db, tx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


const { value, color } = txValue(tx, prevTx);
const { value, color } = tx.value(prevTx);

return {
value: `0x${value.toString(16)}`,
color,
hash: tx.hash(),
from,
from: tx.from(prevTx),
raw: tx.hex(),
blockHash,
blockNumber: `0x${height.toString(16)}`,
transactionIndex: txPos,
to: tx.outputs && tx.outputs.length ? tx.outputs[0].address : null,
to: tx.to(),
gas: '0x0',
gasPrice: '0x0',
nonce: 0,
Expand Down
22 changes: 11 additions & 11 deletions src/api/methods/txResponse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PRIV1 =
'0x9b63fe8147edb8d251a6a66fd18c0ed73873da9fff3f08ea202e1c0a8ead7311';
const PRIV2 =
'0xea3a59a673a9f7e74ad65e92ee04c2330fc5b905d0fa47bb2ae36c0b94af61cd';
const A1 = '0xB8205608d54cb81f44F263bE086027D8610F3C94';
const A1 = '0xb8205608d54cb81f44f263be086027d8610f3c94';
const A2 = '0xD56F7dFCd2BaFfBC1d885F0266b21C7F2912020c';

const fakeDb = tx => ({
Expand All @@ -24,11 +24,11 @@ describe('txResponse', () => {
),
],
[new Output(120, A1, 0)]
).signAll(PRIV1);
).signAll(PRIV2);
const tx = Tx.transfer(
[new Input(new Outpoint(prevTx.hash(), 0))],
[new Output(100, A2, 0)]
).signAll(PRIV2);
).signAll(PRIV1);
const blockHash = '0x0';
const height = 0;
const txIndex = 0;
Expand Down Expand Up @@ -67,11 +67,11 @@ describe('txResponse', () => {
),
],
[new Output(120, A1, 0)]
).signAll(PRIV1);
).signAll(PRIV2);
const tx = Tx.transfer(
[new Input(new Outpoint(prevTx.hash(), 0))],
[new Output(100, A2, 0)]
).signAll(PRIV2);
).signAll(PRIV1);
const blockHash = '0x0';
const height = 0;
const txIndex = 0;
Expand All @@ -84,7 +84,7 @@ describe('txResponse', () => {
);
expect(response).toEqual({
hash: tx.hash(),
from: '',
from: '0xb8205608d54cb81f44f263be086027d8610f3c94',
to: A2,
value: '0x64', // hex 100
color: 0,
Expand All @@ -110,9 +110,9 @@ describe('txResponse', () => {
),
],
[new Output(120, A1, 0)]
).signAll(PRIV1);
).signAll(PRIV2);
const tx = Tx.exit(new Input(new Outpoint(prevTx.hash(), 0))).signAll(
PRIV2
PRIV1
);
const blockHash = '0x0';
const height = 0;
Expand All @@ -127,7 +127,7 @@ describe('txResponse', () => {
expect(response).toEqual({
hash: tx.hash(),
from: A1,
to: null,
to: '0x0000000000000000000000000000000000000000',
value: '0x78', // hex 120
color: 0,
transactionIndex: txIndex,
Expand Down Expand Up @@ -155,8 +155,8 @@ describe('txResponse', () => {
);
expect(response).toEqual({
hash: tx.hash(),
from: '',
to: null,
from: '0x0000000000000000000000000000000000000000',
to: '0x0000000000000000000000000000000000000000',
value: '0x0',
color: 0,
transactionIndex: txIndex,
Expand Down
14 changes: 14 additions & 0 deletions src/api/methods/utils/getPrevTx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { Tx } = require('leap-core');
const { bufferToHex } = require('ethereumjs-util');

module.exports = async (db, tx) => {
if (tx.inputs && tx.inputs.length > 0 && tx.inputs[0].prevout) {
const prevTxHash = bufferToHex(tx.inputs[0].prevout.hash);
const txDoc = await db.getTransaction(prevTxHash);
if (txDoc) {
return Tx.fromJSON(txDoc.txData);
}
}

return null;
};