Skip to content

Commit

Permalink
Fix contract storage readable kv state slot values (#10480)
Browse files Browse the repository at this point in the history
This PR fixes slot values in ContractStorageReadableKVState and expand the slot values to 32 bytes.

---------

Signed-off-by: Kristiyan Selveliev <[email protected]>
  • Loading branch information
kselveliev authored Feb 25, 2025
1 parent ba25c44 commit b7f560a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.hedera.mirror.web3.state.keyvalue;

import static com.hedera.mirror.common.util.DomainUtils.leftPadBytes;

import com.hedera.hapi.node.state.contract.SlotKey;
import com.hedera.hapi.node.state.contract.SlotValue;
import com.hedera.mirror.web3.common.ContractCallContext;
Expand All @@ -10,6 +12,7 @@
import com.hedera.services.utils.EntityIdUtils;
import jakarta.annotation.Nonnull;
import jakarta.inject.Named;
import org.apache.tuweni.bytes.Bytes32;

@Named
public class ContractStorageReadableKVState extends AbstractReadableKVState<SlotKey, SlotValue> {
Expand All @@ -35,7 +38,8 @@ protected SlotValue readFromDataSource(@Nonnull SlotKey slotKey) {
return timestamp
.map(t -> contractStateRepository.findStorageByBlockTimestamp(entityId, keyBytes, t))
.orElse(contractStateRepository.findStorage(entityId, keyBytes))
.map(byteArr -> new SlotValue(Bytes.wrap(byteArr), Bytes.EMPTY, Bytes.EMPTY))
.map(byteArr ->
new SlotValue(Bytes.wrap(leftPadBytes(byteArr, Bytes32.SIZE)), Bytes.EMPTY, Bytes.EMPTY))
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.hedera.mirror.web3.state.keyvalue;

import static com.hedera.mirror.common.util.DomainUtils.leftPadBytes;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
Expand All @@ -19,6 +20,7 @@
import com.hedera.pbj.runtime.io.buffer.Bytes;
import java.util.Collections;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -35,7 +37,7 @@ class ContractStorageReadableKVStateTest {

private static final ContractID CONTRACT_ID =
new ContractID(1L, 0L, new OneOf<>(ContractOneOfType.CONTRACT_NUM, 1L));
private static final Bytes BYTES = Bytes.fromBase64("123456");
private static final Bytes BYTES = Bytes.wrap(leftPadBytes("123456".getBytes(), Bytes32.SIZE));
private static final SlotKey SLOT_KEY = new SlotKey(CONTRACT_ID, BYTES);
private static final EntityId ENTITY_ID =
EntityId.of(CONTRACT_ID.shardNum(), CONTRACT_ID.realmNum(), CONTRACT_ID.contractNum());
Expand Down

0 comments on commit b7f560a

Please sign in to comment.