Skip to content

Commit

Permalink
Testing first stratum function - getAddressInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ripper234 committed Jan 13, 2012
1 parent a0c4fe5 commit ea4b19a
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 16 deletions.
5 changes: 5 additions & 0 deletions app/com/bitcoinpotato/util/Func2.java
@@ -0,0 +1,5 @@
package com.bitcoinpotato.util;

public interface Func2<TArg1, TArg2, TResult> {
TResult apply(TArg1 arg1, TArg2 arg2);
}
32 changes: 32 additions & 0 deletions app/com/bitcoinpotato/util/Maps3.java
@@ -0,0 +1,32 @@
package com.bitcoinpotato.util;

import com.google.common.base.Function;

import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Maps.newHashMap;

public class Maps3 {
public static <K, V> Map<K, V> build(Iterable<K> keys, Function<K, V> buildValue) {
Map<K, V> result = newHashMap();
for (K key : keys) {
checkArgument(!result.containsKey(key));
V value = buildValue.apply(key);
result.put(key, value);
}
return result;
}

public static <K, V1, V2> Map<K, V2> build(Map<K, V1> input, Func2<K, V1, V2> buildValue) {
Map<K, V2> result = newHashMap();
for (Map.Entry<K, V1> entry : input.entrySet()) {
checkArgument(!result.containsKey(entry.getKey()));
K key = entry.getKey();
V1 value1 = entry.getValue();
V2 value = buildValue.apply(key, value1);
result.put(key, value);
}
return result;
}
}
3 changes: 3 additions & 0 deletions app/jobs/Bootstrap.java
Expand Up @@ -16,6 +16,9 @@ public class Bootstrap extends Job {

@Override
public void doJob() throws Exception {
if (Play.runingInTestMode())
return;

// TODO - (This fails in Prod mode)
// Logger.getRootLogger().addAppender(new DBAppender());

Expand Down
1 change: 0 additions & 1 deletion app/models/IncomingTransaction.java
Expand Up @@ -43,4 +43,3 @@ public static List<IncomingTransaction> byStatusSortedChronologically(Status sta
return find("byStatus order by created", status).fetch();
}
}

48 changes: 35 additions & 13 deletions app/org/bitcoin/stratum/Stratum.java
@@ -1,22 +1,27 @@
package org.bitcoin.stratum;

import com.bitcoinpotato.util.Maps3;
import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.core.NetworkParameters;
import com.google.common.base.Function;
import play.libs.WS;

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.base.Preconditions.checkArgument;

public class Stratum {
public Stratum(NetworkParameters networkParameters) {
public Stratum(NetworkParameters networkParameters, String startumServerUrl) {
this.networkParameters = networkParameters;
this.startumServerUrl = startumServerUrl;
}

private final NetworkParameters networkParameters;
private final String startumServerUrl;

public static BigDecimal satoshisToBitcoin(BigInteger satoshis) {
return new BigDecimal("0.00000001").multiply(new BigDecimal(satoshis));
Expand All @@ -27,24 +32,41 @@ public KeyPair newKeyPair() {
return new KeyPair(key.toAddress(networkParameters).toString(), key.getPrivKeyBytes());
}

/**
* Returns a list of all transaction that went to {@code address}, sorted by date.
*/
public List<RemoteTransaction> getRemoteTransactions(String address) {
// TODO
return newArrayList();
}

public void sendTransaction(OutgoingRemoteTransaction outgoingTx) {

}

public AddressInfo getAddressInfo(String publicAddress) {
WS.HttpResponse response = null;
try {
response = WS.url(startumServerUrl)
.setHeader("content-type", "application/stratum")
.setParameter("id", 1)
.setParameter("method", "blockchain.address.get_history")
.setParameter("params", publicAddress)
.timeout("60s")
.post();
} catch (Exception e) {
throw new RuntimeException("Failure doing Startum.getAddress with address " + publicAddress);
}

checkArgument(response.getStatus() == 200);

// TODO
return null;
return new AddressInfo(new BigDecimal("0.01"));
}

/**
* A bulk API version
*/
public Map<String, AddressInfo> getAddressInfo(Collection<String> allPublicAddress) {
return null; //To change body of created methods use File | Settings | File Templates.
// Naive implementation for now

return Maps3.build(allPublicAddress, new Function<String, AddressInfo>() {
public AddressInfo apply(@Nullable String s) {
return null;
}
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/org/bitcoin/stratum/StratumHolder.java
Expand Up @@ -9,7 +9,7 @@ public class StratumHolder {
private StratumHolder() {
}

public static final Stratum Stratum = new Stratum(readNetworkParameters());
public static final Stratum Stratum = new Stratum(readNetworkParameters(), "http://chicago.stratum.bitcoin.cz:8000/");

private static NetworkParameters readNetworkParameters() {
String network = getNetworkType();
Expand Down
5 changes: 4 additions & 1 deletion potato.ipr
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AntConfiguration">
<defaultAnt bundledAnt="true" />
</component>
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
Expand Down Expand Up @@ -227,7 +230,7 @@
<component name="ProjectResources">
<default-html-doctype>http://www.w3.org/1999/xhtml</default-html-doctype>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_5" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK" />
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
Expand Down
17 changes: 17 additions & 0 deletions test/org/bitcoin/stratum/StratumTests.java
@@ -0,0 +1,17 @@
package org.bitcoin.stratum;

import com.google.bitcoin.core.NetworkParameters;
import org.junit.Test;
import play.test.UnitTest;

public class StratumTests extends UnitTest {
@Test
public void sanity() {
Stratum stratum = new Stratum(NetworkParameters.testNet(), "http://chicago.stratum.bitcoin.cz:8000/");
String someTestnetAddress = "mw5h6BDh77GUPc4DCmzjoU7ry2R4exoaYr";

AddressInfo addressInfo = stratum.getAddressInfo(someTestnetAddress);

System.out.println("Foobar");
}
}

0 comments on commit ea4b19a

Please sign in to comment.