Skip to content

Commit

Permalink
Merge branch 'master' of github.com:snowblossomcoin/snowblossom
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed May 23, 2018
2 parents 32b0e9e + 55e27f6 commit 35d5768
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 34 deletions.
3 changes: 2 additions & 1 deletion proto/snowblossom.proto
Expand Up @@ -179,6 +179,7 @@ message PeerInfo {
int64 last_passed = 4;
int64 learned = 5;
string version = 6;
bytes node_id = 7;
}

message RequestBlock {
Expand Down Expand Up @@ -243,8 +244,8 @@ message NullRequest {
message NodeStatus {
int32 mem_pool_size = 1;
int32 connected_peers = 2;

BlockSummary head_summary = 3;
int32 estimated_nodes = 4;
}

// -------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions src/AddressUtil.java
Expand Up @@ -15,6 +15,9 @@
import com.google.common.collect.ImmutableSet;


import org.junit.Assert;
import java.util.List;

public class AddressUtil
{
public static AddressSpecHash getHashForSpec(AddressSpec spec)
Expand Down Expand Up @@ -58,6 +61,22 @@ public static AddressSpec getSimpleSpecForKey(ByteString key_data, int sig_type)
.build())
.build();
}

public static AddressSpec getMultiSig(int required, List<WalletKeyPair> wkp_list)
{
AddressSpec.Builder addrspec = AddressSpec.newBuilder();
addrspec.setRequiredSigners(required);

Assert.assertTrue(required >= wkp_list.size());
for(WalletKeyPair wkp : wkp_list)
{
addrspec.addSigSpecs ( SigSpec.newBuilder()
.setSignatureType( wkp.getSignatureType() )
.setPublicKey ( wkp.getPublicKey() )
.build() );
}
return addrspec.build();
}

public static AddressSpec getSimpleSpecForKey(WalletKeyPair wkp)
{
Expand Down
17 changes: 16 additions & 1 deletion src/BlockIngestor.java
Expand Up @@ -12,7 +12,7 @@
import snowblossom.db.DB;
import org.junit.Assert;


import java.util.HashMap;
import java.util.logging.Logger;
import java.util.logging.Level;

Expand Down Expand Up @@ -41,6 +41,8 @@ public class BlockIngestor

private PrintStream block_log;

private boolean tx_index=false;


public BlockIngestor(SnowBlossomNode node)
throws Exception
Expand All @@ -62,6 +64,8 @@ public BlockIngestor(SnowBlossomNode node)
new ChainHash(chainhead.getHeader().getSnowHash())));
}

tx_index = node.getConfig().getBoolean("tx_index");

}

public boolean ingestBlock(Block blk)
Expand Down Expand Up @@ -99,6 +103,17 @@ public boolean ingestBlock(Block blk)

Validation.deepBlockValidation(node, blk, prev_summary);

if (tx_index)
{
HashMap<ByteString, Transaction> tx_map = new HashMap<>();
for(Transaction tx : blk.getTransactionsList())
{
tx_map.put(tx.getTxHash(), tx);
}
db.getTransactionMap().putAll(tx_map);
}


db.getBlockMap().put( blockhash.getBytes(), blk);
db.getBlockSummaryMap().put( blockhash.getBytes(), summary);

Expand Down
9 changes: 9 additions & 0 deletions src/Config.java
Expand Up @@ -12,6 +12,15 @@ public abstract class Config

public abstract String get(String key);

public String getWithDefault(String key, String def)
{
if (isSet(key))
{
return get(key);
}
return def;
}

