Commit 53422b22 authored by Benoit Lavenier's avatar Benoit Lavenier

[fix] blockchain stats : wrong TX amount - fix #30

parent 076029d1
......@@ -89,10 +89,18 @@ public final class BlockchainBlocks {
.sum();
long outputSum = Arrays.stream(tx.getOutputs())
.map(TX_OUTPUT_PATTERN::matcher)
.filter(Matcher::matches)
.filter(matcher -> issuer.equals(matcher.group(3)))
.mapToLong(matcher -> powBase(Long.parseLong(matcher.group(1)), Integer.parseInt(matcher.group(2))))
.mapToLong(outputStr -> {
try {
TxOutput txOutput = parseOutput(outputStr);
if (issuer.equals(txOutput.recipient)) {
return powBase(txOutput.amount, txOutput.unitbase);
}
}
catch (InvalidFormatException e) {
// not a simple unlock condition
}
return 0;
})
.sum();
return (inputSum - outputSum);
......
package org.duniter.core.client.model;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.client.model.bma.BlockchainBlocks;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by benoit on 27/11/17.
*/
public class BlockchainBlocksTest {
@Test
public void getTxAmount_issue30() {
// This test should detect when TX.inputs format is invalid - see #19
BlockchainBlock block = BlockFileUtils.readBlockFile("block_issue_30.json");
long txAmount = BlockchainBlocks.getTxAmount(block.getTransactions()[0]);
Assert.assertEquals(72000l, txAmount);
}
}
......@@ -65,4 +65,15 @@ public class MovementsTest {
}
}
@Test
public void getMovements_issue30() {
// This test should detect when TX.inputs format is invalid - see #19
BlockchainBlock block = BlockFileUtils.readBlockFile("block_issue_30.json");
List<Movement> mov = Movements.getMovements(block);
Assert.assertEquals(1, mov.size());
Assert.assertEquals(72000l, mov.get(0).getAmount());
}
}
{
"version": 10,
"nonce": 10400000001617,
"number": 72871,
"powMin": 86,
"time": 1511772509,
"medianTime": 1511768858,
"membersCount": 464,
"monetaryMass": 56593557,
"unitbase": 0,
"issuersCount": 32,
"issuersFrame": 161,
"issuersFrameVar": 0,
"currency": "g1",
"issuer": "FEkbc4BfJukSWnCU6Hed6dgwwTuPFTVdgz5LpL4iHr9J",
"signature": "JfN4QQAwcZJ2tnILNyV+hQAGTyrH4rzueyAlGG3YUewAA7j/MhwxDtlN7HxlATkRwGgmcGPzD3BBvdV9MvKNAQ==",
"hash": "000004ED642BB8DFE5F61E9A81EA5B9CF95A7414CCEB3DFB834E050FA5467EA8",
"parameters": "",
"previousHash": "000002F718601968487B55074919687A69189496EB29592402F9A7573EC95C11",
"previousIssuer": "GEWmVzmiKM6RGJsNaE7VcdNdf2T31xSnu6nHXdxuoSmd",
"inner_hash": "659ADA7EBBC15CAB80E7F1D3DB5AFE650CDA32B4891EEC8E41E50A6D4800413C",
"dividend": null,
"identities": [],
"joiners": [],
"actives": [],
"leavers": [],
"revoked": [],
"excluded": [],
"certifications": [],
"transactions": [
{
"version": 10,
"currency": "g1",
"locktime": 0,
"hash": "B04C62C3DA0F872697B3BF5BF388D068F549B949FA4602DCA68A844FDFB2708F",
"blockstamp": "72867-0000010483B9E9DE5B1DB4F1AE671988AB02B76DD287D0664C62112DC92192BE",
"blockstampTime": 0,
"issuers": [
"Hi6geHkDVUn1NTwfrp8UMgUVPrgTiiGmZL82dnRoN5My"
],
"inputs": [
"38300:0:T:F0360D0C87A06ED78193600230054F20CC6943BF17763C17F22BC6CFCEB4EF73:1",
"1068650:0:T:1D8BBA768B4AD390866445C1FAF6711CD7637C6F473E50D248D2664D908C5A18:1"
],
"outputs": [
"72000:0:SIG(Be1eVp7etVfA7cT6er6dcJ9d5KxGJVY2tzCGGCAz3yG)",
"1034950:0:SIG(Hi6geHkDVUn1NTwfrp8UMgUVPrgTiiGmZL82dnRoN5My)"
],
"unlocks": [
"0:SIG(0)",
"1:SIG(0)"
],
"signatures": [
"YCbiwVOxeWl0C+wkuC+badq2bX0ub3mtqPLoQKJGJ8YpntfNy/ZTYc12ufjxiWIDW7aFK83raOX8QcLwbO8lCg=="
],
"comment": "Encheres RML10 G1 / Cartes Jeu Geconomicus"
}
],
"raw": "Version: 10\nType: Block\nCurrency: g1\nNumber: 72871\nPoWMin: 86\nTime: 1511772509\nMedianTime: 1511768858\nUnitBase: 0\nIssuer: FEkbc4BfJukSWnCU6Hed6dgwwTuPFTVdgz5LpL4iHr9J\nIssuersFrame: 161\nIssuersFrameVar: 0\nDifferentIssuersCount: 32\nPreviousHash: 000002F718601968487B55074919687A69189496EB29592402F9A7573EC95C11\nPreviousIssuer: GEWmVzmiKM6RGJsNaE7VcdNdf2T31xSnu6nHXdxuoSmd\nMembersCount: 464\nIdentities:\nJoiners:\nActives:\nLeavers:\nRevoked:\nExcluded:\nCertifications:\nTransactions:\nTX:10:1:2:2:2:1:0\n72867-0000010483B9E9DE5B1DB4F1AE671988AB02B76DD287D0664C62112DC92192BE\nHi6geHkDVUn1NTwfrp8UMgUVPrgTiiGmZL82dnRoN5My\n38300:0:T:F0360D0C87A06ED78193600230054F20CC6943BF17763C17F22BC6CFCEB4EF73:1\n1068650:0:T:1D8BBA768B4AD390866445C1FAF6711CD7637C6F473E50D248D2664D908C5A18:1\n0:SIG(0)\n1:SIG(0)\n72000:0:SIG(Be1eVp7etVfA7cT6er6dcJ9d5KxGJVY2tzCGGCAz3yG)\n1034950:0:SIG(Hi6geHkDVUn1NTwfrp8UMgUVPrgTiiGmZL82dnRoN5My)\nEncheres RML10 G1 / Cartes Jeu Geconomicus\nYCbiwVOxeWl0C+wkuC+badq2bX0ub3mtqPLoQKJGJ8YpntfNy/ZTYc12ufjxiWIDW7aFK83raOX8QcLwbO8lCg==\nInnerHash: 659ADA7EBBC15CAB80E7F1D3DB5AFE650CDA32B4891EEC8E41E50A6D4800413C\nNonce: 10400000001617\n"
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment