Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 17 additions & 1 deletion actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_TVM_BLOB: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_0)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_TVM_BLOB]");
}
if (dynamicPropertiesStore.getAllowTvmBlob() == 1) {
throw new ContractValidateException(
"[ALLOW_TVM_BLOB] has been valid, no need to propose again");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_TVM_BLOB] is only allowed to be 1");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -905,7 +920,8 @@ public enum ProposalType { // current value, value range
MAX_CREATE_ACCOUNT_TX_SIZE(82), // [500, 10000]
ALLOW_TVM_CANCUN(83), // 0, 1
ALLOW_STRICT_MATH(87), // 0, 1
CONSENSUS_LOGIC_OPTIMIZATION(88); // 0, 1
CONSENSUS_LOGIC_OPTIMIZATION(88), // 0, 1
ALLOW_TVM_BLOB(89); // 0, 1

private long code;

Expand Down
4 changes: 4 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/Op.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public class Op {
public static final int SELFBALANCE = 0x47;
// (0x48) Get block's basefee
public static final int BASEFEE = 0x48;
// (0x49) Get blob hash
public static final int BLOBHASH = 0x49;
// (0x4a) Get block's blob basefee
public static final int BLOBBASEFEE = 0x4a;

/* Memory, Storage and Flow Operations */
// (0x50) Remove item from stack
Expand Down
11 changes: 11 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/OperationActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,17 @@ public static void mCopyAction(Program program) {
program.step();
}

public static void blobHashAction(Program program) {
program.stackPop();
program.stackPush(DataWord.ZERO());
program.step();
}

public static void blobBaseFeeAction(Program program) {
program.stackPush(DataWord.ZERO());
program.step();
}

public static void push0Action(Program program) {
program.stackPush(DataWord.ZERO());
program.step();
Expand Down
13 changes: 13 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/OperationRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ public static void adjustForFairEnergy(JumpTable table) {

public static void appendCancunOperations(JumpTable table) {
BooleanSupplier proposal = VMConfig::allowTvmCancun;
BooleanSupplier tvmBlobProposal = VMConfig::allowTvmBlob;

table.set(new Operation(
Op.TLOAD, 1, 1,
Expand All @@ -681,5 +682,17 @@ public static void appendCancunOperations(JumpTable table) {
EnergyCost::getMCopyCost,
OperationActions::mCopyAction,
proposal));

table.set(new Operation(
Op.BLOBHASH, 1, 1,
EnergyCost::getVeryLowTierCost,
OperationActions::blobHashAction,
tvmBlobProposal));

table.set(new Operation(
Op.BLOBBASEFEE, 0, 1,
EnergyCost::getBaseTierCost,
OperationActions::blobBaseFeeAction,
tvmBlobProposal));
}
}
55 changes: 55 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.tron.core.vm;

import static java.util.Arrays.copyOfRange;
import static org.tron.common.crypto.ckzg4844.CKZG4844JNI.BLS_MODULUS;
import static org.tron.common.crypto.ckzg4844.CKZG4844JNI.FIELD_ELEMENTS_PER_BLOB;
import static org.tron.common.math.Maths.max;
import static org.tron.common.math.Maths.min;
import static org.tron.common.runtime.vm.DataWord.WORD_SIZE;
Expand Down Expand Up @@ -32,6 +34,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.tron.common.crypto.ckzg4844.CKZG4844JNI;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -104,6 +107,7 @@ public class PrecompiledContracts {

private static final EthRipemd160 ethRipemd160 = new EthRipemd160();
private static final Blake2F blake2F = new Blake2F();
private static final KZGPointEvaluation kzgPointEvaluation = new KZGPointEvaluation();

// FreezeV2 PrecompileContracts
private static final GetChainParameter getChainParameter = new GetChainParameter();
Expand Down Expand Up @@ -198,6 +202,9 @@ public class PrecompiledContracts {
private static final DataWord blake2FAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000020009");

private static final DataWord kzgPointEvaluationAddr = new DataWord(
"000000000000000000000000000000000000000000000000000000000002000a");

public static PrecompiledContract getOptimizedContractForConstant(PrecompiledContract contract) {
try {
Constructor<?> constructor = contract.getClass().getDeclaredConstructor();
Expand Down Expand Up @@ -279,6 +286,9 @@ public static PrecompiledContract getContractForAddress(DataWord address) {
if (VMConfig.allowTvmCompatibleEvm() && address.equals(blake2FAddr)) {
return blake2F;
}
if (VMConfig.allowTvmBlob() && address.equals(kzgPointEvaluationAddr)) {
return kzgPointEvaluation;
}

if (VMConfig.allowTvmFreezeV2()) {
if (address.equals(getChainParameterAddr)) {
Expand Down Expand Up @@ -2191,4 +2201,49 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
}
}

public static class KZGPointEvaluation extends PrecompiledContract {

private static final int BLOB_VERIFY_INPUT_LENGTH = 192;
private static final byte BLOB_COMMITMENT_VERSION_KZG = 0x01;
private static final byte[] BLOB_PRECOMPILED_RETURN_VALUE =
ByteUtil.merge(ByteUtil.longTo32Bytes(FIELD_ELEMENTS_PER_BLOB),
ByteUtil.bigIntegerToBytes(BLS_MODULUS, 32));

@Override
public long getEnergyForData(byte[] data) {
return 50000;
}

@Override
public Pair<Boolean, byte[]> execute(byte[] data) {
if (data == null || data.length != BLOB_VERIFY_INPUT_LENGTH) {
return Pair.of(false, DataWord.ZERO().getData());
}

byte[] versionedHash = parseBytes(data, 0, 32);
byte[] z = parseBytes(data, 32, 32);
byte[] y = parseBytes(data, 64, 32);
byte[] commitment = parseBytes(data, 96, 48);
byte[] proof = parseBytes(data, 144, 48);

byte[] hash = Sha256Hash.hash(
CommonParameter.getInstance().isECKeyCryptoEngine(), commitment);
hash[0] = BLOB_COMMITMENT_VERSION_KZG;
if (!Arrays.equals(versionedHash, hash)) {
return Pair.of(false, DataWord.ZERO().getData());
}

try {
if (CKZG4844JNI.verifyKzgProof(commitment, z, y, proof)) {
return Pair.of(true, BLOB_PRECOMPILED_RETURN_VALUE);
} else {
return Pair.of(false, DataWord.ZERO().getData());
}
} catch (RuntimeException exception) {
logger.warn("KZG point evaluation precompile contract failed", exception);
return Pair.of(false, DataWord.ZERO().getData());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void load(StoreFactory storeFactory) {
VMConfig.initAllowStrictMath(ds.getAllowStrictMath());
VMConfig.initAllowTvmCancun(ds.getAllowTvmCancun());
VMConfig.initDisableJavaLangMath(ds.getConsensusLogicOptimization());
VMConfig.initAllowTvmBlob(ds.getAllowTvmBlob());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>

private static final byte[] ALLOW_TVM_CANCUN = "ALLOW_TVM_CANCUN".getBytes();

private static final byte[] ALLOW_TVM_BLOB = "ALLOW_TVM_BLOB".getBytes();

@Autowired
private DynamicPropertiesStore(@Value("properties") String dbName) {
super(dbName);
Expand Down Expand Up @@ -2933,6 +2935,17 @@ public long getAllowTvmCancun() {
.orElse(CommonParameter.getInstance().getAllowTvmCancun());
}

public void saveAllowTvmBlob(long allowTvmBlob) {
this.put(ALLOW_TVM_BLOB, new BytesCapsule(ByteArray.fromLong(allowTvmBlob)));
}

public long getAllowTvmBlob() {
return Optional.ofNullable(getUnchecked(ALLOW_TVM_BLOB))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElse(CommonParameter.getInstance().getAllowTvmBlob());
}

private static class DynamicResourceProperties {

private static final byte[] ONE_DAY_NET_LIMIT = "ONE_DAY_NET_LIMIT".getBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,10 @@ public class CommonParameter {
@Setter
public long allowTvmCancun;

@Getter
@Setter
public long allowTvmBlob;

private static double calcMaxTimeRatio() {
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
return 5.0;
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,6 @@ public class Constant {
= "committee.consensusLogicOptimization";

public static final String COMMITTEE_ALLOW_TVM_CANCUN = "committee.allowTvmCancun";

public static final String COMMITTEE_ALLOW_TVM_BLOB = "committee.allowTvmBlob";
}
10 changes: 10 additions & 0 deletions common/src/main/java/org/tron/core/vm/config/VMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class VMConfig {

private static Boolean DISABLE_JAVA_LANG_MATH = false;

private static boolean ALLOW_TVM_BLOB = false;

private VMConfig() {
}

Expand Down Expand Up @@ -160,6 +162,10 @@ public static void initDisableJavaLangMath(long allow) {
DISABLE_JAVA_LANG_MATH = allow == 1;
}

public static void initAllowTvmBlob(long allow) {
ALLOW_TVM_BLOB = allow == 1;
}

public static boolean getEnergyLimitHardFork() {
return CommonParameter.ENERGY_LIMIT_HARD_FORK;
}
Expand Down Expand Up @@ -251,4 +257,8 @@ public static boolean allowTvmCancun() {
public static boolean disableJavaLangMath() {
return DISABLE_JAVA_LANG_MATH;
}

public static boolean allowTvmBlob() {
return ALLOW_TVM_BLOB;
}
}
Loading