Search in sources :

Example 11 with WitnessCapsule

use of org.tron.core.capsule.WitnessCapsule in project java-tron by tronprotocol.

the class Manager method updateWitness.

/**
 * update witness.
 */
public void updateWitness() {
    List<WitnessCapsule> currentWits = getWitnesses();
    final Map<ByteString, Long> countWitness = Maps.newHashMap();
    final List<AccountCapsule> accountList = this.accountStore.getAllAccounts();
    // logger.info("there is account List size is {}", accountList.size());
    accountList.forEach(account -> {
        // logger.info("there is account ,account address is {}",
        // account.createReadableString());
        Optional<Long> sum = account.getVotesList().stream().map(vote -> vote.getVoteCount()).reduce((a, b) -> a + b);
        if (sum.isPresent()) {
            if (sum.get() <= account.getShare()) {
                account.getVotesList().forEach(vote -> {
                    // TODO validate witness //active_witness
                    ByteString voteAddress = vote.getVoteAddress();
                    long voteCount = vote.getVoteCount();
                    if (countWitness.containsKey(voteAddress)) {
                        countWitness.put(voteAddress, countWitness.get(voteAddress) + voteCount);
                    } else {
                        countWitness.put(voteAddress, voteCount);
                    }
                });
            } else {
                logger.info("account" + account.createReadableString() + ",share[" + account.getShare() + "] > voteSum[" + sum.get() + "]");
            }
        }
    });
    witnessStore.getAllWitnesses().forEach(witnessCapsule -> {
        witnessCapsule.setVoteCount(0);
        witnessCapsule.setIsJobs(false);
        this.witnessStore.put(witnessCapsule.createDbKey(), witnessCapsule);
    });
    final List<WitnessCapsule> witnessCapsuleList = Lists.newArrayList();
    logger.info("countWitnessMap size is {}", countWitness.keySet().size());
    // Only possible during the initialization phase
    if (countWitness.size() == 0) {
        witnessCapsuleList.addAll(this.witnessStore.getAllWitnesses());
    }
    countWitness.forEach((address, voteCount) -> {
        final WitnessCapsule witnessCapsule = this.witnessStore.get(createDbKey(address));
        if (null == witnessCapsule) {
            logger.warn("witnessCapsule is null.address is {}", createReadableString(address));
            return;
        }
        ByteString witnessAddress = witnessCapsule.getInstance().getAddress();
        AccountCapsule witnessAccountCapsule = accountStore.get(createDbKey(witnessAddress));
        if (witnessAccountCapsule == null) {
            logger.warn("witnessAccount[" + createReadableString(witnessAddress) + "] not exists");
        } else {
            if (witnessAccountCapsule.getBalance() < WitnessCapsule.MIN_BALANCE) {
                logger.warn("witnessAccount[" + createReadableString(witnessAddress) + "] has balance[" + witnessAccountCapsule.getBalance() + "] < MIN_BALANCE[" + WitnessCapsule.MIN_BALANCE + "]");
            } else {
                witnessCapsule.setVoteCount(witnessCapsule.getVoteCount() + voteCount);
                witnessCapsule.setIsJobs(false);
                witnessCapsuleList.add(witnessCapsule);
                this.witnessStore.put(witnessCapsule.createDbKey(), witnessCapsule);
                logger.info("address is {}  ,countVote is {}", witnessCapsule.createReadableString(), witnessCapsule.getVoteCount());
            }
        }
    });
    sortWitness(witnessCapsuleList);
    if (witnessCapsuleList.size() > MAX_ACTIVE_WITNESS_NUM) {
        setWitnesses(witnessCapsuleList.subList(0, MAX_ACTIVE_WITNESS_NUM));
    } else {
        setWitnesses(witnessCapsuleList);
    }
    getWitnesses().forEach(witnessCapsule -> {
        witnessCapsule.setIsJobs(true);
        this.witnessStore.put(witnessCapsule.createDbKey(), witnessCapsule);
    });
    logger.info("updateWitness,before:{} ", getWitnessStringList(currentWits) + ",\nafter:{} " + getWitnessStringList(getWitnesses()));
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) GenesisBlock(org.tron.core.config.args.GenesisBlock) SOLIDIFIED_THRESHOLD(org.tron.core.config.Parameter.ChainConstant.SOLIDIFIED_THRESHOLD) WitnessCapsule(org.tron.core.capsule.WitnessCapsule) BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException) Autowired(org.springframework.beans.factory.annotation.Autowired) AccountType(org.tron.protos.Protocol.AccountType) HighFreqException(org.tron.core.exception.HighFreqException) BlockCapsule(org.tron.core.capsule.BlockCapsule) ContractExeException(org.tron.core.exception.ContractExeException) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) Args(org.tron.core.config.args.Args) RamUsageEstimator(com.carrotsearch.sizeof.RamUsageEstimator) Map(java.util.Map) TransactionResultCapsule(org.tron.core.capsule.TransactionResultCapsule) AccountCapsule(org.tron.core.capsule.AccountCapsule) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Transaction(org.tron.protos.Protocol.Transaction) Pair(javafx.util.Pair) ECKey(org.tron.common.crypto.ECKey) Set(java.util.Set) WITNESS_PAY_PER_BLOCK(org.tron.core.config.Parameter.ChainConstant.WITNESS_PAY_PER_BLOCK) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) TransactionCapsule(org.tron.core.capsule.TransactionCapsule) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) LevelDbDataSourceImpl(org.tron.common.storage.leveldb.LevelDbDataSourceImpl) TransferContract(org.tron.protos.Protocol.Transaction.Contract.ContractType.TransferContract) Actuator(org.tron.core.actuator.Actuator) Optional(java.util.Optional) DialogOptional(org.tron.common.utils.DialogOptional) RandomGenerator(org.tron.common.utils.RandomGenerator) Setter(lombok.Setter) ContractValidateException(org.tron.core.exception.ContractValidateException) Getter(lombok.Getter) ActuatorFactory(org.tron.core.actuator.ActuatorFactory) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ByteArray(org.tron.common.utils.ByteArray) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException) LinkedList(java.util.LinkedList) Sha256Hash(org.tron.common.utils.Sha256Hash) TransferAssetContract(org.tron.protos.Protocol.Transaction.Contract.ContractType.TransferAssetContract) Iterator(java.util.Iterator) DateTime(org.joda.time.DateTime) Dialog(org.tron.core.db.AbstractRevokingStore.Dialog) Maps(com.google.common.collect.Maps) Lock(java.util.concurrent.locks.Lock) Component(org.springframework.stereotype.Component) BlockUtil(org.tron.core.capsule.utils.BlockUtil) Time(org.tron.common.utils.Time) Collections(java.util.Collections) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) Node(org.tron.common.overlay.discover.Node) WitnessCapsule(org.tron.core.capsule.WitnessCapsule) ByteString(com.google.protobuf.ByteString)

