mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-25 07:27:18 +01:00
Start mock tx parsing
This commit is contained in:
parent
857d31ecc3
commit
5bb9c0e093
7 changed files with 368 additions and 0 deletions
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.bitsquare.dao.tokens;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class MockTxService extends TxService {
|
||||||
|
private final Tx genesisTx;
|
||||||
|
private Map<String, Tx> txMap = new HashMap<>();
|
||||||
|
|
||||||
|
public MockTxService(Tx genesisTx) {
|
||||||
|
txMap.put(genesisTx.id, genesisTx);
|
||||||
|
this.genesisTx = genesisTx;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tx getTx(String txId) {
|
||||||
|
return txMap.get(txId);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.bitsquare.dao.tokens;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TransactionParser {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(TransactionParser.class);
|
||||||
|
|
||||||
|
private String genesisTxId = "83a4454747e5c972f2eb20d587538a330dd30b5cf468f8faea32eae640cebe79";
|
||||||
|
private TxService txService;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Constructor
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public TransactionParser(String genesisTxId, TxService txService) {
|
||||||
|
this.genesisTxId = genesisTxId;
|
||||||
|
this.txService = txService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tx getTx(String txId) {
|
||||||
|
return txService.getTx(txId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
Tx genesisTx = getTx(genesisTxId);
|
||||||
|
List<TxOutput> allUTXOs = findAllUTXOs(genesisTx);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TxOutput> findAllUTXOs(Tx tx) {
|
||||||
|
List<TxOutput> allUTXOs = new ArrayList<>();
|
||||||
|
getOutputs(tx).stream().forEach(txOutput -> {
|
||||||
|
if (txOutput.isSpent) {
|
||||||
|
allUTXOs.addAll(findAllUTXOs(txOutput.spentByTxInput.parentTx));
|
||||||
|
} else if (isValidOutput(txOutput)) {
|
||||||
|
allUTXOs.add(txOutput);
|
||||||
|
} else {
|
||||||
|
log.warn("invalid output " + txOutput);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return allUTXOs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidOutput(TxOutput output) {
|
||||||
|
output.parentTx.inputs.forEach(input -> {
|
||||||
|
if (isValidInput(input)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidInput(TxInput input) {
|
||||||
|
input.parentTx.outputs.forEach(output -> {
|
||||||
|
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<TxOutput> getOutputs(Tx tx) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
70
core/src/main/java/io/bitsquare/dao/tokens/Tx.java
Normal file
70
core/src/main/java/io/bitsquare/dao/tokens/Tx.java
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.bitsquare.dao.tokens;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Tx {
|
||||||
|
public Tx(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tx(String id, List<TxInput> inputs, List<TxOutput> outputs) {
|
||||||
|
this.id = id;
|
||||||
|
this.inputs = inputs;
|
||||||
|
this.outputs = outputs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String id;
|
||||||
|
public List<TxInput> inputs = new ArrayList<>();
|
||||||
|
public List<TxOutput> outputs = new ArrayList<>();
|
||||||
|
|
||||||
|
public void addOutput(TxOutput output) {
|
||||||
|
output.parentTx = this;
|
||||||
|
output.index = outputs.size();
|
||||||
|
outputs.add(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addInput(TxInput input) {
|
||||||
|
input.parentTx = this;
|
||||||
|
input.index = inputs.size();
|
||||||
|
inputs.add(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Tx tx = (Tx) o;
|
||||||
|
|
||||||
|
if (id != null ? !id.equals(tx.id) : tx.id != null) return false;
|
||||||
|
if (outputs != null ? !outputs.equals(tx.outputs) : tx.outputs != null) return false;
|
||||||
|
return !(inputs != null ? !inputs.equals(tx.inputs) : tx.inputs != null);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = id != null ? id.hashCode() : 0;
|
||||||
|
result = 31 * result + (outputs != null ? outputs.hashCode() : 0);
|
||||||
|
result = 31 * result + (inputs != null ? inputs.hashCode() : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
32
core/src/main/java/io/bitsquare/dao/tokens/TxInput.java
Normal file
32
core/src/main/java/io/bitsquare/dao/tokens/TxInput.java
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.bitsquare.dao.tokens;
|
||||||
|
|
||||||
|
public class TxInput {
|
||||||
|
public TxInput(Tx parentTx, TxOutput output, long value) {
|
||||||
|
this.parentTx = parentTx;
|
||||||
|
this.output = output;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TxOutput output;
|
||||||
|
public Tx parentTx;
|
||||||
|
public long value;
|
||||||
|
public int index;
|
||||||
|
public boolean isValid;
|
||||||
|
}
|
34
core/src/main/java/io/bitsquare/dao/tokens/TxOutput.java
Normal file
34
core/src/main/java/io/bitsquare/dao/tokens/TxOutput.java
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.bitsquare.dao.tokens;
|
||||||
|
|
||||||
|
public class TxOutput {
|
||||||
|
public TxOutput(String address, long value) {
|
||||||
|
this.address = address;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tx parentTx;
|
||||||
|
public boolean isSpent;
|
||||||
|
public TxInput spentByTxInput;
|
||||||
|
public String address;
|
||||||
|
public long value;
|
||||||
|
public int index;
|
||||||
|
public boolean isValid;
|
||||||
|
|
||||||
|
}
|
22
core/src/main/java/io/bitsquare/dao/tokens/TxService.java
Normal file
22
core/src/main/java/io/bitsquare/dao/tokens/TxService.java
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.bitsquare.dao.tokens;
|
||||||
|
|
||||||
|
abstract class TxService {
|
||||||
|
abstract public Tx getTx(String txId);
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.bitsquare.dao.tokens;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.Coin;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class TransactionParserTest {
|
||||||
|
private Tx genesisTx;
|
||||||
|
private MockTxService txService;
|
||||||
|
private TransactionParser transactionParser;
|
||||||
|
private TxOutput output1;
|
||||||
|
private TxOutput output2;
|
||||||
|
private TxInput input1;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
genesisTx = getGenesisTx();
|
||||||
|
txService = new MockTxService(genesisTx);
|
||||||
|
transactionParser = new TransactionParser("123", txService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetTx() {
|
||||||
|
Tx genesisTxResult = transactionParser.getTx("id_genesis");
|
||||||
|
assertEquals(genesisTx, genesisTxResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTx1() {
|
||||||
|
Tx genesisTxResult = transactionParser.getTx("id_genesis");
|
||||||
|
assertEquals(genesisTx, genesisTxResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Tx getGenesisTx() {
|
||||||
|
Tx tx = new Tx("id_genesis");
|
||||||
|
|
||||||
|
tx.addInput(new TxInput(new Tx("id_0001", null, null), null, Coin.COIN.value));
|
||||||
|
|
||||||
|
output1 = new TxOutput("addr_1", 1000);
|
||||||
|
tx.addOutput(output1);
|
||||||
|
|
||||||
|
output2 = new TxOutput("addr_2", 2000);
|
||||||
|
tx.addOutput(output2);
|
||||||
|
|
||||||
|
return tx;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Tx getTx1() {
|
||||||
|
Tx tx = new Tx("id_tx1");
|
||||||
|
|
||||||
|
input1 = new TxInput(genesisTx, output1, Coin.COIN.value);
|
||||||
|
tx.addInput(input1);
|
||||||
|
|
||||||
|
TxInput feeInput = new TxInput(new Tx("id_0001", null, null), null, 20_000);
|
||||||
|
Tx feeTx = new Tx("id_fee_1", null, null);
|
||||||
|
|
||||||
|
tx.addOutput(new TxOutput("addr_1", 1000));
|
||||||
|
tx.addOutput(new TxOutput("addr_2", 2000));
|
||||||
|
return tx;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue