Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
More changes to null safe conversion from bytes to string
Browse files Browse the repository at this point in the history
  • Loading branch information
kishansagathiya committed May 9, 2018
1 parent 29c1849 commit b971cf0
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -288,13 +289,13 @@ public void updateBatch(Map<byte[], byte[]> 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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;

Expand Down
17 changes: 9 additions & 8 deletions ethereumj-core/src/main/java/org/ethereum/db/prune/Pruner.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
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;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.ethereum.util.ByteUtil.toHexString;

/**
* This class is responsible for state pruning.
*
Expand Down Expand Up @@ -91,15 +92,15 @@ public boolean init(List<byte[]> 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;
}

QuotientFilter filter = instantiateFilter(sizeInBlocks, FILTER_ENTRIES_FORK);
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);
Expand Down Expand Up @@ -129,7 +130,7 @@ public void withSecondStep(List<byte[]> 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");
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -740,7 +739,7 @@ private List<Block> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -152,7 +152,7 @@ private Message decodeMessage(ChannelHandlerContext ctx, List<Frame> frames) thr
}

if (loggerWire.isDebugEnabled())
loggerWire.debug("Recv: Encoded: {} [{}]", frameType, Hex.toHexString(payload));
loggerWire.debug("Recv: Encoded: {} [{}]", frameType, toHexString(payload));

Message msg;
try {
Expand Down Expand Up @@ -183,7 +183,7 @@ protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> 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<Frame> frames = splitMessageToFrames(msg);

Expand Down Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatagramPacket> {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger("discover");

Expand All @@ -42,7 +43,7 @@ public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object
DiscoveryEvent event = new DiscoveryEvent(msg, packet.sender());
out.add(event);
} catch (Exception e) {
throw new RuntimeException("Exception processing inbound message from " + ctx.channel().remoteAddress() + ": " + Hex.toHexString(encoded), e);
throw new RuntimeException("Exception processing inbound message from " + ctx.channel().remoteAddress() + ": " + toHexString(encoded), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
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.ApplicationContext;
import org.springframework.stereotype.Component;

import static org.ethereum.util.ByteUtil.toHexString;

/**
* This class establishes a listener for incoming connections.
* See <a href="http://netty.io">http://netty.io</a>.
Expand Down Expand Up @@ -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();

Expand Down
Loading

0 comments on commit b971cf0

Please sign in to comment.