Merge pull request #15 from nicksavers/master

Format timestamp for Block details
This commit is contained in:
romanman 2014-05-28 15:51:00 +03:00
commit 67a8bd8e78
5 changed files with 53 additions and 52 deletions

View File

@ -175,6 +175,7 @@ mvn clean package -Dmaven.test.skip=true
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>

View File

@ -6,6 +6,7 @@ import org.ethereum.util.RLP;
import org.ethereum.util.RLPElement;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import java.math.BigInteger;
import java.util.ArrayList;
@ -129,7 +130,7 @@ public class Block {
this.minGasPrice = gpBytes == null ? 0 : (new BigInteger(1, gpBytes)).longValue();
this.gasLimit = glBytes == null ? 0 : (new BigInteger(1, glBytes)).longValue();
this.gasUsed = guBytes == null ? 0 : (new BigInteger(1, guBytes)).longValue();
this.timestamp = tsBytes == null ? 0 : (new BigInteger(1, tsBytes)).longValue();
this.timestamp = tsBytes == null ? 0 : (new BigInteger(tsBytes)).longValue();
this.extraData = ((RLPItem) header.get(11)).getRLPData();
this.nonce = ((RLPItem) header.get(12)).getRLPData();
@ -270,7 +271,7 @@ public class Block {
toStringBuff.append(" minGasPrice=" + minGasPrice).append("\n");
toStringBuff.append(" gasLimit=" + gasLimit).append("\n");
toStringBuff.append(" gasUsed=" + gasUsed).append("\n");
toStringBuff.append(" timestamp=" + timestamp).append("\n");
toStringBuff.append(" timestamp=" + timestamp + " (" + Utils.longToDateTime(timestamp) + ")").append("\n");
toStringBuff.append(" extraData=" + ByteUtil.toHexString(extraData)).append("\n");
toStringBuff.append(" nonce=" + ByteUtil.toHexString(nonce)).append("\n");

View File

@ -27,9 +27,8 @@ public class ByteUtil {
* @return numBytes byte long array.
*/
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
if (b == null) {
return null;
}
if (b == null)
return null;
byte[] bytes = new byte[numBytes];
byte[] biBytes = b.toByteArray();
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
@ -85,7 +84,6 @@ public class ByteUtil {
return new BigInteger(1, b).intValue();
}
/**
* Calculate the number of bytes need
* to encode the number
@ -93,69 +91,60 @@ public class ByteUtil {
* @param val - number
* @return number of min bytes used to encode the number
*/
public static int numBytes(String val){
public static int numBytes(String val) {
BigInteger bInt = new BigInteger(val);
int bytes = 0;
while(!bInt.equals(BigInteger.ZERO)){
while(!bInt.equals(BigInteger.ZERO)) {
bInt = bInt.shiftRight(8);
++bytes;
}
if (bytes == 0) ++bytes;
return bytes;
}
/**
* @param arg - not more that 32 bits
* @return - byts of the value pad with complete to 32 zeroes
* @return - bytes of the value pad with complete to 32 zeroes
*/
public static byte[] encodeValFor32Bits(Object arg){
public static byte[] encodeValFor32Bits(Object arg) {
byte[] data;
byte[] data;
// check if the string is numeric
if (arg.toString().trim().matches("-?\\d+(\\.\\d+)?")){
// check if the string is numeric
if (arg.toString().trim().matches("-?\\d+(\\.\\d+)?")) {
data = new BigInteger(arg.toString().trim()).toByteArray();
} else {
data = arg.toString().trim().getBytes();
}
data = new BigInteger(arg.toString().trim()).toByteArray();
}else{
data = arg.toString().trim().getBytes();
}
if (data.length > 32)
throw new RuntimeException("values can't be more than 32 bits");
if (data.length > 32) throw new Error("values can't be more than 32 bits");
byte[] val = new byte[32];
byte[] val = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int j = 0;
for (int i = data.length; i > 0; --i) {
val[31 - j] = data[i - 1];
++j;
}
return val;
}
int j = 0;
for (int i = data.length; i > 0; --i){
val[31 - j] = data[i-1];
++j;
}
return val;
}
/**
* encode the values and concatenate together
*/
public static byte[] encodeDataList(Object... args){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (Object arg : args){
byte[] val = encodeValFor32Bits(arg);
try {
baos.write(val);
} catch (IOException e) {
throw new Error("Happen something that should never happen ", e);
}
}
return baos.toByteArray();
}
/**
* encode the values and concatenate together
*/
public static byte[] encodeDataList(Object... args) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (Object arg : args) {
byte[] val = encodeValFor32Bits(arg);
try {
baos.write(val);
} catch (IOException e) {
throw new Error("Happen something that should never happen ", e);
}
}
return baos.toByteArray();
}
}

View File

@ -3,6 +3,9 @@ package org.ethereum.util;
import java.math.BigInteger;
import java.net.URL;
import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
@ -26,6 +29,12 @@ public class Utils {
return (new BigInteger(1, numberBytes)).toString();
}
public static String longToDateTime(long timestamp) {
Date date = new Date(timestamp * 1000);
DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
return formatter.format(date);
}
public static ImageIcon getImageIcon(String resource){
URL imageURL = ClassLoader.getSystemResource(resource);
ImageIcon image = new ImageIcon(imageURL);

View File

@ -0,0 +1 @@
java -Dlog4j.configuration=file:./config/log4j.properties -jar ethereumj.jar