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;
}
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);
});
}
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();
}
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);
}
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);
});
}
Aggregations