You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2019/06/12 03:20:57 UTC
[incubator-tuweni] 03/03: Rename fixed size list to vector
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
commit c41bff39d923106cb463a933dfe3c9755a2249ec
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Tue Jun 11 20:19:52 2019 -0700
Rename fixed size list to vector
---
.../java/org/apache/tuweni/ssz/BytesSSZReader.java | 43 +++++++++++++++++-----
ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java | 10 ++---
.../main/java/org/apache/tuweni/ssz/SSZReader.java | 27 ++++++--------
.../main/java/org/apache/tuweni/ssz/SSZWriter.java | 13 +++----
.../org/apache/tuweni/ssz/BytesSSZReaderTest.java | 8 +++-
.../org/apache/tuweni/ssz/BytesSSZWriterTest.java | 2 +-
6 files changed, 60 insertions(+), 43 deletions(-)
diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZReader.java b/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZReader.java
index c01254f..c3d68b1 100644
--- a/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZReader.java
+++ b/ssz/src/main/java/org/apache/tuweni/ssz/BytesSSZReader.java
@@ -132,28 +132,27 @@ final class BytesSSZReader implements SSZReader {
@Override
public List<Bytes> readBytesList(int limit) {
- return readList(remaining -> readByteArray(limit), Bytes::wrap);
+ return readList(remaining -> readBytes(limit));
}
@Override
- public List<Bytes> readBytesList(long listSize, int limit) {
+ public List<Bytes> readVector(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");
+ public List<Bytes> readFixedBytesVector(int listSize, int byteLength, int limit) {
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);
+ return readList(remaining -> readFixedBytes(byteLength, limit));
}
@Override
public List<String> readStringList(int limit) {
- return readList(remaining -> readByteArray(limit), byteArray -> new String(byteArray, UTF_8));
+ return readList(remaining -> readBytes(limit), bytes -> new String(bytes.toArrayUnsafe(), UTF_8));
}
@Override
@@ -225,7 +224,31 @@ final class BytesSSZReader implements SSZReader {
return bytes;
}
- private <T> List<T> readList(LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) {
+ private List<Bytes> readList(LongFunction<Bytes> bytesSupplier) {
+ ensureBytes(4, () -> "SSZ encoded data is not a list");
+ int originalIndex = this.index;
+ List<Bytes> elements;
+ try {
+ // use a long to simulate reading unsigned
+ long listSize = consumeBytes(4).toLong(LITTLE_ENDIAN);
+ elements = new ArrayList<>();
+ while (listSize > 0) {
+ Bytes bytes = bytesSupplier.apply(listSize);
+ elements.add(bytes);
+ listSize -= bytes.size();
+ 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> readList(LongFunction<Bytes> bytesSupplier, Function<Bytes, T> converter) {
ensureBytes(4, () -> "SSZ encoded data is not a list");
int originalIndex = this.index;
List<T> elements;
@@ -234,9 +257,9 @@ final class BytesSSZReader implements SSZReader {
long listSize = consumeBytes(4).toLong(LITTLE_ENDIAN);
elements = new ArrayList<>();
while (listSize > 0) {
- byte[] bytes = bytesSupplier.apply(listSize);
+ Bytes bytes = bytesSupplier.apply(listSize);
elements.add(converter.apply(bytes));
- listSize -= bytes.length;
+ listSize -= bytes.size();
listSize -= 4;
if (listSize < 0) {
throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
@@ -294,7 +317,7 @@ final class BytesSSZReader implements SSZReader {
return elements;
}
- private <T> List<T> readFixedList(long listSize, LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) {
+ private <T> List<T> readFixedList(int listSize, LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) {
int originalIndex = this.index;
List<T> elements;
try {
diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java b/ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java
index 81b67ec..b7ac8e3 100644
--- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java
+++ b/ssz/src/main/java/org/apache/tuweni/ssz/SSZ.java
@@ -547,13 +547,9 @@ 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");
- }
+ static void encodeBytesVectorTo(List<? extends Bytes> elements, Consumer<Bytes> appender) {
for (Bytes bytes : elements) {
- encodeBytesTo(bytes, appender);
+ appender.accept(bytes);
}
}
@@ -1241,7 +1237,7 @@ public final class SSZ {
private static byte[] listLengthPrefix(long nElements, int elementBytes) {
long listSize;
try {
- listSize = Math.multiplyExact(nElements, elementBytes);
+ listSize = Math.multiplyExact(nElements, (long) elementBytes);
} catch (ArithmeticException e) {
listSize = Long.MAX_VALUE;
}
diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java b/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java
index bb32bb5..1fe90a8 100644
--- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java
+++ b/ssz/src/main/java/org/apache/tuweni/ssz/SSZReader.java
@@ -373,7 +373,7 @@ 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.
+ * Note: prefer to use {@link #readVector(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}.
@@ -381,13 +381,12 @@ public interface SSZReader {
* 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);
+ default List<Bytes> readVector(long listSize) {
+ return readVector(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.
+ * Read a known-size fixed length list of known-size fixed length {@link Bytes} from the SSZ source.
*
* @param listSize The size of the fixed-length list being read.
* @param byteLength The number of fixed-length Bytes per homogenous List element.
@@ -397,13 +396,12 @@ public interface SSZReader {
* 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);
+ List<Bytes> readFixedBytesVector(int 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.
+ * Read a known-size fixed length list of known-size fixed length {@link Bytes} from the SSZ source.
*
- * Note: prefer to use {@link #readFixedBytesList(long, int, int)} instead, especially when reading untrusted data.
+ * Note: prefer to use {@link #readFixedBytesVector(int, 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.
@@ -412,8 +410,8 @@ public interface SSZReader {
* 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);
+ default List<Bytes> readFixedBytesVector(int listSize, int byteLength) {
+ return readFixedBytesVector(listSize, byteLength, Integer.MAX_VALUE);
}
/**
@@ -430,8 +428,7 @@ public interface SSZReader {
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.
+ * Read a variable-length list of known-size fixed length {@link Bytes} from the SSZ source.
*
* Note: prefer to use {@link #readFixedBytesList(int, int)} instead, especially when reading untrusted data.
*
@@ -466,7 +463,7 @@ public interface SSZReader {
* 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);
+ List<Bytes> readVector(long listSize, int limit);
/**
* Read a list of byte arrays from the SSZ source.
@@ -722,7 +719,7 @@ public interface SSZReader {
* Read a list of hashes from the SSZ source.
*
* @param hashLength The length of the hash (in bytes).
- * @return A list of 32-byte hashes.
+ * @return A list of hashes.
* @throws InvalidSSZTypeException If the next SSZ value is not a list, there are insufficient encoded bytes for any
* hash in the list.
* @throws EndOfSSZException If there are no more SSZ values to read.
diff --git a/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java b/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java
index e9d100a..fd0270c 100644
--- a/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java
+++ b/ssz/src/main/java/org/apache/tuweni/ssz/SSZWriter.java
@@ -297,13 +297,12 @@ 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.
+ * Write a vector of bytes.
*
- * @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);
+ default void writeVector(List<? extends Bytes> elements) {
+ SSZ.encodeBytesVectorTo(elements, this::writeSSZ);
}
/**
@@ -320,12 +319,10 @@ public interface SSZWriter {
* 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);
+ default void writeFixedBytesVector(List<? extends Bytes> elements) {
+ SSZ.encodeBytesVectorTo(elements, this::writeSSZ);
}
/**
diff --git a/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZReaderTest.java b/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZReaderTest.java
index ac3f7d5..810e39d 100644
--- a/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZReaderTest.java
+++ b/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZReaderTest.java
@@ -181,7 +181,6 @@ class BytesSSZReaderTest {
void shouldRoundtripBytesVararg() {
List<Bytes> toWrite = Arrays.asList(Bytes48.random(), Bytes48.random(), Bytes48.random());
Bytes encoded = SSZ.encode(writer -> writer.writeBytesList(toWrite.toArray(new Bytes[0])));
- System.out.println(encoded);
assertEquals(toWrite, SSZ.decodeBytesList(encoded));
}
@@ -190,8 +189,13 @@ class BytesSSZReaderTest {
void shouldRoundtripBytesList() {
List<Bytes> toWrite = Arrays.asList(Bytes48.random(), Bytes48.random(), Bytes48.random());
Bytes encoded = SSZ.encode(writer -> writer.writeBytesList(toWrite));
- System.out.println(encoded);
assertEquals(toWrite, SSZ.decodeBytesList(encoded));
+ }
+ @Test
+ void shouldRoundtripBytesVector() {
+ List<Bytes> toWrite = Arrays.asList(Bytes48.random(), Bytes48.random(), Bytes48.random());
+ Bytes encoded = SSZ.encode(writer -> writer.writeFixedBytesVector(toWrite));
+ assertEquals(toWrite, SSZ.decode(encoded, reader -> reader.readFixedBytesVector(3, 48)));
}
}
diff --git a/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZWriterTest.java b/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZWriterTest.java
index 758cc0c..ed3349a 100644
--- a/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZWriterTest.java
+++ b/ssz/src/test/java/org/apache/tuweni/ssz/BytesSSZWriterTest.java
@@ -366,7 +366,7 @@ class BytesSSZWriterTest {
}
@Test
- void shouldWriteVaragsListsOfHashes() {
+ void shouldWriteVarargsListsOfHashes() {
assertEquals(
fromHexString(
"0x60000000ED1C978EE1CEEFA5BBD9ED1C8EE1CEEFA5BBD9ED1C978EE1CEEFA5BBD9ED1C978B40CA3893681B062BC06760A4863AFAFA6C4D6226D4C6AFAFA3684A06760CB26B30567B2281FF3BD582B0A633B33A376B95BD3333DB59B673A33B336A0B285D"),
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org