Example 12 with WitnessCapsule

use of org.tron.core.capsule.WitnessCapsule in project java-tron by tronprotocol.

the class WitnessCreateActuator method createWitness.

private void createWitness(final WitnessCreateContract witnessCreateContract) {
    // Create Witness by witnessCreateContract
    final WitnessCapsule witnessCapsule = new WitnessCapsule(witnessCreateContract.getOwnerAddress(), 0, witnessCreateContract.getUrl().toStringUtf8());
    logger.debug("createWitness,address[{}]", witnessCapsule.createReadableString());
    this.dbManager.getWitnessStore().put(witnessCapsule.createDbKey(), witnessCapsule);
}
Also used : WitnessCapsule(org.tron.core.capsule.WitnessCapsule)

Example 13 with WitnessCapsule

use of org.tron.core.capsule.WitnessCapsule in project java-tron by tronprotocol.

the class WitnessUpdateActuator method updateWitness.

private void updateWitness(final WitnessUpdateContract witnessUpdateContract) {
    final WitnessCapsule witnessCapsule = new WitnessCapsule(witnessUpdateContract.getOwnerAddress(), 0, witnessUpdateContract.getUpdateUrl().toString());
    this.dbManager.getWitnessStore().put(witnessCapsule.createDbKey(), witnessCapsule);
}
Also used : WitnessCapsule(org.tron.core.capsule.WitnessCapsule)

Aggregations

WitnessCapsule (org.tron.core.capsule.WitnessCapsule)13 ByteString (com.google.protobuf.ByteString)4 ECKey (org.tron.common.crypto.ECKey)3 AccountCapsule (org.tron.core.capsule.AccountCapsule)3 Test (org.junit.Test)2 BlockCapsule (org.tron.core.capsule.BlockCapsule)2 BalanceInsufficientException (org.tron.core.exception.BalanceInsufficientException)2 RamUsageEstimator (com.carrotsearch.sizeof.RamUsageEstimator)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1