-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Differential testing: Enhance contract bytecode dumper to handl…
…e modular representation (#11523) Signed-off-by: Valentin Tronkov <99957253+vtronkov@users.noreply.github.com>
- Loading branch information
Showing
11 changed files
with
539 additions
and
4 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
39 changes: 39 additions & 0 deletions
39
hedera-node/hedera-app/src/main/java/com/hedera/node/app/bbm/contracts/ByteArrayAsKey.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,39 @@ | ||
/* | ||
* Copyright (C) 2024 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. | ||
*/ | ||
|
||
package com.hedera.node.app.bbm.contracts; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.util.Arrays; | ||
|
||
/** Implements equality-of-content on a byte array so it can be used as a map key */ | ||
public record ByteArrayAsKey(@NonNull byte[] array) { | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
return obj instanceof ByteArrayAsKey other && Arrays.equals(array, other.array); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(array); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ByteArrayAsKey{" + "array=" + Arrays.toString(array) + '}'; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
hedera-node/hedera-app/src/main/java/com/hedera/node/app/bbm/contracts/Contract.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,87 @@ | ||
/* | ||
* Copyright (C) 2024 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. | ||
*/ | ||
|
||
package com.hedera.node.app.bbm.contracts; | ||
|
||
import com.hedera.hapi.node.base.ContractID; | ||
import com.hedera.hapi.node.state.contract.Bytecode; | ||
import com.hedera.node.app.state.merkle.disk.OnDiskKey; | ||
import com.hedera.node.app.state.merkle.disk.OnDiskValue; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.util.Arrays; | ||
import java.util.TreeSet; | ||
import java.util.stream.Collectors; | ||
import org.apache.commons.lang3.builder.EqualsBuilder; | ||
import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
|
||
/** | ||
* A contract - some bytecode associated with its contract id(s) | ||
* | ||
* @param ids - direct from the signed state file there's one contract id for each bytecode, but | ||
* there are duplicates which can be coalesced and then there's a set of ids for the single | ||
* contract; kept in sorted order by the container `TreeSet` so it's easy to get the canonical | ||
* id for the contract, and also you can't forget to process them in a deterministic order | ||
* @param bytecode - bytecode of the contract | ||
* @param validity - whether the contract is valid or note, aka active or deleted | ||
*/ | ||
public record Contract( | ||
@NonNull TreeSet</*@NonNull*/ Integer> ids, @NonNull byte[] bytecode, @NonNull Validity validity) { | ||
|
||
public static Contract fromMod(OnDiskKey<ContractID> id, OnDiskValue<Bytecode> bytecode) { | ||
final var c = new Contract(new TreeSet<>(), bytecode.getValue().code().toByteArray(), Validity.ACTIVE); | ||
if (id.getKey().contractNum() != null) { | ||
c.ids().add(id.getKey().contractNum().intValue()); | ||
} | ||
return c; | ||
} | ||
|
||
// For any set of contract ids with the same bytecode, the lowest contract id is used as the "canonical" | ||
// id for that bytecode (useful for ordering contracts deterministically) | ||
public int canonicalId() { | ||
return ids.first(); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (o == null) { | ||
return false; | ||
} | ||
if (o == this) { | ||
return true; | ||
} | ||
return o instanceof Contract other | ||
&& new EqualsBuilder() | ||
.append(ids, other.ids) | ||
.append(bytecode, other.bytecode) | ||
.append(validity, other.validity) | ||
.isEquals(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder(17, 37) | ||
.append(ids) | ||
.append(bytecode) | ||
.append(validity) | ||
.toHashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
var csvIds = ids.stream().map(Object::toString).collect(Collectors.joining(",")); | ||
return "Contract{ids=(%s), %s, bytecode=%s}".formatted(csvIds, validity, Arrays.toString(bytecode)); | ||
} | ||
} |
Oops, something went wrong.