Search in sources :

Example 1 with AddressSpecHash

use of snowblossom.lib.AddressSpecHash in project snowblossom by snowblossomcoin.

the class ShardUtxoImport method checkTipTrust.

/**
 * Take an inbound tip and see if it is signing a block
 * with a key we trust.  If so, trust that block and maybe some parent blocks.
 *
 * Returns a list of hashes to request ImportedBlock for
 */
public List<ChainHash> checkTipTrust(MetricLog mlog, PeerChainTip tip) throws ValidationException {
    // If there is no block
    if (tip.getHeader().getSnowHash().size() == 0)
        return null;
    int shard_id = tip.getHeader().getShardId();
    // If this is a shard we actually track
    if (node.getInterestShards().contains(shard_id))
        return null;
    mlog.set("trusted_signers", trusted_signers.size());
    // We trust no one
    if (trusted_signers.size() == 0)
        return null;
    if (tip.getSignedHead() == null)
        return null;
    if (tip.getSignedHead().getPayload().size() == 0)
        return null;
    SignedMessagePayload payload = MsgSigUtil.validateSignedMessage(tip.getSignedHead(), node.getParams());
    mlog.set("valid_payload", 1);
    AddressSpecHash signer = AddressUtil.getHashForSpec(payload.getClaim());
    if (!trusted_signers.contains(signer))
        return null;
    mlog.set("trusted_sig", 1);
    // Now we have a valid signed head from a trusted signer
    logger.finer(String.format("Got signed tip from trusted peer (signer:%s)", AddressUtil.getAddressString("node", signer)));
    PeerTipInfo tip_info = payload.getPeerTipInfo();
    if (tip_info.getCoordHead().getSnowHash().size() > 0) {
        BlockPreview bp = tip_info.getCoordHead();
        node.getForgeInfo().saveExtCoordHead(bp);
    }
    LinkedList<ChainHash> request_list = new LinkedList<>();
    for (BlockPreview bp : tip_info.getPreviewsList()) {
        ChainHash hash = new ChainHash(bp.getSnowHash());
        node.getDB().setBlockTrust(hash);
        node.getDB().getChildBlockMapSet().add(bp.getPrevBlockHash(), bp.getSnowHash());
        ImportedBlock ib = getImportBlock(hash);
        if (ib == null) {
            // If we don't have the block, maybe request it
            if (reserveBlock(hash)) {
                request_list.add(hash);
            }
        }
    }
    return request_list;
}
Also used : ChainHash(snowblossom.lib.ChainHash) AddressSpecHash(snowblossom.lib.AddressSpecHash) LinkedList(java.util.LinkedList)

Example 2 with AddressSpecHash

use of snowblossom.lib.AddressSpecHash in project snowblossom by snowblossomcoin.

the class StubUtil method openChannel.

