From 4b3ce71ba6d00058336bad39796a4a4acb964c73 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 14:03:38 +0200 Subject: [PATCH 01/11] update docker docs --- DOCKER.md | 88 ++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 47 deletions(-) diff --git a/DOCKER.md b/DOCKER.md index d2d20a1e..681c9e18 100644 --- a/DOCKER.md +++ b/DOCKER.md @@ -1,59 +1,53 @@ # Docker -The Dockerfile included in this repo builds a working SBX docker container whilst trying to stay the least opinionated as possible. This allows system administrators the option to deploy and configure SBX based on their own individual circumstances and needs. +To run a Helix docker container you have two options: -When building SBX via the Dockerfile provided, Docker 17.05 minimum is required, due to the use of Docker build stages. During docker build, these are the stages invoked: -- java: installs Oracle Java on top of Ubuntu -- build: installs Maven on top of the java stage and compiles SBX -- final container: copies the SBX jar file using the java stage as base +- Build your own image using the provided dockerfile +- Run container from provided dockerhub image +Both options require that you have [docker](https://www.docker.com/get-started) (>=17.05) installed on your machine. -The built container assumes the WORKDIR inside the container is /sbx/data: this means that the database directory will be written inside that directory by default. If a system administrator wants to retain the database across restarts, it is his/her job to mount a docker volume in the right folder. +The provided dockerfile only contains the bare minimum of configuration parameters, as to enable higher degree of customization in terms of configuration and deployment to the node operator. -The docker conatiner supports the env variables to configure advanced options. These variables can be set but are not required to run SBX. +Build stages: +1. java: installs Oracle Java on top of Ubuntu +2. build: installs Maven on top of the java stage and compiles Helix +3. final container: copies the helix jar file using the java stage as base -`JAVA_OPTIONS`: these are the java options to pass right after the java command. It must not contain -Xms nor -Xmx. Defaults to a safe value -`JAVA_MIN_MEMORY`: the value of -Xms option. Defaults to 2G -`JAVA_MAX_MEMORY`: the value of -Xmx option. Defaults to 4G -`DOCKER_SBX_JAR_PATH`: defaults to /sbx/target/sbx*.jar as pushed by the Dockerfile. This is useful if custom SBX binaries want to be executed and the default path needs to be overridden -`DOCKER_SBX_REMOTE_LIMIT_API`: defaults to "interruptAttachToTangle, attachToTangle, addNeighbors, removeNeighbors, getNeighbors" -`DOCKER_SBX_MONITORING_API_PORT_ENABLE`: defaults to 0. If set to 1, a socat on port 14266 directed to 127.0.0.1:DOCKER_SBX_MONITORING_API_PORT_DESTINATION will be open in order to allow all API calls regardless of the DOCKER_SBX_REMOTE_LIMIT_API setting. This is useful to give access to restricted API calls to local tools and still denying access to restricted API calls to the internet. It is highly recommended to use this option together with docker networks (docker run --net). +The built container assumes the WORKDIR inside the container is /helix/data: this means that the database directory will be written inside that directory by default. If a system administrator wants to retain the database across restarts, it is his/her job to mount a docker volume in the right folder -The container entry point is a shell script that performs few additional steps before launching SBX: -- verifies if `DOCKER_SBX_MONITORING_API_PORT_ENABLE` is set to 1 -- launches SBX with all parameters passed as desired +## Getting Started +This section will cover usage information for the provided docker container. -It is important to note that other than --remote and --remote-limit-api "$DOCKER_SBX_REMOTE_LIMIT_API", neither the entrypoint nor the Dockerfile are aware of any SBX configuration option. This is to not tie the Dockerfile and its container to a specific set of SBX options. Instead, this contain still allows the use of an INI file or command line options. Please refer to the SBX documentation to learn what are the allowed options at command line and via the INI file. +### Prerequisities +In order to run this container you'll need docker installed. -**At the time of writing, SBX requires -p to be passed either via INI or via command line. The entrypoint of this docker container does not do that for you.** +* [Windows](https://docs.docker.com/windows/started) +* [OS X](https://docs.docker.com/mac/started/) +* [Linux](https://docs.docker.com/linux/started/) -Here is a systemd unit example you can use with this Docker container. This is just an example and customisation is possible and recommended. In this example the docker network sbx must be created and the paths /mnt/sbx/conf and /mnt/sbx/data are used on the docker host to serve respectively the neighbors file and the data directory. No INI files are used in this example, instead options are passed via command line options, such as --testnet and --zmq-enabled. +### Example +```shell +sudo docker run helixnetwork/helix-1.0:latest -p 8085 ``` -[Unit] -Description=SBX -After=docker.service -Requires=docker.service - -[Service] -TimeoutStartSec=0 -Restart=always -ExecStartPre=-/usr/bin/docker rm %n -ExecStart=/usr/bin/docker run \ ---name %n \ ---hostname sbx \ ---net=sbx \ --v /mnt/sbx/conf:/sbx/conf \ --v /mnt/sbx/data:/sbx/data \ --p 14265:14265 \ --p 15600:15600 \ --p 14600:14600/udp \ -helixnetwork/sbx:v1.5.0 \ --p 14700 \ ---zmq-enabled \ ---testnet - -ExecStop=/usr/bin/docker stop %n -ExecReload=/usr/bin/docker restart %n - -[Install] -WantedBy=multi-user.target +This will run the helix with its API listening on port 8085, with no peers and a fresh database. +The helix docker container is configured to read data from /helix/data. Use the -v option of the docker run command to mount volumes so to have persistent data. +You can also pass more command line options to the docker run command and those will be passed to Helix. Please refer to the [README.md]() for all command line and ini options. + + +### Load options from INI +If you want to use a ``.ini file with the docker container, supposing it's stored under /path/to/conf/iri.ini on your docker host, then pass -v /path/to/conf:/iri/conf and add -c /iri/conf/iri.ini as docker run arguments. So for example the docker run command above would become: + +```shell +docker run -v /path/to/conf:/helix/conf -v /path/to/data:/helix/data helixnetwork/helix-1.0:latest -p 8085 -c /helix/conf/.ini ``` + +## Security + +Helix-1.0 should be run as a non-administrative user with no root privileges! +An unprivileged user can be created on the host and the UID passed to the docker command (e.g. --user 1001). Directories that are mounted to the container from the host should be owned by this user. +In addition the --cap-drop=ALL passed to docker restricts process capabilities and adheres to the principle of least privilege. See https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities for more information. + + + + + From f3385139c4fa6513fa37b22c4b173be85c8d8d97 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 14:03:56 +0200 Subject: [PATCH 02/11] update readme --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d34516d..0e478107 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,8 @@ This is the 1.0 implementation of the Helix Protocol based on [**IRI**](https:// ## Developers -- Please see the [CONTRIBUTING.md](https://github.com/HelixNetwork/helix-1.0/blob/dev/CONTRIBUTING.md) and [STYLEGUIDE.md](https://github.com/HelixNetwork/helix-1.0/blob/dev/STYLEGUIDE.md) if you wish to contribute to this repository! +- Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us. - Please read the [helix-1.0-specifications](https://github.com/HelixNetwork/helix-specs/blob/master/specs/helix-1.0.md) before contributing. -- Disable proof of work in the course of testing by passing `--pow-disabled` flag. ## Installing Make sure you have [**Maven**](https://maven.apache.org/) and [**Java 8**](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) installed on your computer. From 602cd0a366db650b99a074bf841b8d7b30be92e3 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 14:05:33 +0200 Subject: [PATCH 03/11] update dockerfile --- Dockerfile | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 210cc4a7..87a68b7c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,21 @@ FROM helixnetwork/base16.04:latest as builder MAINTAINER Dario Tietz -WORKDIR /testnet-1.0 -COPY . /testnet-1.0 +WORKDIR /helix-1.0 +COPY . /helix-1.0 RUN mvn clean package FROM openjdk:jre-slim -WORKDIR /testnet-1.0 +WORKDIR /helix-1.0 COPY --from=builder /testnet-1.0/target/helix*.jar helix*.jar -VOLUME /testnet-1.0 +VOLUME /helix-1.0 -EXPOSE 14700/udp -EXPOSE 14700 -EXPOSE 14600/udp -ENTRYPOINT ["/usr/bin/java", "-XX:+DisableAttachMechanism", "-Xmx8g", "-Xms256m", "-Dlogback.configurationFile=/testnet-1.0/conf/logback.xml", "-Djava.net.preferIPv4Stack=true", "-jar", "helix*.jar", "-p", "14700", "-u", "14600", "--remote", "$@"] +EXPOSE 8085 +EXPOSE 4100/udp +EXPOSE 5100/tcp +EXPOSE 5556/tcp + +WORKDIR /helix/data + +ENTRYPOINT ["/usr/bin/java", "-XX:+DisableAttachMechanism", "-Xmx8g", "-Xms256m", "-Dlogback.configurationFile=/helix-1.0/conf/logback.xml", "-Djava.net.preferIPv4Stack=true", "-jar", "helix*.jar", "-p", "8085", "$@"] From 78c25dd1e44b6cf9658b11daefc95be6d865a7d4 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 15:51:33 +0200 Subject: [PATCH 04/11] Refactor hexString()->toString() --- src/main/java/net/helix/hlx/model/AbstractHash.java | 9 +++++---- src/main/java/net/helix/hlx/model/Hash.java | 8 +------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/helix/hlx/model/AbstractHash.java b/src/main/java/net/helix/hlx/model/AbstractHash.java index eb05c9ff..3c0bb493 100644 --- a/src/main/java/net/helix/hlx/model/AbstractHash.java +++ b/src/main/java/net/helix/hlx/model/AbstractHash.java @@ -112,9 +112,11 @@ public byte[] bytes() { return safe.getData(); } - public String hexString() { - return Hex.toHexString(bytes()); - } + /** + * Convert to hex string + * @return string string in hex representation + */ + public String toString() { return Hex.toHexString(bytes()); } /** * Reading byte array. @see #fullRead(byte[]) @@ -164,5 +166,4 @@ public static long bytesToLong(byte[] array, int offset) { ((long)(array[offset+6] & 0xff) << 8) | ((long)(array[offset+7] & 0xff)); } - } diff --git a/src/main/java/net/helix/hlx/model/Hash.java b/src/main/java/net/helix/hlx/model/Hash.java index fe4e1610..7aadb7bb 100644 --- a/src/main/java/net/helix/hlx/model/Hash.java +++ b/src/main/java/net/helix/hlx/model/Hash.java @@ -8,7 +8,7 @@ /** * Hash is an implementation of the Serializable, Indexable and HashID interface. * The model class contains a hash Hash , the size of the hash, lock -* and the inner classes ByteSafe and TritSafe . +* and the inner classes ByteSafe */ public interface Hash extends Indexable, HashId { Hash NULL_HASH = HashFactory.TRANSACTION.create(new byte[Sha3.HASH_LENGTH]); @@ -24,12 +24,6 @@ public interface Hash extends Indexable, HashId { */ public byte[] bytes(); - /** - * The data of this hash as hexString - * @return the hexString - */ - public String hexString(); - /** * The amount of zeros this hash has on the end. * Defines the weightMagnitude for a transaction. From c23e57dadb62070225549966867fb301b54f34ae Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 16:08:02 +0200 Subject: [PATCH 05/11] Cleanup converter --- src/main/java/net/helix/hlx/Converter.java | 29 -- .../net/helix/hlx/model/AbstractHash.java | 16 +- .../java/net/helix/hlx/utils/Converter.java | 277 +----------------- 3 files changed, 6 insertions(+), 316 deletions(-) delete mode 100644 src/main/java/net/helix/hlx/Converter.java diff --git a/src/main/java/net/helix/hlx/Converter.java b/src/main/java/net/helix/hlx/Converter.java deleted file mode 100644 index e0217796..00000000 --- a/src/main/java/net/helix/hlx/Converter.java +++ /dev/null @@ -1,29 +0,0 @@ -package net.helix.hlx; - -import java.util.Random; - -public class Converter { - - public static void main(String[] args) { - - Random seed = new Random(); - } - - public static String[] bytesToBitStrings(byte[] bytes) { - String[] byteString = new String[bytes.length]; - for (int i = 0; i < bytes.length; i++) { - String bits = String.format("%8s", Integer.toBinaryString(bytes[i] & 0xFF)).replace(' ', '0'); - byteString[i] = bits; - } - return byteString; - } - - public static byte[] bitsToBytes(String[] bits){ - byte[] bytes = new byte[bits.length]; - for (int i = 0; i < bits.length; i++) { - byte b = (byte)Integer.parseInt(bits[i]); - bytes[i] = b; - } - return bytes; - } -} diff --git a/src/main/java/net/helix/hlx/model/AbstractHash.java b/src/main/java/net/helix/hlx/model/AbstractHash.java index 3c0bb493..fea38901 100644 --- a/src/main/java/net/helix/hlx/model/AbstractHash.java +++ b/src/main/java/net/helix/hlx/model/AbstractHash.java @@ -1,8 +1,10 @@ package net.helix.hlx.model; +import net.helix.hlx.utils.Converter; import net.helix.hlx.model.persistables.Transaction; import net.helix.hlx.model.safe.ByteSafe; import net.helix.hlx.storage.Indexable; + import org.bouncycastle.util.encoders.Hex; import java.io.Serializable; @@ -148,22 +150,10 @@ public int compareTo(Indexable indexable) { if (this.equals(hash)) { return 0; } - long diff = bytesToLong(hash.bytes(), 0) - bytesToLong(bytes(), 0); + long diff = Converter.bytesToLong(hash.bytes(), 0) - Converter.bytesToLong(bytes(), 0); if (Math.abs(diff) > Integer.MAX_VALUE) { return diff > 0L ? Integer.MAX_VALUE : Integer.MIN_VALUE + 1; } return (int) diff; } - - public static long bytesToLong(byte[] array, int offset) { - return - ((long)(array[offset] & 0xff) << 56) | - ((long)(array[offset+1] & 0xff) << 48) | - ((long)(array[offset+2] & 0xff) << 40) | - ((long)(array[offset+3] & 0xff) << 32) | - ((long)(array[offset+4] & 0xff) << 24) | - ((long)(array[offset+5] & 0xff) << 16) | - ((long)(array[offset+6] & 0xff) << 8) | - ((long)(array[offset+7] & 0xff)); - } } diff --git a/src/main/java/net/helix/hlx/utils/Converter.java b/src/main/java/net/helix/hlx/utils/Converter.java index 304c8bdf..34d01454 100644 --- a/src/main/java/net/helix/hlx/utils/Converter.java +++ b/src/main/java/net/helix/hlx/utils/Converter.java @@ -1,41 +1,8 @@ package net.helix.hlx.utils; -import java.util.Arrays; - public class Converter { - public static final int RADIX = 3; - public static final int BYTE_RADIX = 256; - public static final int MAX_TRIT_VALUE = (RADIX - 1) / 2, MIN_TRIT_VALUE = -MAX_TRIT_VALUE; - - public static final int NUMBER_OF_TRITS_IN_A_BYTE = 5; - public static final int NUMBER_OF_TRITS_IN_A_TRYTE = 3; - - static final int[][] BYTE_TO_TRITS_MAPPINGS = new int[243][]; - static final int[][] TRYTE_TO_TRITS_MAPPINGS = new int[27][]; - - public static final int HIGH_INTEGER_BITS = 0xFFFFFFFF; - public static final long HIGH_LONG_BITS = 0xFFFFFFFFFFFFFFFFL; - - public static final String TRYTE_ALPHABET = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - public static final int MIN_TRYTE_VALUE = -13, MAX_TRYTE_VALUE = 13; - - - static { - - final int[] trits = new int[NUMBER_OF_TRITS_IN_A_BYTE]; - - for (int i = 0; i < 243; i++) { - BYTE_TO_TRITS_MAPPINGS[i] = Arrays.copyOf(trits, NUMBER_OF_TRITS_IN_A_BYTE); - increment(trits, NUMBER_OF_TRITS_IN_A_BYTE); - } - - for (int i = 0; i < 27; i++) { - TRYTE_TO_TRITS_MAPPINGS[i] = Arrays.copyOf(trits, NUMBER_OF_TRITS_IN_A_TRYTE); - increment(trits, NUMBER_OF_TRITS_IN_A_TRYTE); - } - } + public static final int RADIX = 2; public static long bytesToLong(byte[] array, int offset) { return @@ -49,250 +16,12 @@ public static long bytesToLong(byte[] array, int offset) { ((long)(array[offset+7] & 0xff)); } - - public static long longValue(final int[] trits, final int srcPos, final int size) { + public static long longValue(final int[] bytes, final int srcPos, final int size) { long value = 0; for (int i = size; i-- > 0; ) { - value = value * RADIX + trits[srcPos + i]; + value = value * RADIX + bytes[srcPos + i]; } return value; } - - - public static byte[] allocateBytesForTrits(int tritCount) { - final int expectedLength = (tritCount + NUMBER_OF_TRITS_IN_A_BYTE - 1) / NUMBER_OF_TRITS_IN_A_BYTE; - return new byte[expectedLength]; - } - - public static int[] allocateTritsForTrytes(int tryteCount) { - return new int[tryteCount * NUMBER_OF_TRITS_IN_A_TRYTE]; - } - - public static void bytes(final int[] trits, final int srcPos, byte[] dest, int destPos, final int tritsLength) { - - final int expectedLength = (tritsLength + NUMBER_OF_TRITS_IN_A_BYTE - 1) / NUMBER_OF_TRITS_IN_A_BYTE; - - if((dest.length - destPos) < expectedLength) { - throw new IllegalArgumentException("Input array not large enough."); - } - - for (int i = 0; i < expectedLength; i++) { - int value = 0; - for (int j = (tritsLength - i * NUMBER_OF_TRITS_IN_A_BYTE) < 5 ? (tritsLength - i * NUMBER_OF_TRITS_IN_A_BYTE) : NUMBER_OF_TRITS_IN_A_BYTE; j-- > 0; ) { - value = value * RADIX + trits[srcPos + i * NUMBER_OF_TRITS_IN_A_BYTE + j]; - } - dest[destPos + i] = (byte)value; - } - } - - public static void bytes(final int[] trits, byte[] dest) { - bytes(trits, 0, dest, 0, trits.length); - } - - public static void getTrits(final byte[] bytes, final int[] trits) { - - int offset = 0; - for (int i = 0; i < bytes.length && offset < trits.length; i++) { - System.arraycopy(BYTE_TO_TRITS_MAPPINGS[bytes[i] < 0 ? (bytes[i] + BYTE_TO_TRITS_MAPPINGS.length) : bytes[i]], 0, trits, offset, trits.length - offset < NUMBER_OF_TRITS_IN_A_BYTE ? (trits.length - offset) : NUMBER_OF_TRITS_IN_A_BYTE); - offset += NUMBER_OF_TRITS_IN_A_BYTE; - } - while (offset < trits.length) { - trits[offset++] = 0; - } - } - - public static void trits(final String trytes, int[] dest, int destOffset) { - if((dest.length - destOffset) < trytes.length() * NUMBER_OF_TRITS_IN_A_TRYTE) { - throw new IllegalArgumentException("Destination array is not large enough."); - } - - for (int i = 0; i < trytes.length(); i++) { - System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, dest, - destOffset + i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE); - } - } - - public static void copyTrits(final long value, final int[] destination, final int offset, final int size) { - - long absoluteValue = value < 0 ? -value : value; - for (int i = 0; i < size; i++) { - - int remainder = (int)(absoluteValue % RADIX); - absoluteValue /= RADIX; - if (remainder > MAX_TRIT_VALUE) { - - remainder = MIN_TRIT_VALUE; - absoluteValue++; - } - destination[offset + i] = remainder; - } - - if (value < 0) { - for (int i = 0; i < size; i++) { - destination[offset + i] = -destination[offset + i]; - } - } - } - - - public static String trytes(final int[] trits, final int offset, final int size) { - - final StringBuilder trytes = new StringBuilder(); - for (int i = 0; i < (size + NUMBER_OF_TRITS_IN_A_TRYTE - 1) / NUMBER_OF_TRITS_IN_A_TRYTE; i++) { - int j = trits[offset + i * 3] + trits[offset + i * 3 + 1] * 3 + trits[offset + i * 3 + 2] * 9; - if (j < 0) { - j += TRYTE_ALPHABET.length(); - } - trytes.append(TRYTE_ALPHABET.charAt(j)); - } - return trytes.toString(); - } - - public static String trytes(final int[] trits) { - return trytes(trits, 0, trits.length); - } - - public static int tryteValue(final int[] trits, final int offset) { - return trits[offset] + trits[offset + 1] * 3 + trits[offset + 2] * 9; - } - - public static Pair intPair(int[] trits) { - int[] low = new int[trits.length]; - int[] hi = new int[trits.length]; - for(int i = 0; i< trits.length; i++) { - low[i] = trits[i] != 1 ? HIGH_INTEGER_BITS: 0; - hi[i] = trits[i] != -1 ? HIGH_INTEGER_BITS: 0; - } - return new Pair<>(low, hi); - } - - public static Pair longPair(int[] trits) { - long[] low = new long[trits.length]; - long[] hi = new long[trits.length]; - for(int i = 0; i< trits.length; i++) { - low[i] = trits[i] != 1 ? HIGH_LONG_BITS: 0; - hi[i] = trits[i] != -1 ? HIGH_LONG_BITS: 0; - } - return new Pair<>(low, hi); - } - - public static void shiftPair(Pair source, Pair dest) { - if(source.low.length == dest.low.length && source.hi.length == dest.hi.length) { - for(int i = 0; i < dest.low.length; i++) { - dest.low[i] <<= 1; - dest.low[i] |= source.low[i] & 1; - } - for(int i = 0; i < dest.hi.length; i++) { - dest.hi[i] <<= 1; - dest.hi[i] |= source.hi[i] & 1; - } - } - } - - public static int[] trits(final Pair pair, final int bitIndex) { - final int length; - if(pair.low.length == pair.hi.length || pair.low.length < pair.hi.length) { - length = pair.low.length; - } else { - length = pair.hi.length; - } - final int[] trits = new int[length]; - long low; - long hi; - int mask = 1 << bitIndex; - for(int i = 0; i < length; i++) { - low = pair.low[i] & mask; - hi = pair.hi[i] & mask; - if( hi == low ) { - trits[i] = 0; - } else if ( low == 0 ) { - trits[i] = 1; - } else if ( hi == 0 ) { - trits[i] = -1; - } - } - return trits; - } - - public static int[] trits(long[] low, long[] hi) { - int[] trits = new int[low.length]; - for(int i = 0; i < trits.length; i++) { - trits[i] = low[i] == 0 ? 1 : hi[i] == 0 ? -1 : 0; - } - return trits; - } - - public static int[] trits(int[] low, int[] hi) { - int[] trits = new int[low.length]; - for(int i = 0; i < trits.length; i++) { - trits[i] = low[i] == 0 ? 1 : hi[i] == 0 ? -1 : 0; - } - return trits; - } - - private static void increment(final int[] trits, final int size) { - for (int i = 0; i < size; i++) { - if (++trits[i] > Converter.MAX_TRIT_VALUE) { - trits[i] = Converter.MIN_TRIT_VALUE; - } else { - break; - } - } - } - - public static String asciiToTrytes(String input) { - StringBuilder sb = new StringBuilder(80); - for (int i = 0; i < input.length(); i++) { - int asciiValue = input.charAt(i); - // If not recognizable ASCII character, return null - if (asciiValue > 255) { - return null; - } - int firstValue = asciiValue % 27; - int secondValue = (asciiValue - firstValue) / 27; - sb.append(TRYTE_ALPHABET.charAt(firstValue)); - sb.append(TRYTE_ALPHABET.charAt(secondValue)); - } - return sb.toString(); - } - - public static byte[] hexStringToByteArray(String s) { - int len = s.length(); - byte[] data = new byte[len / 2]; - for (int i = 0; i < len; i += 2) { - data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) - + Character.digit(s.charAt(i+1), 16)); - } - return data; - } - public static byte[] toBytesFromString(String input) { - if (input.length() % 2 != 0) { - System.out.println("Invalid length: " + input.length()); - // throw exception - } - byte[] result = new byte[input.length()/2]; - int ct = 0; - for(int i = 0; i Date: Fri, 19 Jul 2019 16:30:33 +0200 Subject: [PATCH 06/11] Refactor hexstring --- src/main/java/net/helix/hlx/network/Node.java | 8 ++++---- .../hlx/network/TransactionRequester.java | 4 ++-- src/main/java/net/helix/hlx/service/API.java | 4 ++-- .../java/net/helix/hlx/service/Spammer.java | 2 +- .../service/ledger/impl/LedgerServiceImpl.java | 4 ++-- .../impl/LatestSolidMilestoneTrackerImpl.java | 6 +++--- .../milestone/impl/MilestoneServiceImpl.java | 10 +++++----- .../snapshot/impl/SnapshotProviderImpl.java | 8 ++++---- .../stats/TransactionStatsPublisher.java | 1 - .../impl/CumulativeWeightCalculator.java | 2 +- .../tipselection/impl/WalkValidatorImpl.java | 12 ++++++------ .../service/tipselection/impl/WalkerAlpha.java | 2 +- .../helix/hlx/zmq/MessageQProviderImpl.java | 18 +++++++++--------- .../helix/hlx/service/APIIntegrationTest.java | 3 +-- .../snapshot/impl/SnapshotServiceImplTest.java | 4 ++-- 15 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/main/java/net/helix/hlx/network/Node.java b/src/main/java/net/helix/hlx/network/Node.java index d2fefce0..66b433fa 100644 --- a/src/main/java/net/helix/hlx/network/Node.java +++ b/src/main/java/net/helix/hlx/network/Node.java @@ -461,7 +461,7 @@ public void processReceivedData(TransactionViewModel receivedTransactionViewMode try { stored = receivedTransactionViewModel.store(tangle, snapshotProvider.getInitialSnapshot()); if (this.graph != null) { - this.graph.addNode(receivedTransactionViewModel.getHash().hexString(), receivedTransactionViewModel.getTrunkTransactionHash().hexString(), receivedTransactionViewModel.getBranchTransactionHash().hexString()); + this.graph.addNode(receivedTransactionViewModel.getHash().toString(), receivedTransactionViewModel.getTrunkTransactionHash().toString(), receivedTransactionViewModel.getBranchTransactionHash().toString()); } } catch (Exception e) { log.error("Error accessing persistence store.", e); @@ -491,8 +491,8 @@ public void processReceivedData(TransactionViewModel receivedTransactionViewMode for (Hash txHash : receivedBundle.getHashes()) { TransactionViewModel transactionViewModel = TransactionViewModel.fromHash(tangle, txHash); JsonObject addressTopicJson = new JsonObject(); - addressTopicJson.addProperty("tx_hash", transactionViewModel.getHash().hexString()); - addressTopicJson.addProperty("bundle_hash", transactionViewModel.getBundleHash().hexString()); + addressTopicJson.addProperty("tx_hash", transactionViewModel.getHash().toString()); + addressTopicJson.addProperty("bundle_hash", transactionViewModel.getBundleHash().toString()); addressTopicJson.addProperty("signature", Hex.toHexString(transactionViewModel.getSignature())); addressTopicJson.addProperty("bundle_index", transactionViewModel.getCurrentIndex()); preBundle.add(addressTopicJson); @@ -500,7 +500,7 @@ public void processReceivedData(TransactionViewModel receivedTransactionViewMode for (int i = preBundle.size()-1; i >= 0; i--) { publishBundle.add(preBundle.get(i)); } - tangle.publish("%s %s", "ORACLE_" + receivedTransactionViewModel.getAddressHash().hexString(), publishBundle.toString()); + tangle.publish("%s %s", "ORACLE_" + receivedTransactionViewModel.getAddressHash().toString(), publishBundle.toString()); } } catch (Exception e) { log.error("Error publishing bundle.", e); diff --git a/src/main/java/net/helix/hlx/network/TransactionRequester.java b/src/main/java/net/helix/hlx/network/TransactionRequester.java index fb714f41..b751c540 100644 --- a/src/main/java/net/helix/hlx/network/TransactionRequester.java +++ b/src/main/java/net/helix/hlx/network/TransactionRequester.java @@ -135,8 +135,8 @@ public Hash transactionToRequest(boolean milestone) throws Exception { // if we have received the transaction in the mean time .... if (TransactionViewModel.exists(tangle, hash)) { // ... dump a log message ... - log.info("Removed existing tx from request list: " + hash); - tangle.publish("rtl %s", hash.hexString()); + log.info("Removed existing tx from request list: " + hash.toString()); + tangle.publish("rtl %s", hash.toString()); // ... and continue to the next element in the set continue; diff --git a/src/main/java/net/helix/hlx/service/API.java b/src/main/java/net/helix/hlx/service/API.java index 30724b81..995a9f11 100644 --- a/src/main/java/net/helix/hlx/service/API.java +++ b/src/main/java/net/helix/hlx/service/API.java @@ -587,7 +587,7 @@ private void gatherStatisticsOnTipSelection() { private synchronized AbstractResponse getTipsStatement() throws Exception { return GetTipsResponse.create(tipsViewModel.getTips() .stream() - .map(Hash::hexString) + .map(Hash::toString) .collect(Collectors.toList())); } @@ -621,7 +621,7 @@ public void storeTransactionsStatement(final List txHex) throws Exceptio } if (graph != null) { - graph.addNode(transactionViewModel.getHash().hexString(), transactionViewModel.getTrunkTransactionHash().hexString(), transactionViewModel.getBranchTransactionHash().hexString()); + graph.addNode(transactionViewModel.getHash().toString(), transactionViewModel.getTrunkTransactionHash().toString(), transactionViewModel.getBranchTransactionHash().toString()); } } } diff --git a/src/main/java/net/helix/hlx/service/Spammer.java b/src/main/java/net/helix/hlx/service/Spammer.java index 5128082f..0dde8b25 100644 --- a/src/main/java/net/helix/hlx/service/Spammer.java +++ b/src/main/java/net/helix/hlx/service/Spammer.java @@ -24,7 +24,7 @@ public Spammer(SpamConfig config, API api) { this.api = api; this.config = config; this.message = StringUtils.repeat('0', 1024*2); - this.address = Hash.NULL_HASH.hexString(); + this.address = Hash.NULL_HASH.toString(); this.delay = config.getSpamDelay(); } diff --git a/src/main/java/net/helix/hlx/service/ledger/impl/LedgerServiceImpl.java b/src/main/java/net/helix/hlx/service/ledger/impl/LedgerServiceImpl.java index 78d38d76..d13e4953 100644 --- a/src/main/java/net/helix/hlx/service/ledger/impl/LedgerServiceImpl.java +++ b/src/main/java/net/helix/hlx/service/ledger/impl/LedgerServiceImpl.java @@ -195,14 +195,14 @@ public Map generateBalanceDiff(Set visitedTransactions, Hash s //TODO: graphstream if (graph != null) { - graph.setValidity(transactionViewModel.getHash().hexString(), transactionViewModel.getValidity()); + graph.setValidity(transactionViewModel.getHash().toString(), transactionViewModel.getValidity()); } for (final List bundleTransactionViewModels : bundleTransactions) { //TODO: graphstream if (graph != null) { for (final TransactionViewModel bundleTransactionViewModel : bundleTransactionViewModels) { - graph.setValidity(bundleTransactionViewModel.getHash().hexString(), bundleTransactionViewModel.getValidity()); + graph.setValidity(bundleTransactionViewModel.getHash().toString(), bundleTransactionViewModel.getValidity()); } } diff --git a/src/main/java/net/helix/hlx/service/milestone/impl/LatestSolidMilestoneTrackerImpl.java b/src/main/java/net/helix/hlx/service/milestone/impl/LatestSolidMilestoneTrackerImpl.java index 25bc36de..4090fb30 100644 --- a/src/main/java/net/helix/hlx/service/milestone/impl/LatestSolidMilestoneTrackerImpl.java +++ b/src/main/java/net/helix/hlx/service/milestone/impl/LatestSolidMilestoneTrackerImpl.java @@ -146,7 +146,7 @@ public void trackLatestSolidMilestone() throws MilestoneException { //TODO: graphstream if (ledgerService.getGraph() != null) { - ledgerService.getGraph().setMilestone(nextMilestone.getHash().hexString()); + ledgerService.getGraph().setMilestone(nextMilestone.getHash().toString()); } syncLatestMilestoneTracker(nextMilestone.getHash(), nextMilestone.index()); @@ -262,7 +262,7 @@ private void syncLatestMilestoneTracker(Hash milestoneHash, int milestoneIndex) * Emits a log message whenever the latest solid milestone changes.
*
* It simply compares the current latest milestone index against the previous milestone index and emits the log - * messages using the {@link #log} and the {@link #messageQ} instances if it differs.
+ * messages using the {@link #log} and the {@link #MessageQ} instances if it differs.
* * @param prevSolidMilestoneIndex the milestone index before the change */ @@ -275,7 +275,7 @@ private void logChange(int prevSolidMilestoneIndex) { log.info("Latest SOLID milestone index changed from #" + prevSolidMilestoneIndex + " to #" + latestMilestoneIndex); tangle.publish("lmsi %d %d", prevSolidMilestoneIndex, latestMilestoneIndex); - tangle.publish("lmhs %s", latestMilestoneHash.hexString()); + tangle.publish("lmhs %s", latestMilestoneHash.toString()); } } diff --git a/src/main/java/net/helix/hlx/service/milestone/impl/MilestoneServiceImpl.java b/src/main/java/net/helix/hlx/service/milestone/impl/MilestoneServiceImpl.java index 81534343..ff64bc98 100644 --- a/src/main/java/net/helix/hlx/service/milestone/impl/MilestoneServiceImpl.java +++ b/src/main/java/net/helix/hlx/service/milestone/impl/MilestoneServiceImpl.java @@ -412,14 +412,14 @@ private void updateMilestoneIndexOfSingleTransaction(TransactionViewModel transa } JsonObject addressTopicJson = new JsonObject(); - addressTopicJson.addProperty("hash", transaction.getHash().hexString()); + addressTopicJson.addProperty("hash", transaction.getHash().toString()); addressTopicJson.addProperty("signature", Hex.toHexString(transaction.getSignature())); addressTopicJson.addProperty("index", index); - tangle.publish("%s %s", transaction.getAddressHash().hexString(), addressTopicJson.toString()); - tangle.publish("sn %d %s %s %s %s %s", index, transaction.getHash().hexString(), transaction.getAddressHash().hexString(), - transaction.getTrunkTransactionHash().hexString(), transaction.getBranchTransactionHash().hexString(), - transaction.getBundleHash().hexString()); + tangle.publish("%s %s", transaction.getAddressHash().toString(), addressTopicJson.toString()); + tangle.publish("sn %d %s %s %s %s %s", index, transaction.getHash().toString(), transaction.getAddressHash().toString(), + transaction.getTrunkTransactionHash().toString(), transaction.getBranchTransactionHash().toString(), + transaction.getBundleHash().toString()); } /** diff --git a/src/main/java/net/helix/hlx/service/snapshot/impl/SnapshotProviderImpl.java b/src/main/java/net/helix/hlx/service/snapshot/impl/SnapshotProviderImpl.java index 1341e851..1fe61c67 100644 --- a/src/main/java/net/helix/hlx/service/snapshot/impl/SnapshotProviderImpl.java +++ b/src/main/java/net/helix/hlx/service/snapshot/impl/SnapshotProviderImpl.java @@ -388,7 +388,7 @@ private void writeSnapshotStateToDisk(SnapshotState snapshotState, String snapsh () -> snapshotState.getBalances().entrySet() .stream() .filter(entry -> entry.getValue() != 0) - .map(entry -> entry.getKey().hexString() + ";" + entry.getValue()) + .map(entry -> entry.getKey().toString() + ";" + entry.getValue()) .sorted() .iterator() ); @@ -628,7 +628,7 @@ private void writeSnapshotMetaDataToDisk(SnapshotMetaData snapshotMetaData, Stri Paths.get(filePath), () -> Stream.concat( Stream.of( - snapshotMetaData.getHash().hexString(), + snapshotMetaData.getHash().toString(), String.valueOf(snapshotMetaData.getIndex()), String.valueOf(snapshotMetaData.getTimestamp()), String.valueOf(solidEntryPoints.size()), @@ -638,11 +638,11 @@ private void writeSnapshotMetaDataToDisk(SnapshotMetaData snapshotMetaData, Stri solidEntryPoints.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) - .map(entry -> entry.getKey().hexString() + ";" + entry.getValue()), + .map(entry -> entry.getKey().toString() + ";" + entry.getValue()), seenMilestones.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) - .map(entry -> entry.getKey().hexString() + ";" + entry.getValue()) + .map(entry -> entry.getKey().toString() + ";" + entry.getValue()) ) ).iterator() ); diff --git a/src/main/java/net/helix/hlx/service/stats/TransactionStatsPublisher.java b/src/main/java/net/helix/hlx/service/stats/TransactionStatsPublisher.java index c7c26304..103fbeab 100644 --- a/src/main/java/net/helix/hlx/service/stats/TransactionStatsPublisher.java +++ b/src/main/java/net/helix/hlx/service/stats/TransactionStatsPublisher.java @@ -103,7 +103,6 @@ private long getAllTransactionsCount(Instant now) throws Exception { long count = 0; for (Hash tip : tipsViewModel.getTips()) { // count the tip, if it is the valid time window - log.debug("DZMQ: {}", tip.hexString()); if (approveeCounter.isInTimeWindow(now, TransactionViewModel.fromHash(tangle, tip))) { count += 1 + approveeCounter.getCount(now, tip, processedTransactions, false); } else { diff --git a/src/main/java/net/helix/hlx/service/tipselection/impl/CumulativeWeightCalculator.java b/src/main/java/net/helix/hlx/service/tipselection/impl/CumulativeWeightCalculator.java index 807f1109..106bf949 100644 --- a/src/main/java/net/helix/hlx/service/tipselection/impl/CumulativeWeightCalculator.java +++ b/src/main/java/net/helix/hlx/service/tipselection/impl/CumulativeWeightCalculator.java @@ -46,7 +46,7 @@ public CumulativeWeightCalculator(Tangle tangle, SnapshotProvider snapshotProvid @Override public UnIterableMap calculate(Hash entryPoint) throws Exception { - log.debug("Start calculating cw starting with tx hash {}", (entryPoint.hexString())); + log.debug("Start calculating cw starting with tx hash {}", (entryPoint.toString())); LinkedHashSet txHashesToRate = sortTransactionsInTopologicalOrder(entryPoint); return calculateCwInOrder(txHashesToRate); diff --git a/src/main/java/net/helix/hlx/service/tipselection/impl/WalkValidatorImpl.java b/src/main/java/net/helix/hlx/service/tipselection/impl/WalkValidatorImpl.java index 34ebcf96..89b4e73f 100644 --- a/src/main/java/net/helix/hlx/service/tipselection/impl/WalkValidatorImpl.java +++ b/src/main/java/net/helix/hlx/service/tipselection/impl/WalkValidatorImpl.java @@ -60,25 +60,25 @@ public boolean isValid(Hash transactionHash) throws Exception { TransactionViewModel transactionViewModel = TransactionViewModel.fromHash(tangle, transactionHash); if (transactionViewModel.getType() == TransactionViewModel.PREFILLED_SLOT) { - log.debug("Validation failed: {} is missing in db", transactionHash.hexString()); + log.debug("Validation failed: {} is missing in db", transactionHash.toString()); return false; } else if (transactionViewModel.getCurrentIndex() != 0) { - log.debug("Validation failed: {} not a tail", transactionHash.hexString()); + log.debug("Validation failed: {} not a tail", transactionHash.toString()); return false; } else if (!transactionViewModel.isSolid()) { - log.debug("Validation failed: {} is not solid", transactionHash.hexString()); + log.debug("Validation failed: {} is not solid", transactionHash.toString()); return false; } else if (belowMaxDepth(transactionViewModel.getHash(), snapshotProvider.getLatestSnapshot().getIndex() - config.getMaxDepth())) { - log.debug("Validation failed: {} is below max depth", transactionHash.hexString()); + log.debug("Validation failed: {} is below max depth", transactionHash.toString()); return false; } else if (!ledgerService.updateDiff(myApprovedHashes, myDiff, transactionViewModel.getHash())) { - log.debug("Validation failed: {} is not consistent", transactionHash.hexString()); + log.debug("Validation failed: {} is not consistent", transactionHash.toString()); return false; } //TODO fix bundle validation else if (!ledgerService.isBalanceDiffConsistent(myApprovedHashes, myDiff, transactionViewModel.getHash())) { - log.debug("Validation failed: {} is not consistent", transactionHash.hexString()); + log.debug("Validation failed: {} is not consistent", transactionHash.toString()); return false; } return true; diff --git a/src/main/java/net/helix/hlx/service/tipselection/impl/WalkerAlpha.java b/src/main/java/net/helix/hlx/service/tipselection/impl/WalkerAlpha.java index 23bdfdff..239487fa 100644 --- a/src/main/java/net/helix/hlx/service/tipselection/impl/WalkerAlpha.java +++ b/src/main/java/net/helix/hlx/service/tipselection/impl/WalkerAlpha.java @@ -66,7 +66,7 @@ public void setAlpha(double alpha) { @Override public Hash walk(Hash entryPoint, UnIterableMap ratings, WalkValidator walkValidator) throws Exception { if (!walkValidator.isValid(entryPoint)) { - throw new IllegalStateException("entry point failed consistency check: " + entryPoint.hexString()); + throw new IllegalStateException("entry point failed consistency check: " + entryPoint.toString()); } Optional nextStep; diff --git a/src/main/java/net/helix/hlx/zmq/MessageQProviderImpl.java b/src/main/java/net/helix/hlx/zmq/MessageQProviderImpl.java index be5d8d0f..df2c82ee 100644 --- a/src/main/java/net/helix/hlx/zmq/MessageQProviderImpl.java +++ b/src/main/java/net/helix/hlx/zmq/MessageQProviderImpl.java @@ -50,23 +50,23 @@ private void publishTx(TransactionViewModel transactionViewModel) { try { txStringBuilder.append("tx_hash "); - txStringBuilder.append(transactionViewModel.getHash().hexString()); txStringBuilder.append("\n"); + txStringBuilder.append(transactionViewModel.getHash().toString()); txStringBuilder.append("\n"); txStringBuilder.append("tx_address "); - txStringBuilder.append(transactionViewModel.getAddressHash().hexString()); txStringBuilder.append("\n"); + txStringBuilder.append(transactionViewModel.getAddressHash().toString()); txStringBuilder.append("\n"); txStringBuilder.append("tx_msg "); txStringBuilder.append(Hex.toHexString(transactionViewModel.getSignature())); - txStringBuilder.append(transactionViewModel.getHash().hexString()); txStringBuilder.append(" "); - txStringBuilder.append(transactionViewModel.getAddressHash().hexString()); txStringBuilder.append(" "); + txStringBuilder.append(transactionViewModel.getHash().toString()); txStringBuilder.append(" "); + txStringBuilder.append(transactionViewModel.getAddressHash().toString()); txStringBuilder.append(" "); txStringBuilder.append(String.valueOf(transactionViewModel.value())); txStringBuilder.append(" "); - txStringBuilder.append(transactionViewModel.getBundleNonceHash().hexString()); txStringBuilder.append(" "); + txStringBuilder.append(transactionViewModel.getBundleNonceHash().toString()); txStringBuilder.append(" "); txStringBuilder.append(String.valueOf(transactionViewModel.getTimestamp())); txStringBuilder.append(" "); txStringBuilder.append(String.valueOf(transactionViewModel.getCurrentIndex())); txStringBuilder.append(" "); txStringBuilder.append(String.valueOf(transactionViewModel.lastIndex())); txStringBuilder.append(" "); - txStringBuilder.append(transactionViewModel.getBundleHash().hexString()); txStringBuilder.append(" "); - txStringBuilder.append(transactionViewModel.getTrunkTransactionHash().hexString()); txStringBuilder.append(" "); - txStringBuilder.append(transactionViewModel.getBranchTransactionHash().hexString()); txStringBuilder.append(" "); + txStringBuilder.append(transactionViewModel.getBundleHash().toString()); txStringBuilder.append(" "); + txStringBuilder.append(transactionViewModel.getTrunkTransactionHash().toString()); txStringBuilder.append(" "); + txStringBuilder.append(transactionViewModel.getBranchTransactionHash().toString()); txStringBuilder.append(" "); txStringBuilder.append(String.valueOf(transactionViewModel.getArrivalTime())); txStringBuilder.append(" "); - txStringBuilder.append(transactionViewModel.getTagValue().hexString()); + txStringBuilder.append(transactionViewModel.getTagValue().toString()); messageQ.publish(txStringBuilder.toString()); } catch (Exception e) { diff --git a/src/test/java/net/helix/hlx/service/APIIntegrationTest.java b/src/test/java/net/helix/hlx/service/APIIntegrationTest.java index 6fc6efb6..b109d342 100644 --- a/src/test/java/net/helix/hlx/service/APIIntegrationTest.java +++ b/src/test/java/net/helix/hlx/service/APIIntegrationTest.java @@ -523,7 +523,6 @@ public void shouldSendTransactionAndFetchByTagTest() { } private String getHash(String hex) { - return TransactionHash.calculate(hex, 0, TransactionViewModel.SIZE, SpongeFactory.create(SpongeFactory.Mode.S256)).hexString(); + return TransactionHash.calculate(hex, 0, TransactionViewModel.SIZE, SpongeFactory.create(SpongeFactory.Mode.S256)).toString(); } - } diff --git a/src/test/java/net/helix/hlx/service/snapshot/impl/SnapshotServiceImplTest.java b/src/test/java/net/helix/hlx/service/snapshot/impl/SnapshotServiceImplTest.java index d93a0fd2..e4ce4dd5 100644 --- a/src/test/java/net/helix/hlx/service/snapshot/impl/SnapshotServiceImplTest.java +++ b/src/test/java/net/helix/hlx/service/snapshot/impl/SnapshotServiceImplTest.java @@ -37,8 +37,8 @@ public class SnapshotServiceImplTest { public MockitoRule mockitoRule = MockitoJUnit.rule(); private enum MockedMilestone { - A(getTransactionHash().hexString(), 70001, 1542146728L), - B(getTransactionHash().hexString(), 70005, 1546146728L); + A(getTransactionHash().toString(), 70001, 1542146728L), + B(getTransactionHash().toString(), 70005, 1546146728L); private final Hash transactionHash; From 93f04f1685aea891c67cdf3927b4f53dee4ed817 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 16:33:18 +0200 Subject: [PATCH 07/11] update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1799b5f3..5ef1cccf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.5.8 + - Cleanup Converter + - Refactor hexString() + - Refactor hbytes + # 0.5.7 - Added APIIntegrationTest - Added SnapshotMockUtils From c9c6937355b1bb37882ae0ce1f70157bf2982d57 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 16:34:36 +0200 Subject: [PATCH 08/11] bump version --- README.md | 2 +- pom.xml | 8 ++++---- src/main/java/net/helix/hlx/HLX.java | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0e478107..54637b57 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ # Helix-1.0 This is the 1.0 implementation of the Helix Protocol based on [**IRI**](https://github.com/iotaledger/iri/). -* **Latest release:** 0.5.7 pre-release +* **Latest release:** 0.5.8 pre-release * **License:** GPLv3 ## Developers diff --git a/pom.xml b/pom.xml index c64b7d9d..9493824a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,15 +5,15 @@ net.helix helix - 0.5.7 + 0.5.8 Helix Helix-1.0 - https://github.com/helixnetwork/testnet-1.0/ - scm:git:git://github.com/helixnetwork/testnet-1.0.git - scm:git:git@github.com/helixnetwork/testnet-1.0.git + https://github.com/helixnetwork/helix-1.0/ + scm:git:git://github.com/helixnetwork/helix-1.0.git + scm:git:git@github.com/helixnetwork/helix-1.0.git diff --git a/src/main/java/net/helix/hlx/HLX.java b/src/main/java/net/helix/hlx/HLX.java index ac9da0ab..9a6c5de8 100644 --- a/src/main/java/net/helix/hlx/HLX.java +++ b/src/main/java/net/helix/hlx/HLX.java @@ -44,7 +44,7 @@ public class HLX { public static final String MAINNET_NAME = "HLX"; public static final String TESTNET_NAME = "HLX Testnet"; - public static final String VERSION = "0.5.7"; + public static final String VERSION = "0.5.8"; /** * The entry point of the helix sandbox. From 991590be960e7c65406703911240138552cd6941 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 17:20:50 +0200 Subject: [PATCH 09/11] Refactor hbytes --- CHANGELOG.md | 4 +- .../java/net/helix/hlx/conf/APIConfig.java | 6 +-- .../net/helix/hlx/conf/BaseHelixConfig.java | 14 +++---- .../java/net/helix/hlx/crypto/Winternitz.java | 2 +- src/main/java/net/helix/hlx/network/Node.java | 8 ++-- src/main/java/net/helix/hlx/service/API.java | 42 +++++++++---------- .../net/helix/hlx/service/ApiCommand.java | 4 +- .../service/dto/AttachToTangleResponse.java | 16 +++---- .../hlx/service/dto/GetHBytesResponse.java | 40 ------------------ .../dto/GetNodeAPIConfigurationResponse.java | 10 ++--- .../dto/GetTransactionStringsResponse.java | 40 ++++++++++++++++++ 11 files changed, 93 insertions(+), 93 deletions(-) delete mode 100644 src/main/java/net/helix/hlx/service/dto/GetHBytesResponse.java create mode 100644 src/main/java/net/helix/hlx/service/dto/GetTransactionStringsResponse.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ef1cccf..c4b21c0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # 0.5.8 - Cleanup Converter - - Refactor hexString() - - Refactor hbytes + - Refactor hexString() (#83) + - Refactor hbytes: new param name, "tx", for `attachToTangle`, `storeTransaction`, `broadcastTransaction`. # 0.5.7 - Added APIIntegrationTest diff --git a/src/main/java/net/helix/hlx/conf/APIConfig.java b/src/main/java/net/helix/hlx/conf/APIConfig.java index 7d7a3621..f62f3db3 100644 --- a/src/main/java/net/helix/hlx/conf/APIConfig.java +++ b/src/main/java/net/helix/hlx/conf/APIConfig.java @@ -38,9 +38,9 @@ public interface APIConfig extends Config { int getMaxRequestsList(); /** - * @return {@value Descriptions#MAX_GET_BYTES} + * @return {@value Descriptions#MAX_GET_TRANSACTION_STRINGS} */ - int getMaxBytes(); + int getMaxTransactionStrings(); /** * @return {@value Descriptions#MAX_BODY_LENGTH} @@ -65,7 +65,7 @@ interface Descriptions { String REMOTE_AUTH = "A string in the form of :. Used to access the API"; String MAX_FIND_TRANSACTIONS = "The maximal number of transactions that may be returned by the \"findTransactions\" API call. If the number of transactions found exceeds this number an error will be returned."; String MAX_REQUESTS_LIST = "The maximal number of parameters one can place in an API call. If the number parameters exceeds this number an error will be returned"; - String MAX_GET_BYTES = "The maximal number of bytes that may be returned by the \"getHBytes\" API call. If the number of transactions found exceeds this number an error will be returned."; + String MAX_GET_TRANSACTION_STRINGS = "The maximal number of transaction strings that may be returned by the \"getTransactionStrings\" API call. If the number of transactions found exceeds this number an error will be returned."; String MAX_BODY_LENGTH = "The maximal number of characters the body of an API call may hold. If a request body length exceeds this number an error will be returned."; String REMOTE = "Open the API interface to any host. Equivalent to \"--api-host 0.0.0.0\""; String IS_POW_DISABLED = "If pow is disabled the node will not require a valid nonce."; diff --git a/src/main/java/net/helix/hlx/conf/BaseHelixConfig.java b/src/main/java/net/helix/hlx/conf/BaseHelixConfig.java index 42e86752..73993606 100644 --- a/src/main/java/net/helix/hlx/conf/BaseHelixConfig.java +++ b/src/main/java/net/helix/hlx/conf/BaseHelixConfig.java @@ -33,7 +33,7 @@ public abstract class BaseHelixConfig implements HelixConfig { protected List remoteTrustedApiHosts = Defaults.REMOTE_LIMIT_API_HOSTS; protected int maxFindTransactions = Defaults.MAX_FIND_TRANSACTIONS; protected int maxRequestsList = Defaults.MAX_REQUESTS_LIST; - protected int maxGetBytes = Defaults.MAX_GET_BYTES; + protected int maxGetTransactionStrings = Defaults.MAX_GET_TRANSACTION_STRINGS; protected int maxBodyLength = Defaults.MAX_BODY_LENGTH; protected String remoteAuth = Defaults.REMOTE_AUTH; protected boolean powDisabled = Defaults.IS_POW_DISABLED; @@ -239,14 +239,14 @@ protected void setMaxRequestsList(int maxRequestsList) { } @Override - public int getMaxBytes() { - return maxGetBytes; + public int getMaxTransactionStrings() { + return maxGetTransactionStrings; } @JsonProperty - @Parameter(names = {"--max-get-bytes"}, description = APIConfig.Descriptions.MAX_GET_BYTES) - protected void setMaxGetBytes(int maxGetBytes) { - this.maxGetBytes = maxGetBytes; + @Parameter(names = {"--max-get-transaction-strings"}, description = APIConfig.Descriptions.MAX_GET_TRANSACTION_STRINGS) + protected void setMaxGetTransactionStrings(int maxGetTransactionStrings) { + this.maxGetTransactionStrings = maxGetTransactionStrings; } @Override @@ -865,7 +865,7 @@ public interface Defaults { List REMOTE_LIMIT_API_HOSTS = HelixUtils.createImmutableList(REMOTE_LIMIT_API_DEFAULT_HOST); int MAX_FIND_TRANSACTIONS = 100_000; int MAX_REQUESTS_LIST = 1_000; - int MAX_GET_BYTES = 10_000; + int MAX_GET_TRANSACTION_STRINGS = 10_000; int MAX_BODY_LENGTH = 1_000_000; String REMOTE_AUTH = ""; boolean IS_POW_DISABLED = false; diff --git a/src/main/java/net/helix/hlx/crypto/Winternitz.java b/src/main/java/net/helix/hlx/crypto/Winternitz.java index 25ae88e3..80a2358a 100644 --- a/src/main/java/net/helix/hlx/crypto/Winternitz.java +++ b/src/main/java/net/helix/hlx/crypto/Winternitz.java @@ -175,7 +175,7 @@ public static byte[] digest(SpongeFactory.Mode mode, final byte[] bundleFragment * Deterministically Normalize the bundle hash.
*
    *
  1. map each byte in {@code bundle} to base 16 {@code [-13 , 13]}
  2. - *
  3. sum all mapped hbytes together
  4. + *
  5. sum all mapped bytes together
  6. *
  7. if sum != 0, start inc/dec each byte till sum equals 0
  8. *
* diff --git a/src/main/java/net/helix/hlx/network/Node.java b/src/main/java/net/helix/hlx/network/Node.java index 66b433fa..2a560eeb 100644 --- a/src/main/java/net/helix/hlx/network/Node.java +++ b/src/main/java/net/helix/hlx/network/Node.java @@ -536,18 +536,18 @@ public void replyToRequest(Hash requestedHash, Neighbor neighbor) { log.error("Error getting random tip.", e); } } else { - //find requested hbytes + //find requested txvm try { //transactionViewModel = TransactionViewModel.find(Arrays.copyOf(requestedHash.bytes(), TransactionRequester.REQUEST_HASH_SIZE)); transactionViewModel = TransactionViewModel.fromHash(tangle, HashFactory.TRANSACTION.create(requestedHash.bytes(), 0, reqHashSize)); - //log.debug("Requested Hash: " + requestedHash + " \nFound: " + transactionViewModel.getHash()); + //log.debug("Requested Hash: " + requestedHash + " \nFound: " + transactionViewModel.getHash()); TODO: remove unused code } catch (Exception e) { log.error("Error while searching for transaction.", e); } } if (transactionViewModel != null && transactionViewModel.getType() == TransactionViewModel.FILLED_SLOT) { - //send hbytes back to neighbor + // send txvm back to neighbor try { sendPacket(sendingPacket, transactionViewModel, neighbor); @@ -559,7 +559,7 @@ public void replyToRequest(Hash requestedHash, Neighbor neighbor) { log.error("Error fetching transaction to request.", e); } } else { - //hbytes not found + // txvm not found if (!requestedHash.equals(Hash.NULL_HASH) && rnd.nextDouble() < configuration.getpPropagateRequest()) { //request is an actual transaction and missing in request queue add it. try { diff --git a/src/main/java/net/helix/hlx/service/API.java b/src/main/java/net/helix/hlx/service/API.java index 995a9f11..91266885 100644 --- a/src/main/java/net/helix/hlx/service/API.java +++ b/src/main/java/net/helix/hlx/service/API.java @@ -109,7 +109,7 @@ public class API { private final int maxFindTxs; private final int maxRequestList; - private final int maxGetBytes; + private final int maxGetTransactionStrings; private final String[] features; @@ -167,7 +167,7 @@ public API(HelixConfig configuration, XI XI, TransactionRequester transactionReq maxFindTxs = configuration.getMaxFindTransactions(); maxRequestList = configuration.getMaxRequestsList(); - maxGetBytes = configuration.getMaxBytes(); + maxGetTransactionStrings = configuration.getMaxTransactionStrings(); milestoneStartIndex = configuration.getMilestoneStartIndex(); features = Feature.calculateFeatureNames(configuration); @@ -184,7 +184,7 @@ public API(HelixConfig configuration, XI XI, TransactionRequester transactionReq commandRoute.put(ApiCommand.GET_NODE_API_CONFIG, getNodeAPIConfiguration()); commandRoute.put(ApiCommand.GET_TIPS, getTips()); commandRoute.put(ApiCommand.GET_TRANSACTIONS_TO_APPROVE, getTransactionsToApprove()); - commandRoute.put(ApiCommand.GET_TRYTES, getHBytes()); + commandRoute.put(ApiCommand.GET_TRANSACTION_STRINGS, getTransactionStrings()); commandRoute.put(ApiCommand.INTERRUPT_ATTACHING_TO_TANGLE, interruptAttachingToTangle()); commandRoute.put(ApiCommand.REMOVE_NEIGHBORS, removeNeighbors()); commandRoute.put(ApiCommand.STORE_TRANSACTIONS, storeTransactions()); @@ -490,9 +490,9 @@ private AbstractResponse removeNeighborsStatement(List uris) { * See utility and {@link Transaction} functions in an Helix library for more details. * * @param hashes The transaction hashes you want to get bytes from. - * @return {@link net.helix.hlx.service.dto.GetHBytesResponse} + * @return {@link GetTransactionStringsResponse} **/ - private synchronized AbstractResponse getHBytesStatement(List hashes) throws Exception { + private synchronized AbstractResponse getTransactionStringsStatement(List hashes) throws Exception { final List elements = new LinkedList<>(); for (final String hash : hashes) { final TransactionViewModel transactionViewModel = TransactionViewModel.fromHash(tangle, HashFactory.TRANSACTION.create(hash)); @@ -500,10 +500,10 @@ private synchronized AbstractResponse getHBytesStatement(List hashes) th elements.add(Hex.toHexString(transactionViewModel.getBytes())); } } - if (elements.size() > maxGetBytes){ + if (elements.size() > maxGetTransactionStrings){ return ErrorResponse.create(OVER_MAX_ERROR_MESSAGE); } - return GetHBytesResponse.create(elements); + return GetTransactionStringsResponse.create(elements); } /** @@ -596,14 +596,14 @@ private synchronized AbstractResponse getTipsStatement() throws Exception { * The bytes to be used for this call should be valid, attached transaction bytes. * These bytes are returned by attachToTangle, or by doing proof of work somewhere else. * - * @param txHex Transaction data to be stored. + * @param txString Transaction data to be stored. * @throws Exception When storing or updating a transaction fails **/ - public void storeTransactionsStatement(final List txHex) throws Exception { + public void storeTransactionsStatement(final List txString) throws Exception { final List elements = new LinkedList<>(); byte[] txBytes; - for (final String hex : txHex) { + for (final String hex : txString) { //validate all tx txBytes = Hex.decode(hex); final TransactionViewModel transactionViewModel = transactionValidator.validateBytes(txBytes, @@ -1016,12 +1016,12 @@ private HashSet getParameterAsSet(Map request, String pa * The bytes to be used for this call should be valid, attached transaction bytes. * These bytes are returned by attachToTangle, or by doing proof of work somewhere else. * - * @param txHex the list of transaction bytes to broadcast + * @param txString the list of transaction bytes to broadcast **/ - public void broadcastTransactionsStatement(final List txHex) { + public void broadcastTransactionsStatement(final List txString) { final List elements = new LinkedList<>(); byte[] txBytes; - for (final String hex : txHex) { + for (final String hex : txString) { //validate all tx txBytes = Hex.decode(hex); // TODO something possibly going wrong here. @@ -1570,17 +1570,17 @@ private Function, AbstractResponse> attachToTangle() { final Hash branchTransaction = HashFactory.TRANSACTION.create(getParameterAsStringAndValidate(request,"branchTransaction", HASH_SIZE)); final int minWeightMagnitude = getParameterAsInt(request,"minWeightMagnitude"); - final List hbytes = getParameterAsList(request,"hbytes", BYTES_SIZE); + final List txString = getParameterAsList(request,"tx", BYTES_SIZE); - List elements = attachToTangleStatement(trunkTransaction, branchTransaction, minWeightMagnitude, hbytes); + List elements = attachToTangleStatement(trunkTransaction, branchTransaction, minWeightMagnitude, txString); return AttachToTangleResponse.create(elements); }; } private Function, AbstractResponse> broadcastTransactions() { return request -> { - final List hbytes = getParameterAsList(request,"hbytes", BYTES_SIZE); - broadcastTransactionsStatement(hbytes); + final List txString = getParameterAsList(request,"tx", BYTES_SIZE); + broadcastTransactionsStatement(txString); return AbstractResponse.createEmptyResponse(); }; } @@ -1666,11 +1666,11 @@ private Function, AbstractResponse> getTransactionsToApprove }; } - private Function, AbstractResponse> getHBytes() { + private Function, AbstractResponse> getTransactionStrings() { return request -> { final List hashes = getParameterAsList(request,"hashes", HASH_SIZE); try { - return getHBytesStatement(hashes); + return getTransactionStringsStatement(hashes); } catch (Exception e) { throw new IllegalStateException(e); } @@ -1692,8 +1692,8 @@ private Function, AbstractResponse> removeNeighbors() { private Function, AbstractResponse> storeTransactions() { return request -> { try { - final List hbytes = getParameterAsList(request,"hbytes", BYTES_SIZE); - storeTransactionsStatement(hbytes); + final List txString = getParameterAsList(request,"tx", BYTES_SIZE); + storeTransactionsStatement(txString); } catch (Exception e) { //transaction not valid return ErrorResponse.create("Invalid bytes input"); diff --git a/src/main/java/net/helix/hlx/service/ApiCommand.java b/src/main/java/net/helix/hlx/service/ApiCommand.java index 6c37f366..f977793a 100644 --- a/src/main/java/net/helix/hlx/service/ApiCommand.java +++ b/src/main/java/net/helix/hlx/service/ApiCommand.java @@ -73,9 +73,9 @@ public enum ApiCommand { GET_TRANSACTIONS_TO_APPROVE("getTransactionsToApprove"), /** - * Get hex bytes of a transaction by its hash + * Get corresponding hex strings for a list of transaction hashes */ - GET_TRYTES("getHBytes"), + GET_TRANSACTION_STRINGS("getTransactionStrings"), /** * Stop attaching to the tangle diff --git a/src/main/java/net/helix/hlx/service/dto/AttachToTangleResponse.java b/src/main/java/net/helix/hlx/service/dto/AttachToTangleResponse.java index 769a97fe..0745aa23 100644 --- a/src/main/java/net/helix/hlx/service/dto/AttachToTangleResponse.java +++ b/src/main/java/net/helix/hlx/service/dto/AttachToTangleResponse.java @@ -13,28 +13,28 @@ public class AttachToTangleResponse extends AbstractResponse { /** - * List of the attached transaction bytes. + * List of the attached transactions in hexadecimal representation. * The last 96 bytes of the return value consist of the: * trunkTransaction + branchTransaction + nonce. */ - private List hbytes; + private List transactionStrings; /** * Creates a new {@link AttachToTangleResponse} - * @param elements {@link #hbytes} - * @return an {@link AttachToTangleResponse} filled with the hbytes + * @param elements {@link #transactionStrings} + * @return an {@link AttachToTangleResponse} filled with the txs */ public static AbstractResponse create(List elements) { AttachToTangleResponse res = new AttachToTangleResponse(); - res.hbytes = elements; + res.transactionStrings = elements; return res; } /** * - * @return {@link #hbytes} + * @return {@link #transactionStrings} */ - public List getHBytes() { - return hbytes; + public List getTransactionStrings() { + return transactionStrings; } } diff --git a/src/main/java/net/helix/hlx/service/dto/GetHBytesResponse.java b/src/main/java/net/helix/hlx/service/dto/GetHBytesResponse.java deleted file mode 100644 index a7263118..00000000 --- a/src/main/java/net/helix/hlx/service/dto/GetHBytesResponse.java +++ /dev/null @@ -1,40 +0,0 @@ -package net.helix.hlx.service.dto; - -import net.helix.hlx.service.API; - -import java.util.List; - -/** - * - * Contains information about the result of a successful {@code getBytes} API call. - * See {@link API#getHBytesStatement} for how this response is created. - * - */ -public class GetHBytesResponse extends AbstractResponse { - /** - * The raw transaction data {hbytes} of the specified transactions. - * These hbytes can then be easily converted into the actual transaction object. - * See library functions as to how to transform back to a {@link net.helix.hlx.model.persistables.Transaction}. - */ - private String[] hbytes; - - /** - * Creates a new {@link GetHBytesResponse} - * - * @param elements {@link #hbytes} - * @return a {@link GetHBytesResponse} filled with the provided tips - */ - public static GetHBytesResponse create(List elements) { - GetHBytesResponse res = new GetHBytesResponse(); - res.hbytes = elements.toArray(new String[] {}); - return res; - } - - /** - * - * @return {@link #hbytes} - */ - public String [] getHBytes() { - return hbytes; - } -} diff --git a/src/main/java/net/helix/hlx/service/dto/GetNodeAPIConfigurationResponse.java b/src/main/java/net/helix/hlx/service/dto/GetNodeAPIConfigurationResponse.java index 5a967a8f..1ed0d0d4 100644 --- a/src/main/java/net/helix/hlx/service/dto/GetNodeAPIConfigurationResponse.java +++ b/src/main/java/net/helix/hlx/service/dto/GetNodeAPIConfigurationResponse.java @@ -9,7 +9,7 @@ public class GetNodeAPIConfigurationResponse extends AbstractResponse { private int maxFindTransactions; private int maxRequestsList; - private int maxBytes; + private int maxTransactionStrings; private int maxBodyLength; private boolean testNet; private int milestoneStartIndex; @@ -36,7 +36,7 @@ public static AbstractResponse create(HelixConfig configuration) { res.maxFindTransactions = configuration.getMaxFindTransactions(); res.maxRequestsList = configuration.getMaxRequestsList(); - res.maxBytes = configuration.getMaxBytes(); + res.maxTransactionStrings = configuration.getMaxTransactionStrings(); res.maxBodyLength = configuration.getMaxBodyLength(); res.testNet = configuration.isTestnet(); res.milestoneStartIndex = configuration.getMilestoneStartIndex(); @@ -54,9 +54,9 @@ public int getMaxRequestsList() { return maxRequestsList; } - /** {@link HelixConfig#getMaxBytes()} */ - public int getMaxBytes() { - return maxBytes; + /** {@link HelixConfig#getMaxTransactionStrings()} */ + public int getMaxTransactionStrings() { + return maxTransactionStrings; } /** {@link HelixConfig#getMaxBodyLength()} */ diff --git a/src/main/java/net/helix/hlx/service/dto/GetTransactionStringsResponse.java b/src/main/java/net/helix/hlx/service/dto/GetTransactionStringsResponse.java new file mode 100644 index 00000000..b7020076 --- /dev/null +++ b/src/main/java/net/helix/hlx/service/dto/GetTransactionStringsResponse.java @@ -0,0 +1,40 @@ +package net.helix.hlx.service.dto; + +import net.helix.hlx.service.API; + +import java.util.List; + +/** + * + * Contains information about the result of a successful {@code getBytes} API call. + * See {@link API#getTxHexStatement} for how this response is created. + * + */ +public class GetTransactionStringsResponse extends AbstractResponse { + /** + * The raw transaction data {txHex} of the specified transactions. + * These transaction hex strings can then be easily converted into the actual transaction object. + * See library functions as to how to transform back to a {@link net.helix.hlx.model.persistables.Transaction}. + */ + private String[] transactionStrings; + + /** + * Creates a new {@link GetTransactionStringsResponse} + * + * @param elements {@link #txHex} + * @return a {@link GetTransactionStringsResponse} filled with the provided tips + */ + public static GetTransactionStringsResponse create(List elements) { + GetTransactionStringsResponse res = new GetTransactionStringsResponse(); + res.transactionStrings = elements.toArray(new String[] {}); + return res; + } + + /** + * + * @return {@link #txHex} + */ + public String [] getTransactionStrings() { + return transactionStrings; + } +} From f4d00a2b4ed78980102ecce52b09bbfa5c2e8eb8 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 18:02:34 +0200 Subject: [PATCH 10/11] Refactoring fixes --- CHANGELOG.md | 2 +- src/main/java/net/helix/hlx/service/API.java | 6 +-- .../service/dto/AttachToTangleResponse.java | 10 ++--- .../dto/GetTransactionStringsResponse.java | 18 ++++---- .../java/net/helix/hlx/conf/ConfigTest.java | 10 ++--- .../helix/hlx/service/APIIntegrationTest.java | 42 +++++++++---------- 6 files changed, 44 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4b21c0a..692c0980 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # 0.5.8 - Cleanup Converter - Refactor hexString() (#83) - - Refactor hbytes: new param name, "tx", for `attachToTangle`, `storeTransaction`, `broadcastTransaction`. + - Refactor hbytes: new param name, "txs", for `attachToTangle`, `storeTransaction`, `broadcastTransaction`. # 0.5.7 - Added APIIntegrationTest diff --git a/src/main/java/net/helix/hlx/service/API.java b/src/main/java/net/helix/hlx/service/API.java index 91266885..5fae3a4e 100644 --- a/src/main/java/net/helix/hlx/service/API.java +++ b/src/main/java/net/helix/hlx/service/API.java @@ -1570,7 +1570,7 @@ private Function, AbstractResponse> attachToTangle() { final Hash branchTransaction = HashFactory.TRANSACTION.create(getParameterAsStringAndValidate(request,"branchTransaction", HASH_SIZE)); final int minWeightMagnitude = getParameterAsInt(request,"minWeightMagnitude"); - final List txString = getParameterAsList(request,"tx", BYTES_SIZE); + final List txString = getParameterAsList(request,"txs", BYTES_SIZE); List elements = attachToTangleStatement(trunkTransaction, branchTransaction, minWeightMagnitude, txString); return AttachToTangleResponse.create(elements); @@ -1579,7 +1579,7 @@ private Function, AbstractResponse> attachToTangle() { private Function, AbstractResponse> broadcastTransactions() { return request -> { - final List txString = getParameterAsList(request,"tx", BYTES_SIZE); + final List txString = getParameterAsList(request,"txs", BYTES_SIZE); broadcastTransactionsStatement(txString); return AbstractResponse.createEmptyResponse(); }; @@ -1692,7 +1692,7 @@ private Function, AbstractResponse> removeNeighbors() { private Function, AbstractResponse> storeTransactions() { return request -> { try { - final List txString = getParameterAsList(request,"tx", BYTES_SIZE); + final List txString = getParameterAsList(request,"txs", BYTES_SIZE); storeTransactionsStatement(txString); } catch (Exception e) { //transaction not valid diff --git a/src/main/java/net/helix/hlx/service/dto/AttachToTangleResponse.java b/src/main/java/net/helix/hlx/service/dto/AttachToTangleResponse.java index 0745aa23..96fed0bb 100644 --- a/src/main/java/net/helix/hlx/service/dto/AttachToTangleResponse.java +++ b/src/main/java/net/helix/hlx/service/dto/AttachToTangleResponse.java @@ -13,11 +13,11 @@ public class AttachToTangleResponse extends AbstractResponse { /** - * List of the attached transactions in hexadecimal representation. + * List of the attached transactions in hexadecimal representation (txs). * The last 96 bytes of the return value consist of the: * trunkTransaction + branchTransaction + nonce. */ - private List transactionStrings; + private List txs; /** * Creates a new {@link AttachToTangleResponse} @@ -26,15 +26,15 @@ public class AttachToTangleResponse extends AbstractResponse { */ public static AbstractResponse create(List elements) { AttachToTangleResponse res = new AttachToTangleResponse(); - res.transactionStrings = elements; + res.txs = elements; return res; } /** * - * @return {@link #transactionStrings} + * @return {@link #txs} */ public List getTransactionStrings() { - return transactionStrings; + return txs; } } diff --git a/src/main/java/net/helix/hlx/service/dto/GetTransactionStringsResponse.java b/src/main/java/net/helix/hlx/service/dto/GetTransactionStringsResponse.java index b7020076..03ba02ce 100644 --- a/src/main/java/net/helix/hlx/service/dto/GetTransactionStringsResponse.java +++ b/src/main/java/net/helix/hlx/service/dto/GetTransactionStringsResponse.java @@ -6,35 +6,35 @@ /** * - * Contains information about the result of a successful {@code getBytes} API call. - * See {@link API#getTxHexStatement} for how this response is created. + * Contains information about the result of a successful {@code getTransactionStrings} API call. + * See {@link API#getTransactionStrings} for how this response is created. * */ public class GetTransactionStringsResponse extends AbstractResponse { /** - * The raw transaction data {txHex} of the specified transactions. + * The raw transaction data {txs} of the specified transactions. * These transaction hex strings can then be easily converted into the actual transaction object. * See library functions as to how to transform back to a {@link net.helix.hlx.model.persistables.Transaction}. */ - private String[] transactionStrings; + private String[] txs; /** * Creates a new {@link GetTransactionStringsResponse} * - * @param elements {@link #txHex} + * @param elements {@link #transactionStrings} * @return a {@link GetTransactionStringsResponse} filled with the provided tips */ public static GetTransactionStringsResponse create(List elements) { GetTransactionStringsResponse res = new GetTransactionStringsResponse(); - res.transactionStrings = elements.toArray(new String[] {}); + res.txs = elements.toArray(new String[] {}); return res; } /** * - * @return {@link #txHex} + * @return {@link #txs} */ - public String [] getTransactionStrings() { - return transactionStrings; + public String[] getTransactionStrings() { + return txs; } } diff --git a/src/test/java/net/helix/hlx/conf/ConfigTest.java b/src/test/java/net/helix/hlx/conf/ConfigTest.java index 524d9106..be36cd33 100644 --- a/src/test/java/net/helix/hlx/conf/ConfigTest.java +++ b/src/test/java/net/helix/hlx/conf/ConfigTest.java @@ -59,7 +59,7 @@ public void argsParsingMainnetTest() { "--remote-limit-api", "call1 call2, call3", "--max-find-transactions", "500", "--max-requests-list", "1000", - "--max-get-bytes", "4000", + "--max-get-transaction-strings", "4000", "--max-body-length", "220", "--remote-auth", "2.2.2.2", "--p-remove-request", "0.23", @@ -92,7 +92,7 @@ public void argsParsingMainnetTest() { helixConfig.getRemoteLimitApi()); Assert.assertEquals("max find transactions", 500, helixConfig.getMaxFindTransactions()); Assert.assertEquals("max requests list", 1000, helixConfig.getMaxRequestsList()); - Assert.assertEquals("max get bytes", 4000, helixConfig.getMaxBytes()); + Assert.assertEquals("max get bytes", 4000, helixConfig.getMaxTransactionStrings()); Assert.assertEquals("max body length", 220, helixConfig.getMaxBodyLength()); Assert.assertEquals("remote-auth", "2.2.2.2", helixConfig.getRemoteAuth()); Assert.assertEquals("p remove request", 0.23d, helixConfig.getpRemoveRequest(), 0d); @@ -127,7 +127,7 @@ public void argsParsingTestnetTest() { "--remote-limit-api", "call1 call2, call3", "--max-find-transactions", "500", "--max-requests-list", "1000", - "--max-get-bytes", "4000", + "--max-get-transaction-strings", "4000", "--max-body-length", "220", "--remote-auth", "2.2.2.2", "--p-remove-request", "0.23", @@ -160,7 +160,7 @@ public void argsParsingTestnetTest() { helixConfig.getRemoteLimitApi()); Assert.assertEquals("max find transactions", 500, helixConfig.getMaxFindTransactions()); Assert.assertEquals("max requests list", 1000, helixConfig.getMaxRequestsList()); - Assert.assertEquals("max get bytes", 4000, helixConfig.getMaxBytes()); + Assert.assertEquals("max get tx strings", 4000, helixConfig.getMaxTransactionStrings()); Assert.assertEquals("max body length", 220, helixConfig.getMaxBodyLength()); Assert.assertEquals("remote-auth", "2.2.2.2", helixConfig.getRemoteAuth()); Assert.assertEquals("p remove request", 0.23d, helixConfig.getpRemoveRequest(), 0d); @@ -336,7 +336,7 @@ public enum LegacyDefaultConf { MAX_RANDOM_WALKS, MAX_FIND_TRANSACTIONS, MAX_REQUESTS_LIST, - MAX_GET_BYTES, + MAX_GET_TRANSACTION_STRINGS, MAX_BODY_LENGTH, MAX_DEPTH, MWM, diff --git a/src/test/java/net/helix/hlx/service/APIIntegrationTest.java b/src/test/java/net/helix/hlx/service/APIIntegrationTest.java index b109d342..632246ae 100644 --- a/src/test/java/net/helix/hlx/service/APIIntegrationTest.java +++ b/src/test/java/net/helix/hlx/service/APIIntegrationTest.java @@ -64,7 +64,7 @@ public class APIIntegrationTest { private static final String[] URIS = {"udp://8.8.8.8:14266", "udp://8.8.8.5:14266"}; private static final String[] ADDRESSES = {"d0e7e549a4ffe5b4f8343973f0237db9ede3597baced22715c22dcd8c76ae738"}; private static final String[] HASHES = {"0000f13be306d278fae139dc4a54deb40389a8d1c3677a872a9a198f57aad343"}; - private static final String[] HBYTES = {"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c2eb2d5297f4e70f3e40e3d7aa3f5c1d7405264aeb72232d06776605d8b6121100000000000000000000000000000000000000000000000000000000000000000000000000000000000000005d092fc0000000000000000000000000000000005031b48d241283c312c68c777bc4563ddd7cbe1ae6a2c58079e1bf3cfef826790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000068656c6c6f68656c0000016b6c93ca0e0000000000000000000000000000007f000000000000006f0000000000000000000000000000007f00000000000091b0"}; + private static final String[] TX_HEX = {"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c2eb2d5297f4e70f3e40e3d7aa3f5c1d7405264aeb72232d06776605d8b6121100000000000000000000000000000000000000000000000000000000000000000000000000000000000000005d092fc0000000000000000000000000000000005031b48d241283c312c68c777bc4563ddd7cbe1ae6a2c58079e1bf3cfef826790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000068656c6c6f68656c0000016b6c93ca0e0000000000000000000000000000007f000000000000006f0000000000000000000000000000007f00000000000091b0"}; private static final String NULL_HASH = "0000000000000000000000000000000000000000000000000000000000000000"; private static final String[] NULL_HASH_LIST = {NULL_HASH}; private static final TemporaryFolder dbFolder = new TemporaryFolder(); @@ -83,7 +83,7 @@ public static void setup() throws Exception { logFolder.create(); if (spawnNode) { //configure node parameters - log.info("IRI integration tests - initializing node."); + log.info("Helix integration tests - initializing node."); configuration = ConfigFactory.createHelixConfig(true); String[] args = {"-p", portStr, "--testnet", "true", "--db-path", dbFolder.getRoot().getAbsolutePath(), "--db-log-path", @@ -230,7 +230,7 @@ public void _order03_getHelixConfigTest() { spec(specSuccessResponse). body(containsString("maxFindTransactions")). body(containsString("maxRequestsList")). - body(containsString("maxBytes")). + body(containsString("maxTransactionStrings")). body(containsString("maxBodyLength")). body(containsString("testNet")). body(containsString("milestoneStartIndex")); @@ -319,10 +319,10 @@ public void findTransactionsTest() { } @Test - public void getHBytesTest() { + public void getTransactionStringsTest() { final Map request = new HashMap<>(); - request.put("command", "getHBytes"); + request.put("command", "getTransactionStrings"); request.put("hashes", HASHES); given(). @@ -331,7 +331,7 @@ public void getHBytesTest() { post("/"). then(). spec(specSuccessResponse). - body(containsString("hbytes")); + body(containsString("tx")); } //@Test @@ -394,7 +394,7 @@ public void broadcastTransactionsTest() { final Map request = new HashMap<>(); request.put("command", "broadcastTransactions"); - request.put("hbytes", HBYTES); + request.put("txs", TX_HEX); given(). body(gson().toJson(request)). @@ -410,7 +410,7 @@ public void storeTransactionsTest() { final Map request = new HashMap<>(); request.put("command", "storeTransactions"); - request.put("hbytes", HBYTES); + request.put("txs", TX_HEX); given(). body(gson().toJson(request)). @@ -426,7 +426,7 @@ public void attachToTangleTest() { final Map request = new HashMap<>(); request.put("command", "attachToTangle"); - request.put("hbytes", HBYTES); + request.put("txs", TX_HEX); request.put("trunkTransaction", NULL_HASH); request.put("branchTransaction", NULL_HASH); request.put("minWeightMagnitude", configuration.getMwm()); @@ -437,18 +437,18 @@ public void attachToTangleTest() { post("/"). then(). spec(specSuccessResponse). - body(containsString("hbytes")); + body(containsString("txs")); } - private List sendTransfer(String[] hbytesArray) { - return sendTransfer(hbytesArray, NULL_HASH, NULL_HASH); + private List sendTransfer(String[] txArray) { + return sendTransfer(txArray, NULL_HASH, NULL_HASH); } - private List sendTransfer(String[] hbytesArray, String branch, String trunk) { + private List sendTransfer(String[] txArray, String branch, String trunk) { //do PoW final Map request = new HashMap<>(); request.put("command", "attachToTangle"); - request.put("hbytes", hbytesArray); + request.put("txs", txArray); request.put("trunkTransaction", branch); request.put("branchTransaction", trunk); request.put("minWeightMagnitude", configuration.getMwm()); @@ -459,12 +459,12 @@ private List sendTransfer(String[] hbytesArray, String branch, String tr post("/"); response.getBody(); JsonPath responseJson = response.jsonPath(); - List hbytes = responseJson.getList("hbytes"); + List txs = responseJson.getList("txs"); //Store request.clear(); request.put("command", "storeTransactions"); - request.put("hbytes", hbytes); + request.put("txs", txs); given(). body(gson().toJson(request)). when(). @@ -472,7 +472,7 @@ private List sendTransfer(String[] hbytesArray, String branch, String tr then(). log().all().and().spec(specSuccessResponse); - return hbytes; + return txs; } private List findTransactions(String key, String[] values) { @@ -492,8 +492,8 @@ private List findTransactions(String key, String[] values) { @Test public void shouldSendTransactionAndFetchByAddressTest() { - List hbytes = sendTransfer(HBYTES); - String temp = (String) hbytes.get(0); + List txs = sendTransfer(TX_HEX); + String temp = (String) txs.get(0); String hash = getHash(temp); String[] addresses = {temp.substring(TransactionViewModel.ADDRESS_OFFSET * 2, @@ -505,8 +505,8 @@ public void shouldSendTransactionAndFetchByAddressTest() { //@Test //HAS TO BE FIXED: transactions can't be found by tag public void shouldSendTransactionAndFetchByTagTest() { - List hbytes = sendTransfer(HBYTES); - String temp = (String) hbytes.get(0); + List tx = sendTransfer(TX_HEX); + String temp = (String) tx.get(0); String hash = getHash(temp); //Tag From 24e868e3f87d09fead7b763b8303d1dba230a356 Mon Sep 17 00:00:00 2001 From: ofo42 Date: Fri, 19 Jul 2019 18:20:09 +0200 Subject: [PATCH 11/11] Added test for toString() to HashTest --- src/test/java/net/helix/hlx/model/HashTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/net/helix/hlx/model/HashTest.java b/src/test/java/net/helix/hlx/model/HashTest.java index 79702d4b..f5cf13a2 100644 --- a/src/test/java/net/helix/hlx/model/HashTest.java +++ b/src/test/java/net/helix/hlx/model/HashTest.java @@ -78,6 +78,14 @@ public void toHexStringTest() throws Exception { Assert.assertNotEquals(Hex.toHexString(hash.bytes()).length(), 0); } + @Test + public void toStringTest() throws Exception { + byte[] bytes = TransactionViewModelTest.getRandomTransactionBytes(); + Hash hash = TransactionHash.calculate(SpongeFactory.Mode.S256, bytes); + Assert.assertEquals(Hash.NULL_HASH.toString(), "0000000000000000000000000000000000000000000000000000000000000000"); + Assert.assertNotEquals(hash.toString(), "0000000000000000000000000000000000000000000000000000000000000000"); + Assert.assertNotEquals(hash.toString().length(), 0); + } @Test public void txBytesTest() throws Exception {