Skip to content

Commit

Permalink
Adapt leaf key types to follow new eip-6800 spec (#67)
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Pinto <[email protected]>
  • Loading branch information
lu-pinto authored Sep 4, 2024
1 parent 8e9817c commit 3194987
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
*/
package org.hyperledger.besu.ethereum.trie.verkle.adapter;

import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.BALANCE_LEAF_KEY;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.CODE_KECCAK_LEAF_KEY;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.BASIC_DATA_LEAF_KEY;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.CODE_HASH_LEAF_KEY;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.CODE_OFFSET;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.CODE_SIZE_LEAF_KEY;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.HEADER_STORAGE_OFFSET;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.HEADER_STORAGE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.MAIN_STORAGE_OFFSET_SHIFT_LEFT_VERKLE_NODE_WIDTH;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.NONCE_LEAF_KEY;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERKLE_NODE_WIDTH;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERKLE_NODE_WIDTH_LOG2;
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERSION_LEAF_KEY;

import org.hyperledger.besu.ethereum.trie.verkle.hasher.Hasher;

Expand Down Expand Up @@ -145,53 +142,23 @@ public Bytes32 swapLastByte(Bytes32 base, Bytes subIndex) {
}

/**
* Generates a version key for a given address.
* Generates a basic data key for a given address.
*
* @param address The address.
* @return The generated version key.
*/
public Bytes32 versionKey(Bytes address) {
return headerKey(address, VERSION_LEAF_KEY);
public Bytes32 basicDataKey(Bytes address) {
return headerKey(address, BASIC_DATA_LEAF_KEY);
}

/**
* Generates a balance key for a given address.
*
* @param address The address.
* @return The generated balance key.
*/
public Bytes32 balanceKey(Bytes address) {
return headerKey(address, BALANCE_LEAF_KEY);
}

/**
* Generates a nonce key for a given address.
*
* @param address The address.
* @return The generated nonce key.
*/
public Bytes32 nonceKey(Bytes address) {
return headerKey(address, NONCE_LEAF_KEY);
}

/**
* Generates a code Keccak key for a given address.
* Generates a code hash key for a given address.
*
* @param address The address.
* @return The generated code Keccak key.
*/
public Bytes32 codeKeccakKey(Bytes address) {
return headerKey(address, CODE_KECCAK_LEAF_KEY);
}

/**
* Generates a code size key for a given address.
*
* @param address The address.
* @return The generated code size key.
*/
public Bytes32 codeSizeKey(Bytes address) {
return (headerKey(address, CODE_SIZE_LEAF_KEY));
public Bytes32 codeHashKey(Bytes address) {
return headerKey(address, CODE_HASH_LEAF_KEY);
}

public int getNbChunk(Bytes bytecode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
import org.apache.tuweni.units.bigints.UInt256;

public class Parameters {
public static final UInt256 VERSION_LEAF_KEY = UInt256.valueOf(0);
public static final UInt256 BALANCE_LEAF_KEY = UInt256.valueOf(1);
public static final UInt256 NONCE_LEAF_KEY = UInt256.valueOf(2);
public static final UInt256 CODE_KECCAK_LEAF_KEY = UInt256.valueOf(3);
public static final UInt256 CODE_SIZE_LEAF_KEY = UInt256.valueOf(4);
public static final UInt256 BASIC_DATA_LEAF_KEY = UInt256.valueOf(0);
public static final UInt256 CODE_HASH_LEAF_KEY = UInt256.valueOf(1);
public static final UInt256 HEADER_STORAGE_OFFSET = UInt256.valueOf(64);
public static final UInt256 CODE_OFFSET = UInt256.valueOf(128);
public static final UInt256 VERKLE_NODE_WIDTH = UInt256.valueOf(256);
public static final UInt256 VERKLE_NODE_WIDTH_LOG2 = UInt256.valueOf(8);
public static final UInt256 HEADER_STORAGE_OFFSET = UInt256.valueOf(64);
public static final UInt256 MAIN_STORAGE_OFFSET_SHIFT_LEFT_VERKLE_NODE_WIDTH =
UInt256.ONE.shiftLeft(UInt256.valueOf(8 * 31).subtract(VERKLE_NODE_WIDTH_LOG2).intValue());
public static final UInt256 CODE_OFFSET = UInt256.valueOf(128);
public static final UInt256 HEADER_STORAGE_SIZE = CODE_OFFSET.subtract(HEADER_STORAGE_OFFSET);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,19 @@ public void testCodeChunkKey2() {
}

@Test
public void testVersionKey() {
public void testBasicDataKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034400");
assertThat(adapter.versionKey(address)).isEqualTo(expected);
assertThat(adapter.basicDataKey(address)).isEqualTo(expected);
}

@Test
public void testBalanceKey() {
public void testCodeHashKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034401");
assertThat(adapter.balanceKey(address)).isEqualTo(expected);
}

@Test
public void testNonceKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034402");
assertThat(adapter.nonceKey(address)).isEqualTo(expected);
}

@Test
public void testCodeKeccakKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034403");
assertThat(adapter.codeKeccakKey(address)).isEqualTo(expected);
}

@Test
public void testCodeSizeKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034404");
assertThat(adapter.codeSizeKey(address)).isEqualTo(expected);
assertThat(adapter.codeHashKey(address)).isEqualTo(expected);
}

private static final ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,28 @@ public class TrieKeyBatchAdapterTest {
@Test
public void testAccountKeys() {
final List<Bytes32> expectedIndexes = new ArrayList<>();
expectedIndexes.add(Parameters.VERSION_LEAF_KEY);
expectedIndexes.add(Parameters.BALANCE_LEAF_KEY);
expectedIndexes.add(Parameters.NONCE_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_KECCAK_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_SIZE_LEAF_KEY);
expectedIndexes.add(Parameters.BASIC_DATA_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_HASH_LEAF_KEY);

final Map<Bytes32, Bytes32> generatedHashes =
adapter.manyTrieKeyHashes(address, expectedIndexes, new ArrayList<>(), new ArrayList<>());
final TrieKeyAdapter cachedTrieKeyAdapter =
new TrieKeyAdapter(new CachedPedersenHasher(generatedHashes, new FailedHasher()));
assertThat(cachedTrieKeyAdapter.versionKey(address))
assertThat(cachedTrieKeyAdapter.basicDataKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034400"));
assertThat(cachedTrieKeyAdapter.balanceKey(address))
assertThat(cachedTrieKeyAdapter.codeHashKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034401"));
assertThat(cachedTrieKeyAdapter.nonceKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034402"));
assertThat(cachedTrieKeyAdapter.codeKeccakKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034403"));
assertThat(cachedTrieKeyAdapter.codeSizeKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034404"));
}

@Test
public void testAccountKeysWithStorage() {
final List<Bytes32> expectedIndexes = new ArrayList<>();
expectedIndexes.add(Parameters.VERSION_LEAF_KEY);
expectedIndexes.add(Parameters.BALANCE_LEAF_KEY);
expectedIndexes.add(Parameters.NONCE_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_KECCAK_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_SIZE_LEAF_KEY);
expectedIndexes.add(Parameters.BASIC_DATA_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_HASH_LEAF_KEY);

final UInt256 storage = UInt256.valueOf(64);
final UInt256 storage2 =
Expand All @@ -100,26 +82,14 @@ public void testAccountKeysWithStorage() {

final TrieKeyAdapter cachedTrieKeyAdapter =
new TrieKeyAdapter(new CachedPedersenHasher(generatedHashes, new FailedHasher()));
assertThat(cachedTrieKeyAdapter.versionKey(address))
assertThat(cachedTrieKeyAdapter.basicDataKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034400"));
assertThat(cachedTrieKeyAdapter.balanceKey(address))
assertThat(cachedTrieKeyAdapter.codeHashKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034401"));
assertThat(cachedTrieKeyAdapter.nonceKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034402"));
assertThat(cachedTrieKeyAdapter.codeKeccakKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034403"));
assertThat(cachedTrieKeyAdapter.codeSizeKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034404"));
assertThat(cachedTrieKeyAdapter.storageKey(address, storage))
.isEqualTo(
Bytes32.fromHexString(
Expand All @@ -133,11 +103,8 @@ public void testAccountKeysWithStorage() {
@Test
public void testAccountKeysWithCode() {
final List<Bytes32> expectedIndexes = new ArrayList<>();
expectedIndexes.add(Parameters.VERSION_LEAF_KEY);
expectedIndexes.add(Parameters.BALANCE_LEAF_KEY);
expectedIndexes.add(Parameters.NONCE_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_KECCAK_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_SIZE_LEAF_KEY);
expectedIndexes.add(Parameters.BASIC_DATA_LEAF_KEY);
expectedIndexes.add(Parameters.CODE_HASH_LEAF_KEY);

final UInt256 chunkId = UInt256.valueOf(24);
expectedIndexes.add(chunkId);
Expand All @@ -146,26 +113,14 @@ public void testAccountKeysWithCode() {
adapter.manyTrieKeyHashes(address, expectedIndexes, new ArrayList<>(), List.of(chunkId));
final TrieKeyAdapter cachedTrieKeyAdapter =
new TrieKeyAdapter(new CachedPedersenHasher(generatedHashes, new FailedHasher()));
assertThat(cachedTrieKeyAdapter.versionKey(address))
assertThat(cachedTrieKeyAdapter.basicDataKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034400"));
assertThat(cachedTrieKeyAdapter.balanceKey(address))
assertThat(cachedTrieKeyAdapter.codeHashKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034401"));
assertThat(cachedTrieKeyAdapter.nonceKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034402"));
assertThat(cachedTrieKeyAdapter.codeKeccakKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034403"));
assertThat(cachedTrieKeyAdapter.codeSizeKey(address))
.isEqualTo(
Bytes32.fromHexString(
"0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034404"));
assertThat(cachedTrieKeyAdapter.codeChunkKey(address, chunkId))
.isEqualTo(
Bytes32.fromHexString(
Expand All @@ -174,12 +129,12 @@ public void testAccountKeysWithCode() {

private static final ObjectMapper objectMapper = new ObjectMapper();

static class KeyValueData {
public static class KeyValueData {
public String key;
public String value;
}

static class TestCodeData {
public static class TestCodeData {
public String address;
public String bytecode;
public ArrayList<KeyValueData> chunks;
Expand All @@ -188,7 +143,7 @@ static class TestCodeData {
public static List<TestCodeData> JsonContractCodeData() throws IOException {
InputStream inputStream =
TrieKeyBatchAdapterTest.class.getResourceAsStream("/contractCode.json");
return objectMapper.readValue(inputStream, new TypeReference<List<TestCodeData>>() {});
return objectMapper.readValue(inputStream, new TypeReference<>() {});
}

@ParameterizedTest
Expand Down

0 comments on commit 3194987

Please sign in to comment.