Search in sources :

Example 1 with WitnessCapsule

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

the class Manager method getScheduledWitness.

/**
 * get ScheduledWitness by slot.
 */
public ByteString getScheduledWitness(final long slot) {
    final long currentSlot = getHeadSlot() + slot;
    if (currentSlot < 0) {
        throw new RuntimeException("currentSlot should be positive.");
    }
    final List<WitnessCapsule> currentShuffledWitnesses = this.getShuffledWitnessStates();
    if (CollectionUtils.isEmpty(currentShuffledWitnesses)) {
        throw new RuntimeException("ShuffledWitnesses is null.");
    }
    final int witnessIndex = (int) currentSlot % currentShuffledWitnesses.size();
    final ByteString scheduledWitness = currentShuffledWitnesses.get(witnessIndex).getAddress();
    return scheduledWitness;
}
Also used : WitnessCapsule(org.tron.core.capsule.WitnessCapsule) ByteString(com.google.protobuf.ByteString)

Example 2 with WitnessCapsule

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

the class Manager method initWitness.

/**
 * save witnesses into database.
 */
private void initWitness() {
    final Args args = Args.getInstance();
    final GenesisBlock genesisBlockArg = args.getGenesisBlock();
    genesisBlockArg.getWitnesses().forEach(key -> {
        byte[] keyAddress = ByteArray.fromHexString(key.getAddress());
        ByteString address = ByteString.copyFrom(keyAddress);
        if (!this.accountStore.has(keyAddress)) {
            final AccountCapsule accountCapsule = new AccountCapsule(ByteString.EMPTY, address, AccountType.AssetIssue, 0L);
            this.accountStore.put(keyAddress, accountCapsule);
        }
        final WitnessCapsule witnessCapsule = new WitnessCapsule(address, key.getVoteCount(), key.getUrl());
        witnessCapsule.setIsJobs(true);
        this.witnessStore.put(keyAddress, witnessCapsule);
        this.wits.add(witnessCapsule);
    });
}
Also used : GenesisBlock(org.tron.core.config.args.GenesisBlock) AccountCapsule(org.tron.core.capsule.AccountCapsule) Args(org.tron.core.config.args.Args) WitnessCapsule(org.tron.core.capsule.WitnessCapsule) ByteString(com.google.protobuf.ByteString)

Example 3 with WitnessCapsule

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

the class ManagerTest method fork.

@Test
public void fork() {
    Args.setParam(new String[] { "--witness" }, Configuration.getByPath(Constant.NORMAL_CONF));
    long size = dbManager.getBlockStore().dbSource.allKeys().size();
    String key = "00f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62";
    byte[] privateKey = ByteArray.fromHexString(key);
    final ECKey ecKey = ECKey.fromPrivate(privateKey);
    byte[] address = ecKey.getAddress();
    WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
    dbManager.addWitness(witnessCapsule);
    dbManager.addWitness(witnessCapsule);
    dbManager.addWitness(witnessCapsule);
    IntStream.range(0, 5).forEach(i -> {
        try {
            dbManager.generateBlock(witnessCapsule, System.currentTimeMillis(), privateKey);
        } catch (ValidateSignatureException | ContractValidateException | ContractExeException | UnLinkedBlockException e) {
            logger.debug(e.getMessage(), e);
        }
    });
    try {
        long num = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
        BlockCapsule blockCapsule1 = new BlockCapsule(num, dbManager.getHead().getParentHash().getByteString(), System.currentTimeMillis(), witnessCapsule.getAddress());
        blockCapsule1.generatedByMyself = true;
        BlockCapsule blockCapsule2 = new BlockCapsule(num + 1, blockCapsule1.getBlockId().getByteString(), System.currentTimeMillis(), witnessCapsule.getAddress());
        blockCapsule2.generatedByMyself = true;
        logger.error("******1*******" + "block1 id:" + blockCapsule1.getBlockId());
        logger.error("******2*******" + "block2 id:" + blockCapsule2.getBlockId());
        dbManager.pushBlock(blockCapsule1);
        dbManager.pushBlock(blockCapsule2);
        logger.error("******in blockStore block size:" + dbManager.getBlockStore().dbSource.allKeys().size());
        logger.error("******in blockStore block:" + dbManager.getBlockStore().dbSource.allKeys().stream().map(ByteArray::toHexString).collect(Collectors.toList()));
        Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule1.getBlockId().getBytes()));
        Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule2.getBlockId().getBytes()));
        Assert.assertEquals(dbManager.getBlockStore().get(blockCapsule2.getBlockId().getBytes()).getParentHash(), blockCapsule1.getBlockId());
        Assert.assertEquals(dbManager.getBlockStore().dbSource.allKeys().size(), size + 6);
        Assert.assertEquals(dbManager.getBlockIdByNum(dbManager.getHead().getNum() - 1), blockCapsule1.getBlockId());
        Assert.assertEquals(dbManager.getBlockIdByNum(dbManager.getHead().getNum() - 2), blockCapsule1.getParentHash());
        Assert.assertEquals(blockCapsule2.getBlockId().getByteString(), dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
        Assert.assertEquals(dbManager.getHead().getBlockId().getByteString(), dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
    } catch (ValidateSignatureException | ContractValidateException | ContractExeException | UnLinkedBlockException e) {
        logger.debug(e.getMessage(), e);
    }
    dbManager.getWitnesses().clear();
}
Also used : WitnessCapsule(org.tron.core.capsule.WitnessCapsule) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) ContractValidateException(org.tron.core.exception.ContractValidateException) ECKey(org.tron.common.crypto.ECKey) ByteString(com.google.protobuf.ByteString) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) BlockCapsule(org.tron.core.capsule.BlockCapsule) ContractExeException(org.tron.core.exception.ContractExeException) Test(org.junit.Test)

