-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare for EIP-4762: make access event keys in line with eip-6800
- Loading branch information
Showing
5 changed files
with
198 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...letrie/src/main/java/org/hyperledger/besu/ethereum/verkletrie/util/SuffixTreeEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.hyperledger.besu.ethereum.verkletrie.util; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
|
||
public class SuffixTreeEncoder { | ||
private static final int VERSION_OFFSET = 0; | ||
private static final int CODE_SIZE_OFFSET = 5; | ||
private static final int NONCE_OFFSET = 8; | ||
private static final int BALANCE_OFFSET = 16; | ||
|
||
public static Bytes32 encodeVersion(final Bytes version) { | ||
return encodeIntoBasicDataLeaf(version, VERSION_OFFSET); | ||
} | ||
|
||
public static Bytes32 encodeCodeSize(final Bytes version) { | ||
return encodeIntoBasicDataLeaf(version, CODE_SIZE_OFFSET); | ||
} | ||
|
||
public static Bytes32 encodeNonce(final Bytes version) { | ||
return encodeIntoBasicDataLeaf(version, NONCE_OFFSET); | ||
} | ||
|
||
public static Bytes32 encodeBalance(final Bytes version) { | ||
return encodeIntoBasicDataLeaf(version, BALANCE_OFFSET); | ||
} | ||
|
||
/** | ||
* Encoding of a field into the BasicDataLeaf 32 byte value, using Little-Endian order. | ||
* @param value to encode into a 32 byte value | ||
* @param byteShift byte position of `value` within the final 32 byte value | ||
* @throws IllegalArgumentException if `value` does not fit within 32 bytes after being encoded | ||
* @return encoded BasicDataLeaf value | ||
*/ | ||
public static Bytes32 encodeIntoBasicDataLeaf(final Bytes value, final int byteShift) { | ||
Bytes32 value32Bytes = Bytes32.leftPad(value); | ||
if (byteShift == 0) { | ||
return value32Bytes; | ||
} else if (byteShift > 0 && value.size() + byteShift <= 32) { | ||
return value32Bytes.shiftLeft(byteShift * 8); | ||
} | ||
throw new IllegalArgumentException("invalid byteShift " + byteShift + " parameter"); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ie/src/test/java/org/hyperledger/besu/ethereum/verkletrie/util/SuffixTreeEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.hyperledger.besu.ethereum.verkletrie.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class SuffixTreeEncoderTest { | ||
|
||
public static Stream<Arguments> valuesStart() { | ||
return Stream.of( | ||
Arguments.of(Bytes32.ZERO, Bytes32.ZERO), | ||
Arguments.of(Bytes.of(0xff), Bytes32.fromHexString("0x00000000000000000000000000000000000000000000000000000000000000FF")), | ||
Arguments.of(Bytes.repeat((byte) 0xff, 12), Bytes32.fromHexString("0x0000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")), | ||
Arguments.of(Bytes.fromHexString("0xadef"), Bytes32.fromHexString("0x000000000000000000000000000000000000000000000000000000000000ADEF")), | ||
Arguments.of(Bytes.fromHexString("0x1123d3"), Bytes32.fromHexString("0x00000000000000000000000000000000000000000000000000000000001123D3")) | ||
); | ||
} | ||
|
||
public static Stream<Arguments> valuesMiddle() { | ||
return Stream.of( | ||
Arguments.of(Bytes32.ZERO, Bytes32.ZERO), | ||
Arguments.of(Bytes.of(0xff), Bytes32.fromHexString("0x00000000000000000000000000000000FF000000000000000000000000000000")), | ||
Arguments.of(Bytes.repeat((byte) 0xff, 12), Bytes32.fromHexString("0x00000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000")), | ||
Arguments.of(Bytes.fromHexString("0xadef"), Bytes32.fromHexString("0x000000000000000000000000000000ADEF000000000000000000000000000000")), | ||
Arguments.of(Bytes.fromHexString("0x1123d3"), Bytes32.fromHexString("0x0000000000000000000000000000001123D30000000000000000000000000000")) | ||
); | ||
} | ||
|
||
public static Stream<Arguments> valuesEnd() { | ||
return Stream.of( | ||
Arguments.of(Bytes32.ZERO, Bytes32.ZERO), | ||
Arguments.of(Bytes.repeat((byte) 0xff, 12), Bytes32.fromHexString("0xFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000000")), | ||
Arguments.of(Bytes.of(0xff), Bytes32.fromHexString("0xFF00000000000000000000000000000000000000000000000000000000000000")), | ||
Arguments.of(Bytes.fromHexString("0xadef"), Bytes32.fromHexString("0xADEF000000000000000000000000000000000000000000000000000000000000")), | ||
Arguments.of(Bytes.fromHexString("0x1123d3"), Bytes32.fromHexString("0x1123D30000000000000000000000000000000000000000000000000000000000")) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("valuesStart") | ||
void encodeBytesStart(final Bytes value, final Bytes32 expected) { | ||
assertEquals(expected, | ||
SuffixTreeEncoder.encodeIntoBasicDataLeaf(value, 0)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("valuesMiddle") | ||
void encodeBytesMiddle(final Bytes value, final Bytes32 expected) { | ||
assertEquals(expected, | ||
SuffixTreeEncoder.encodeIntoBasicDataLeaf(value, (32 - value.size()) / 2)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("valuesEnd") | ||
void encodeBytesEnd(final Bytes value, final Bytes32 expected) { | ||
assertEquals(expected, | ||
SuffixTreeEncoder.encodeIntoBasicDataLeaf(value, 32 - value.size())); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("valuesStart") | ||
void encodeBytesOutsideRange(final Bytes value, final Bytes32 ignoredExpected) { | ||
assertThrows(IllegalArgumentException.class, () -> SuffixTreeEncoder.encodeIntoBasicDataLeaf(value, 32)); | ||
} | ||
} |