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

add suffix tree decoder #74

Merged
merged 4 commits into from
Oct 14, 2024
Merged
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
@@ -0,0 +1,69 @@
/*
* Copyright Hyperledger Besu Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.verkle.util;

import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.BALANCE_BYTE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.BALANCE_OFFSET;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.CODE_SIZE_BYTE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.CODE_SIZE_OFFSET;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.NONCE_BYTE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.NONCE_OFFSET;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.VERSION_BYTE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.VERSION_OFFSET;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.bytes.MutableBytes32;

public class SuffixTreeDecoder {

private static final Bytes32 VERSION_VALUE_MASK;
private static final Bytes32 CODE_SIZE_VALUE_MASK;
private static final Bytes32 NONCE_VALUE_MASK;
private static final Bytes32 BALANCE_VALUE_MASK;

static {
VERSION_VALUE_MASK = createMask(VERSION_OFFSET, VERSION_BYTE_SIZE);
CODE_SIZE_VALUE_MASK = createMask(CODE_SIZE_OFFSET, CODE_SIZE_BYTE_SIZE);
NONCE_VALUE_MASK = createMask(NONCE_OFFSET, NONCE_BYTE_SIZE);
BALANCE_VALUE_MASK = createMask(BALANCE_OFFSET, BALANCE_BYTE_SIZE);
}

private static Bytes32 createMask(final int offset, final int size) {
final MutableBytes32 value = MutableBytes32.create();
value.set(offset, Bytes.repeat((byte) 0xff, size));
return value;
}

public static Bytes[] decodeBasicDataLeaf(final Bytes32 value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how are you using this on the caller side, don't you need to convert from Bytes -> int, long, UInt256, etc... ? Fields have different sizes so I'm not sure if it won't be beneficial to have a method for each value:

int decodeCodeSize()
long decodeNonce()
Wei decodeBalance()
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the moment I prefer to let the logic of the conversion to the VerkleAccount decoder class in Besu. will see later if it's better to move it into this class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add the two way, an user can chose

Bytes[] decodedFields = new Bytes[4];

decodedFields[0] =
extractField(value, VERSION_OFFSET, VERSION_VALUE_MASK).slice(0, VERSION_BYTE_SIZE);
decodedFields[1] =
extractField(value, CODE_SIZE_OFFSET, CODE_SIZE_VALUE_MASK).slice(0, CODE_SIZE_BYTE_SIZE);
decodedFields[2] =
extractField(value, NONCE_OFFSET, NONCE_VALUE_MASK).slice(0, NONCE_BYTE_SIZE);
decodedFields[3] =
extractField(value, BALANCE_OFFSET, BALANCE_VALUE_MASK).slice(0, BALANCE_BYTE_SIZE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're slicing do you need to shift them? Can't you just slice on the range? E.g. for code size value.slice(CODE_SIZE_OFFSET, CODE_SIZE_BYTE_SIZE) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I modified the code


return decodedFields;
}

private static Bytes extractField(final Bytes32 value, int offset, Bytes32 mask) {
return value.and(mask).shiftLeft(offset * 8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Hyperledger Besu Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.verkle.util;

public class SuffixTreeDescriptor {

protected static final int VERSION_OFFSET = 0;
protected static final int VERSION_BYTE_SIZE = 1;
protected static final int CODE_SIZE_OFFSET = 5;
protected static final int CODE_SIZE_BYTE_SIZE = 3;
protected static final int NONCE_OFFSET = 8;
protected static final int NONCE_BYTE_SIZE = 8;
protected static final int BALANCE_OFFSET = 16;
protected static final int BALANCE_BYTE_SIZE = 16;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
*/
package org.hyperledger.besu.ethereum.trie.verkle.util;

import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.BALANCE_BYTE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.BALANCE_OFFSET;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.CODE_SIZE_BYTE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.CODE_SIZE_OFFSET;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.NONCE_BYTE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.NONCE_OFFSET;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.VERSION_BYTE_SIZE;
import static org.hyperledger.besu.ethereum.trie.verkle.util.SuffixTreeDescriptor.VERSION_OFFSET;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

public class SuffixTreeEncoder {
private static final int VERSION_OFFSET = 0;
private static final int VERSION_BYTE_SIZE = 1;
private static final int CODE_SIZE_OFFSET = 5;
private static final int CODE_SIZE_BYTE_SIZE = 3;
private static final int NONCE_OFFSET = 8;
private static final int NONCE_BYTE_SIZE = 8;
private static final int BALANCE_OFFSET = 16;
private static final int BALANCE_BYTE_SIZE = 16;

private static final Bytes32 VERSION_VALUE_MASK;
private static final Bytes32 CODE_SIZE_VALUE_MASK;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright Hyperledger Besu Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.verkle.util;

import java.util.stream.Stream;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class SuffixTreeDecoderTest {

public static Stream<Arguments> decodeValues() {
return Stream.of(
Arguments.of(
Bytes32.ZERO,
new Bytes[] {
Bytes.of(0x00), // Version
Bytes.repeat((byte) 0x00, 3), // Code Size
Bytes.repeat((byte) 0x00, 8), // Nonce
Bytes.repeat((byte) 0x00, 16) // Balance
}),
Arguments.of(
Bytes32.fromHexString(
"0x0A00000000000000000000000000000000000000000000000000000000000000"),
new Bytes[] {
Bytes.of(10), // Version
Bytes.repeat((byte) 0x00, 3), // Code Size
Bytes.repeat((byte) 0x00, 8), // Nonce
Bytes.repeat((byte) 0x00, 16) // Balance
}),
Arguments.of(
Bytes32.fromHexString(
"0x0000000000000036000000000000000000000000000000000000000000000000"),
new Bytes[] {
Bytes.of(0x00), // Version
Bytes.of(0x00, 0x00, 54), // Code Size
Bytes.repeat((byte) 0x00, 8), // Nonce
Bytes.repeat((byte) 0x00, 16) // Balance
}),
Arguments.of(
Bytes32.fromHexString(
"0x0000000000000000000000000000001400000000000000000000000000000000"),
new Bytes[] {
Bytes.of(0x00), // Version
Bytes.repeat((byte) 0x00, 3), // Code Size
Bytes.fromHexString("0x0000000000000014"), // Nonce
Bytes.repeat((byte) 0x00, 16) // Balance
}),
Arguments.of(
Bytes32.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000A00"),
new Bytes[] {
Bytes.of(0x00), // Version
Bytes.repeat((byte) 0x00, 3), // Code Size
Bytes.repeat((byte) 0x00, 8), // Nonce
Bytes.fromHexString("0x00000000000000000000000000000a00") // Balance
}),
Arguments.of(
Bytes32.fromHexString(
"0xAB343123BD1231214BC13213EF23434FF213124423247124FF12312EE12AC234"),
new Bytes[] {
Bytes.fromHexString("0xAB"), // Version
Bytes.fromHexString("0x123121"), // Code Size
Bytes.fromHexString("0x4bc13213ef23434f"), // Nonce
Bytes.fromHexString("0xf213124423247124ff12312ee12ac234") // Balance
}));
}

@ParameterizedTest
@MethodSource("decodeValues")
void decodeBasicDataLeafTest(final Bytes32 basicDataLeafValue, final Bytes[] expected) {
Bytes[] decodedValues = SuffixTreeDecoder.decodeBasicDataLeaf(basicDataLeafValue);
for (int i = 0; i < expected.length; i++) {
Assertions.assertThat(expected[i]).isEqualTo(decodedValues[i]);
}
}
}
Loading