public static ManagedChannel openChannel(String uri, NetworkParams params) throws Exception {
    URI u = new URI(uri);
    String host = u.getHost();
    int port = u.getPort();
    String scheme = u.getScheme();
    if (scheme == null)
        scheme = "grpc";
    if (port == -1) {
        if (scheme.equals("grpc")) {
            port = params.getDefaultPort();
        } else if (scheme.equals("grpc+tls")) {
            port = params.getDefaultTlsPort();
        } else {
            throw new Exception("Unknown scheme: " + scheme);
        }
    }
    if (scheme.equals("grpc")) {
        if (port == -1)
            port = params.getDefaultPort();
        return ManagedChannelBuilder.forAddress(host, port).usePlaintext().maxInboundMessageSize(params.getGrpcMaxMessageSize()).build();
    } else if (scheme.equals("grpc+tls")) {
        if (!SystemUtil.isJvm64Bit()) {
            logger.log(Level.SEVERE, "Unable to use grpc+tls without 64-bit JVM");
            throw new Exception("64 bit JVM required for TLS");
        }
        if (port == -1)
            port = params.getDefaultTlsPort();
        AddressSpecHash expected_key = null;
        String query = u.getQuery();
        Properties query_props = new Properties();
        if (query != null) {
            query_props.load(new ByteArrayInputStream(query.replace('&', '\n').getBytes()));
            if (query_props.getProperty("key") != null) {
                expected_key = new AddressSpecHash(query_props.getProperty("key"), Globals.NODE_ADDRESS_STRING);
            }
        }
        SslContext ssl_ctx = GrpcSslContexts.forClient().trustManager(SnowTrustManagerFactorySpi.getFactory(expected_key, params)).build();
        return NettyChannelBuilder.forAddress(host, port).useTransportSecurity().sslContext(ssl_ctx).maxInboundMessageSize(params.getGrpcMaxMessageSize()).build();
    } else {
        throw new Exception("Unknown scheme: " + scheme);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Properties(java.util.Properties) URI(java.net.URI) AddressSpecHash(snowblossom.lib.AddressSpecHash) SslContext(io.netty.handler.ssl.SslContext)

Example 3 with AddressSpecHash

use of snowblossom.lib.AddressSpecHash in project snowblossom by snowblossomcoin.

the class WalletUtil method getAllUnused.

public static Collection<AddressSpecHash> getAllUnused(WalletDatabase db, NetworkParams params) {
    TreeMap<String, AddressSpecHash> addr_to_hash_map = new TreeMap<>();
    for (AddressSpec spec : db.getAddressesList()) {
        String addr = AddressUtil.getAddressString(spec, params);
        addr_to_hash_map.put(addr, AddressUtil.getHashForSpec(spec));
    }
    for (String addr : db.getUsedAddressesMap().keySet()) {
        addr_to_hash_map.remove(addr);
    }
    return addr_to_hash_map.values();
}
Also used : ByteString(com.google.protobuf.ByteString) TreeMap(java.util.TreeMap) AddressSpecHash(snowblossom.lib.AddressSpecHash)

Example 4 with AddressSpecHash

use of snowblossom.lib.AddressSpecHash in project snowblossom by snowblossomcoin.

the class WalletUtil method getAddressesByAge.

public static Collection<AddressSpecHash> getAddressesByAge(WalletDatabase db, NetworkParams params) {
    TreeMultimap<Long, AddressSpecHash> age_map = TreeMultimap.create();
    for (AddressSpec spec : db.getAddressesList()) {
        String addr = AddressUtil.getAddressString(spec, params);
        long tm = 0;
        if (db.getAddressCreateTimeMap().containsKey(addr)) {
            tm = db.getAddressCreateTimeMap().get(addr);
        }
        age_map.put(tm, AddressUtil.getHashForSpec(spec));
    }
    return age_map.values();
}
Also used : ByteString(com.google.protobuf.ByteString) AddressSpecHash(snowblossom.lib.AddressSpecHash)

Example 5 with AddressSpecHash

use of snowblossom.lib.AddressSpecHash in project snowblossom by snowblossomcoin.

the class WalletUtil method loadNodeWalletFromConfig.

/**
 * Loads or creates a single key wallet based on the config param name pointing
 * to a path.
 */
public static WalletDatabase loadNodeWalletFromConfig(NetworkParams params, Config config, String param_name) throws Exception {
    if (!config.isSet(param_name))
        return null;
    config.require(param_name);
    TreeMap<String, String> wallet_config_map = new TreeMap<>();
    wallet_config_map.put("wallet_path", config.get(param_name));
    wallet_config_map.put("key_count", "1");
    wallet_config_map.put("key_mode", WalletUtil.MODE_STANDARD);
    ConfigMem config_wallet = new ConfigMem(wallet_config_map);
    File wallet_path = new File(config_wallet.get("wallet_path"));
    WalletDatabase wallet_db = WalletUtil.loadWallet(wallet_path, true, params);
    if (wallet_db == null) {
        logger.log(Level.WARNING, String.format("Directory %s does not contain keys, creating new keys", wallet_path.getPath()));
        wallet_db = WalletUtil.makeNewDatabase(config_wallet, params);
        WalletUtil.saveWallet(wallet_db, wallet_path);
        AddressSpecHash spec = AddressUtil.getHashForSpec(wallet_db.getAddresses(0));
        String addr = AddressUtil.getAddressString("node", spec);
        File dir = new File(config.get(param_name));
        PrintStream out = new PrintStream(new FileOutputStream(new File(dir, "address.txt"), false));
        out.println(addr);
        out.close();
    }
    return wallet_db;
}
Also used : PrintStream(java.io.PrintStream) AtomicFileOutputStream(duckutil.AtomicFileOutputStream) FileOutputStream(java.io.FileOutputStream) ByteString(com.google.protobuf.ByteString) TreeMap(java.util.TreeMap) ConfigMem(duckutil.ConfigMem) File(java.io.File) AddressSpecHash(snowblossom.lib.AddressSpecHash)

Aggregations

AddressSpecHash (snowblossom.lib.AddressSpecHash)39 Test (org.junit.Test)20 ByteString (com.google.protobuf.ByteString)16 File (java.io.File)13 KeyPair (java.security.KeyPair)12 SnowBlossomMiner (snowblossom.miner.SnowBlossomMiner)9 SnowBlossomNode (snowblossom.node.SnowBlossomNode)9 Random (java.util.Random)7 SnowBlossomClient (snowblossom.client.SnowBlossomClient)6 ChainHash (snowblossom.lib.ChainHash)5 TransactionBridge (snowblossom.lib.TransactionBridge)4 LinkedList (java.util.LinkedList)3 TreeMap (java.util.TreeMap)3 PoolMiner (snowblossom.miner.PoolMiner)3 MrPlow (snowblossom.miner.plow.MrPlow)3 HashSet (java.util.HashSet)2 AtomicFileOutputStream (duckutil.AtomicFileOutputStream)1 ConfigMem (duckutil.ConfigMem)1 Pair (duckutil.Pair)1 SslContext (io.netty.handler.ssl.SslContext)1