Skip to content

Commit

Permalink
Initial merge, with data-compressing keys/values
Browse files Browse the repository at this point in the history
Signed-off-by: tinker-michaelj <michael.tinker@hedera.com>
  • Loading branch information
tinker-michaelj committed Oct 19, 2021
2 parents d90cea3 + aa306ea commit 5db3537
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ hedera-node/data/onboard/feeSchedule.txt
hedera-node/data/onboard/GenesisPub32Key.txt
hedera-node/data/recordstreams/
hedera-node/data/saved/
hedera-node/data/jasperdb/
hedera-node/output/
hedera-node/target/
hedera-node/src/test/resources/diskFs
Expand Down
8 changes: 8 additions & 0 deletions hedera-node/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@
<groupId>com.swirlds</groupId>
<artifactId>swirlds-fchashmap</artifactId>
</dependency>
<dependency>
<groupId>com.swirlds</groupId>
<artifactId>swirlds-jasperdb</artifactId>
</dependency>
<dependency>
<groupId>com.swirlds</groupId>
<artifactId>swirlds-merkle</artifactId>
Expand All @@ -265,6 +269,10 @@
<groupId>com.swirlds</groupId>
<artifactId>swirlds-fcqueue</artifactId>
</dependency>
<dependency>
<groupId>com.swirlds</groupId>
<artifactId>swirlds-virtualmap</artifactId>
</dependency>
<dependency>
<groupId>com.hedera.hashgraph</groupId>
<artifactId>ethereumj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class StateChildren {
private MerkleMap<EntityNumPair, MerkleUniqueToken> uniqueTokens;
private MerkleMap<EntityNum, MerkleSchedule> schedules;
private VirtualMap<VirtualBlobKey, VirtualBlobValue> storage;
private MerkleMap<EntityNumPair, MerkleTokenRelStatus> tokenAssociations;
private VirtualMap<ContractKey, ContractValue> contractStorage;
private MerkleMap<EntityNumPair, MerkleTokenRelStatus> tokenAssociations;
private FCOneToManyRelation<EntityNum, Long> uniqueTokenAssociations;
private FCOneToManyRelation<EntityNum, Long> uniqueOwnershipAssociations;
private FCOneToManyRelation<EntityNum, Long> uniqueOwnershipTreasuryAssociations;
Expand All @@ -64,14 +64,6 @@ public class StateChildren {
private MerkleSpecialFiles specialFiles;
private RecordsRunningHashLeaf runningHashLeaf;

public VirtualMap<ContractKey, ContractValue> getContractStorage() {
return contractStorage;
}

public void setContractStorage(VirtualMap<ContractKey, ContractValue> contractStorage) {
this.contractStorage = contractStorage;
}

public MerkleMap<EntityNum, MerkleAccount> getAccounts() {
Objects.requireNonNull(accounts);
return accounts;
Expand Down Expand Up @@ -117,6 +109,15 @@ public void setStorage(VirtualMap<VirtualBlobKey, VirtualBlobValue> storage) {
this.storage = storage;
}

public void setContractStorage(VirtualMap<ContractKey, ContractValue> contractStorage) {
this.contractStorage = contractStorage;
}

public VirtualMap<ContractKey, ContractValue> getContractStorage() {
Objects.requireNonNull(contractStorage);
return contractStorage;
}

public MerkleMap<EntityNumPair, MerkleTokenRelStatus> getTokenAssociations() {
Objects.requireNonNull(tokenAssociations);
return tokenAssociations;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.hedera.services.contracts.virtual;

/*-
* ‌
* Hedera Services Node
* ​
* Copyright (C) 2018 - 2021 Hedera Hashgraph, LLC
* ​
* 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.
* ‍
*/

import com.swirlds.common.io.SerializableDataInputStream;
import com.swirlds.common.io.SerializableDataOutputStream;
import com.swirlds.virtualmap.VirtualKey;
import org.hyperledger.besu.datatypes.Address;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;

public class ContractKey implements VirtualKey {
static final long RUNTIME_CONSTRUCTABLE_ID = 0xbd02f789da2d61a5L;
static final int RELEASE_0200_VERSION = 1;

private static final int KEY_SIZE = 32;
private static final int SIZE = Address.SIZE + KEY_SIZE;

private byte[] contract;

private byte[] key;

public ContractKey() {
this.contract = new byte[Address.SIZE];
this.key = new byte[KEY_SIZE];
}

public ContractKey(byte[] contract, byte[] key) {
this.setContract(contract);
this.setKey(key);
}

private void setContract(byte[] contract) {
Objects.requireNonNull(contract);
if (contract.length != Address.SIZE) {
throw new IllegalArgumentException("invalid contract address length");
}
this.contract = contract;
}

private void setKey(byte[] key) {
Objects.requireNonNull(key);
if (key.length != KEY_SIZE) {
throw new IllegalArgumentException("invalid key length");
}
this.key = key;
}

@Override
public void serialize(ByteBuffer byteBuffer) throws IOException {
byteBuffer.put(contract);
byteBuffer.put(key);
}

@Override
public void deserialize(ByteBuffer byteBuffer, int i) throws IOException {
byteBuffer.get(contract);
byteBuffer.get(key);
}

@Override
public boolean equals(ByteBuffer byteBuffer, int i) throws IOException {
byte[] contract = new byte[Address.SIZE];
byteBuffer.get(contract);
byte[] key = new byte[KEY_SIZE];
byteBuffer.get(key);

return Arrays.equals(this.contract, contract) && Arrays.equals(this.key, key);
}

@Override
public void deserialize(SerializableDataInputStream serializableDataInputStream, int i) throws IOException {
serializableDataInputStream.read(contract);
serializableDataInputStream.read(contract);
}

@Override
public long getClassId() {
return RUNTIME_CONSTRUCTABLE_ID;
}

@Override
public void serialize(SerializableDataOutputStream serializableDataOutputStream) throws IOException {
serializableDataOutputStream.write(contract);
serializableDataOutputStream.write(key);
}

@Override
public int getVersion() {
return RELEASE_0200_VERSION;
}

@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(this.contract), Arrays.hashCode(this.key));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ContractKey that = (ContractKey) o;
return Arrays.equals(this.contract, that.contract) && Arrays.equals(this.key, that.key);
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Uint256Key{");
sb.append("contract:(");
for (int i = 0; i < 20; i++) {
sb.append(String.format("%02X ", this.contract[i]).toUpperCase());
}
sb.append("),");
sb.append("key:(");
for (int i = 0; i < 32; i++) {
sb.append(String.format("%02X ", this.key[i]).toUpperCase());
}
sb.append(")}");

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.hedera.services.contracts.virtual;

/*-
* ‌
* Hedera Services Node
* ​
* Copyright (C) 2018 - 2021 Hedera Hashgraph, LLC
* ​
* 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.
* ‍
*/

import com.swirlds.common.io.SerializableDataOutputStream;
import com.swirlds.jasperdb.files.hashmap.KeySerializer;

import java.io.IOException;
import java.nio.ByteBuffer;

public class ContractKeySerializer implements KeySerializer<ContractKey> {
@Override
public int deserializeKeySize(ByteBuffer byteBuffer) {
return 52; // TODO:
}

@Override
public int getSerializedSize() {
return 52; // TODO:
}

@Override
public long getCurrentDataVersion() {
return 1;
}

@Override
public ContractKey deserialize(ByteBuffer byteBuffer, long dataVersion) throws IOException {
final ContractKey key = new ContractKey();
key.deserialize(byteBuffer, (int) dataVersion);
return key;
}

@Override
public int serialize(ContractKey contractKey, SerializableDataOutputStream serializableDataOutputStream) throws IOException {
contractKey.serialize(serializableDataOutputStream);
return 52; // TODO:
}
}
Loading

0 comments on commit 5db3537

Please sign in to comment.