Skip to content

Commit

Permalink
refactor TupleType byteLength and byteLengthPacked;
Browse files Browse the repository at this point in the history
add @FunctionalInterface annotation;
  • Loading branch information
esaulpaugh committed Jan 12, 2021
1 parent 8767447 commit 1539dff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/main/java/com/esaulpaugh/headlong/abi/TupleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.IntFunction;

import static com.esaulpaugh.headlong.abi.Encoding.OFFSET_LENGTH_BYTES;

Expand Down Expand Up @@ -73,21 +74,11 @@ public int typeCode() {
@Override
int byteLength(Object value) {
final Object[] elements = ((Tuple) value).elements;
int len = 0;
for (int i = 0; i < elementTypes.length; i++) {
return len((i) -> {
ABIType<?> type = elementTypes[i];
int byteLen = type.byteLength(elements[i]);
len += !type.dynamic ? byteLen : OFFSET_LENGTH_BYTES + byteLen;
}
return len;
}

private int staticByteLengthPacked() {
int len = 0;
for (ABIType<?> elementType : elementTypes) {
len += elementType.byteLengthPacked(null);
}
return len;
return type.dynamic ? OFFSET_LENGTH_BYTES + byteLen : byteLen;
});
}

/**
Expand All @@ -97,12 +88,16 @@ private int staticByteLengthPacked() {
@Override
public int byteLengthPacked(Object value) {
if (value == null) {
return staticByteLengthPacked();
return len((i) -> elementTypes[i].byteLengthPacked(null));
}
final Object[] elements = ((Tuple) value).elements;
return len((i) -> elementTypes[i].byteLengthPacked(elements[i]));
}

private int len(IntFunction<Integer> elementCount) {
int len = 0;
for (int i = 0; i < elementTypes.length; i++) {
len += elementTypes[i].byteLengthPacked(elements[i]);
for(int i = 0; i < elementTypes.length; i++) {
len += elementCount.apply(i);
}
return len;
}
Expand All @@ -116,13 +111,11 @@ public int validate(final Object value) {
if(elements.length == elementTypes.length) {
int i = 0;
try {
int len = 0;
for (; i < elementTypes.length; i++) {
ABIType<?> type = elementTypes[i];
int byteLen = type.validate(elements[i]);
len += !type.dynamic ? byteLen : OFFSET_LENGTH_BYTES + byteLen;
}
return len;
return len((j) -> {
ABIType<?> type = elementTypes[j];
int byteLen = type.validate(elements[j]);
return type.dynamic ? OFFSET_LENGTH_BYTES + byteLen : byteLen;
});
} catch (NullPointerException | IllegalArgumentException e) {
throw new IllegalArgumentException("tuple index " + i + ": " + e.getMessage());
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/esaulpaugh/headlong/rlp/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public interface Signer {
byte[] sign(byte[] message, int off, int len);
}

@FunctionalInterface
public interface Verifier {
void verify(byte[] signature, byte[] content) throws SignatureException;
}
Expand Down

0 comments on commit 1539dff

Please sign in to comment.