Skip to content

Commit

Permalink
Merge pull request #192 from vision-consensus/feature/mainnet_1.2.1
Browse files Browse the repository at this point in the history
Feature/mainnet 1.2.1 to master
  • Loading branch information
linusoutman authored Aug 4, 2023
2 parents 636915c + 59bc6a5 commit 1277e31
Show file tree
Hide file tree
Showing 21 changed files with 541 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public class ChainBaseManager {
@Getter
private AccountFrozenStageResourceStore accountFrozenStageResourceStore;

@Autowired
@Getter
private FreezeAccountStore freezeAccountStore;


public void closeOneStore(IVisionChainBase database) {
logger.info("******** begin to close " + database.getName() + " ********");
try {
Expand Down Expand Up @@ -229,6 +234,7 @@ public void closeAllStore() {
closeOneStore(pbftSignDataStore);
closeOneStore(sectionBloomStore);
closeOneStore(accountFrozenStageResourceStore);
closeOneStore(freezeAccountStore);
}

// for test only
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.vision.core.capsule;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.extern.slf4j.Slf4j;
import org.vision.protos.Protocol.FreezeAccount;

import java.util.List;


@Slf4j(topic = "capsule")
public class FreezeAccountCapsule implements ProtoCapsule<FreezeAccount> {

private FreezeAccount freezeAccount;

public FreezeAccountCapsule(final FreezeAccount freezeAccount) {
this.freezeAccount = freezeAccount;
}

public FreezeAccountCapsule(final byte[] data) {
try {
this.freezeAccount = FreezeAccount.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
logger.debug(e.getMessage(), e);
}
}

public FreezeAccountCapsule(List<ByteString> freezeAccounts) {
this.freezeAccount = FreezeAccount.newBuilder()
.addAllFreezeAccounts(freezeAccounts)
.build();
}


public void setAllAddresses(List<ByteString> freezeAccounts) {
this.freezeAccount = this.freezeAccount.toBuilder()
.clearFreezeAccounts()
.addAllFreezeAccounts(freezeAccounts)
.build();
}

public void addAddress(ByteString freezeAddress) {
this.freezeAccount = this.freezeAccount.toBuilder()
.addFreezeAccounts(freezeAddress)
.build();
}

public void addAllAddress(List<ByteString> freezeAccounts) {
this.freezeAccount = this.freezeAccount.toBuilder()
.addAllFreezeAccounts(freezeAccounts)
.build();
}

public List<ByteString> getAddressesList() {
return this.freezeAccount.getFreezeAccountsList();
}

public boolean checkFreeze(ByteString address) {
return this.getAddressesList().contains(address);
}

public byte[] createFreezeAccountDbKey() {
return "FREEZE_ACCOUNT_KEY".getBytes();
}

@Override
public byte[] getData() {
return this.freezeAccount.toByteArray();
}

@Override
public FreezeAccount getInstance() {
return this.freezeAccount;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2941,6 +2941,43 @@ public Long getRefreezeConsiderationPeriodResult() {
return getRefreezeConsiderationPeriod() * FROZEN_PERIOD;
}

public void saveAllowFreezeAccount(Long value) {
this.put(DynamicResourceProperties.ALLOW_FREEZE_ACCOUNT, new BytesCapsule(ByteArray.fromLong(value)));
}

public boolean supportFreezeAccount() {
return getAllowFreezeAccount() == 1;
}

public long getAllowFreezeAccount() {
return Optional.ofNullable(getUnchecked(DynamicResourceProperties.ALLOW_FREEZE_ACCOUNT))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElse(0L);
}

public void saveFreezeAccountOwner(String value) {
this.put(DynamicResourceProperties.FREEZE_ACCOUNT_OWNER, new BytesCapsule(ByteArray.fromString(value)));
}

public String getFreezeAccountOwner() {
return Optional.ofNullable(getUnchecked(DynamicResourceProperties.FREEZE_ACCOUNT_OWNER))
.map(BytesCapsule::getData)
.map(ByteArray::toStr)
.orElse("");
}

public void saveFreezeAccountList(String value) {
this.put(DynamicResourceProperties.FREEZE_ACCOUNT_LIST, new BytesCapsule(ByteArray.fromString(value)));
}

public String getFreezeAccountList() {
return Optional.ofNullable(getUnchecked(DynamicResourceProperties.FREEZE_ACCOUNT_LIST))
.map(BytesCapsule::getData)
.map(ByteArray::toStr)
.orElse("");
}

public void saveAllowVPFreezeStageWeight(Long value) {
this.put(DynamicResourceProperties.ALLOW_VP_FREEZE_STAGE_WEIGHT,
new BytesCapsule(ByteArray.fromLong(value)));
Expand Down Expand Up @@ -3356,6 +3393,11 @@ private static class DynamicResourceProperties {
public static final byte[] TOTAL_FREEZE_STAGE5_ENTROPY_WEIGHT = "TOTAL_FREEZE_STAGE5_ENTROPY_WEIGHT".getBytes();
public static final byte[] ALLOW_ETHEREUM_COMPATIBLE_TRANSACTION_NATIVE_STEP1 = "ALLOW_ETHEREUM_COMPATIBLE_TRANSACTION_NATIVE_STEP1".getBytes();
public static final byte[] MODIFY_SPREAD_MINT_PARENT_FEE = "MODIFY_SPREAD_MINT_PARENT_FEE".getBytes();

private static final byte[] ALLOW_FREEZE_ACCOUNT = "ALLOW_FREEZE_ACCOUNT".getBytes();
private static final byte[] FREEZE_ACCOUNT_OWNER = "FREEZE_ACCOUNT_OWNER".getBytes();
private static final byte[] FREEZE_ACCOUNT_LIST = "FREEZE_ACCOUNT_LIST".getBytes();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.vision.core.store;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.vision.core.capsule.FreezeAccountCapsule;
import org.vision.core.db.VisionStoreWithRevoking;



@Component
public class FreezeAccountStore extends VisionStoreWithRevoking<FreezeAccountCapsule> {

@Autowired
public FreezeAccountStore(@Value("freeze-account") String dbName) {
super(dbName);
}

@Override
public FreezeAccountCapsule get(byte[] key) {
byte[] value = revokingDB.getUnchecked(key);
return ArrayUtils.isEmpty(value) ? null : new FreezeAccountCapsule(value);
}

public void put(byte[] key, FreezeAccountCapsule item) {
super.put(key, item);
}

@Override
public void delete(byte[] key) {
super.delete(key);
}

public byte[] createFreezeAccountDbKey() {
return "FREEZE_ACCOUNT_KEY".getBytes();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ public class CommonParameter {
public long proposalExpireTime; // (ms)
@Getter
@Setter
public long proposalEffectiveBlockNumber;
@Getter
@Setter
public long proposalEffectiveExpireTime; // (ms)
@Getter
@Setter
public int checkFrozenTime; // for test only
@Getter
@Setter
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/org/vision/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public class Constant {

public static final String BLOCK_MAINTENANCE_TIME_INTERVAL = "block.maintenanceTimeInterval";
public static final String BLOCK_PROPOSAL_EXPIRE_TIME = "block.proposalExpireTime";
public static final String BLOCK_PROPOSAL_EFFECTIVE_BLOCK_NUMBER = "block.proposalEffectiveBlockNumber";
public static final String BLOCK_PROPOSAL_EFFECTIVE_EXPIRE_TIME = "block.proposalEffectiveExpireTime";

public static final String BLOCK_CHECK_FROZEN_TIME = "block.checkFrozenTime";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ActuatorConstant {
public static final String CONTRACT_NOT_EXIST = "No contract!";
public static final String STORE_NOT_EXIST = "No account store or dynamic store!";
public static final String FROZEN_TIME_NOT_TO_STANDARD = "Insufficient frozen time!";
public static final String ACCOUNT_CANNOT_TRANSACTION = "] cannot create transaction";
public static final int ONE_HUNDRED = 100;
public static final int PRECISION_DECIMAL = 6;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
import org.vision.protos.Protocol.Transaction.Result.code;
import org.vision.protos.contract.ProposalContract.ProposalCreateContract;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.vision.core.actuator.ActuatorConstant.ACCOUNT_EXCEPTION_STR;
import static org.vision.core.actuator.ActuatorConstant.NOT_EXIST_STR;
import static org.vision.core.actuator.ActuatorConstant.WITNESS_EXCEPTION_STR;
import static org.vision.core.utils.ProposalUtil.ProposalType.ALLOW_FREEZE_ACCOUNT;
import static org.vision.core.utils.ProposalUtil.ProposalType.FREEZE_ACCOUNT_LIST;

@Slf4j(topic = "actuator")
public class ProposalCreateActuator extends AbstractActuator {
Expand Down Expand Up @@ -55,7 +59,7 @@ public boolean execute(Object result) throws ContractExeException {

long currentMaintenanceTime =
chainBaseManager.getDynamicPropertiesStore().getNextMaintenanceTime();
long now3 = now + CommonParameter.getInstance().getProposalExpireTime();
long now3 = now + getProposalExpiredTime(proposalCreateContract);
long round = (now3 - currentMaintenanceTime) / maintenanceTimeInterval;
long expirationTime =
currentMaintenanceTime + (round + 1) * maintenanceTimeInterval;
Expand Down Expand Up @@ -112,22 +116,34 @@ public boolean validate() throws ContractValidateException {

if (chainBaseManager.getDynamicPropertiesStore().getSeparateProposalStringParameters() == 1L){
boolean existParameters = false;
List<Long> proposalIds = new ArrayList<>();
if (contract.getParametersMap().size() != 0) {
existParameters = true;
for (Map.Entry<Long, Long> entry : contract.getParametersMap().entrySet()) {
validateValue(entry);
proposalIds.add(entry.getKey());
}
}
if (contract.getStringParametersMap().size() != 0) {
existParameters = true;
for (Map.Entry<Long, String> entry : contract.getStringParametersMap().entrySet()) {
validateStringValue(entry);
proposalIds.add(entry.getKey());
}
}

if (!existParameters){
throw new ContractValidateException("This proposal has no parameter or string parameter.");
}

if (checkFreezeAccountBlockNumber() && checkFreezeAccountProposal(proposalIds)) {
for (Long id: proposalIds) {
if (id != ALLOW_FREEZE_ACCOUNT.getCode() && id != FREEZE_ACCOUNT_LIST.getCode()) {
throw new ContractValidateException("This proposal has wrong parameter or string parameter[67 or 69].");
}
}
}

}else {
if (contract.getParametersMap().size() != 0) {
for (Map.Entry<Long, Long> entry : contract.getParametersMap().entrySet()) {
Expand All @@ -153,7 +169,7 @@ private void validateValue(Map.Entry<Long, Long> entry) throws ContractValidateE

private void validateStringValue(Map.Entry<Long, String> entry) throws ContractValidateException {
ProposalUtil
.validatorString(chainBaseManager.getDynamicPropertiesStore(), forkController, entry.getKey(),
.validatorString(chainBaseManager.getDynamicPropertiesStore(), chainBaseManager.getFreezeAccountStore(), forkController, entry.getKey(),
entry.getValue());
}

Expand All @@ -167,4 +183,33 @@ public long calcFee() {
return 0;
}

public boolean checkFreezeAccountProposal(List<Long> proposalIds){
return !proposalIds.isEmpty() && (proposalIds.contains(ALLOW_FREEZE_ACCOUNT.getCode()) || proposalIds.contains(FREEZE_ACCOUNT_LIST.getCode()));
}

public boolean checkFreezeAccountBlockNumber() {
return chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() > CommonParameter.getInstance().getProposalEffectiveBlockNumber();
}

public long getProposalExpiredTime(ProposalCreateContract proposalCreateContract) {
long proposalExpiredTime = CommonParameter.getInstance().getProposalExpireTime();
if (checkFreezeAccountBlockNumber()) {
List<Long> proposalIds = new ArrayList<>();
if (proposalCreateContract.getParametersMap().size() != 0) {
for (Map.Entry<Long, Long> entry : proposalCreateContract.getParametersMap().entrySet()) {
proposalIds.add(entry.getKey());
}
}
if (proposalCreateContract.getStringParametersMap().size() != 0) {
for (Map.Entry<Long, String> entry : proposalCreateContract.getStringParametersMap().entrySet()) {
proposalIds.add(entry.getKey());
}
}
if (checkFreezeAccountProposal(proposalIds)) {
proposalExpiredTime = CommonParameter.getInstance().getProposalEffectiveExpireTime();
}
}
return proposalExpiredTime;
}

}
Loading

0 comments on commit 1277e31

Please sign in to comment.