Skip to content

Commit

Permalink
added test for interrupted storage range requests
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte committed Oct 25, 2023
1 parent 1c5ea06 commit 05f5fb6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ MessageData constructGetStorageRangeResponse(final MessageData message) {
statefulPredicate);
collectedStorages.add(accountStorages);

if (!statefulPredicate.shouldGetMore()) {
if (!statefulPredicate.shouldGetMore() && accountStorages.size() > 0) {
// if we were interrupted in the middle of account storage, save last key
needsProofStorageSlot =
Optional.of(accountStorages.lastKey()).map(Hash::wrap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void assertEmptyRangeLeftProofOfExclusionAndNextAccount() {
}

@Test
public void assertLimitRangeResponse() {
public void assertAccountLimitRangeResponse() {
// assert we limit the range response according to size
final int acctCount = 2000;
final long acctRLPSize = 105;
Expand Down Expand Up @@ -178,6 +178,7 @@ public void assertCompleteStorageForSingleAccount() {
assertThat(slotsData.slots().size()).isEqualTo(1);
var firstAccountStorages = slotsData.slots().first();
assertThat(firstAccountStorages.size()).isEqualTo(10);
// no proofs for complete storage range:
assertThat(slotsData.proofs().size()).isEqualTo(0);

// TODO: fixme, prob a fixture issue with contract storage.
Expand All @@ -186,6 +187,52 @@ public void assertCompleteStorageForSingleAccount() {
// .isTrue();
}

@Test
public void assertStorageLimitRangeResponse() {
// assert we limit the range response according to bytessize
final int storageSlotSize = 70;
final int storageSlotCount = 16;
insertTestAccounts(acct1, acct2, acct3, acct4);

final BytesValueRLPOutput tmp = new BytesValueRLPOutput();
tmp.startList();
tmp.writeBigIntegerScalar(BigInteger.ONE);
tmp.writeBytes(storageTrie.getRootHash());
tmp.writeList(
List.of(acct3.addressHash, acct4.addressHash),
(hash, rlpOutput) -> rlpOutput.writeBytes(hash));
tmp.writeBytes(Hash.ZERO);
tmp.writeBytes(HASH_LAST);
tmp.writeBigIntegerScalar(BigInteger.valueOf(storageSlotCount * storageSlotSize));
tmp.endList();
var tinyRangeLimit = new GetStorageRangeMessage(tmp.encoded());

var rangeData =
(StorageRangeMessage) snapServer.constructGetStorageRangeResponse(tinyRangeLimit);

// assert proofs are valid for the requested range
assertThat(rangeData).isNotNull();
var slotsData = rangeData.slotsData(false);
assertThat(slotsData).isNotNull();
assertThat(slotsData.slots()).isNotNull();
assertThat(slotsData.slots().size()).isEqualTo(2);
var firstAccountStorages = slotsData.slots().first();
// expecting to see complete 10 slot storage for acct3
assertThat(firstAccountStorages.size()).isEqualTo(10);
var secondAccountStorages = slotsData.slots().last();
// expecting to see only 6 since request was limited to 16 slots
assertThat(secondAccountStorages.size()).isEqualTo(6);
// proofs required for interrupted storage range:
assertThat(slotsData.proofs().size()).isNotEqualTo(0);

// TODO: fixme, prob a fixture issue with contract storage.
// assertThat(
// assertIsValidStorageProof(acct4, Hash.ZERO, secondAccountStorages,
// slotsData.proofs()))
// .isTrue();

}

@Test
public void assertAccountTriePathRequest() {
insertTestAccounts(acct1, acct2, acct3, acct4);
Expand Down

0 comments on commit 05f5fb6

Please sign in to comment.