Skip to content

Commit

Permalink
mid stage simple tx changes
Browse files Browse the repository at this point in the history
  • Loading branch information
milansismanovic committed Mar 9, 2018
1 parent 7da894f commit a8d9141
Show file tree
Hide file tree
Showing 15 changed files with 519 additions and 46 deletions.
6 changes: 3 additions & 3 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.source=1.7
2 changes: 1 addition & 1 deletion .settings/org.eclipse.wst.common.project.facet.core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<installed facet="jst.web" version="2.5"/>
<installed facet="jst.jaxrs" version="2.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.8"/>
<installed facet="java" version="1.7"/>
</faceted-project>
10 changes: 9 additions & 1 deletion src/main/java/com/_37coins/bcJsonRpc/BitcoindInterface.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com._37coins.bcJsonRpc;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand All @@ -12,6 +13,8 @@
import com._37coins.bcJsonRpc.pojo.Info;
import com._37coins.bcJsonRpc.pojo.LastBlock;
import com._37coins.bcJsonRpc.pojo.RawTransaction;
import com._37coins.bcJsonRpc.pojo.RawTxInput;
import com._37coins.bcJsonRpc.pojo.RawTxOutput;
import com._37coins.bcJsonRpc.pojo.Transaction;


Expand Down Expand Up @@ -43,7 +46,6 @@ public interface BitcoindInterface {
public BigDecimal getbalance();
//If [account] is specified, returns the balance in the account.
public BigDecimal getbalance(String account);
//
public BigDecimal getbalance(String account, int minimumConfirmations);
//Returns information about the block with the given hash.
public Block getblock(String blockHash);
Expand All @@ -63,6 +65,12 @@ public interface BitcoindInterface {
public long gethashespersec();
//Returns an object about the given transaction hash.
public Transaction gettransaction(String hash);

//creates a raw transaction and returns its byte representations
public String createrawtransaction(Collection<RawTxInput> rawTxInput, RawTxOutput rawTxOutput);
//creates a raw transaction and returns its byte representations
public String sendrawtransaction(String rawTransaction);

//Returns an object about the given transaction hash.
public String getrawtransaction(String hash);
//Returns an object about the given transaction hash.
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/com/_37coins/bcJsonRpc/pojo/RawTxInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com._37coins.bcJsonRpc.pojo;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "value", "n", "scriptPubKey" })
public class RawTxInput {

@JsonProperty("txid")
private String txid;
@JsonProperty("vout")
private Integer vout;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public RawTxInput(String txid, Integer vout, Map<String, Object> additionalProperties) {
super();
this.txid = txid;
this.vout = vout;
this.additionalProperties = additionalProperties;
}

@JsonProperty("txid")
public String getTxid() {
return txid;
}

@JsonProperty("txid")
public void setTxid(String txid) {
this.txid = txid;
}

@JsonProperty("vout")
public Integer getVout() {
return vout;
}

@JsonProperty("vout")
public void setVout(Integer vout) {
this.vout = vout;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("txid", txid).append("vout", vout)
.toString();
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(additionalProperties).append(txid).append(vout).toHashCode();
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof RawTxInput) == false) {
return false;
}
RawTxInput rhs = ((RawTxInput) other);
return new EqualsBuilder().append(additionalProperties, rhs.additionalProperties).append(txid, rhs.txid)
.append(vout, rhs.vout).isEquals();
}

}
59 changes: 59 additions & 0 deletions src/main/java/com/_37coins/bcJsonRpc/pojo/RawTxOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com._37coins.bcJsonRpc.pojo;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class RawTxOutput {

private Map<String, Object> addressValuePairs = new HashMap<String, Object>();

public RawTxOutput(Map<String, Object> addressValuePairs) {
super();
this.addressValuePairs = addressValuePairs;
}

public RawTxOutput() {
}

@JsonAnyGetter
public Map<String, Object> getAddressValuePairs() {
return this.addressValuePairs;
}

@JsonAnySetter
public void setAddressValuePairs(String name, Object value) {
this.addressValuePairs.put(name, value);
}

@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append(addressValuePairs).toString();
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(addressValuePairs).toHashCode();
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof RawTxOutput) == false) {
return false;
}
RawTxOutput rhs = ((RawTxOutput) other);
return new EqualsBuilder().append(addressValuePairs, rhs.addressValuePairs).isEquals();
}
}
3 changes: 2 additions & 1 deletion src/main/java/rest/bitcoin/Bitcoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Collection<String> getClientaddresses1() {
// // TODO implement a clever sync from store to the bitcoin network
// // suggestion: create anonymous inner class to run the sync to update the db
// // as a singleton to ensure the thread is only spanned once.
// // i.e. use BlockListener from the BitcoindClient4J
// class DBRefresher {
//
// }
Expand Down Expand Up @@ -212,7 +213,7 @@ public List<Transaction> startTransaction(@QueryParam("toAddress") String toAddr
@QueryParam("satoshiAmount") int satoshiAmount, @QueryParam("changeAddress") String changeAddress)
throws BlockStoreException {
// TODO create new list of transactions to be signed with key 1
SortedSet<StoredTransaction> utxos = store.getUnspentTx(getClientaddresses1());
SortedSet<StoredVout> utxos = store.getUnspentVouts(getClientaddresses1());
// create the tx
// create the inputs
// create the outputs
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/store/bitcoin/BlockStore.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package store.bitcoin;

import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;

import store.bitcoin.pojo.StoredBlock;
import store.bitcoin.pojo.StoredTransaction;
import store.bitcoin.pojo.StoredVout;

public abstract class BlockStore {
StoredBlock chainHeadBlock;
Expand Down Expand Up @@ -41,6 +41,15 @@ public int getChainHeadHeight() throws BlockStoreException {
return chainHeadHeigth;
}

public BigDecimal getBalance(Collection<String> addresses) throws BlockStoreException{
SortedSet<StoredVout> utxos = getUnspentVouts(addresses);
BigDecimal balance = new BigDecimal(0);
for (StoredVout vout : utxos) {
balance = balance.add(vout.getAmount());
}
return balance;
}

public abstract StoredBlock get(String hash) throws BlockStoreException;

public abstract StoredBlock get(int height) throws BlockStoreException;
Expand All @@ -49,9 +58,7 @@ public int getChainHeadHeight() throws BlockStoreException {

public abstract SortedSet<StoredTransaction> getTx(Collection<String> addresses) throws BlockStoreException;

public abstract SortedSet<StoredTransaction> getUnspentTx(Collection<String> addresses) throws BlockStoreException;

public abstract BigInteger getBalance(List<String> addresses) throws BlockStoreException;
public abstract SortedSet<StoredVout> getUnspentVouts(Collection<String> addresses) throws BlockStoreException;

public abstract void resetStore() throws BlockStoreException;

Expand Down
17 changes: 6 additions & 11 deletions src/main/java/store/bitcoin/DBBlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
Expand Down Expand Up @@ -421,16 +420,6 @@ public SortedSet<StoredTransaction> getTx(Collection<String> addresses) throws B
return txs;
}

public SortedSet<StoredTransaction> getUnspentTx(Collection<String> addresses) throws BlockStoreException {
// TODO Auto-generated method stub
return null;
}

public BigInteger getBalance(List<String> addresses) throws BlockStoreException {
// TODO Auto-generated method stub
return null;
}

@Override
public void updateUTXO(StoredTransaction tx, StoredTransaction parentTx, int parentVoutIndex)
throws BlockStoreException {
Expand Down Expand Up @@ -464,4 +453,10 @@ public void test(List<String> addresses) throws BlockStoreException {
}
}

@Override
public SortedSet<StoredVout> getUnspentVouts(Collection<String> addresses) throws BlockStoreException {
// TODO Auto-generated method stub
return null;
}

}
26 changes: 10 additions & 16 deletions src/main/java/store/bitcoin/MemoryBlockStore.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package store.bitcoin;

import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
Expand All @@ -23,7 +22,7 @@
* @author milan
*
*/
public class MemoryBlockStore extends BlockStore{
public class MemoryBlockStore extends BlockStore {
private static final Logger log = LoggerFactory.getLogger(MemoryBlockStore.class);

Map<String, StoredBlock> blockMap = new HashMap<String, StoredBlock>();
Expand Down Expand Up @@ -92,7 +91,8 @@ void addAddressTransactions(String address, StoredTransaction tx) {
log.info("stx hashcodes {}", stx.hashCode());
log.info("tx hashcodes {}", tx.hashCode());
if (tx.equals(stx)) {
// evidence 1 of erroneous set behavior: contains returns false even though we are iterating through
// evidence 1 of erroneous set behavior: contains returns false even though we
// are iterating through
// the set's content.
log.info("set.contains(tx):{}", thisaddressTransactions.contains(tx));
// evidence 2: the hashcode and equals show stx and tx are the same
Expand Down Expand Up @@ -174,25 +174,19 @@ public void resetStore() throws BlockStoreException {
}

@Override
public SortedSet<StoredTransaction> getUnspentTx(Collection<String> addresses) throws BlockStoreException {
SortedSet<StoredTransaction> utxos = new TreeSet<>();
public SortedSet<StoredVout> getUnspentVouts(Collection<String> addresses) throws BlockStoreException {
SortedSet<StoredTransaction> allTx = getTx(addresses);
for(StoredTransaction tx:allTx) {
if(tx.getVouts()!=null) {
for(StoredVout vout:tx.getVouts()) {
if(vout.isUnspent()) {
utxos.add(tx);
SortedSet<StoredVout> utxos = new TreeSet<>();
for (StoredTransaction tx : allTx) {
if (tx.getVouts() != null) {
for (StoredVout vout : tx.getVouts()) {
if (vout.isUnspent()) {
utxos.add(vout);
continue;
}
}
}
}
return utxos;
}

@Override
public BigInteger getBalance(List<String> addresses) throws BlockStoreException {
// TODO Auto-generated method stub
throw new RuntimeException("not yet implemented");
}
}
3 changes: 2 additions & 1 deletion src/main/java/store/bitcoin/StoreLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ List<StoredTransaction> createStoredTxList(BlockVerbose block) {
}
// get all vouts into svouts
List<StoredVout> svouts = new LinkedList<>();
int index = 0;
for (Vout vout : tx.getVout()) {
List<String> addressList = vout.getScriptPubKey().getAddresses();
// the check if the tx has been spent must be done in a second run
// therefore we set its unspent value to null meaning unknown
StoredVout svout = new StoredVout(tx.getTxid(), addressList, vout.getValue(), null);
StoredVout svout = new StoredVout(tx.getTxid(), index++, addressList, vout.getValue(), null);
svouts.add(svout);
}
// create tx and add it to the list
Expand Down
Loading

0 comments on commit a8d9141

Please sign in to comment.