public void require(String key)
{
if(get(key) == null)
Expand Down
2 changes: 2 additions & 0 deletions src/Globals.java
Expand Up @@ -38,6 +38,8 @@ public class Globals
public static final int MAX_BLOCK_SIZE = 3800000;
public static final int MAX_TX_SIZE = 1000000;

public static final int MAX_NODE_ID_SIZE = 8;


public static void addCryptoProvider()
{
Expand Down
2 changes: 1 addition & 1 deletion src/PeerLink.java
Expand Up @@ -119,7 +119,7 @@ else if (msg.hasTip())
PeerChainTip tip = msg.getTip();
if (!node.getParams().getNetworkName().equals(tip.getNetworkName()))
{
logger.log(Level.INFO, String.format("Peer has wrong name: %s", tip.getNetworkName()));
logger.log(Level.FINE, String.format("Peer has wrong name: %s", tip.getNetworkName()));
close();
return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/PeerUtil.java
Expand Up @@ -15,8 +15,8 @@ public static PeerInfo mergePeers(PeerInfo a, PeerInfo b)

if (a.getLearned() > b.getLearned())
{
n.mergeFrom(b);
n.mergeFrom(a);
n.mergeFrom(b);
n.mergeFrom(a);
}
else
{
Expand All @@ -35,4 +35,5 @@ public static String getString(PeerInfo a)
{
return a.getHost() + ":" + a.getPort();
}

}
40 changes: 38 additions & 2 deletions src/Peerage.java
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.LinkedList;
import java.net.URL;
import java.util.HashSet;
import java.util.Random;
import java.net.InetAddress;

Expand All @@ -52,6 +53,7 @@ public class Peerage
private Map<String, PeerInfo> peer_rumor_list;

private ImmutableSet<String> self_peer_names;
private ImmutableList<PeerInfo> self_peer_info;

public Peerage(SnowBlossomNode node)
{
Expand Down Expand Up @@ -121,14 +123,14 @@ private PeerChainTip getTip()
}
}

for(int i=0; i<8; i++)
tip.addAllPeers(self_peer_info);
for(int i=0; i<4; i++)
{
if (peer_map.size() > 0)
{
tip.addPeers(peer_map.pollFirstEntry().getValue());
}
}


return tip.build();
}
Expand All @@ -141,6 +143,20 @@ public int getConnectedPeerCount()
}
}

public int getEstimateUniqueNodes()
{
HashSet<ByteString> set = new HashSet<>();
synchronized(peer_rumor_list)
{
for(PeerInfo info : peer_rumor_list.values())
{
set.add(info.getNodeId());
}
}
return set.size();

}

