This is an automated email from the ASF dual-hosted git repository. jrhea pushed a commit to branch stevens-ssz in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
commit 27d83378df4963b770f55802c977092cac06205e Author: jonny rhea <[email protected]> AuthorDate: Mon May 27 10:44:13 2019 -0500 checking in steven's ssz code --- .../org/apache/tuweni/ssz/ByteBufferSSZWriter.java | 8 +- .../java/org/apache/tuweni/ssz/BytesSSZReader.java | 93 +++++- .../java/org/apache/tuweni/ssz/BytesSSZWriter.java | 8 +- .../org/apache/tuweni/ssz/EndOfSSZException.java | 8 +- .../apache/tuweni/ssz/InvalidSSZTypeException.java | 8 +- ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java | 54 +++- .../java/org/apache/tuweni/ssz/SSZException.java | 8 +- .../main/java/org/apache/tuweni/ssz/SSZReader.java | 135 +++++++- .../main/java/org/apache/tuweni/ssz/SSZWriter.java | 51 ++- .../java/org/apache/tuweni/ssz/package-info.java | 15 +- .../tuweni/ssz/experimental/BytesSSZReader.kt | 87 +++++ .../tuweni/ssz/experimental/BytesSSZWriter.kt | 117 +++++++ .../org/apache/tuweni/ssz/experimental/SSZ.kt | 187 +++++++++++ .../apache/tuweni/ssz/experimental/SSZReader.kt} | 319 +++++++------------ .../apache/tuweni/ssz/experimental/SSZWriter.kt} | 349 +++++++-------------- .../apache/tuweni/ssz/ByteBufferWriterTest.java | 8 +- .../org/apache/tuweni/ssz/BytesSSZReaderTest.java | 8 +- .../org/apache/tuweni/ssz/BytesSSZWriterTest.java | 8 +- .../org/apache/tuweni/ssz/HashTreeRootTest.java | 8 +- .../org/apache/tuweni/ssz/experimental/SSZTest.kt | 38 +++ 20 files changed, 1016 insertions(+), 501 deletions(-) diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/ByteBufferSSZWriter.java b/ssz/src/main/java/org/apache/tuweni/ssz/ByteBufferSSZWriter.java old mode 100644 new mode 100755 index 9540973..9de08a1 --- a/ssz/src/main/java/org/apache/tuweni/ssz/ByteBufferSSZWriter.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/ByteBufferSSZWriter.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZReader.java b/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZReader.java old mode 100644 new mode 100755 index 58fa3f5..d70dab5 --- a/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZReader.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZReader.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -57,6 +57,12 @@ final class BytesSSZReader implements SSZReader { } @Override + public Bytes readFixedBytes(int byteLength, int limit) { + ensureBytes(byteLength, () -> "SSZ encoded data is not a fixed-length byte array"); + return consumeBytes(byteLength); + } + + @Override public int readInt(int bitLength) { checkArgument(bitLength % 8 == 0, "bitLength must be a multiple of 8"); int byteLength = bitLength / 8; @@ -130,6 +136,22 @@ final class BytesSSZReader implements SSZReader { } @Override + public List<Bytes> readBytesList(long listSize, int limit) { + return readList(listSize, remaining -> readByteArray(limit), Bytes::wrap); + } + + @Override + public List<Bytes> readFixedBytesList(long listSize, int byteLength, int limit) { + checkArgument(listSize <= Integer.MAX_VALUE, "Cannot read bytes list: expected length is too large"); + return readFixedList(listSize, remaining -> readFixedByteArray(byteLength, limit), Bytes::wrap); + } + + @Override + public List<Bytes> readFixedBytesList(int byteLength, int limit) { + return readFixedList(remaining -> readFixedByteArray(byteLength, limit), Bytes::wrap); + } + + @Override public List<String> readStringList(int limit) { return readList(remaining -> readByteArray(limit), byteArray -> new String(byteArray, UTF_8)); } @@ -227,6 +249,69 @@ final class BytesSSZReader implements SSZReader { return elements; } + private <T> List<T> readList(long listSize, LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) { + int originalIndex = this.index; + List<T> elements; + try { + elements = new ArrayList<>(); + while (listSize > 0) { + byte[] bytes = bytesSupplier.apply(listSize); + elements.add(converter.apply(bytes)); + listSize -= bytes.length; + listSize -= 4; + if (listSize < 0) { + throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements"); + } + } + } catch (Exception e) { + this.index = originalIndex; + throw e; + } + return elements; + } + + private <T> List<T> readFixedList(LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) { + int originalIndex = this.index; + List<T> elements; + try { + // use a long to simulate reading unsigned + long listSize = consumeBytes(4).toLong(LITTLE_ENDIAN); + elements = new ArrayList<>(); + while (listSize > 0) { + byte[] bytes = bytesSupplier.apply(listSize); + elements.add(converter.apply(bytes)); + listSize -= bytes.length; + if (listSize < 0) { + throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements"); + } + } + } catch (Exception e) { + this.index = originalIndex; + throw e; + } + return elements; + } + + private <T> List<T> readFixedList(long listSize, LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) { + int originalIndex = this.index; + List<T> elements; + try { + elements = new ArrayList<>(); + while (listSize > 0) { + byte[] bytes = bytesSupplier.apply(listSize); + elements.add(converter.apply(bytes)); + listSize -= bytes.length; + if (listSize < 0) { + throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements"); + } + } + } catch (Exception e) { + this.index = originalIndex; + throw e; + } + return elements; + } + private <T> List<T> readList(int elementSize, Supplier<T> elementSupplier) { ensureBytes(4, () -> "SSZ encoded data is not a list"); int originalIndex = this.index; diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZWriter.java b/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZWriter.java old mode 100644 new mode 100755 index 4ccda97..397b358 --- a/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZWriter.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZWriter.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/EndOfSSZException.java b/ssz/src/main/java/org/apache/tuweni/ssz/EndOfSSZException.java old mode 100644 new mode 100755 index 51534d4..d40bbb8 --- a/ssz/src/main/java/org/apache/tuweni/ssz/EndOfSSZException.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/EndOfSSZException.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/InvalidSSZTypeException.java b/ssz/src/main/java/org/apache/tuweni/ssz/InvalidSSZTypeException.java old mode 100644 new mode 100755 index 5460cdb..c6fdc72 --- a/ssz/src/main/java/org/apache/tuweni/ssz/InvalidSSZTypeException.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/InvalidSSZTypeException.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java b/ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java old mode 100644 new mode 100755 index fd93aa5..eaff36d --- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -154,6 +154,11 @@ public final class SSZ { appender.accept(value); } + static void encodeFixedBytesTo(int byteLength, Bytes value, Consumer<Bytes> appender) { + checkArgument(byteLength == value.size(), "byteLength must be the same size as the value being encoded"); + appender.accept(value); + } + /** * Encode a value to a {@link Bytes} value. * @@ -542,6 +547,47 @@ public final class SSZ { } } + static void encodeBytesListTo(long listSize, List<? extends Bytes> elements, Consumer<Bytes> appender) { + checkArgument(listSize > 0, "Cannot serialize list: list size must be positive"); + if (listSize > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Cannot serialize list: overall length is too large"); + } + for (Bytes bytes : elements) { + encodeBytesTo(bytes, appender); + } + } + + static void encodeFixedBytesListTo(int byteLength, List<? extends Bytes> elements, Consumer<Bytes> appender) { + // pre-calculate the list size - relies on knowing how encodeBytesTo does its serialization, but is worth it + // to avoid having to pre-serialize all the elements + long listSize = 0; + for (Bytes bytes : elements) { + listSize += 4; + listSize += bytes.size(); + if (listSize > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Cannot serialize list: overall length is too large"); + } + } + appender.accept(encodeUInt32(listSize)); + for (Bytes bytes : elements) { + encodeFixedBytesTo(byteLength, bytes, appender); + } + } + + static void encodeFixedBytesListTo( + long listSize, + int byteLength, + List<? extends Bytes> elements, + Consumer<Bytes> appender) { + checkArgument(listSize > 0, "Cannot serialize list: list size must be positive"); + if (listSize > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Cannot serialize list: overall length is too large"); + } + for (Bytes bytes : elements) { + encodeFixedBytesTo(byteLength, bytes, appender); + } + } + /** * Encode a list of strings. * diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZException.java b/ssz/src/main/java/org/apache/tuweni/ssz/SSZException.java old mode 100644 new mode 100755 index 49d0656..bdc876f --- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZException.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/SSZException.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java b/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java old mode 100644 new mode 100755 index 436f77e..a0dc570 --- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -52,6 +52,32 @@ public interface SSZReader { Bytes readBytes(int limit); /** + * Read a known size fixed-length bytes value from the SSZ source. + * + * Note: prefer to use {@link #readFixedBytes(int, int)} instead, especially when reading untrusted data. + * + * @param byteLength The number of fixed-length Bytes (no length mixin) to read. + * @return The bytes for the next value. + * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or is too large (greater than 2^32 + * bytes). + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + default Bytes readFixedBytes(int byteLength) { + return readFixedBytes(byteLength, Integer.MAX_VALUE); + } + + /** + * Read a known size fixed-length bytes value from the SSZ source. + * + * @param byteLength The number of fixed-length Bytes (no length mixin) to read. + * @param limit The maximum number of bytes to read. + * @return The bytes for the next value. + * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or would exceed the limit. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + Bytes readFixedBytes(int byteLength, int limit); + + /** * Read a byte array from the SSZ source. * * Note: prefer to use {@link #readByteArray(int)} instead, especially when reading untrusted data. @@ -78,6 +104,19 @@ public interface SSZReader { } /** + * Read an array of fixed-length homogenous Bytes from the SSZ source. + * + * @param byteLength The number of fixed-length Bytes per array element. + * @param limit The maximum number of bytes to read. + * @return The byte array for the next value. + * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or would exceed the limit. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + default byte[] readFixedByteArray(int byteLength, int limit) { + return readFixedBytes(byteLength, limit).toArrayUnsafe(); + } + + /** * Read a string value from the SSZ source. * * Note: prefer to use {@link #readString(int)} instead, especially when reading untrusted data. @@ -331,6 +370,82 @@ public interface SSZReader { } /** + * Read a known-size fixed-length list of {@link Bytes} from the SSZ source. The list WILL NOT have a length mixin, + * where as the elements WILL. + * + * Note: prefer to use {@link #readBytesList(long, int)} instead, especially when reading untrusted data. + * + * @param listSize The size of the fixed-length list being read. + * @return A list of {@link Bytes}. + * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or + * any byte array is too large (greater than 2^32 bytes). + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + default List<Bytes> readBytesList(long listSize) { + return readBytesList(listSize, Integer.MAX_VALUE); + } + + /** + * Read a known-size fixed length list of known-size fixed length {@link Bytes} from the SSZ source. No length mixin + * is expected in either the list or the list elements. + * + * @param listSize The size of the fixed-length list being read. + * @param byteLength The number of fixed-length Bytes per homogenous List element. + * @param limit The maximum number of bytes to read for each list element. + * @return A list of {@link Bytes}. + * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or + * the size of any byte array would exceed the limit. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + List<Bytes> readFixedBytesList(long listSize, int byteLength, int limit); + + /** + * Read a known-size fixed length list of known-size fixed length {@link Bytes} from the SSZ source. No length mixin + * is expected in either the list or the list elements. + * + * Note: prefer to use {@link #readFixedBytesList(long, int, int)} instead, especially when reading untrusted data. + * + * @param listSize The size of the fixed-length list being read. + * @param byteLength The number of fixed-length Bytes per homogenous List element. + * @return A list of {@link Bytes}. + * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or + * any byte array is too large (greater than 2^32 bytes). + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + default List<Bytes> readFixedBytesList(long listSize, int byteLength) { + return readFixedBytesList(listSize, byteLength, Integer.MAX_VALUE); + } + + /** + * Read a list of known-size fixed length {@link Bytes} from the SSZ source. A length mixin IS expected for the list, + * but IS NOT expected for the list elements. + * + * @param byteLength The number of fixed-length Bytes per homogenous List element. + * @param limit The maximum number of bytes to read for each list element. + * @return A list of {@link Bytes}. + * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or + * the size of any byte array would exceed the limit. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + List<Bytes> readFixedBytesList(int byteLength, int limit); + + /** + * Read a list of known-size fixed length {@link Bytes} from the SSZ source. A length mixin IS expected for the list, + * but IS NOT expected for the list elements. + * + * Note: prefer to use {@link #readFixedBytesList(int, int)} instead, especially when reading untrusted data. + * + * @param byteLength The number of fixed-length Bytes per homogenous List element. + * @return A list of {@link Bytes}. + * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or + * any byte array is too large (greater than 2^32 bytes). + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + default List<Bytes> readFixedBytesList(int byteLength) { + return readFixedBytesList(byteLength, Integer.MAX_VALUE); + } + + /** * Read a list of {@link Bytes} from the SSZ source. * * @param limit The maximum number of bytes to read for each list element. @@ -342,6 +457,18 @@ public interface SSZReader { List<Bytes> readBytesList(int limit); /** + * Read a known-size fixed-length list of {@link Bytes} from the SSZ source. + * + * @param listSize The size of the fixed-length list being read. + * @param limit The maximum number of bytes to read for each list element. + * @return A list of {@link Bytes}. + * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or + * the size of any byte array would exceed the limit. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + List<Bytes> readBytesList(long listSize, int limit); + + /** * Read a list of byte arrays from the SSZ source. * * Note: prefer to use {@link #readByteArrayList(int)} instead, especially when reading untrusted data. diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java b/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java old mode 100644 new mode 100755 index bf5f9f9..b29fbd0 --- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -65,6 +65,17 @@ public interface SSZWriter { } /** + * Encode a known fixed-length {@link Bytes} value to SSZ without the length mixin. + * + * @param byteLength the number of byts to write + * @param value the byte array to encode + * @throws IllegalArgumentException if the byteLength is not the same size as value. + */ + default void writeFixedBytes(int byteLength, Bytes value) { + SSZ.encodeFixedBytesTo(byteLength, value, this::writeSSZ); + } + + /** * Write a string to the output. * * @param str the string to write @@ -286,6 +297,38 @@ public interface SSZWriter { } /** + * Write a known-size fixed-length list of bytes. The list itself WILL NOT have a length mixin, but the elements WILL. + * + * @param listSize the number of elements in the list + * @param elements the bytes to write as a list + */ + default void writeBytesList(long listSize, List<? extends Bytes> elements) { + SSZ.encodeBytesListTo(listSize, elements, this::writeSSZ); + } + + /** + * Write a list of known-size homogenous bytes. The list itself WILL have a length mixin, but the elements WILL NOT. + * + * @param byteLength the number of bytes in each element + * @param elements the known-size bytes to write as a list + */ + default void writeFixedBytesList(int byteLength, List<? extends Bytes> elements) { + SSZ.encodeFixedBytesListTo(byteLength, elements, this::writeSSZ); + } + + /** + * Write a known-size fixed-length list of known-size homogenous bytes. Neither the list nor the elements in the list + * will have a length mixin. + * + * @param listSize the number of elements in the list + * @param byteLength the number of bytes in each element + * @param elements the bytes to write as a list + */ + default void writeFixedBytesList(long listSize, int byteLength, List<? extends Bytes> elements) { + SSZ.encodeFixedBytesListTo(listSize, byteLength, elements, this::writeSSZ); + } + + /** * Write a list of strings, which must be of the same length * * @param elements the strings to write as a list diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/package-info.java b/ssz/src/main/java/org/apache/tuweni/ssz/package-info.java old mode 100644 new mode 100755 index 1a5c9b6..74c66a9 --- a/ssz/src/main/java/org/apache/tuweni/ssz/package-info.java +++ b/ssz/src/main/java/org/apache/tuweni/ssz/package-info.java @@ -1,22 +1,11 @@ /** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * * Simple Serialize (SSZ) encoding and decoding. * <p> * An implementation of the Ethereum Simple Serialize (SSZ) algorithm, as described at <a href= * "https://github.com/ethereum/eth2.0-specs/blob/master/specs/simple-serialize.md">https://github.com/ethereum/eth2.0-specs/blob/master/specs/simple-serialize.md</a>. * <p> - * These classes are included in the standard Tuweni distribution, or separately when using the gradle dependency - * 'org.apache.tuweni:tuweni-ssz' (tuweni-ssz.jar). + * These classes are included in the standard Cava distribution, or separately when using the gradle dependency + * 'org.apache.tuweni:cava-ssz' (cava-ssz.jar). */ @ParametersAreNonnullByDefault package org.apache.tuweni.ssz; diff --git a/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/BytesSSZReader.kt b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/BytesSSZReader.kt new file mode 100755 index 0000000..23a5310 --- /dev/null +++ b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/BytesSSZReader.kt @@ -0,0 +1,87 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuweni.ssz.experimental + +import org.apache.tuweni.bytes.Bytes +import org.apache.tuweni.units.bigints.UInt256 +import org.apache.tuweni.units.bigints.UInt384 +import java.math.BigInteger + +@ExperimentalUnsignedTypes +internal class BytesSSZReader(private val delegate: org.apache.tuweni.ssz.SSZReader) : SSZReader { + override fun readBytes(limit: Int): Bytes = + delegate.readBytes(limit) + + override fun readInt(bitLength: Int): Int = + delegate.readInt(bitLength) + + override fun readLong(bitLength: Int): Long = + delegate.readLong(bitLength) + + override fun readBigInteger(bitLength: Int): BigInteger = + delegate.readBigInteger(bitLength) + + override fun readUnsignedBigInteger(bitLength: Int): BigInteger = + delegate.readUnsignedBigInteger(bitLength) + + override fun readUInt256(): UInt256 = + delegate.readUInt256() + + override fun readUInt384(): UInt384 = + delegate.readUInt384() + + override fun readAddress(): Bytes = + delegate.readAddress() + + override fun readHash(hashLength: Int): Bytes = + delegate.readHash(hashLength) + + override fun readBytesList(limit: Int): List<Bytes> = + delegate.readBytesList(limit) + + override fun readByteArrayList(limit: Int): List<ByteArray> = + delegate.readByteArrayList(limit) + + override fun readStringList(limit: Int): List<String> = + delegate.readStringList(limit) + + override fun readIntList(bitLength: Int): List<Int> = + delegate.readIntList(bitLength) + + override fun readLongIntList(bitLength: Int): List<Long> = + delegate.readLongIntList(bitLength) + + override fun readBigIntegerList(bitLength: Int): List<BigInteger> = + delegate.readBigIntegerList(bitLength) + + override fun readUnsignedBigIntegerList(bitLength: Int): List<BigInteger> = + delegate.readUnsignedBigIntegerList(bitLength) + + override fun readUInt256List(): List<UInt256> = + delegate.readUInt256List() + + override fun readUInt384List(): List<UInt384> = + delegate.readUInt384List() + + override fun readAddressList(): List<Bytes> = + delegate.readAddressList() + + override fun readHashList(hashLength: Int): List<Bytes> = + delegate.readHashList(hashLength) + + override fun readBooleanList(): List<Boolean> = + delegate.readBooleanList() + + override val isComplete: Boolean + get() = delegate.isComplete +} diff --git a/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/BytesSSZWriter.kt b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/BytesSSZWriter.kt new file mode 100755 index 0000000..218786c --- /dev/null +++ b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/BytesSSZWriter.kt @@ -0,0 +1,117 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuweni.ssz.experimental + +import org.apache.tuweni.bytes.Bytes +import org.apache.tuweni.units.bigints.UInt256 +import org.apache.tuweni.units.bigints.UInt384 +import java.math.BigInteger + +@ExperimentalUnsignedTypes +internal class BytesSSZWriter(private val delegate: org.apache.tuweni.ssz.SSZWriter) : SSZWriter { + override fun writeSSZ(value: Bytes) = + delegate.writeSSZ(value) + + override fun writeBytes(value: Bytes) = + delegate.writeBytes(value) + + override fun writeBytes(value: ByteArray) = + delegate.writeBytes(value) + + override fun writeString(str: String) = + delegate.writeString(str) + + override fun writeInt(value: Int, bitLength: Int) = + delegate.writeInt(value, bitLength) + + override fun writeLong(value: Long, bitLength: Int) = + delegate.writeLong(value, bitLength) + + override fun writeUInt(value: UInt, bitLength: Int) = + delegate.writeUInt(value.toInt(), bitLength) + + override fun writeULong(value: ULong, bitLength: Int) = + delegate.writeULong(value.toLong(), bitLength) + + override fun writeBytesList(vararg elements: Bytes) = + delegate.writeBytesList(*elements) + + override fun writeBytesList(elements: List<Bytes>) = + delegate.writeBytesList(elements) + + override fun writeStringList(vararg elements: String) = + delegate.writeStringList(*elements) + + override fun writeStringList(elements: List<String>) = + delegate.writeStringList(elements) + + override fun writeIntList(bitLength: Int, vararg elements: Int) = + delegate.writeIntList(bitLength, *elements) + + override fun writeIntList(bitLength: Int, elements: List<Int>) = + delegate.writeIntList(bitLength, elements) + + override fun writeLongIntList(bitLength: Int, vararg elements: Long) = + delegate.writeLongIntList(bitLength, *elements) + + override fun writeLongIntList(bitLength: Int, elements: List<Long>) = + delegate.writeLongIntList(bitLength, elements) + + override fun writeBigIntegerList(bitLength: Int, vararg elements: BigInteger) = + delegate.writeBigIntegerList(bitLength, *elements) + + override fun writeBigIntegerList(bitLength: Int, elements: List<BigInteger>) = + delegate.writeBigIntegerList(bitLength, elements) + + override fun writeUIntList(bitLength: Int, vararg elements: UInt) = + delegate.writeUIntList(bitLength, elements.map { i -> i.toInt() }) + + override fun writeUIntList(bitLength: Int, elements: List<UInt>) = + delegate.writeUIntList(bitLength, elements.map { i -> i.toInt() }) + + override fun writeULongIntList(bitLength: Int, vararg elements: ULong) = + delegate.writeULongIntList(bitLength, elements.map { i -> i.toLong() }) + + override fun writeULongIntList(bitLength: Int, elements: List<ULong>) = + delegate.writeULongIntList(bitLength, elements.map { i -> i.toLong() }) + + override fun writeUInt256List(vararg elements: UInt256) = + delegate.writeUInt256List(*elements) + + override fun writeUInt256List(elements: List<UInt256>) = + delegate.writeUInt256List(elements) + + override fun writeUInt384List(vararg elements: UInt384) = + delegate.writeUInt384List(*elements) + + override fun writeUInt384List(elements: List<UInt384>) = + delegate.writeUInt384List(elements) + + override fun writeHashList(vararg elements: Bytes) = + delegate.writeHashList(*elements) + + override fun writeHashList(elements: List<Bytes>) = + delegate.writeHashList(elements) + + override fun writeAddressList(vararg elements: Bytes) = + delegate.writeAddressList(*elements) + + override fun writeAddressList(elements: List<Bytes>) = + delegate.writeAddressList(elements) + + override fun writeBooleanList(vararg elements: Boolean) = + delegate.writeBooleanList(*elements) + + override fun writeBooleanList(elements: List<Boolean>) = + delegate.writeBooleanList(elements) +} diff --git a/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZ.kt b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZ.kt new file mode 100755 index 0000000..28ef22d --- /dev/null +++ b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZ.kt @@ -0,0 +1,187 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuweni.ssz.experimental + +import org.apache.tuweni.bytes.Bytes +import org.apache.tuweni.ssz.EndOfSSZException +import org.apache.tuweni.ssz.InvalidSSZTypeException +import java.nio.BufferOverflowException +import java.nio.ByteBuffer +import java.nio.ReadOnlyBufferException + +/** + * Simple Serialize (SSZ) encoding and decoding. + */ +@ExperimentalUnsignedTypes +object SSZ { + + // Encoding + + /** + * Encode values to a {@link Bytes} value. + * + * @param fn A consumer that will be provided with a {@link SSZWriter} that can consume values. + * @return The SSZ encoding in a {@link Bytes} value. + */ + fun encode(fn: (SSZWriter) -> Unit): Bytes = org.apache.tuweni.ssz.SSZ.encode { w -> fn(BytesSSZWriter(w)) } + + /** + * Encode values to a {@link ByteBuffer}. + * + * @param buffer The buffer to write into, starting from its current position. + * @param fn A consumer that will be provided with a {@link SSZWriter} that can consume values. + * @param <T> The type of the buffer. + * @return The buffer. + * @throws BufferOverflowException If the writer attempts to write more than the provided buffer can hold. + * @throws ReadOnlyBufferException If the provided buffer is read-only. + */ + fun <T : ByteBuffer> encodeTo(buffer: T, fn: (SSZWriter) -> Unit): T = + org.apache.tuweni.ssz.SSZ.encodeTo(buffer) { w -> fn(BytesSSZWriter(w)) } + + /** + * Encode an unsigned integer to a {@link Bytes} value. + * + * @param value The integer to encode. + * @param bitLength The bit length of the encoded integer value (must be a multiple of 8). + * @return The SSZ encoding in a {@link Bytes} value. + * @throws IllegalArgumentException If the value is too large for the specified {@code bitLength}. + */ + fun encodeUInt(value: UInt, bitLength: Int): Bytes = + org.apache.tuweni.ssz.SSZ.encodeULong(value.toLong(), bitLength) + + /** + * Encode an unsigned long integer to a {@link Bytes} value. + * + * @param value The long to encode. + * @param bitLength The bit length of the integer value (must be a multiple of 8). + * @return The SSZ encoding in a {@link Bytes} value. + * @throws IllegalArgumentException If the value is too large for the specified {@code bitLength}. + */ + fun encodeULong(value: ULong, bitLength: Int): Bytes = + org.apache.tuweni.ssz.SSZ.encodeULong(value.toLong(), bitLength) + + /** + * Encode an 8-bit unsigned integer to a {@link Bytes} value. + * + * @param value The integer to encode. + * @return The SSZ encoding in a {@link Bytes} value. + * @throws IllegalArgumentException If the value is too large for the specified {@code bitLength}. + */ + fun encodeUInt8(value: UInt): Bytes = encodeUInt(value, 8) + + /** + * Encode a 16-bit unsigned integer to a {@link Bytes} value. + * + * @param value The integer to encode. + * @return The SSZ encoding in a {@link Bytes} value. + * @throws IllegalArgumentException If the value is too large for the specified {@code bitLength}. + */ + fun encodeUInt16(value: UInt): Bytes = encodeUInt(value, 16) + + /** + * Encode a 32-bit unsigned integer to a {@link Bytes} value. + * + * @param value The integer to encode. + * @return The SSZ encoding in a {@link Bytes} value. + * @throws IllegalArgumentException If the value is too large for the specified {@code bitLength}. + */ + fun encodeUInt32(value: UInt): Bytes = encodeUInt(value, 32) + + /** + * Encode a 64-bit unsigned integer to a {@link Bytes} value. + * + * @param value The integer to encode. + * @return The SSZ encoding in a {@link Bytes} value. + * @throws IllegalArgumentException If the value is too large for the specified {@code bitLength}. + */ + fun encodeUInt64(value: ULong): Bytes = encodeULong(value, 64) + + // Decoding + + /** + * Read and decode SSZ from a {@link Bytes} value. + * + * @param source The SSZ encoded bytes. + * @param fn A function that will be provided a {@link SSZReader}. + * @param <T> The result type of the reading function. + * @return The result from the reading function. + */ + fun <T> decode(source: Bytes, fn: (SSZReader) -> T): T = + org.apache.tuweni.ssz.SSZ.decode(source) { r -> fn(BytesSSZReader(r)) } + + /** + * Read a SSZ encoded unsigned integer from a {@link Bytes} value. + * + * @param source The SSZ encoded bytes. + * @param bitLength The bit length of the integer to read (a multiple of 8). + * @return An unsigned int. + * @throws InvalidSSZTypeException If there are insufficient encoded bytes for the desired bit length, or the decoded + * value was too large to fit into an int. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + fun decodeUInt(source: Bytes, bitLength: Int): UInt = + org.apache.tuweni.ssz.SSZ.decodeUInt(source, bitLength).toUInt() + + /** + * Read a SSZ encoded unsigned long integer from a {@link Bytes} value. + * + * @param source The SSZ encoded bytes. + * @param bitLength The bit length of the integer to read (a multiple of 8). + * @return An unsigned long. + * @throws InvalidSSZTypeException If there are insufficient encoded bytes for the desired bit length, or the decoded + * value was too large to fit into a long. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + fun decodeULong(source: Bytes, bitLength: Int): ULong = + org.apache.tuweni.ssz.SSZ.decodeULong(source, bitLength).toULong() + + /** + * Read an 8-bit unsigned integer from the SSZ source. + * + * @param source The SSZ encoded bytes. + * @return An unsigned int. + * @throws InvalidSSZTypeException If there are insufficient encoded bytes for an 8-bit int. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + fun decodeUInt8(source: Bytes) = decodeUInt(source, 8) + + /** + * Read a 16-bit unsigned integer from the SSZ source. + * + * @param source The SSZ encoded bytes. + * @return An unsigned int. + * @throws InvalidSSZTypeException If there are insufficient encoded bytes for an 8-bit int. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + fun decodeUInt16(source: Bytes) = decodeUInt(source, 16) + + /** + * Read a 32-bit unsigned integer from the SSZ source. + * + * @param source The SSZ encoded bytes. + * @return An unsigned int. + * @throws InvalidSSZTypeException If there are insufficient encoded bytes for an 8-bit int. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + fun decodeUInt32(source: Bytes) = decodeUInt(source, 32) + + /** + * Read a 64-bit unsigned integer from the SSZ source. + * + * @param source The SSZ encoded bytes. + * @return An unsigned long. + * @throws InvalidSSZTypeException If there are insufficient encoded bytes for an 8-bit int. + * @throws EndOfSSZException If there are no more SSZ values to read. + */ + fun decodeUInt64(source: Bytes) = decodeULong(source, 64) +} diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZReader.kt old mode 100644 new mode 100755 similarity index 64% copy from ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java copy to ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZReader.kt index 436f77e..7c0954b --- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java +++ b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZReader.kt @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -10,36 +10,22 @@ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package org.apache.tuweni.ssz; - -import static java.nio.charset.StandardCharsets.UTF_8; +package org.apache.tuweni.ssz.experimental -import org.apache.tuweni.bytes.Bytes; -import org.apache.tuweni.units.bigints.UInt256; -import org.apache.tuweni.units.bigints.UInt384; - -import java.math.BigInteger; -import java.util.List; -import java.util.stream.Collectors; +import org.apache.tuweni.bytes.Bytes +import org.apache.tuweni.ssz.EndOfSSZException +import org.apache.tuweni.ssz.InvalidSSZTypeException +import org.apache.tuweni.units.bigints.UInt256 +import org.apache.tuweni.units.bigints.UInt384 +import java.math.BigInteger +import java.nio.charset.StandardCharsets.UTF_8 /** * A reader for consuming values from an SSZ encoded source. */ -public interface SSZReader { - - /** - * Read bytes from the SSZ source. - * - * Note: prefer to use {@link #readBytes(int)} instead, especially when reading untrusted data. - * - * @return The bytes for the next value. - * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or is too large (greater than 2^32 - * bytes). - * @throws EndOfSSZException If there are no more SSZ values to read. - */ - default Bytes readBytes() { - return readBytes(Integer.MAX_VALUE); - } +// Does not extend org.apache.tuweni.ssz.SSZReader (unlike SSZWriter) as the return types vary for the UInt methods. +@ExperimentalUnsignedTypes +interface SSZReader { /** * Read bytes from the SSZ source. @@ -49,21 +35,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or would exceed the limit. * @throws EndOfSSZException If there are no more SSZ values to read. */ - Bytes readBytes(int limit); - - /** - * Read a byte array from the SSZ source. - * - * Note: prefer to use {@link #readByteArray(int)} instead, especially when reading untrusted data. - * - * @return The byte array for the next value. - * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or is too large (greater than 2^32 - * bytes). - * @throws EndOfSSZException If there are no more SSZ values to read. - */ - default byte[] readByteArray() { - return readByteArray(Integer.MAX_VALUE); - } + fun readBytes(limit: Int): Bytes /** * Read a byte array from the SSZ source. @@ -73,23 +45,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or would exceed the limit. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default byte[] readByteArray(int limit) { - return readBytes(limit).toArrayUnsafe(); - } - - /** - * Read a string value from the SSZ source. - * - * Note: prefer to use {@link #readString(int)} instead, especially when reading untrusted data. - * - * @return A string. - * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or is too large (greater than 2^32 - * bytes). - * @throws EndOfSSZException If there are no more SSZ values to read. - */ - default String readString() { - return new String(readByteArray(), UTF_8); - } + fun readByteArray(limit: Int = Integer.MAX_VALUE): ByteArray = readBytes(limit).toArrayUnsafe() /** * Read a string value from the SSZ source. @@ -99,9 +55,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If the next SSZ value is not a byte array, or would exceed the limit. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default String readString(int limit) { - return new String(readByteArray(limit), UTF_8); - } + fun readString(limit: Int): String = String(readByteArray(limit), UTF_8) /** * Read a two's-compliment int value from the SSZ source. @@ -109,10 +63,10 @@ public interface SSZReader { * @param bitLength The bit length of the integer to read (a multiple of 8). * @return An int. * @throws InvalidSSZTypeException If there are insufficient encoded bytes for the desired bit length, or the decoded - * value was too large to fit into an int. + * value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - int readInt(int bitLength); + fun readInt(bitLength: Int): Int /** * Read a two's-compliment long value from the SSZ source. @@ -120,10 +74,10 @@ public interface SSZReader { * @param bitLength The bit length of the integer to read (a multiple of 8). * @return A long. * @throws InvalidSSZTypeException If there are insufficient encoded bytes for the desired bit length, or the decoded - * value was too large to fit into a long. + * value was too large to fit into a long. * @throws EndOfSSZException If there are no more SSZ values to read. */ - long readLong(int bitLength); + fun readLong(bitLength: Int): Long /** * Read a big integer value from the SSZ source. @@ -133,7 +87,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for the desired bit length. * @throws EndOfSSZException If there are no more SSZ values to read. */ - BigInteger readBigInteger(int bitLength); + fun readBigInteger(bitLength: Int): BigInteger /** * Read an 8-bit two's-compliment integer from the SSZ source. @@ -142,9 +96,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for an 8-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default int readInt8() { - return readInt(8); - } + fun readInt8(): Int = readInt(8) /** * Read a 16-bit two's-compliment integer from the SSZ source. @@ -153,9 +105,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 16-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default int readInt16() { - return readInt(16); - } + fun readInt16(): Int = readInt(16) /** * Read a 32-bit two's-compliment integer from the SSZ source. @@ -164,9 +114,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 32-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default int readInt32() { - return readInt(32); - } + fun readInt32(): Int = readInt(32) /** * Read a 64-bit two's-compliment integer from the SSZ source. @@ -175,9 +123,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 64-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default long readInt64() { - return readLong(64); - } + fun readInt64(): Long = readLong(64) /** * Read an unsigned int value from the SSZ source. @@ -185,13 +131,10 @@ public interface SSZReader { * @param bitLength The bit length of the integer to read (a multiple of 8). * @return An unsigned int. * @throws InvalidSSZTypeException If there are insufficient encoded bytes for the desired bit length, or the decoded - * value was too large to fit into an int. + * value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default int readUInt(int bitLength) { - // encoding is the same for unsigned - return readInt(bitLength); - } + fun readUInt(bitLength: Int): UInt = readInt(bitLength).toUInt() /** * Read an unsigned long value from the SSZ source. @@ -199,13 +142,10 @@ public interface SSZReader { * @param bitLength The bit length of the integer to read (a multiple of 8). * @return An unsigned long. * @throws InvalidSSZTypeException If there are insufficient encoded bytes for the desired bit length, or the decoded - * value was too large to fit into a long. + * value was too large to fit into a long. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default long readULong(int bitLength) { - // encoding is the same for unsigned - return readLong(bitLength); - } + fun readULong(bitLength: Int): ULong = readLong(bitLength).toULong() /** * Read an unsigned big integer value from the SSZ source. @@ -215,7 +155,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for the desired bit length. * @throws EndOfSSZException If there are no more SSZ values to read. */ - BigInteger readUnsignedBigInteger(int bitLength); + fun readUnsignedBigInteger(bitLength: Int): BigInteger /** * Read an 8-bit unsigned integer from the SSZ source. @@ -224,9 +164,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for an 8-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default int readUInt8() { - return readUInt(8); - } + fun readUInt8(): UInt = readUInt(8) /** * Read a 16-bit unsigned integer from the SSZ source. @@ -235,9 +173,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 16-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default int readUInt16() { - return readUInt(16); - } + fun readUInt16(): UInt = readUInt(16) /** * Read a 32-bit unsigned integer from the SSZ source. @@ -246,9 +182,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 32-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default long readUInt32() { - return readULong(32); - } + fun readUInt32(): ULong = readULong(32) /** * Read a 64-bit unsigned integer from the SSZ source. @@ -257,27 +191,25 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 64-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default long readUInt64() { - return readULong(64); - } + fun readUInt64(): ULong = readULong(64) /** - * Read a {@link UInt256} from the SSZ source. + * Read a [UInt256] from the SSZ source. * - * @return A {@link UInt256}. + * @return A [UInt256]. * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 256-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - UInt256 readUInt256(); + fun readUInt256(): UInt256 /** - * Read a {@link UInt384} from the SSZ source. + * Read a [UInt384] from the SSZ source. * - * @return A {@link UInt384}. + * @return A [UInt384]. * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 384-bit int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - UInt384 readUInt384(); + fun readUInt384(): UInt384 /** * Read a boolean from the SSZ source. @@ -286,15 +218,10 @@ public interface SSZReader { * @throws InvalidSSZTypeException If the decoded value is not a boolean. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default boolean readBoolean() { - int value = readInt(8); - if (value == 0) { - return false; - } else if (value == 1) { - return true; - } else { - throw new InvalidSSZTypeException("decoded value is not a boolean"); - } + fun readBoolean(): Boolean = when (readInt(8)) { + 0 -> false + 1 -> true + else -> throw InvalidSSZTypeException("decoded value is not a boolean") } /** @@ -304,7 +231,7 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 20-byte address. * @throws EndOfSSZException If there are no more SSZ values to read. */ - Bytes readAddress(); + fun readAddress(): Bytes /** * Read a hash from the SSZ source. @@ -314,46 +241,42 @@ public interface SSZReader { * @throws InvalidSSZTypeException If there are insufficient encoded bytes for a 32-byte hash. * @throws EndOfSSZException If there are no more SSZ values to read. */ - Bytes readHash(int hashLength); + fun readHash(hashLength: Int): Bytes /** - * Read a list of {@link Bytes} from the SSZ source. + * Read a list of [Bytes] from the SSZ source. * - * Note: prefer to use {@link #readBytesList(int)} instead, especially when reading untrusted data. + * Note: prefer to use [.readBytesList] instead, especially when reading untrusted data. * - * @return A list of {@link Bytes}. + * @return A list of [Bytes]. * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or - * any byte array is too large (greater than 2^32 bytes). + * any byte array is too large (greater than 2^32 bytes). * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Bytes> readBytesList() { - return readBytesList(Integer.MAX_VALUE); - } + fun readBytesList(): List<Bytes> = readBytesList(Integer.MAX_VALUE) /** - * Read a list of {@link Bytes} from the SSZ source. + * Read a list of [Bytes] from the SSZ source. * * @param limit The maximum number of bytes to read for each list element. - * @return A list of {@link Bytes}. + * @return A list of [Bytes]. * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or - * the size of any byte array would exceed the limit. + * the size of any byte array would exceed the limit. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<Bytes> readBytesList(int limit); + fun readBytesList(limit: Int): List<Bytes> /** * Read a list of byte arrays from the SSZ source. * - * Note: prefer to use {@link #readByteArrayList(int)} instead, especially when reading untrusted data. + * Note: prefer to use [.readByteArrayList] instead, especially when reading untrusted data. * * @return A list of byte arrays. * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or - * any byte array is too large (greater than 2^32 bytes). + * any byte array is too large (greater than 2^32 bytes). * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<byte[]> readByteArrayList() { - return readByteArrayList(Integer.MAX_VALUE); - } + fun readByteArrayList(): List<ByteArray> = readByteArrayList(Integer.MAX_VALUE) /** * Read a list of byte arrays from the SSZ source. @@ -361,26 +284,22 @@ public interface SSZReader { * @param limit The maximum number of bytes to read for each list element. * @return A list of byte arrays. * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a byte array, or - * the size of any byte array would exceed the limit. + * the size of any byte array would exceed the limit. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<byte[]> readByteArrayList(int limit) { - return readBytesList(limit).stream().map(Bytes::toArrayUnsafe).collect(Collectors.toList()); - } + fun readByteArrayList(limit: Int): List<ByteArray> /** * Read a list of strings from the SSZ source. * - * Note: prefer to use {@link #readStringList(int)} instead, especially when reading untrusted data. + * Note: prefer to use [.readStringList] instead, especially when reading untrusted data. * * @return A list of strings. * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a string, or any - * string is too large (greater than 2^32 bytes). + * string is too large (greater than 2^32 bytes). * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<String> readStringList() { - return readStringList(Integer.MAX_VALUE); - } + fun readStringList(): List<String> = readStringList(Integer.MAX_VALUE) /** * Read a list of strings from the SSZ source. @@ -388,10 +307,10 @@ public interface SSZReader { * @param limit The maximum number of bytes to read for each list element. * @return A list of strings. * @throws InvalidSSZTypeException If the next SSZ value is not a list, any value in the list is not a string, or the - * size of any string would exceed the limit. + * size of any string would exceed the limit. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<String> readStringList(int limit); + fun readStringList(limit: Int): List<String> /** * Read a list of two's-compliment int values from the SSZ source. @@ -399,10 +318,10 @@ public interface SSZReader { * @param bitLength The bit length of the integers to read (a multiple of 8). * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<Integer> readIntList(int bitLength); + fun readIntList(bitLength: Int): List<Int> /** * Read a list of two's-compliment long int values from the SSZ source. @@ -410,10 +329,10 @@ public interface SSZReader { * @param bitLength The bit length of the integers to read (a multiple of 8). * @return A list of longs. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into a long. + * desired bit length or any value in the list, or any decoded value was too large to fit into a long. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<Long> readLongIntList(int bitLength); + fun readLongIntList(bitLength: Int): List<Long> /** * Read a list of two's-compliment big integer values from the SSZ source. @@ -421,58 +340,50 @@ public interface SSZReader { * @param bitLength The bit length of the integers to read (a multiple of 8). * @return A list of big integers. * @throws InvalidSSZTypeException If the next SSZ value is not a list, or there are insufficient encoded bytes for - * the desired bit length or any value in the list. + * the desired bit length or any value in the list. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<BigInteger> readBigIntegerList(int bitLength); + fun readBigIntegerList(bitLength: Int): List<BigInteger> /** * Read a list of 8-bit two's-compliment int values from the SSZ source. * * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Integer> readInt8List() { - return readIntList(8); - } + fun readInt8List(): List<Int> = readIntList(8) /** * Read a list of 16-bit two's-compliment int values from the SSZ source. * * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Integer> readInt16List() { - return readIntList(16); - } + fun readInt16List(): List<Int> = readIntList(16) /** * Read a list of 32-bit two's-compliment int values from the SSZ source. * * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Integer> readInt32List() { - return readIntList(32); - } + fun readInt32List(): List<Int> = readIntList(32) /** * Read a list of 64-bit two's-compliment int values from the SSZ source. * * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Long> readInt64List() { - return readLongIntList(64); - } + fun readInt64List(): List<Long> = readLongIntList(64) /** * Read a list of unsigned int values from the SSZ source. @@ -480,12 +391,12 @@ public interface SSZReader { * @param bitLength The bit length of the integers to read (a multiple of 8). * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Integer> readUIntList(int bitLength) { + fun readUIntList(bitLength: Int): List<UInt> { // encoding is the same for unsigned - return readIntList(bitLength); + return readIntList(bitLength).map { i -> i.toUInt() } } /** @@ -494,12 +405,12 @@ public interface SSZReader { * @param bitLength The bit length of the integers to read (a multiple of 8). * @return A list of longs. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into a long. + * desired bit length or any value in the list, or any decoded value was too large to fit into a long. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Long> readULongIntList(int bitLength) { + fun readULongIntList(bitLength: Int): List<ULong> { // encoding is the same for unsigned - return readLongIntList(bitLength); + return readLongIntList(bitLength).map { i -> i.toULong() } } /** @@ -508,88 +419,80 @@ public interface SSZReader { * @param bitLength The bit length of the integers to read (a multiple of 8). * @return A list of big integers. * @throws InvalidSSZTypeException If the next SSZ value is not a list, or there are insufficient encoded bytes for - * the desired bit length or any value in the list. + * the desired bit length or any value in the list. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<BigInteger> readUnsignedBigIntegerList(int bitLength); + fun readUnsignedBigIntegerList(bitLength: Int): List<BigInteger> /** * Read a list of 8-bit unsigned int values from the SSZ source. * * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Integer> readUInt8List() { - return readUIntList(8); - } + fun readUInt8List(): List<UInt> = readUIntList(8) /** * Read a list of 16-bit unsigned int values from the SSZ source. * * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Integer> readUInt16List() { - return readUIntList(16); - } + fun readUInt16List(): List<UInt> = readUIntList(16) /** * Read a list of 32-bit unsigned int values from the SSZ source. * * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Long> readUInt32List() { - return readULongIntList(32); - } + fun readUInt32List(): List<ULong> = readULongIntList(32) /** * Read a list of 64-bit unsigned int values from the SSZ source. * * @return A list of ints. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - default List<Long> readUInt64List() { - return readULongIntList(64); - } + fun readUInt64List(): List<ULong> = readULongIntList(64) /** * Read a list of 256-bit unsigned int values from the SSZ source. * - * @return A list of {@link UInt256}. + * @return A list of [UInt256]. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<UInt256> readUInt256List(); + fun readUInt256List(): List<UInt256> /** * Read a list of 384-bit unsigned int values from the SSZ source. * - * @return A list of {@link UInt384}. + * @return A list of [UInt256]. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for the - * desired bit length or any value in the list, or any decoded value was too large to fit into an int. + * desired bit length or any value in the list, or any decoded value was too large to fit into an int. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<UInt384> readUInt384List(); + fun readUInt384List(): List<UInt384> /** * Read a list of 20-byte addresses from the SSZ source. * * @return A list of 20-byte addresses. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for any - * address in the list. + * address in the list. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<Bytes> readAddressList(); + fun readAddressList(): List<Bytes> /** * Read a list of hashes from the SSZ source. @@ -597,25 +500,25 @@ public interface SSZReader { * @param hashLength The length of the hash (in bytes). * @return A list of 32-byte hashes. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for any - * hash in the list. + * hash in the list. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<Bytes> readHashList(int hashLength); + fun readHashList(hashLength: Int): List<Bytes> /** * Read a list of booleans from the SSZ source. * * @return A list of booleans. * @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for all - * the booleans in the list. + * the booleans in the list. * @throws EndOfSSZException If there are no more SSZ values to read. */ - List<Boolean> readBooleanList(); + fun readBooleanList(): List<Boolean> /** * Check if all values have been read. * - * @return {@code true} if all values have been read. + * @return `true` if all values have been read. */ - boolean isComplete(); + val isComplete: Boolean } diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZWriter.kt old mode 100644 new mode 100755 similarity index 51% copy from ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java copy to ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZWriter.kt index bf5f9f9..11b9e9c --- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java +++ b/ssz/src/main/kotlin/org/apache/tuweni/ssz/experimental/SSZWriter.kt @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -10,68 +10,57 @@ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package org.apache.tuweni.ssz; - -import org.apache.tuweni.bytes.Bytes; -import org.apache.tuweni.units.bigints.UInt256; -import org.apache.tuweni.units.bigints.UInt384; +package org.apache.tuweni.ssz.experimental -import java.math.BigInteger; -import java.util.List; +import org.apache.tuweni.bytes.Bytes +import org.apache.tuweni.ssz.SSZ +import org.apache.tuweni.units.bigints.UInt256 +import org.apache.tuweni.units.bigints.UInt384 +import java.math.BigInteger -/** - * A writer for encoding values to SSZ. - */ -public interface SSZWriter { +@ExperimentalUnsignedTypes +interface SSZWriter { /** * Append an already SSZ encoded value. * - * Note that this method <b>may not</b> validate that {@code value} is a valid SSZ sequence. Appending an invalid SSZ + * Note that this method **may not** validate that `value` is a valid SSZ sequence. Appending an invalid SSZ * sequence will cause the entire SSZ encoding produced by this writer to also be invalid. * * @param value the SSZ encoded bytes to append */ - void writeSSZ(Bytes value); + fun writeSSZ(value: Bytes) /** * Append an already SSZ encoded value. * - * Note that this method <b>may not</b> validate that {@code value} is a valid SSZ sequence. Appending an invalid SSZ + * Note that this method **may not** validate that `value` is a valid SSZ sequence. Appending an invalid SSZ * sequence will cause the entire SSZ encoding produced by this writer to also be invalid. * * @param value the SSZ encoded bytes to append */ - default void writeSSZ(byte[] value) { - writeSSZ(Bytes.wrap(value)); - } + fun writeSSZ(value: ByteArray) = writeSSZ(Bytes.wrap(value)) /** - * Encode a {@link Bytes} value to SSZ. + * Encode a [Bytes] value to SSZ. * * @param value the byte array to encode */ - default void writeBytes(Bytes value) { - SSZ.encodeBytesTo(value, this::writeSSZ); - } + fun writeBytes(value: Bytes) /** * Encode a byte array to SSZ. * * @param value the byte array to encode */ - default void writeBytes(byte[] value) { - SSZ.encodeByteArrayTo(value, this::writeSSZ); - } + fun writeBytes(value: ByteArray) /** * Write a string to the output. * * @param str the string to write */ - default void writeString(String str) { - SSZ.encodeStringTo(str, this::writeSSZ); - } + fun writeString(str: String) /** * Write a two's-compliment integer to the output. @@ -80,9 +69,7 @@ public interface SSZWriter { * @param bitLength the bit length of the integer value * @throws IllegalArgumentException if the value is too large for the specified bit length */ - default void writeInt(int value, int bitLength) { - writeSSZ(SSZ.encodeLongToByteArray(value, bitLength)); - } + fun writeInt(value: Int, bitLength: Int) /** * Write a two's-compliment long to the output. @@ -91,9 +78,7 @@ public interface SSZWriter { * @param bitLength the bit length of the integer value * @throws IllegalArgumentException if the value is too large for the specified bit length */ - default void writeLong(long value, int bitLength) { - writeSSZ(SSZ.encodeLongToByteArray(value, bitLength)); - } + fun writeLong(value: Long, bitLength: Int) /** * Write a big integer to the output. @@ -102,8 +87,8 @@ public interface SSZWriter { * @param bitLength the bit length of the integer value * @throws IllegalArgumentException if the value is too large for the specified bit length */ - default void writeBigInteger(BigInteger value, int bitLength) { - writeSSZ(SSZ.encodeBigIntegerToByteArray(value, bitLength)); + fun writeBigInteger(value: BigInteger, bitLength: Int) { + writeSSZ(SSZ.encodeBigIntegerToByteArray(value, bitLength)) } /** @@ -112,18 +97,18 @@ public interface SSZWriter { * @param value the integer to write * @throws IllegalArgumentException if the value is too large to be represented in 8 bits */ - default void writeInt8(int value) { - writeInt(value, 8); + fun writeInt8(value: Int) { + writeInt(value, 8) } /** * Write a 16-bit two's-compliment integer to the output. * * @param value the integer to write - * @throws IllegalArgumentException If the value is too large to be represented in 16 bits + * @throws IllegalArgumentException if the value is too large to be represented in 16 bits */ - default void writeInt16(int value) { - writeInt(value, 16); + fun writeInt16(value: Int) { + writeInt(value, 16) } /** @@ -131,8 +116,8 @@ public interface SSZWriter { * * @param value the integer to write */ - default void writeInt32(int value) { - writeInt(value, 32); + fun writeInt32(value: Int) { + writeInt(value, 32) } /** @@ -140,46 +125,27 @@ public interface SSZWriter { * * @param value the long to write */ - default void writeInt64(long value) { - writeLong(value, 64); + fun writeInt64(value: Long) { + writeLong(value, 64) } /** * Write an unsigned integer to the output. * - * Note that the argument {@code value} is a native signed int but will be interpreted as an unsigned value. - * * @param value the integer to write * @param bitLength the bit length of the integer value * @throws IllegalArgumentException if the value is too large for the specified bit length */ - default void writeUInt(int value, int bitLength) { - writeSSZ(SSZ.encodeULongToByteArray(value, bitLength)); - } + fun writeUInt(value: UInt, bitLength: Int) /** * Write an unsigned long to the output. * - * Note that the argument {@code value} is a native signed long but will be interpreted as an unsigned value. - * * @param value the long value to write * @param bitLength the bit length of the integer value * @throws IllegalArgumentException if the value is too large for the specified bit length */ - default void writeULong(long value, int bitLength) { - writeSSZ(SSZ.encodeULongToByteArray(value, bitLength)); - } - - /** - * Write an unsigned big integer to the output. - * - * @param value the integer to write - * @param bitLength the bit length of the integer value - * @throws IllegalArgumentException if the value is too large for the specified bit length or the value is negative - */ - default void writeUBigInteger(BigInteger value, int bitLength) { - writeSSZ(SSZ.encodeUBigIntegerToByteArray(value, bitLength)); - } + fun writeULong(value: ULong, bitLength: Int) /** * Write an 8-bit unsigned integer to the output. @@ -187,18 +153,18 @@ public interface SSZWriter { * @param value the integer to write * @throws IllegalArgumentException if the value is too large to be represented in 8 bits */ - default void writeUInt8(int value) { - writeUInt(value, 8); + fun writeUInt8(value: UInt) { + writeUInt(value, 8) } /** * Write a 16-bit unsigned integer to the output. * * @param value the integer to write - * @throws IllegalArgumentException If the value is too large to be represented in 16 bits + * @throws IllegalArgumentException if the value is too large to be represented in 16 bits */ - default void writeUInt16(int value) { - writeUInt(value, 16); + fun writeUInt16(value: UInt) { + writeUInt(value, 16) } /** @@ -206,37 +172,26 @@ public interface SSZWriter { * * @param value the integer to write */ - default void writeUInt32(long value) { - writeULong(value, 32); + fun writeUInt32(value: UInt) { + writeUInt(value, 32) } /** * Write a 64-bit unsigned integer to the output. * - * Note that the argument {@code value} is a native signed long but will be interpreted as an unsigned value. - * * @param value the long to write */ - default void writeUInt64(long value) { - writeULong(value, 64); - } - - /** - * Write a {@link UInt256} to the output. - * - * @param value the {@link UInt256} to write - */ - default void writeUInt256(UInt256 value) { - writeSSZ(SSZ.encodeUInt256(value)); + fun writeUInt64(value: ULong) { + writeULong(value, 64) } /** - * Write a {@link UInt384} to the output. + * Write a [UInt256] to the output. * - * @param value the {@link UInt384} to write + * @param value the [UInt256] to write */ - default void writeUInt384(UInt384 value) { - writeSSZ(SSZ.encodeUInt384(value)); + fun writeUInt256(value: UInt256) { + writeSSZ(SSZ.encodeUInt256(value)) } /** @@ -244,18 +199,18 @@ public interface SSZWriter { * * @param value the boolean value */ - default void writeBoolean(boolean value) { - writeSSZ(SSZ.encodeBoolean(value)); + fun writeBoolean(value: Boolean) { + writeSSZ(SSZ.encodeBoolean(value)) } /** * Write an address. * * @param address the address (must be exactly 20 bytes) - * @throws IllegalArgumentException if {@code address.size != 20} + * @throws IllegalArgumentException if `address.size != 20` */ - default void writeAddress(Bytes address) { - writeSSZ(SSZ.encodeAddress(address)); + fun writeAddress(address: Bytes) { + writeSSZ(SSZ.encodeAddress(address)) } /** @@ -263,8 +218,8 @@ public interface SSZWriter { * * @param hash the hash */ - default void writeHash(Bytes hash) { - writeSSZ(SSZ.encodeHash(hash)); + fun writeHash(hash: Bytes) { + writeSSZ(SSZ.encodeHash(hash)) } /** @@ -272,36 +227,28 @@ public interface SSZWriter { * * @param elements the bytes to write as a list */ - default void writeBytesList(Bytes... elements) { - SSZ.encodeBytesListTo(elements, this::writeSSZ); - } + fun writeBytesList(vararg elements: Bytes) /** * Write a list of bytes. * * @param elements the bytes to write as a list */ - default void writeBytesList(List<? extends Bytes> elements) { - SSZ.encodeBytesListTo(elements, this::writeSSZ); - } + fun writeBytesList(elements: List<Bytes>) /** * Write a list of strings, which must be of the same length * * @param elements the strings to write as a list */ - default void writeStringList(String... elements) { - SSZ.encodeStringListTo(elements, this::writeSSZ); - } + fun writeStringList(vararg elements: String) /** * Write a list of strings, which must be of the same length * * @param elements the strings to write as a list */ - default void writeStringList(List<String> elements) { - SSZ.encodeStringListTo(elements, this::writeSSZ); - } + fun writeStringList(elements: List<String>) /** * Write a list of two's compliment integers. @@ -310,9 +257,7 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large for the specified bit length */ - default void writeIntList(int bitLength, int... elements) { - SSZ.encodeIntListTo(bitLength, elements, this::writeSSZ); - } + fun writeIntList(bitLength: Int, vararg elements: Int) /** * Write a list of two's compliment integers. @@ -321,9 +266,7 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large for the specified bit length */ - default void writeIntList(int bitLength, List<Integer> elements) { - SSZ.encodeIntListTo(bitLength, elements, this::writeSSZ); - } + fun writeIntList(bitLength: Int, elements: List<Int>) /** * Write a list of two's compliment long integers. @@ -332,9 +275,7 @@ public interface SSZWriter { * @param elements the long integers to write as a list * @throws IllegalArgumentException if any values are too large for the specified bit length */ - default void writeLongIntList(int bitLength, long... elements) { - SSZ.encodeLongIntListTo(bitLength, elements, this::writeSSZ); - } + fun writeLongIntList(bitLength: Int, vararg elements: Long) /** * Write a list of two's compliment long integers. @@ -343,9 +284,7 @@ public interface SSZWriter { * @param elements the long integers to write as a list * @throws IllegalArgumentException if any values are too large for the specified bit length */ - default void writeLongIntList(int bitLength, List<Long> elements) { - SSZ.encodeLongIntListTo(bitLength, elements, this::writeSSZ); - } + fun writeLongIntList(bitLength: Int, elements: List<Long>) /** * Write a list of big integers. @@ -354,9 +293,7 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if an integer cannot be stored in the number of bytes provided */ - default void writeBigIntegerList(int bitLength, BigInteger... elements) { - SSZ.encodeBigIntegerListTo(bitLength, elements, this::writeSSZ); - } + fun writeBigIntegerList(bitLength: Int, vararg elements: BigInteger) /** * Write a list of big integers. @@ -365,9 +302,7 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if an integer cannot be stored in the number of bytes provided */ - default void writeBigIntegerList(int bitLength, List<BigInteger> elements) { - SSZ.encodeBigIntegerListTo(bitLength, elements, this::writeSSZ); - } + fun writeBigIntegerList(bitLength: Int, elements: List<BigInteger>) /** * Write a list of 8-bit two's compliment integers. @@ -375,18 +310,18 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large to be represented in 8 bits */ - default void writeInt8List(int... elements) { - writeIntList(8, elements); + fun writeInt8List(vararg elements: Int) { + writeIntList(8, *elements) } /** * Write a list of 8-bit two's compliment integers. * - * @param elements the integers to write as a list. + * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large to be represented in 8 bits */ - default void writeInt8List(List<Integer> elements) { - writeIntList(8, elements); + fun writeInt8List(elements: List<Int>) { + writeIntList(8, elements) } /** @@ -395,8 +330,8 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large to be represented in 16 bits */ - default void writeInt16List(int... elements) { - writeIntList(16, elements); + fun writeInt16List(vararg elements: Int) { + writeIntList(16, *elements) } /** @@ -405,8 +340,8 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large to be represented in 16 bits */ - default void writeInt16List(List<Integer> elements) { - writeIntList(16, elements); + fun writeInt16List(elements: List<Int>) { + writeIntList(16, elements) } /** @@ -414,8 +349,8 @@ public interface SSZWriter { * * @param elements the integers to write as a list */ - default void writeInt32List(int... elements) { - writeIntList(32, elements); + fun writeInt32List(vararg elements: Int) { + writeIntList(32, *elements) } /** @@ -423,8 +358,8 @@ public interface SSZWriter { * * @param elements the integers to write as a list */ - default void writeInt32List(List<Integer> elements) { - writeIntList(32, elements); + fun writeInt32List(elements: List<Int>) { + writeIntList(32, elements) } /** @@ -432,8 +367,8 @@ public interface SSZWriter { * * @param elements the integers to write as a list */ - default void writeInt64List(long... elements) { - writeLongIntList(64, elements); + fun writeInt64List(vararg elements: Long) { + writeLongIntList(64, *elements) } /** @@ -441,61 +376,45 @@ public interface SSZWriter { * * @param elements the integers to write as a list */ - default void writeInt64List(List<Long> elements) { - writeLongIntList(64, elements); + fun writeInt64List(elements: List<Long>) { + writeLongIntList(64, elements) } /** * Write a list of unsigned integers. * - * Note that the {@code elements} are native signed ints, but will be interpreted as an unsigned values. - * * @param bitLength the bit length of the encoded integers (must be a multiple of 8) * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large for the specified bit length */ - default void writeUIntList(int bitLength, int... elements) { - SSZ.encodeUIntListTo(bitLength, elements, this::writeSSZ); - } + fun writeUIntList(bitLength: Int, vararg elements: UInt) /** * Write a list of unsigned integers. * - * Note that the {@code elements} are native signed ints, but will be interpreted as an unsigned values. - * * @param bitLength the bit length of the encoded integers (must be a multiple of 8) * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large for the specified bit length */ - default void writeUIntList(int bitLength, List<Integer> elements) { - SSZ.encodeUIntListTo(bitLength, elements, this::writeSSZ); - } + fun writeUIntList(bitLength: Int, elements: List<UInt>) /** * Write a list of unsigned long integers. * - * Note that the {@code elements} are native signed longs, but will be interpreted as an unsigned values. - * - * @param bitLength the bit length of the encoded integers (must be a multiple of 8) - * @param elements the long integers to write as a list - * @throws IllegalArgumentException if any values are too large for the specified bit length + * @param bitLength The bit length of the encoded integers (must be a multiple of 8). + * @param elements The long integers to write as a list. + * @throws IllegalArgumentException If any values are too large for the specified bit length. */ - default void writeULongIntList(int bitLength, long... elements) { - SSZ.encodeULongIntListTo(bitLength, elements, this::writeSSZ); - } + fun writeULongIntList(bitLength: Int, vararg elements: ULong) /** * Write a list of unsigned long integers. * - * Note that the {@code elements} are native signed longs, but will be interpreted as an unsigned values. - * - * @param bitLength the bit length of the encoded integers (must be a multiple of 8) - * @param elements the long integers to write as a list - * @throws IllegalArgumentException if any values are too large for the specified bit length + * @param bitLength The bit length of the encoded integers (must be a multiple of 8). + * @param elements The long integers to write as a list. + * @throws IllegalArgumentException If any values are too large for the specified bit length. */ - default void writeULongIntList(int bitLength, List<Long> elements) { - SSZ.encodeULongIntListTo(bitLength, elements, this::writeSSZ); - } + fun writeULongIntList(bitLength: Int, elements: List<ULong>) /** * Write a list of 8-bit unsigned integers. @@ -503,8 +422,8 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large to be represented in 8 bits */ - default void writeUInt8List(int... elements) { - writeUIntList(8, elements); + fun writeUInt8List(vararg elements: UInt) { + writeUIntList(8, *elements) } /** @@ -513,8 +432,8 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large to be represented in 8 bits */ - default void writeUInt8List(List<Integer> elements) { - writeUIntList(8, elements); + fun writeUInt8List(elements: List<UInt>) { + writeUIntList(8, elements) } /** @@ -523,8 +442,8 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large to be represented in 16 bits */ - default void writeUInt16List(int... elements) { - writeUIntList(16, elements); + fun writeUInt16List(vararg elements: UInt) { + writeUIntList(16, *elements) } /** @@ -533,50 +452,44 @@ public interface SSZWriter { * @param elements the integers to write as a list * @throws IllegalArgumentException if any values are too large to be represented in 16 bits */ - default void writeUInt16List(List<Integer> elements) { - writeUIntList(16, elements); + fun writeUInt16List(elements: List<UInt>) { + writeUIntList(16, elements) } /** * Write a list of 32-bit unsigned integers. * * @param elements the integers to write as a list - * @throws IllegalArgumentException if any values are too large to be represented in 32 bits */ - default void writeUInt32List(long... elements) { - writeULongIntList(32, elements); + fun writeUInt32List(vararg elements: UInt) { + writeUIntList(32, *elements) } /** * Write a list of 32-bit unsigned integers. * * @param elements the integers to write as a list - * @throws IllegalArgumentException if any values are too large to be represented in 32 bits */ - default void writeUInt32List(List<Long> elements) { - writeULongIntList(32, elements); + fun writeUInt32List(elements: List<UInt>) { + writeUIntList(32, elements) } /** * Write a list of 64-bit unsigned integers. * - * Note that the {@code elements} are native signed longs, but will be interpreted as an unsigned values. - * * @param elements the integers to write as a list */ - default void writeUInt64List(long... elements) { - writeULongIntList(64, elements); + fun writeUInt64List(vararg elements: ULong) { + writeULongIntList(64, *elements) } /** * Write a list of 64-bit unsigned integers. * - * Note that the {@code elements} are native signed longs, but will be interpreted as an unsigned values. - * * @param elements the integers to write as a list */ - default void writeUInt64List(List<Long> elements) { - writeULongIntList(64, elements); + fun writeUInt64List(elements: List<ULong>) { + writeULongIntList(64, elements) } /** @@ -584,90 +497,70 @@ public interface SSZWriter { * * @param elements the integers to write as a list */ - default void writeUInt256List(UInt256... elements) { - SSZ.encodeUInt256ListTo(elements, this::writeSSZ); - } + fun writeUInt256List(vararg elements: UInt256) /** * Write a list of unsigned 256-bit integers. * * @param elements the integers to write as a list */ - default void writeUInt256List(List<UInt256> elements) { - SSZ.encodeUInt256ListTo(elements, this::writeSSZ); - } + fun writeUInt256List(elements: List<UInt256>) /** * Write a list of unsigned 384-bit integers. * * @param elements the integers to write as a list */ - default void writeUInt384List(List<UInt384> elements) { - SSZ.encodeUInt384ListTo(elements, this::writeSSZ); - } + fun writeUInt384List(vararg elements: UInt384) /** - * Write a list of unsigned 384-bit integers. + * Write a list of unsigned 256-bit integers. * * @param elements the integers to write as a list */ - default void writeUInt384List(UInt384... elements) { - SSZ.encodeUInt384ListTo(elements, this::writeSSZ); - } + fun writeUInt384List(elements: List<UInt384>) /** * Write a list of hashes. * * @param elements the hashes to write as a list */ - default void writeHashList(Bytes... elements) { - SSZ.encodeHashListTo(elements, this::writeSSZ); - } + fun writeHashList(vararg elements: Bytes) /** * Write a list of hashes. * * @param elements the hashes to write as a list */ - default void writeHashList(List<? extends Bytes> elements) { - SSZ.encodeHashListTo(elements, this::writeSSZ); - } + fun writeHashList(elements: List<Bytes>) /** * Write a list of addresses. * * @param elements the addresses to write as a list - * @throws IllegalArgumentException if any {@code address.size != 20} + * @throws IllegalArgumentException if any `address.size != 20` */ - default void writeAddressList(Bytes... elements) { - SSZ.encodeAddressListTo(elements, this::writeSSZ); - } + fun writeAddressList(vararg elements: Bytes) /** * Write a list of addresses. * * @param elements the addresses to write as a list - * @throws IllegalArgumentException if any {@code address.size != 20} + * @throws IllegalArgumentException if any `address.size != 20` */ - default void writeAddressList(List<? extends Bytes> elements) { - SSZ.encodeAddressListTo(elements, this::writeSSZ); - } + fun writeAddressList(elements: List<Bytes>) /** * Write a list of booleans. * * @param elements the booleans to write as a list */ - default void writeBooleanList(boolean... elements) { - SSZ.encodeBooleanListTo(elements, this::writeSSZ); - } + fun writeBooleanList(vararg elements: Boolean) /** * Write a list of booleans. * * @param elements the booleans to write as a list */ - default void writeBooleanList(List<Boolean> elements) { - SSZ.encodeBooleanListTo(elements, this::writeSSZ); - } + fun writeBooleanList(elements: List<Boolean>) } diff --git a/ssz/src/test/java/org/apache/tuweni/ssz/ByteBufferWriterTest.java b/ssz/src/test/java/org/apache/tuweni/ssz/ByteBufferWriterTest.java old mode 100644 new mode 100755 index 5e84ff0..ae9392f --- a/ssz/src/test/java/org/apache/tuweni/ssz/ByteBufferWriterTest.java +++ b/ssz/src/test/java/org/apache/tuweni/ssz/ByteBufferWriterTest.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZReaderTest.java b/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZReaderTest.java old mode 100644 new mode 100755 index ac3f7d5..c2eec4f --- a/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZReaderTest.java +++ b/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZReaderTest.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZWriterTest.java b/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZWriterTest.java old mode 100644 new mode 100755 index 758cc0c..e0ddf3b --- a/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZWriterTest.java +++ b/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZWriterTest.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/test/java/org/apache/tuweni/ssz/HashTreeRootTest.java b/ssz/src/test/java/org/apache/tuweni/ssz/HashTreeRootTest.java old mode 100644 new mode 100755 index f414c07..1ea9ea8 --- a/ssz/src/test/java/org/apache/tuweni/ssz/HashTreeRootTest.java +++ b/ssz/src/test/java/org/apache/tuweni/ssz/HashTreeRootTest.java @@ -1,8 +1,8 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE - * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at + * Copyright 2019 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/ssz/src/test/kotlin/org/apache/tuweni/ssz/experimental/SSZTest.kt b/ssz/src/test/kotlin/org/apache/tuweni/ssz/experimental/SSZTest.kt new file mode 100755 index 0000000..6ec0115 --- /dev/null +++ b/ssz/src/test/kotlin/org/apache/tuweni/ssz/experimental/SSZTest.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuweni.ssz.experimental + +import org.apache.tuweni.bytes.Bytes.fromHexString +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +@ExperimentalUnsignedTypes +class SSZTest { + + @Test + fun shouldEncodeUnsigned() { + assertEquals(fromHexString("0000"), SSZ.encodeUInt16(0.toUInt())) + assertEquals(fromHexString("00000000"), SSZ.encodeUInt32(0.toUInt())) + assertEquals(fromHexString("0000000000000000"), SSZ.encodeUInt64(0L.toULong())) + assertEquals(fromHexString("0000000000000000"), SSZ.encodeUInt64(0L.toULong())) + + assertEquals(fromHexString("FFFF"), SSZ.encodeUInt16(65535.toUInt())) + assertEquals(fromHexString("FFFF0000"), SSZ.encodeUInt32(65535.toUInt())) + } + + @Test + fun shouldWriteUnsigned() { + assertEquals(fromHexString("FFFF"), SSZ.encode { w -> w.writeUInt16(65535.toUInt()) }) + assertEquals(fromHexString("FFFF0000"), SSZ.encode { w -> w.writeUInt32(65535.toUInt()) }) + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
