forked from hashgraph/hedera-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(state): add virtual map for contract key/values
Signed-off-by: failfmi <oscurocalma@gmail.com>
- Loading branch information
Showing
10 changed files
with
416 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
hedera-node/src/main/java/com/hedera/services/contracts/virtual/ContractKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 != 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(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
hedera-node/src/main/java/com/hedera/services/contracts/virtual/ContractKeySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} |
Oops, something went wrong.