From b971cf020995b7e9ae7c6c3e9de042a3b2f17a63 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 9 May 2018 15:54:40 +0530 Subject: [PATCH] More changes to null safe conversion from bytes to string --- .../datasource/rocksdb/RocksDbDataSource.java | 17 ++++----- .../java/org/ethereum/db/prune/Pruner.java | 17 ++++----- .../org/ethereum/facade/EthereumImpl.java | 4 ++- .../org/ethereum/listener/EventListener.java | 4 +-- .../org/ethereum/manager/WorldManager.java | 3 +- .../org/ethereum/net/eth/handler/Eth62.java | 5 ++- .../org/ethereum/net/eth/handler/Eth63.java | 6 ++-- .../org/ethereum/net/rlpx/Handshaker.java | 4 ++- .../org/ethereum/net/rlpx/MessageCodec.java | 8 ++--- .../net/rlpx/discover/PacketDecoder.java | 5 +-- .../org/ethereum/net/server/PeerServer.java | 5 +-- .../java/org/ethereum/net/swarm/Hive.java | 36 +++++++++---------- .../samples/CreateContractSample.java | 4 ++- .../ethereum/samples/EventListenerSample.java | 5 +-- .../ethereum/samples/PendingStateSample.java | 7 ++-- .../org/ethereum/samples/TransactionBomb.java | 3 +- .../org/ethereum/solidity/SolidityType.java | 5 +-- .../org/ethereum/sync/BlockDownloader.java | 4 +-- .../org/ethereum/sync/FastSyncManager.java | 9 ++--- .../org/ethereum/sync/HeadersDownloader.java | 5 +-- .../java/org/ethereum/sync/SyncManager.java | 6 ++-- .../main/java/org/ethereum/trie/TrieImpl.java | 5 +-- 22 files changed, 92 insertions(+), 75 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/datasource/rocksdb/RocksDbDataSource.java b/ethereumj-core/src/main/java/org/ethereum/datasource/rocksdb/RocksDbDataSource.java index a979d00da8..b1b7dff799 100644 --- a/ethereumj-core/src/main/java/org/ethereum/datasource/rocksdb/RocksDbDataSource.java +++ b/ethereumj-core/src/main/java/org/ethereum/datasource/rocksdb/RocksDbDataSource.java @@ -40,6 +40,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import static java.lang.System.arraycopy; +import static org.ethereum.util.ByteUtil.toHexString; /** * @author Mikhail Kalinin @@ -288,13 +289,13 @@ public void updateBatch(Map rows) { public void put(byte[] key, byte[] val) { resetDbLock.readLock().lock(); try { - if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.put(): " + name + ", key: " + Hex.toHexString(key) + ", " + (val == null ? "null" : val.length)); + if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.put(): " + name + ", key: " + toHexString(key) + ", " + (val == null ? "null" : val.length)); if (val != null) { db.put(key, val); } else { db.delete(key); } - if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.put(): " + name + ", key: " + Hex.toHexString(key) + ", " + (val == null ? "null" : val.length)); + if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.put(): " + name + ", key: " + toHexString(key) + ", " + (val == null ? "null" : val.length)); } catch (RocksDBException e) { logger.error("Failed to put into db '{}'", name, e); hintOnTooManyOpenFiles(e); @@ -308,9 +309,9 @@ public void put(byte[] key, byte[] val) { public byte[] get(byte[] key) { resetDbLock.readLock().lock(); try { - if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.get(): " + name + ", key: " + Hex.toHexString(key)); + if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.get(): " + name + ", key: " + toHexString(key)); byte[] ret = db.get(readOpts, key); - if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.get(): " + name + ", key: " + Hex.toHexString(key) + ", " + (ret == null ? "null" : ret.length)); + if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.get(): " + name + ", key: " + toHexString(key) + ", " + (ret == null ? "null" : ret.length)); return ret; } catch (RocksDBException e) { logger.error("Failed to get from db '{}'", name, e); @@ -325,9 +326,9 @@ public byte[] get(byte[] key) { public void delete(byte[] key) { resetDbLock.readLock().lock(); try { - if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.delete(): " + name + ", key: " + Hex.toHexString(key)); + if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.delete(): " + name + ", key: " + toHexString(key)); db.delete(key); - if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.delete(): " + name + ", key: " + Hex.toHexString(key)); + if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.delete(): " + name + ", key: " + toHexString(key)); } catch (RocksDBException e) { logger.error("Failed to delete from db '{}'", name, e); throw new RuntimeException(e); @@ -345,7 +346,7 @@ public byte[] prefixLookup(byte[] key, int prefixBytes) { resetDbLock.readLock().lock(); try { - if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.prefixLookup(): " + name + ", key: " + Hex.toHexString(key)); + if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.prefixLookup(): " + name + ", key: " + toHexString(key)); // RocksDB sets initial position of iterator to the first key which is greater or equal to the seek key // since keys in RocksDB are ordered in asc order iterator must be initiated with the lowest key @@ -366,7 +367,7 @@ public byte[] prefixLookup(byte[] key, int prefixBytes) { throw new RuntimeException(e); } - if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.prefixLookup(): " + name + ", key: " + Hex.toHexString(key) + ", " + (ret == null ? "null" : ret.length)); + if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.prefixLookup(): " + name + ", key: " + toHexString(key) + ", " + (ret == null ? "null" : ret.length)); return ret; diff --git a/ethereumj-core/src/main/java/org/ethereum/db/prune/Pruner.java b/ethereumj-core/src/main/java/org/ethereum/db/prune/Pruner.java index bd9d994249..3b26171d7b 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/prune/Pruner.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/prune/Pruner.java @@ -8,7 +8,6 @@ import org.ethereum.util.ByteArraySet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.spongycastle.util.encoders.Hex; import java.util.Arrays; import java.util.Collection; @@ -16,6 +15,8 @@ import java.util.Set; import java.util.stream.Collectors; +import static org.ethereum.util.ByteUtil.toHexString; + /** * This class is responsible for state pruning. * @@ -91,7 +92,7 @@ public boolean init(List forkWindow, int sizeInBlocks) { if (ready) return true; if (!forkWindow.isEmpty() && journal.get(forkWindow.get(0)) == null) { - logger.debug("pruner init aborted: can't fetch update " + Hex.toHexString(forkWindow.get(0))); + logger.debug("pruner init aborted: can't fetch update " + toHexString(forkWindow.get(0))); return false; } @@ -99,7 +100,7 @@ public boolean init(List forkWindow, int sizeInBlocks) { for (byte[] hash : forkWindow) { JournalSource.Update update = journal.get(hash); if (update == null) { - logger.debug("pruner init aborted: can't fetch update " + Hex.toHexString(hash)); + logger.debug("pruner init aborted: can't fetch update " + toHexString(hash)); return false; } update.getInsertedKeys().forEach(filter::insert); @@ -129,7 +130,7 @@ public void withSecondStep(List mainChainWindow, int sizeInBlocks) { update.getInsertedKeys().forEach(filter::insert); } logger.debug("distant filter initialized with set of " + (i < 0 ? mainChainWindow.size() : mainChainWindow.size() - i) + - " hashes, last hash " + Hex.toHexString(mainChainWindow.get(i < 0 ? 0 : i))); + " hashes, last hash " + toHexString(mainChainWindow.get(i < 0 ? 0 : i))); } else { logger.debug("distant filter initialized with empty set"); } @@ -216,7 +217,7 @@ public void prune(Segment segment) { public void persist(byte[] hash) { if (!ready || !withSecondStep()) return; - logger.trace("persist [{}]", Hex.toHexString(hash)); + logger.trace("persist [{}]", toHexString(hash)); long t = System.currentTimeMillis(); JournalSource.Update update = journal.get(hash); @@ -276,7 +277,7 @@ private int postpone(Chain chain) { for (byte[] hash : chain.getHashes()) { JournalSource.Update update = journal.get(hash); if (update == null) { - logger.debug("postponing: can't fetch update " + Hex.toHexString(hash)); + logger.debug("postponing: can't fetch update " + toHexString(hash)); continue; } // feed distant filter @@ -298,7 +299,7 @@ private int persist(Chain chain) { for (byte[] hash : chain.getHashes()) { JournalSource.Update update = journal.get(hash); if (update == null) { - logger.debug("pruning aborted: can't fetch update of main chain " + Hex.toHexString(hash)); + logger.debug("pruning aborted: can't fetch update of main chain " + toHexString(hash)); return 0; } // persist deleted keys @@ -339,7 +340,7 @@ private void revert(Chain chain) { for (byte[] hash : chain.getHashes()) { JournalSource.Update update = journal.get(hash); if (update == null) { - logger.debug("reverting chain " + chain + " aborted: can't fetch update " + Hex.toHexString(hash)); + logger.debug("reverting chain " + chain + " aborted: can't fetch update " + toHexString(hash)); return; } // clean up filter diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java index f6f5d11c47..71851d9c74 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -60,6 +60,8 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import static org.ethereum.util.ByteUtil.toHexString; + /** * @author Roman Mandeleil * @since 27.07.2014 @@ -113,7 +115,7 @@ public EthereumImpl(final SystemProperties config, final CompositeEthereumListen this.config = config; System.out.println(); this.compositeEthereumListener.addListener(gasPriceTracker); - gLogger.info("EthereumJ node started: enode://" + Hex.toHexString(config.nodeId()) + "@" + config.externalIp() + ":" + config.listenPort()); + gLogger.info("EthereumJ node started: enode://" + toHexString(config.nodeId()) + "@" + config.externalIp() + ":" + config.listenPort()); } @Override diff --git a/ethereumj-core/src/main/java/org/ethereum/listener/EventListener.java b/ethereumj-core/src/main/java/org/ethereum/listener/EventListener.java index 0248f79124..f00fe5e3b5 100644 --- a/ethereumj-core/src/main/java/org/ethereum/listener/EventListener.java +++ b/ethereumj-core/src/main/java/org/ethereum/listener/EventListener.java @@ -30,7 +30,6 @@ import org.ethereum.vm.LogInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.spongycastle.util.encoders.Hex; import java.math.BigInteger; import java.util.ArrayList; @@ -40,6 +39,7 @@ import java.util.concurrent.Executors; import static org.ethereum.sync.BlockDownloader.MAX_IN_REQUEST; +import static org.ethereum.util.ByteUtil.toHexString; /** * The base class for tracking events generated by a Solidity contract @@ -183,7 +183,7 @@ public void onBlockImpl(BlockSummary blockSummary) { public void onPendingTransactionUpdateImpl(TransactionReceipt txReceipt, PendingTransactionState state, Block block) { try { if (state != PendingTransactionState.DROPPED || pendings.containsKey(txReceipt.getTransaction().getHash())) { - logger.debug("onPendingTransactionUpdate: " + Hex.toHexString(txReceipt.getTransaction().getHash()) + ", " + state); + logger.debug("onPendingTransactionUpdate: " + toHexString(txReceipt.getTransaction().getHash()) + ", " + state); } onReceipt(txReceipt, block, state); } catch (Exception e) { diff --git a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java index b4a5da5eb9..33a4599380 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java @@ -47,6 +47,7 @@ import java.util.HashMap; import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH; +import static org.ethereum.util.ByteUtil.toHexString; /** * WorldManager is a singleton containing references to different parts of the system. @@ -221,7 +222,7 @@ public void loadBlockchain() { logger.info("*** Loaded up to block [{}] totalDifficulty [{}] with stateRoot [{}]", blockchain.getBestBlock().getNumber(), blockchain.getTotalDifficulty().toString(), - Hex.toHexString(blockchain.getBestBlock().getStateRoot())); + toHexString(blockchain.getBestBlock().getStateRoot())); } if (config.rootHashStart() != null) { diff --git a/ethereumj-core/src/main/java/org/ethereum/net/eth/handler/Eth62.java b/ethereumj-core/src/main/java/org/ethereum/net/eth/handler/Eth62.java index ad0b249985..a87959bc65 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/eth/handler/Eth62.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/eth/handler/Eth62.java @@ -39,7 +39,6 @@ import org.ethereum.validator.BlockHeaderValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.spongycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -56,7 +55,7 @@ import static org.ethereum.sync.PeerState.*; import static org.ethereum.sync.PeerState.BLOCK_RETRIEVING; import static org.ethereum.util.Utils.longToTimePeriod; -import static org.spongycastle.util.encoders.Hex.toHexString; +import static org.ethereum.util.ByteUtil.toHexString; /** * Eth 62 @@ -740,7 +739,7 @@ private List validateAndMerge(BlockBodiesMessage response) { if (bodies.hasNext()) { logger.info("Peer {}: invalid BLOCK_BODIES response: at least one block body doesn't correspond to any of requested headers: ", - channel.getPeerIdShort(), Hex.toHexString(bodies.next())); + channel.getPeerIdShort(), toHexString(bodies.next())); return null; } diff --git a/ethereumj-core/src/main/java/org/ethereum/net/eth/handler/Eth63.java b/ethereumj-core/src/main/java/org/ethereum/net/eth/handler/Eth63.java index 7f99bebc57..8d84cd0f0b 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/eth/handler/Eth63.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/eth/handler/Eth63.java @@ -36,7 +36,6 @@ import org.ethereum.sync.PeerState; import org.ethereum.util.ByteArraySet; import org.ethereum.util.Value; -import org.spongycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; @@ -47,6 +46,7 @@ import java.util.Set; import static org.ethereum.net.eth.EthVersion.V63; +import static org.ethereum.util.ByteUtil.toHexString; /** * Fast synchronization (PV63) Handler @@ -114,7 +114,7 @@ protected synchronized void processGetNodeData(GetNodeDataMessage msg) { Value value = new Value(rawNode); nodeValues.add(value); if (nodeValues.size() >= MAX_HASHES_TO_SEND) break; - logger.trace("Eth63: " + Hex.toHexString(nodeKey).substring(0, 8) + " -> " + value); + logger.trace("Eth63: " + toHexString(nodeKey).substring(0, 8) + " -> " + value); } } @@ -194,7 +194,7 @@ protected synchronized void processNodeData(NodeDataMessage msg) { for (Value nodeVal : msg.getDataList()) { byte[] hash = nodeVal.hash(); if (!requestedNodes.contains(hash)) { - String err = "Received NodeDataMessage contains non-requested node with hash :" + Hex.toHexString(hash) + " . Dropping peer " + channel; + String err = "Received NodeDataMessage contains non-requested node with hash :" + toHexString(hash) + " . Dropping peer " + channel; dropUselessPeer(err); return; } diff --git a/ethereumj-core/src/main/java/org/ethereum/net/rlpx/Handshaker.java b/ethereumj-core/src/main/java/org/ethereum/net/rlpx/Handshaker.java index 51ba9e71c5..da5b125880 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/rlpx/Handshaker.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/rlpx/Handshaker.java @@ -35,6 +35,8 @@ import java.net.URISyntaxException; import java.util.Arrays; +import static org.ethereum.util.ByteUtil.toHexString; + /** * Created by devrandom on 2015-04-09. */ @@ -54,7 +56,7 @@ public static void main(String[] args) throws IOException, URISyntaxException { public Handshaker() { myKey = new ECKey(); nodeId = myKey.getNodeId(); - System.out.println("Node ID " + Hex.toHexString(nodeId)); + System.out.println("Node ID " + toHexString(nodeId)); } /** diff --git a/ethereumj-core/src/main/java/org/ethereum/net/rlpx/MessageCodec.java b/ethereumj-core/src/main/java/org/ethereum/net/rlpx/MessageCodec.java index 038879f7e8..f4b08feffb 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/rlpx/MessageCodec.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/rlpx/MessageCodec.java @@ -36,7 +36,6 @@ import org.ethereum.net.swarm.bzz.BzzMessageCodes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.spongycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -47,6 +46,7 @@ import static java.lang.Math.min; import static org.ethereum.net.rlpx.FrameCodec.Frame; +import static org.ethereum.util.ByteUtil.toHexString; /** * The Netty codec which encodes/decodes RPLx frames to subprotocol Messages @@ -152,7 +152,7 @@ private Message decodeMessage(ChannelHandlerContext ctx, List frames) thr } if (loggerWire.isDebugEnabled()) - loggerWire.debug("Recv: Encoded: {} [{}]", frameType, Hex.toHexString(payload)); + loggerWire.debug("Recv: Encoded: {} [{}]", frameType, toHexString(payload)); Message msg; try { @@ -183,7 +183,7 @@ protected void encode(ChannelHandlerContext ctx, Message msg, List out) byte[] encoded = msg.getEncoded(); if (loggerWire.isDebugEnabled()) - loggerWire.debug("Send: Encoded: {} [{}]", getCode(msg.getCommand()), Hex.toHexString(encoded)); + loggerWire.debug("Send: Encoded: {} [{}]", getCode(msg.getCommand()), toHexString(encoded)); List frames = splitMessageToFrames(msg); @@ -271,7 +271,7 @@ private Message createMessage(byte code, byte[] payload) { return bzzMessageFactory.create(resolved, payload); } - throw new IllegalArgumentException("No such message: " + code + " [" + Hex.toHexString(payload) + "]"); + throw new IllegalArgumentException("No such message: " + code + " [" + toHexString(payload) + "]"); } public void setChannel(Channel channel){ diff --git a/ethereumj-core/src/main/java/org/ethereum/net/rlpx/discover/PacketDecoder.java b/ethereumj-core/src/main/java/org/ethereum/net/rlpx/discover/PacketDecoder.java index 0fc53d59ef..e0da22cc2e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/rlpx/discover/PacketDecoder.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/rlpx/discover/PacketDecoder.java @@ -25,10 +25,11 @@ import org.ethereum.crypto.ECKey; import org.ethereum.net.rlpx.Message; import org.slf4j.LoggerFactory; -import org.spongycastle.util.encoders.Hex; import java.util.List; +import static org.ethereum.util.ByteUtil.toHexString; + public class PacketDecoder extends MessageToMessageDecoder { private static final org.slf4j.Logger logger = LoggerFactory.getLogger("discover"); @@ -42,7 +43,7 @@ public void decode(ChannelHandlerContext ctx, DatagramPacket packet, Listhttp://netty.io. @@ -96,7 +97,7 @@ public void start(int port) { // Start the client. logger.info("Listening for incoming connections, port: [{}] ", port); - logger.info("NodeId: [{}] ", Hex.toHexString(config.nodeId())); + logger.info("NodeId: [{}] ", toHexString(config.nodeId())); channelFuture = b.bind(port).sync(); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/swarm/Hive.java b/ethereumj-core/src/main/java/org/ethereum/net/swarm/Hive.java index 2d1a1ba02c..609a3c3e7f 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/swarm/Hive.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/swarm/Hive.java @@ -1,20 +1,20 @@ -/* - * Copyright (c) [2016] [ ] - * This file is part of the ethereumJ library. - * - * The ethereumJ library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * The ethereumJ library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with the ethereumJ library. If not, see . - */ +/* + * Copyright (c) [2016] [ ] + * This file is part of the ethereumJ library. + * + * The ethereumJ library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The ethereumJ library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the ethereumJ library. If not, see . + */ package org.ethereum.net.swarm; import org.ethereum.net.rlpx.Node; @@ -90,7 +90,7 @@ public Collection getPeers(Key key, int maxCount) { ArrayList ret = new ArrayList<>(); for (Node node : closestNodes) { // TODO connect to Node -// ret.add(thisPeer.getPeer(new PeerAddress(node))); + // ret.add(thisPeer.getPeer(new PeerAddress(node))); BzzProtocol peer = connectedPeers.get(node); if (peer != null) { ret.add(peer); diff --git a/ethereumj-core/src/main/java/org/ethereum/samples/CreateContractSample.java b/ethereumj-core/src/main/java/org/ethereum/samples/CreateContractSample.java index 5e51e6bab0..947461e5fa 100644 --- a/ethereumj-core/src/main/java/org/ethereum/samples/CreateContractSample.java +++ b/ethereumj-core/src/main/java/org/ethereum/samples/CreateContractSample.java @@ -36,6 +36,8 @@ import java.math.BigInteger; import java.util.*; +import static org.ethereum.util.ByteUtil.toHexString; + /** * Created by Anton Nashatyrev on 03.03.2016. */ @@ -92,7 +94,7 @@ public void onBlock(Block block, List receipts) { } byte[] contractAddress = receipt.getTransaction().getContractAddress(); - logger.info("Contract created: " + Hex.toHexString(contractAddress)); + logger.info("Contract created: " + toHexString(contractAddress)); logger.info("Calling the contract function 'inc'"); CallTransaction.Contract contract = new CallTransaction.Contract(metadata.abi); diff --git a/ethereumj-core/src/main/java/org/ethereum/samples/EventListenerSample.java b/ethereumj-core/src/main/java/org/ethereum/samples/EventListenerSample.java index 8ba0e397c4..e31f584d31 100644 --- a/ethereumj-core/src/main/java/org/ethereum/samples/EventListenerSample.java +++ b/ethereumj-core/src/main/java/org/ethereum/samples/EventListenerSample.java @@ -54,6 +54,7 @@ import java.util.Scanner; import static org.ethereum.crypto.HashUtil.sha3; +import static org.ethereum.util.ByteUtil.toHexString; /** * Sample usage of events listener API. @@ -271,7 +272,7 @@ private void waitForEther(byte[] address, BigInteger requiredBalance) throws Int while(true) { BigInteger balance = ethereum.getRepository().getBalance(address); if (balance.compareTo(requiredBalance) > 0) { - logger.info("Address {} successfully funded. Balance: {} wei", "0x" + Hex.toHexString(address), balance); + logger.info("Address {} successfully funded. Balance: {} wei", "0x" + toHexString(address), balance); break; } synchronized (this) { @@ -305,7 +306,7 @@ public void onBlock(Block block, List receipts) { } byte[] address = receipt.getTransaction().getContractAddress(); - logger.info("Contract created: " + Hex.toHexString(address)); + logger.info("Contract created: " + toHexString(address)); IncEventListener eventListener = new IncEventListener(pendingState, metadata.abi, address); ethereum.addListener(eventListener.listener); diff --git a/ethereumj-core/src/main/java/org/ethereum/samples/PendingStateSample.java b/ethereumj-core/src/main/java/org/ethereum/samples/PendingStateSample.java index 16a40095d2..7ff7f6b3b2 100644 --- a/ethereumj-core/src/main/java/org/ethereum/samples/PendingStateSample.java +++ b/ethereumj-core/src/main/java/org/ethereum/samples/PendingStateSample.java @@ -26,13 +26,14 @@ import org.ethereum.facade.EthereumFactory; import org.ethereum.listener.EthereumListenerAdapter; import org.ethereum.util.ByteUtil; -import org.spongycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import java.math.BigInteger; import java.util.*; +import static org.ethereum.util.ByteUtil.toHexString; + /** * PendingState is the ability to track the changes made by transaction immediately and not wait for * the block containing that transaction. @@ -137,7 +138,7 @@ void onPendingTransactionReceived(Transaction tx) { if (Arrays.equals(tx.getSender(), senderAddress)) { BigInteger receiverBalance = ethereum.getRepository().getBalance(receiverAddress); BigInteger receiverBalancePending = pendingState.getRepository().getBalance(receiverAddress); - logger.info(" + New pending transaction 0x" + Hex.toHexString(tx.getHash()).substring(0, 8)); + logger.info(" + New pending transaction 0x" + toHexString(tx.getHash()).substring(0, 8)); pendingTxs.put(new ByteArrayWrapper(tx.getHash()), tx); @@ -156,7 +157,7 @@ public void onBlock(Block block, List receipts) { ByteArrayWrapper txHash = new ByteArrayWrapper(tx.getHash()); Transaction ptx = pendingTxs.get(txHash); if (ptx != null) { - logger.info(" - Pending transaction cleared 0x" + Hex.toHexString(tx.getHash()).substring(0, 8) + + logger.info(" - Pending transaction cleared 0x" + toHexString(tx.getHash()).substring(0, 8) + " in block " + block.getShortDescr()); pendingTxs.remove(txHash); diff --git a/ethereumj-core/src/main/java/org/ethereum/samples/TransactionBomb.java b/ethereumj-core/src/main/java/org/ethereum/samples/TransactionBomb.java index 6ee85459da..3a464a5ee2 100644 --- a/ethereumj-core/src/main/java/org/ethereum/samples/TransactionBomb.java +++ b/ethereumj-core/src/main/java/org/ethereum/samples/TransactionBomb.java @@ -30,6 +30,7 @@ import static org.ethereum.crypto.HashUtil.sha3; import static org.ethereum.util.ByteUtil.longToBytesNoLeadZeroes; +import static org.ethereum.util.ByteUtil.toHexString; public class TransactionBomb extends EthereumListenerAdapter { @@ -93,7 +94,7 @@ private void sendTx(long nonce){ tx.sign(privKey); ethereum.getChannelManager().sendTransaction(Collections.singletonList(tx), null); - System.err.println("Sending tx: " + Hex.toHexString(tx.getHash())); + System.err.println("Sending tx: " + toHexString(tx.getHash())); } private void sleep(int millis){ diff --git a/ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java b/ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java index 87f3a9e914..5393aa80a8 100644 --- a/ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java +++ b/ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java @@ -21,7 +21,6 @@ import com.fasterxml.jackson.annotation.JsonValue; import org.ethereum.util.ByteUtil; import org.ethereum.vm.DataWord; -import org.spongycastle.util.encoders.Hex; import java.lang.reflect.Array; import java.math.BigInteger; @@ -30,6 +29,8 @@ import java.util.Arrays; import java.util.List; +import static org.ethereum.util.ByteUtil.toHexString; + public abstract class SolidityType { protected String name; @@ -343,7 +344,7 @@ public byte[] encode(Object value) { byte[] addr = super.encode(value); for (int i = 0; i < 12; i++) { if (addr[i] != 0) { - throw new RuntimeException("Invalid address (should be 20 bytes length): " + Hex.toHexString(addr)); + throw new RuntimeException("Invalid address (should be 20 bytes length): " + toHexString(addr)); } } return addr; diff --git a/ethereumj-core/src/main/java/org/ethereum/sync/BlockDownloader.java b/ethereumj-core/src/main/java/org/ethereum/sync/BlockDownloader.java index 3194addefe..31b897780d 100644 --- a/ethereumj-core/src/main/java/org/ethereum/sync/BlockDownloader.java +++ b/ethereumj-core/src/main/java/org/ethereum/sync/BlockDownloader.java @@ -26,7 +26,6 @@ import org.ethereum.validator.BlockHeaderValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.spongycastle.util.encoders.Hex; import java.util.*; import java.util.concurrent.*; @@ -34,6 +33,7 @@ import static java.lang.Math.max; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; +import static org.ethereum.util.ByteUtil.toHexString; /** * Created by Anton Nashatyrev on 27.10.2016. @@ -366,7 +366,7 @@ private boolean validateAndAddHeaders(List headers, byte[] nodeId) if (!isValid(header)) { if (logger.isDebugEnabled()) { - logger.debug("{}: Invalid header RLP: {}", Hex.toHexString(header.getEncoded()), name); + logger.debug("{}: Invalid header RLP: {}", toHexString(header.getEncoded()), name); } return false; diff --git a/ethereumj-core/src/main/java/org/ethereum/sync/FastSyncManager.java b/ethereumj-core/src/main/java/org/ethereum/sync/FastSyncManager.java index c2862ac622..0d6e147a2e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/sync/FastSyncManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/sync/FastSyncManager.java @@ -59,6 +59,7 @@ import static org.ethereum.listener.EthereumListener.SyncState.UNSECURE; import static org.ethereum.trie.TrieKey.fromPacked; import static org.ethereum.util.CompactEncoder.hasTerminator; +import static org.ethereum.util.ByteUtil.toHexString; /** * Created by Anton Nashatyrev on 24.10.2016. @@ -413,7 +414,7 @@ public void onSuccess(List> result) { TrieNodeRequest request = pendingNodes.get(pair.getKey()); if (request == null) { long t = System.currentTimeMillis(); - logger.debug("Received node which was not requested: " + Hex.toHexString(pair.getKey()) + " from " + idle); + logger.debug("Received node which was not requested: " + toHexString(pair.getKey()) + " from " + idle); return; } Set intersection = request.requestIdsSnapshot(); @@ -591,7 +592,7 @@ private void syncSecure() { headersDownloader.waitForStop(); if (!FastByteComparisons.equal(headersDownloader.getGenesisHash(), config.getGenesis().getHash())) { logger.error("FASTSYNC FATAL ERROR: after downloading header chain starting from the pivot block (" + - pivot.getShortDescr() + ") obtained genesis block doesn't match ours: " + Hex.toHexString(headersDownloader.getGenesisHash())); + pivot.getShortDescr() + ") obtained genesis block doesn't match ours: " + toHexString(headersDownloader.getGenesisHash())); logger.error("Can't recover and exiting now. You need to restart from scratch (all DBs will be reset)"); System.exit(-666); } @@ -737,7 +738,7 @@ private BlockHeader getPivotBlock() throws InterruptedException { long s = start; if (pivotBlockHash != null) { - logger.info("FastSync: fetching trusted pivot block with hash " + Hex.toHexString(pivotBlockHash)); + logger.info("FastSync: fetching trusted pivot block with hash " + toHexString(pivotBlockHash)); } else { logger.info("FastSync: looking for best block number..."); BlockIdentifier bestKnownBlock; @@ -827,7 +828,7 @@ private BlockHeader getPivotHeaderByHash(byte[] pivotBlockHash) throws Exception return ret; } logger.warn("Peer " + bestIdle + " returned pivot block with another hash: " + - Hex.toHexString(ret.getHash()) + " Dropping the peer."); + toHexString(ret.getHash()) + " Dropping the peer."); bestIdle.disconnect(ReasonCode.USELESS_PEER); } else { logger.warn("Peer " + bestIdle + " doesn't returned correct pivot block. Dropping the peer."); diff --git a/ethereumj-core/src/main/java/org/ethereum/sync/HeadersDownloader.java b/ethereumj-core/src/main/java/org/ethereum/sync/HeadersDownloader.java index 7371c6a01b..a723089856 100644 --- a/ethereumj-core/src/main/java/org/ethereum/sync/HeadersDownloader.java +++ b/ethereumj-core/src/main/java/org/ethereum/sync/HeadersDownloader.java @@ -28,13 +28,14 @@ import org.ethereum.validator.BlockHeaderValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.spongycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import java.util.List; +import static org.ethereum.util.ByteUtil.toHexString; + /** * Created by Anton Nashatyrev on 27.10.2016. */ @@ -74,7 +75,7 @@ public HeadersDownloader(BlockHeaderValidator headerValidator) { } public void init(byte[] startFromBlockHash) { - logger.info("HeaderDownloader init: startHash = " + Hex.toHexString(startFromBlockHash)); + logger.info("HeaderDownloader init: startHash = " + toHexString(startFromBlockHash)); SyncQueueReverseImpl syncQueue = new SyncQueueReverseImpl(startFromBlockHash, true); super.init(syncQueue, syncPool, "HeadersDownloader"); syncPool.init(channelManager, blockchain); diff --git a/ethereumj-core/src/main/java/org/ethereum/sync/SyncManager.java b/ethereumj-core/src/main/java/org/ethereum/sync/SyncManager.java index 5fa9f80647..fd330558d3 100644 --- a/ethereumj-core/src/main/java/org/ethereum/sync/SyncManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/sync/SyncManager.java @@ -29,7 +29,6 @@ import org.ethereum.validator.BlockHeaderValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.spongycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -48,6 +47,7 @@ import static java.util.Collections.singletonList; import static org.ethereum.core.ImportResult.*; import static org.ethereum.util.Utils.longToTimePeriod; +import static org.ethereum.util.ByteUtil.toHexString; /** * @author Mikhail Kalinin @@ -284,7 +284,7 @@ private void produceQueue() { wrapper.getBlock().getTransactionsList().size(), ts); if (syncDone && (importResult == IMPORTED_BEST || importResult == IMPORTED_NOT_BEST)) { - if (logger.isDebugEnabled()) logger.debug("Block dump: " + Hex.toHexString(wrapper.getBlock().getEncoded())); + if (logger.isDebugEnabled()) logger.debug("Block dump: " + toHexString(wrapper.getBlock().getEncoded())); // Propagate block to the net after successful import asynchronously if (wrapper.isNewBlock()) channelManager.onNewForeignBlock(wrapper); } @@ -301,7 +301,7 @@ private void produceQueue() { } catch (Throwable e) { if (wrapper != null) { logger.error("Error processing block {}: ", wrapper.getBlock().getShortDescr(), e); - logger.error("Block dump: {}", Hex.toHexString(wrapper.getBlock().getEncoded())); + logger.error("Block dump: {}", toHexString(wrapper.getBlock().getEncoded())); } else { logger.error("Error processing unknown block", e); } diff --git a/ethereumj-core/src/main/java/org/ethereum/trie/TrieImpl.java b/ethereumj-core/src/main/java/org/ethereum/trie/TrieImpl.java index 8720622073..72f27da706 100644 --- a/ethereumj-core/src/main/java/org/ethereum/trie/TrieImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/trie/TrieImpl.java @@ -41,6 +41,7 @@ import static org.ethereum.util.RLP.EMPTY_ELEMENT_RLP; import static org.ethereum.util.RLP.encodeElement; import static org.ethereum.util.RLP.encodeList; +import static org.ethereum.util.ByteUtil.toHexString; /** * Created by Anton Nashatyrev on 07.02.2017. @@ -112,8 +113,8 @@ public boolean resolveCheck() { private void resolve() { if (!resolveCheck()) { - logger.error("Invalid Trie state, can't resolve hash " + Hex.toHexString(hash)); - throw new RuntimeException("Invalid Trie state, can't resolve hash " + Hex.toHexString(hash)); + logger.error("Invalid Trie state, can't resolve hash " + toHexString(hash)); + throw new RuntimeException("Invalid Trie state, can't resolve hash " + toHexString(hash)); } }