Example 4 with WitnessCapsule

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

the class ManagerTest method updateWits.

// @Test
public void updateWits() {
    int sizePrv = dbManager.getWitnesses().size();
    dbManager.getWitnesses().forEach(witnessCapsule -> {
        logger.info("witness address is {}", ByteArray.toHexString(witnessCapsule.getAddress().toByteArray()));
    });
    logger.info("------------");
    WitnessCapsule witnessCapsulef = new WitnessCapsule(ByteString.copyFrom(ByteArray.fromHexString("0x0011")), "www.tron.net/first");
    witnessCapsulef.setIsJobs(true);
    WitnessCapsule witnessCapsules = new WitnessCapsule(ByteString.copyFrom(ByteArray.fromHexString("0x0012")), "www.tron.net/second");
    witnessCapsules.setIsJobs(true);
    WitnessCapsule witnessCapsulet = new WitnessCapsule(ByteString.copyFrom(ByteArray.fromHexString("0x0013")), "www.tron.net/three");
    witnessCapsulet.setIsJobs(false);
    dbManager.getWitnesses().forEach(witnessCapsule -> {
        logger.info("witness address is {}", ByteArray.toHexString(witnessCapsule.getAddress().toByteArray()));
    });
    logger.info("---------");
    dbManager.getWitnessStore().put(witnessCapsulef.getAddress().toByteArray(), witnessCapsulef);
    dbManager.getWitnessStore().put(witnessCapsules.getAddress().toByteArray(), witnessCapsules);
    dbManager.getWitnessStore().put(witnessCapsulet.getAddress().toByteArray(), witnessCapsulet);
    dbManager.updateWits();
    dbManager.getWitnesses().forEach(witnessCapsule -> {
        logger.info("witness address is {}", ByteArray.toHexString(witnessCapsule.getAddress().toByteArray()));
    });
    int sizeTis = dbManager.getWitnesses().size();
    Assert.assertEquals("update add witness size is ", 2, sizeTis - sizePrv);
}
Also used : WitnessCapsule(org.tron.core.capsule.WitnessCapsule)

Example 5 with WitnessCapsule

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

the class WitnessService method init.

/**
 * Initialize the local witnesses
 */
@Override
public void init() {
    Args.getInstance().getLocalWitnesses().getPrivateKeys().forEach(key -> {
        byte[] privateKey = ByteArray.fromHexString(key);
        final ECKey ecKey = ECKey.fromPrivate(privateKey);
        byte[] address = ecKey.getAddress();
        WitnessCapsule witnessCapsule = this.db.getWitnessStore().get(address);
        // need handle init witness
        if (null == witnessCapsule) {
            logger.warn("WitnessCapsule[" + address + "] is not in witnessStore");
            witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
        }
        this.privateKeyMap.put(witnessCapsule.getAddress(), privateKey);
        this.localWitnessStateMap.put(witnessCapsule.getAddress(), witnessCapsule);
    });
}
Also used : WitnessCapsule(org.tron.core.capsule.WitnessCapsule) ECKey(org.tron.common.crypto.ECKey)

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