Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix contract storage readable kv state slot values #10480

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import lombok.CustomLog;
import lombok.experimental.UtilityClass;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.datatypes.Address;

@CustomLog
Expand Down Expand Up @@ -69,4 +70,14 @@ public static boolean isMirror(final byte[] address) {

return Arrays.equals(MIRROR_PREFIX, 0, 12, address, 0, 12);
}

public static byte[] padToBytes32(byte[] input) {
if (input.length >= Bytes32.SIZE) {
return input;
}
byte[] padded = new byte[Bytes32.SIZE];
int offset = Bytes32.SIZE - input.length;
System.arraycopy(input, 0, padded, offset, input.length);
return padded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

import static com.hedera.mirror.web3.state.Utils.padToBytes32;

import com.hedera.hapi.node.state.contract.SlotKey;
import com.hedera.hapi.node.state.contract.SlotValue;
import com.hedera.mirror.web3.common.ContractCallContext;
Expand Down Expand Up @@ -49,7 +51,7 @@ 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(padToBytes32(byteArr)), Bytes.EMPTY, Bytes.EMPTY))
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import static com.hedera.mirror.web3.state.Utils.padToBytes32;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
Expand Down Expand Up @@ -49,7 +50,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(padToBytes32("123456".getBytes()));
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
Loading