Skip to content

Commit

Permalink
Enhance ContractController to handle all remaining block types (pendi…
Browse files Browse the repository at this point in the history
…ng, safe, finalized) (#6975)

ContractController is modified, so that block with value "pending", "safe", "finalized" is treated as a "latest" block and could be passed by users.
---------

Signed-off-by: Bilyana Gospodinova <[email protected]>
  • Loading branch information
bilyana-gospodinova authored Oct 14, 2023
1 parent d794c2b commit e92f862
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,31 @@

public record BlockType(String name, long number) {

private static final String HEX_PREFIX = "0x";

public static final BlockType EARLIEST = new BlockType("earliest", 0L);
public static final BlockType LATEST = new BlockType("latest", Long.MAX_VALUE);
public static final BlockType PENDING = new BlockType("pending", Long.MAX_VALUE);
private static final String HEX_PREFIX = "0x";

public static BlockType of(final String value) {
if (StringUtils.isEmpty(value)) {
return LATEST;
}

if (StringUtils.isEmpty(value) || BlockType.LATEST.name().equalsIgnoreCase(value)) {
return BlockType.LATEST;
} else if (BlockType.EARLIEST.name().equalsIgnoreCase(value)) {
return BlockType.EARLIEST;
} else if (BlockType.PENDING.name().equalsIgnoreCase(value)) {
return BlockType.PENDING;
final String blockTypeName = value.toLowerCase();
switch (blockTypeName) {
case "earliest" -> {
return EARLIEST;
}
case "latest", "safe", "pending", "finalized" -> {
return LATEST;
}
default -> {
return extractNumericBlock(value);
}
}
}

private static BlockType extractNumericBlock(String value) {
int radix = 10;
var cleanedValue = value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void transferWithoutSender() {

@NullAndEmptySource
@ParameterizedTest
@ValueSource(strings = {"earliest", "pending", "latest", "0", "0x1a"})
@ValueSource(strings = {"earliest", "latest", "0", "0x1a", "pending", "safe", "finalized"})
void callValidBlockType(String value) {
final var request = request();
request.setBlock(BlockType.of(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class BlockTypeTest {
"earliest,0",
"EARLIEST,0",
"latest," + MAX_VALUE,
"latest," + MAX_VALUE,
"pending," + MAX_VALUE,
"PENDING," + MAX_VALUE,
"latest," + MAX_VALUE
})
@ParameterizedTest
void valid(String value, long number) {
Expand All @@ -47,6 +45,13 @@ void valid(String value, long number) {
assertThat(blockType).isNotNull().returns(valueLower, BlockType::name).returns(number, BlockType::number);
}

@CsvSource({"pending", "PENDING", "safe", "SAFE", "finalized", "FINALIZED"})
@ParameterizedTest
void unsupportedDefaultToLatest(String value) {
var blockType = BlockType.of(value);
assertThat(blockType).isNotNull().returns(BlockType.LATEST.name(), BlockType::name);
}

@ValueSource(strings = {MAX_VALUE + "1", "0xabcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", "lastest"})
@ParameterizedTest
void invalid(String value) {
Expand Down

0 comments on commit e92f862

Please sign in to comment.