Skip to content

Commit

Permalink
Add validation on loaded DepositTreeSnapshot (#8053)
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 authored Mar 8, 2024
1 parent 86c2f3d commit b490fad
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions beacon/pow/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dependencies {
implementation project(':ethereum:pow:api')
implementation project(':ethereum:pow:merkletree')
implementation project(':ethereum:spec')
implementation project(':infrastructure:async')
implementation project(':infrastructure:bls')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.tuweni.bytes.Bytes;
import tech.pegasys.teku.ethereum.pow.api.DepositTreeSnapshot;
import tech.pegasys.teku.ethereum.pow.api.schema.LoadDepositSnapshotResult;
import tech.pegasys.teku.ethereum.pow.merkletree.DepositTree;
import tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException;
import tech.pegasys.teku.infrastructure.http.UrlSanitizer;
import tech.pegasys.teku.infrastructure.io.resource.ResourceLoader;
Expand Down Expand Up @@ -68,11 +69,19 @@ public LoadDepositSnapshotResult loadDepositSnapshot() {
try {
STATUS_LOG.loadingDepositSnapshotResource(sanitizedUrl);
final DepositTreeSnapshot depositTreeSnapshot = loadFromUrl(depositSnapshotResourceUrl);
// Validate
DepositTree.fromSnapshot(depositTreeSnapshot);
STATUS_LOG.onDepositSnapshot(
depositTreeSnapshot.getDepositCount(), depositTreeSnapshot.getExecutionBlockHash());
return LoadDepositSnapshotResult.create(Optional.of(depositTreeSnapshot));
} catch (final Exception e) {
LOG.warn("Failed to load deposit tree snapshot from " + sanitizedUrl, e);
if (e instanceof IllegalArgumentException) {
LOG.warn(
"Deposit tree snapshot loaded from " + sanitizedUrl + " is not a correct snapshot",
e);
} else {
LOG.warn("Failed to load deposit tree snapshot from " + sanitizedUrl, e);
}

if (isRequired) {
throw new InvalidConfigurationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class DepositSnapshotFileLoaderTest {

private static final String SNAPSHOT_BUNDLED_RESOURCE = "deposit_tree_snapshot_bundled.ssz";
private static final String SNAPSHOT_BEACON_API_RESOURCE = "deposit_tree_snapshot_beaconAPI.json";
private static final String SNAPSHOT_BEACON_API_BROKEN_RESOURCE =
"deposit_tree_snapshot_broken_beaconAPI.json";

private final Spec spec = TestSpecFactory.createMinimalBellatrix();
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
Expand Down Expand Up @@ -174,6 +176,36 @@ public void shouldFallbackToJsonDeserialization() {
});
}

@Test
public void shouldNotLoadBrokenDepositTree() {
// broken has 1106575 deposits
final String brokenResourcePath = getResourceFilePath(SNAPSHOT_BEACON_API_BROKEN_RESOURCE);
final String validResourcePath = getResourceFilePath(SNAPSHOT_BEACON_API_RESOURCE);
// assertion values from SNAPSHOT_BEACON_API_RESOURCE
final int deposits = 1106572;
final int blockNumber = 18754822;

depositSnapshotLoader =
new DepositSnapshotFileLoader.Builder()
.addOptionalResource(brokenResourcePath)
.addRequiredResource(validResourcePath)
.build();

final LoadDepositSnapshotResult result = depositSnapshotLoader.loadDepositSnapshot();
assertThat(result.getDepositTreeSnapshot()).isPresent();
assertWith(
result,
snapshotResult -> {
assertThat(snapshotResult.getDepositTreeSnapshot().isPresent()).isTrue();
assertThat(snapshotResult.getDepositTreeSnapshot().get().getDepositCount())
.isEqualTo(deposits);
assertThat(snapshotResult.getReplayDepositsResult().getLastProcessedDepositIndex())
.contains(BigInteger.valueOf(deposits - 1));
assertThat(snapshotResult.getReplayDepositsResult().getLastProcessedBlockNumber())
.isEqualTo(blockNumber);
});
}

private String getResourceFilePath(final String resource) {
final URL resourceUrl = DepositSnapshotFileLoader.class.getResource(resource);
return resourceUrl.getFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"data": {
"finalized": [
"0x8ba2e667c5fbe7666ff631bbe8fef23f10a9591d0ecbdf6ffe14c02c510b0b16",
"0xc3e847543de3fa849ba7f0e0fc9903027fc4dd4f4dc86a5ce204ab5af0847dd5",
"0x59612f909188dbe6a9a4a537b991cfbe843acf6e0cf5da5927c1f7cfef522edd",
"0xf455e7dae74453fbdb9888a7847862ca7f120419b76766557f325d15f3252d2f",
"0x80ca3f52135dda86aab4c473a0c67a3e362466f74702ee5585cba088657d49e8",
"0x89be2df72904c7baf0d4df9933c801a4d13fa008bd17b0de50a48c689d778ffc",
"0x6c84736312d3ac6019608a222e0c606b3acd6f61ccae7ef9c1d2ad7da3de801f",
"0x32101937fa9b68126ebfd88ecad666836fc6e37488fda70b722e1b33808277b9",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
],
"deposit_root": "0xf50343723e7ac18bf84339c835a944f6829c09ba7624ad18a3210ee70ab58932",
"deposit_count": "1106575",
"execution_block_hash": "0xaf17f4a62338f74919b89d64443f8bce715cf7e0ed7b917d3dabb09b63c89755",
"execution_block_height": "18754822"
}
}

0 comments on commit b490fad

Please sign in to comment.