You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/05/30 06:18:56 UTC
[incubator-tuweni] 02/07: Add AccountState eth domain object
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
commit e4f872c898dc39697983ccb3c79b1f4b706b83c7
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Fri May 29 23:05:33 2020 -0700
Add AccountState eth domain object
---
.../java/org/apache/tuweni/eth/AccountState.java | 118 +++++++++++++++++++++
.../org/apache/tuweni/eth/AccountStateTest.java | 37 +++++++
2 files changed, 155 insertions(+)
diff --git a/eth/src/main/java/org/apache/tuweni/eth/AccountState.java b/eth/src/main/java/org/apache/tuweni/eth/AccountState.java
new file mode 100644
index 0000000..ce86002
--- /dev/null
+++ b/eth/src/main/java/org/apache/tuweni/eth/AccountState.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
+ * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to You 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.
+ */
+package org.apache.tuweni.eth;
+
+import static java.util.Objects.requireNonNull;
+
+import org.apache.tuweni.bytes.Bytes;
+import org.apache.tuweni.rlp.RLP;
+import org.apache.tuweni.units.bigints.UInt256;
+import org.apache.tuweni.units.ethereum.Wei;
+
+import java.util.Objects;
+
+/**
+ * State of an account as stored on chain.
+ *
+ */
+public final class AccountState {
+
+ private static final int DEFAULT_VERSION = 0;
+
+ private final UInt256 nonce;
+
+ private final Wei balance;
+
+ private final Hash storageRoot;
+
+ private final Hash codeHash;
+ private final int version;
+
+ /**
+ * Reads an account state from a RLP representation.
+ *
+ * @param bytes the bytes of the serialized acount state.
+ * @return the AccountState read from the bytes.
+ */
+ public static AccountState fromBytes(Bytes bytes) {
+ requireNonNull(bytes);
+ return RLP.decodeList(bytes, reader -> {
+ return new AccountState(
+ reader.readUInt256(),
+ Wei.valueOf(reader.readUInt256()),
+ Hash.fromBytes(reader.readValue()),
+ Hash.fromBytes(reader.readValue()),
+ reader.isComplete() ? DEFAULT_VERSION : reader.readInt());
+ });
+ }
+
+ /**
+ * Default constructor for account state without a version.
+ *
+ * @param nonce the nonce of the account.
+ * @param balance the balance of the account.
+ * @param storageRoot the root hash of the storage.
+ * @param codeHash hash of the code of the account.
+ */
+ public AccountState(UInt256 nonce, Wei balance, Hash storageRoot, Hash codeHash) {
+ this(nonce, balance, storageRoot, codeHash, DEFAULT_VERSION);
+ }
+
+ /**
+ * Default constructor for account state.
+ *
+ * @param nonce the nonce of the account.
+ * @param balance the balance of the account.
+ * @param storageRoot the root hash of the storage.
+ * @param codeHash hash of the code of the account.
+ * @param version the version of the account state.
+ */
+ public AccountState(UInt256 nonce, Wei balance, Hash storageRoot, Hash codeHash, int version) {
+ this.nonce = nonce;
+ this.balance = balance;
+ this.storageRoot = storageRoot;
+ this.codeHash = codeHash;
+ this.version = version;
+ }
+
+ public Bytes toBytes() {
+ return RLP.encodeList(writer -> {
+ writer.writeValue(nonce.toMinimalBytes());
+ writer.writeValue(balance.toMinimalBytes());
+ writer.writeValue(storageRoot);
+ writer.writeValue(codeHash);
+ if (version != DEFAULT_VERSION) {
+ writer.writeInt(version);
+ }
+ });
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o)
+ return true;
+ if (o == null || getClass() != o.getClass())
+ return false;
+ AccountState that = (AccountState) o;
+ return version == that.version
+ && Objects.equals(nonce, that.nonce)
+ && Objects.equals(balance, that.balance)
+ && Objects.equals(storageRoot, that.storageRoot)
+ && Objects.equals(codeHash, that.codeHash);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(nonce, balance, storageRoot, codeHash, version);
+ }
+}
diff --git a/eth/src/test/java/org/apache/tuweni/eth/AccountStateTest.java b/eth/src/test/java/org/apache/tuweni/eth/AccountStateTest.java
new file mode 100644
index 0000000..85138fb
--- /dev/null
+++ b/eth/src/test/java/org/apache/tuweni/eth/AccountStateTest.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
+ * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to You 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.
+ */
+package org.apache.tuweni.eth;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.tuweni.bytes.Bytes;
+import org.apache.tuweni.bytes.Bytes32;
+import org.apache.tuweni.units.bigints.UInt256;
+import org.apache.tuweni.units.ethereum.Wei;
+
+import org.junit.jupiter.api.Test;
+
+class AccountStateTest {
+
+ @Test
+ void roundtripRLP() {
+ AccountState state = new AccountState(
+ UInt256.fromBytes(Bytes32.random()),
+ Wei.valueOf(32L),
+ Hash.fromBytes(Bytes32.random()),
+ Hash.fromBytes(Bytes32.random()),
+ 2);
+ Bytes message = state.toBytes();
+ assertEquals(state, AccountState.fromBytes(message));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org