private ImmutableList<PeerLink> getLinkList()
{
synchronized(links)
Expand Down Expand Up @@ -219,6 +235,7 @@ public void learnSelfAndSeed()
public void learnPeer(PeerInfo info)
{
if (info.getLearned() + PEER_EXPIRE_TIME < System.currentTimeMillis()) return;
if (info.getNodeId().size() > Globals.MAX_NODE_ID_SIZE) return;

synchronized(peer_rumor_list)
{
Expand Down Expand Up @@ -339,6 +356,21 @@ private void runPrune()
}
}

private ByteString getNodeId()
{
ByteString id = node.getDB().getSpecialMap().get("node_id");
if (id == null)
{
Random rnd = new Random();
byte[] b = new byte[Globals.MAX_NODE_ID_SIZE];
rnd.nextBytes(b);
id = ByteString.copyFrom(b);
node.getDB().getSpecialMap().put("node_id", id);
}

return id;
}

private List<PeerInfo> getSelfPeers()
{
List<String> advertise_hosts= new LinkedList<>();
Expand All @@ -362,6 +394,8 @@ private List<PeerInfo> getSelfPeers()
}
catch(Throwable t){}

ByteString node_id = getNodeId();

int port = node.getConfig().getInt("service_port");
for(String host : advertise_hosts)
{
Expand All @@ -370,6 +404,7 @@ private List<PeerInfo> getSelfPeers()
.setPort(port)
.setLearned(System.currentTimeMillis())
.setVersion(Globals.VERSION)
.setNodeId(node_id)
.build();

self_peers.add(pi);
Expand All @@ -380,6 +415,7 @@ private List<PeerInfo> getSelfPeers()

}
self_peer_names = ImmutableSet.copyOf(self_names);
self_peer_info = ImmutableList.copyOf(self_peers);

return self_peers;

Expand Down
1 change: 1 addition & 0 deletions src/SnowUserService.java
Expand Up @@ -222,6 +222,7 @@ public void getNodeStatus(NullRequest null_request, StreamObserver<NodeStatus> r
.setMemPoolSize(node.getMemPool().getMemPoolSize())
.setConnectedPeers(node.getPeerage().getConnectedPeerCount())
.setHeadSummary(node.getBlockIngestor().getHead())
.setEstimatedNodes(node.getPeerage().getEstimateUniqueNodes())
.build();

responseObserver.onNext(ns);
Expand Down
2 changes: 1 addition & 1 deletion src/TransactionUtil.java
Expand Up @@ -272,7 +272,7 @@ public static void prettyDisplayTx(Transaction tx, PrintStream out, NetworkParam
throws ValidationException
{
ChainHash tx_hash = new ChainHash(tx.getTxHash());
out.println("Transaction: " + tx_hash);
out.println("Transaction: " + tx_hash + " size: " + tx.toByteString().size());
TreeSet<String> sign_set=new TreeSet<>();
DecimalFormat df = new DecimalFormat("0.000000");

Expand Down
29 changes: 3 additions & 26 deletions src/client/SnowBlossomClient.java
Expand Up @@ -147,9 +147,11 @@ else if (command.equals("loadtest"))

private File wallet_path;
private WalletDatabase wallet_database;
private Config config;

public SnowBlossomClient(Config config) throws Exception
{
this.config = config;
config.require("node_host");

String host = config.get("node_host");
Expand Down Expand Up @@ -201,7 +203,7 @@ public void loadWallet()
else
{
logger.log(Level.WARNING, String.format("File %s does not exist, creating new wallet", db_file.getPath()));
wallet_database = makeNewDatabase();
wallet_database = WalletUtil.makeNewDatabase(config);
saveWallet();
}

Expand Down Expand Up @@ -247,31 +249,6 @@ public void saveWallet()

}

public WalletDatabase makeNewDatabase()
{
WalletDatabase.Builder builder = WalletDatabase.newBuilder();

for(int i=0;i<8; i++)
{
genNewKey(builder);
}

return builder.build();
}

public void genNewKey(WalletDatabase.Builder wallet_builder)
{

WalletKeyPair wkp = KeyUtil.generateWalletStandardECKey();

wallet_builder.addKeys(wkp);

AddressSpec claim = AddressUtil.getSimpleSpecForKey(wkp);

wallet_builder.addAddresses(claim);

}

public void showBalances()
{
long total_confirmed = 0;
Expand Down
67 changes: 67 additions & 0 deletions src/client/WalletUtil.java
@@ -0,0 +1,67 @@
package snowblossom.client;

import snowblossom.*;
import snowblossom.proto.*;
import com.google.common.collect.ImmutableList;

import java.util.logging.Logger;
import java.util.logging.Level;


public class WalletUtil
{
public static final String MODE_STANDARD="standard";
public static final String MODE_QHARD="qhard";

private static final Logger logger = Logger.getLogger("snowblossom.client");

public static WalletDatabase makeNewDatabase(Config config)
{
WalletDatabase.Builder builder = WalletDatabase.newBuilder();

int count = config.getIntWithDefault("key_count", 8);
for(int i=0;i<count; i++)
{
genNewKey(builder, config);
}

return builder.build();
}

public static void genNewKey(WalletDatabase.Builder wallet_builder, Config config)
{
String key_mode = config.getWithDefault("key_mode", MODE_STANDARD).toLowerCase();

if (key_mode.equals(MODE_STANDARD))
{

WalletKeyPair wkp = KeyUtil.generateWalletStandardECKey();
wallet_builder.addKeys(wkp);
AddressSpec claim = AddressUtil.getSimpleSpecForKey(wkp);
wallet_builder.addAddresses(claim);
}
else if (key_mode.equals(MODE_QHARD))
{
logger.info("Creating QHARD key set. This takes a while.");
WalletKeyPair k_ec = KeyUtil.generateWalletStandardECKey();
WalletKeyPair k_rsa = KeyUtil.generateWalletRSAKey(8192);
WalletKeyPair k_dstu = KeyUtil.generateWalletDSTU4145Key(9);

wallet_builder.addKeys(k_ec);
wallet_builder.addKeys(k_rsa);
wallet_builder.addKeys(k_dstu);

AddressSpec claim = AddressUtil.getMultiSig(3, ImmutableList.of(k_ec, k_rsa, k_dstu));
wallet_builder.addAddresses(claim);


}
else
{
throw new RuntimeException("Unknown key_mode: " + key_mode);
}

}


}

0 comments on commit 35d5768

Please sign in to comment.