public static String getHashListShort(List<byte[]> blockHashes) { if (blockHashes.isEmpty()) return "[]"; StringBuilder sb = new StringBuilder(); String firstHash = Hex.toHexString(blockHashes.get(0)); String lastHash = Hex.toHexString(blockHashes.get(blockHashes.size() - 1)); return sb.append(" ").append(firstHash).append("...").append(lastHash).toString(); }
/** * @return generates random peer id for the HelloMessage */ public static byte[] randomPeerId() { byte[] peerIdBytes = new BigInteger(512, Utils.getRandom()).toByteArray(); final String peerId; if (peerIdBytes.length > 64) peerId = Hex.toHexString(peerIdBytes, 1, 64); else peerId = Hex.toHexString(peerIdBytes); return Hex.decode(peerId); }
private String bytesToAscii(byte[] b) { String hex = Hex.toHexString(b); StringBuilder output = new StringBuilder(); for (int i = 0; i < hex.length(); i += 2) { String str = hex.substring(i, i + 2); output.append((char) Integer.parseInt(str, 16)); } return output.toString(); }
@Override public ValidationResult validate(BlockHeader header) { if (!FastByteComparisons.equal(header.getHash(), blockHash)) { return fault("Block " + header.getNumber() + " hash constraint violated. Expected:" + Hex.toHexString(blockHash) + ", got: " + Hex.toHexString(header.getHash())); } return Success; } }
private Block loadGenesisFromFile(String resPath) { Block genesis = GenesisLoader.loadGenesis(getClass().getResourceAsStream(resPath)); logger.info(genesis.toString()); logger.info("genesis hash: [{}]", Hex.toHexString(genesis.getHash())); logger.info("genesis rlp: [{}]", Hex.toHexString(genesis.getEncoded())); return genesis; }
@Override public String toString() { return "NodeHandler[state: " + state + ", node: " + node.getHost() + ":" + node.getPort() + ", id=" + (node.getId().length >= 4 ? Hex.toHexString(node.getId(), 0, 4) : "empty") + "]"; }
@Test public void testRIPEMD160_Multiple() { String expected1 = "9295fac879006ff44812e43b83b515a06c2950aa"; String result1 = Hex.toHexString(HashUtil.ripemd160("test1".getBytes())); assertEquals(expected1, result1); String expected2 = "80b85ebf641abccdd26e327c5782353137a0a0af"; String result2 = Hex.toHexString(HashUtil.ripemd160("test2".getBytes())); assertEquals(expected2, result2); }
@Test /* real block hash calc */ public void test8() { String blockRaw = "F885F8818080A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347940000000000000000000000000000000000000000A0BCDDD284BF396739C224DBA0411566C891C32115FEB998A3E2B4E61F3F35582AA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D4934783800000808080C0C0"; byte[] blockHashB = HashUtil.sha3(Hex.decode(blockRaw)); String blockHash = Hex.toHexString(blockHashB); System.out.println(blockHash); }
@Test public void testSha256_Test() { String expected2 = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"; String result2 = Hex.toHexString(HashUtil.sha256("test".getBytes())); assertEquals(expected2, result2); }
@Test public void testSha3_Test() { String expected2 = "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658"; String result2 = Hex.toHexString(HashUtil.sha3("test".getBytes())); assertEquals(expected2, result2); }
@Test public void test3() { BigInteger privKey = new BigInteger("cd244b3015703ddf545595da06ada5516628c5feadbf49dc66049c4b370cc5d8", 16); byte[] addr = ECKey.fromPrivate(privKey).getAddress(); assertEquals("89b44e4d3c81ede05d0f5de8d1a68f754d73d997", Hex.toHexString(addr)); }
@Test public void test3() { logger.info("\n{}", funcJson3); CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson3); Assert.assertEquals("a4f72f5a" + "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb2e" + "00000000000000000000000000000000000000000000000000000000000004d2" + "000000000000000000000000000000000000000000000000000000000000007b61" + "000000000000000000000000000000000000000000000000000000000000007468" + "6520737472696e6700000000000000000000000000000000000000000000", Hex.toHexString(function.encode(-1234, 1234, 123, "a", "the string"))); }
/** * Test genesis with short nonce * + alloc addresses with 0x */ @Test public void testGenesisShortNonce() { Block genesis = loadGenesisFromFile("/genesis/nonce-messy2.json"); assertEquals(MESSY_NONCE_GENESIS_HASH, Hex.toHexString(genesis.getHash())); assertEquals(MESSY_NONCE_GENESIS_RLP, Hex.toHexString(genesis.getEncoded())); }
@Test public void testSha3_EmptyString() { String expected1 = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; String result1 = Hex.toHexString(HashUtil.sha3(new byte[0])); assertEquals(expected1, result1); }
@Test public void testECKey() { ECKey key = new ECKey(); assertTrue(key.isPubKeyCanonical()); assertNotNull(key.getPubKey()); assertNotNull(key.getPrivKeyBytes()); log.debug(Hex.toHexString(key.getPrivKeyBytes()) + " :Generated privkey"); log.debug(Hex.toHexString(key.getPubKey()) + " :Generated pubkey"); }
private void assertInvalidNodeId(String nodeId) { String hexEncodedNodeId = Hex.toHexString(nodeId.getBytes()); ActivePeer nodeWithInvalidNodeId = ActivePeer.asNodeWithId("node-1", "1.1.1.1", hexEncodedNodeId); Config config = createActivePeersConfig(nodeWithInvalidNodeId); SystemProperties props = new SystemProperties(); try { props.overrideParams(config); fail("Should've thrown exception for invalid node id"); } catch (RuntimeException ignore) { } }
@Test // rlp decode public void test_2() { LogInfo log = new LogInfo(Hex.decode("d5ccd26ba09ce1d85148b5081fa3ed77949417be"), null, null); assertEquals("d794d5ccd26ba09ce1d85148b5081fa3ed77949417bec080", Hex.toHexString(log.getEncoded())); logger.info("{}", log); }
private void assertInvalidPrivateKey(byte[] privateKey) { String hexEncodedPrivateKey = Hex.toHexString(privateKey); SystemProperties props = new SystemProperties(); try { props.overrideParams("peer.privateKey", hexEncodedPrivateKey); props.privateKey(); fail("Should've thrown exception for invalid private key"); } catch (RuntimeException ignore) { } }
@Test public void testSingleItem() { StringTrie trie = new StringTrie(mockDb); trie.put("A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); assertEquals("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab", Hex.toHexString(trie.getRootHash())); }
@Ignore //TODO #POC9 @Test public void testGenesisAccounts() { Trie trie = generateGenesisState(); assertEquals(GENESIS_STATE_ROOT, Hex.toHexString(trie.getRootHash())); }