mirror of
https://github.com/vacp2p/linea-besu.git
synced 2026-01-09 23:47:57 -05:00
[PAN-2946] Add strict short hex strings for retesteth (#1812)
The retesteth program wants byte trimmed UInt256, but our current impl does nybble trimmed data (which is what the JSON-RPC spec calls for). Add an API so we can do both. Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
This commit is contained in:
@@ -166,6 +166,25 @@ public interface UInt256Value<T extends UInt256Value<T>> extends Bytes32Backed,
|
||||
return "0x" + hex.substring(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return This value represented as a byte-minimal hexadecimal string (without leading zero
|
||||
* pairs).
|
||||
*/
|
||||
default String toStrictShortHexString() {
|
||||
final String hex = toHexString();
|
||||
// Skipping '0x'
|
||||
if (hex.charAt(2) != '0') return hex;
|
||||
|
||||
int i = 3;
|
||||
while (i < hex.length() - 1 && hex.charAt(i) == '0') {
|
||||
i++;
|
||||
}
|
||||
// Align the trim so we get full bytes, not stray nybbles.
|
||||
i = i & 0xFFFFFFFE;
|
||||
|
||||
return "0x" + hex.substring(i);
|
||||
}
|
||||
|
||||
/** @return This value represented as an hexadecimal string without a 0x prefix. */
|
||||
default String toUnprefixedHexString() {
|
||||
return toHexString().substring(2);
|
||||
|
||||
@@ -191,7 +191,7 @@ public class UInt256BytesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hexStrings() {
|
||||
public void shortHexStrings() {
|
||||
assertThat(UInt256.of(0).toShortHexString()).isEqualTo("0x0");
|
||||
assertThat(UInt256.of(1).toShortHexString()).isEqualTo("0x1");
|
||||
assertThat(UInt256.fromHexString("0xdeadbeef").toShortHexString()).isEqualTo("0xdeadbeef");
|
||||
@@ -202,6 +202,33 @@ public class UInt256BytesTest {
|
||||
.isEqualTo("0xdecafbad");
|
||||
assertThat(UInt256.fromHexString("cafebabe").toShortHexString()).isEqualTo("0xcafebabe");
|
||||
assertThat(UInt256.fromHexString("facefeed").toShortHexString()).isEqualTo("0xfacefeed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void strictShortHexStrings() {
|
||||
assertThat(UInt256.of(0).toStrictShortHexString()).isEqualTo("0x00");
|
||||
assertThat(UInt256.of(1).toStrictShortHexString()).isEqualTo("0x01");
|
||||
assertThat(UInt256.fromHexString("0xdeadbeef").toStrictShortHexString())
|
||||
.isEqualTo("0xdeadbeef");
|
||||
assertThat(
|
||||
UInt256.fromHexString(
|
||||
"0x00000000000000000000000000000000000000000000000000000000decafbad")
|
||||
.toStrictShortHexString())
|
||||
.isEqualTo("0xdecafbad");
|
||||
assertThat(UInt256.fromHexString("cafebabe").toStrictShortHexString()).isEqualTo("0xcafebabe");
|
||||
assertThat(UInt256.fromHexString("facefeed").toStrictShortHexString()).isEqualTo("0xfacefeed");
|
||||
assertThat(UInt256.fromHexString("0xdedbeef").toStrictShortHexString()).isEqualTo("0x0dedbeef");
|
||||
assertThat(
|
||||
UInt256.fromHexString(
|
||||
"0x000000000000000000000000000000000000000000000000000000000dcafbad")
|
||||
.toStrictShortHexString())
|
||||
.isEqualTo("0x0dcafbad");
|
||||
assertThat(UInt256.fromHexString("cafebab").toStrictShortHexString()).isEqualTo("0x0cafebab");
|
||||
assertThat(UInt256.fromHexString("facefed").toStrictShortHexString()).isEqualTo("0x0facefed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullHexStrings() {
|
||||
assertThat(UInt256.of(0).toHexString())
|
||||
.isEqualTo("0x0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assertThat(UInt256.of(1).toHexString())
|
||||
|
||||
Reference in New Issue
Block a user