use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class ParentNumberRuleTest method parentNumberGreaterThanBlockNumber.
// no pass rule
@Test
public void parentNumberGreaterThanBlockNumber() {
BlockHeader header = getHeader(100);
BlockHeader parent = getHeader(101);
assertFalse(rule.validate(header, parent));
}
use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class ParentNumberRuleTest method parentNumberEqualBlockNumber.
// no pass rule
@Test
public void parentNumberEqualBlockNumber() {
BlockHeader header = getHeader(100);
BlockHeader parent = getHeader(100);
assertFalse(rule.validate(header, parent));
}
use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class ParentNumberRuleTest method parentNumberEqualBlockNumberMinusOne.
// pass rule
@Test
public void parentNumberEqualBlockNumberMinusOne() {
BlockHeader header = getHeader(10000);
BlockHeader parent = getHeader(9999);
assertTrue(rule.validate(header, parent));
}
use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class Remasc method processMinersFees.
/**
* Implements the actual Remasc distribution logic
*/
void processMinersFees() throws IOException, BlockStoreException {
if (!(executionTx instanceof RemascTransaction)) {
// 2) invocation to remasc from another contract (ie call opcode)
throw new RemascInvalidInvocationException("Invoked Remasc outside last tx of the block");
}
this.addNewSiblings();
long blockNbr = executionBlock.getNumber();
long processingBlockNumber = blockNbr - remascConstants.getMaturity();
if (processingBlockNumber < 1) {
logger.debug("First block has not reached maturity yet, current block is {}", blockNbr);
return;
}
Block processingBlock = blockStore.getBlockByHashAndDepth(executionBlock.getParentHash().getBytes(), remascConstants.getMaturity() - 1);
BlockHeader processingBlockHeader = processingBlock.getHeader();
// Adds current block fees to accumulated rewardBalance
Coin processingBlockReward = processingBlockHeader.getPaidFees();
Coin rewardBalance = provider.getRewardBalance();
rewardBalance = rewardBalance.add(processingBlockReward);
provider.setRewardBalance(rewardBalance);
if (processingBlockNumber - remascConstants.getSyntheticSpan() < 0) {
logger.debug("First block has not reached maturity+syntheticSpan yet, current block is {}", executionBlock.getNumber());
return;
}
// Takes from rewardBalance this block's height reward.
Coin fullBlockReward = rewardBalance.divide(BigInteger.valueOf(remascConstants.getSyntheticSpan()));
rewardBalance = rewardBalance.subtract(fullBlockReward);
provider.setRewardBalance(rewardBalance);
// Pay RSK labs cut
Coin payToRskLabs = fullBlockReward.divide(BigInteger.valueOf(remascConstants.getRskLabsDivisor()));
feesPayer.payMiningFees(processingBlockHeader.getHash().getBytes(), payToRskLabs, remascConstants.getRskLabsAddress(), logs);
fullBlockReward = fullBlockReward.subtract(payToRskLabs);
// TODO to improve
// this type choreography is only needed because the RepositoryTrack support the
// get snapshot to method
Repository processingRepository = ((RepositoryTrack) repository).getOriginRepository().getSnapshotTo(processingBlockHeader.getStateRoot());
// TODO to improve
// and we need a RepositoryTrack to feed RemascFederationProvider
// because it supports the update of bytes (notably, RepositoryImpl don't)
// the update of bytes is needed, because BridgeSupport creation could alter
// the storage when getChainHead is null (specially in production)
processingRepository = processingRepository.startTracking();
BridgeSupport bridgeSupport = new BridgeSupport(config, processingRepository, null, PrecompiledContracts.BRIDGE_ADDR, processingBlock);
RemascFederationProvider federationProvider = new RemascFederationProvider(bridgeSupport);
Coin payToFederation = fullBlockReward.divide(BigInteger.valueOf(remascConstants.getFederationDivisor()));
byte[] processingBlockHash = processingBlockHeader.getHash().getBytes();
int nfederators = federationProvider.getFederationSize();
Coin payToFederator = payToFederation.divide(BigInteger.valueOf(nfederators));
Coin restToLastFederator = payToFederation.subtract(payToFederator.multiply(BigInteger.valueOf(nfederators)));
Coin paidToFederation = Coin.ZERO;
for (int k = 0; k < nfederators; k++) {
RskAddress federatorAddress = federationProvider.getFederatorAddress(k);
if (k == nfederators - 1 && restToLastFederator.compareTo(Coin.ZERO) > 0) {
feesPayer.payMiningFees(processingBlockHash, payToFederator.add(restToLastFederator), federatorAddress, logs);
} else {
feesPayer.payMiningFees(processingBlockHash, payToFederator, federatorAddress, logs);
}
paidToFederation = paidToFederation.add(payToFederator);
}
fullBlockReward = fullBlockReward.subtract(payToFederation);
List<Sibling> siblings = provider.getSiblings().get(processingBlockNumber);
if (CollectionUtils.isNotEmpty(siblings)) {
// Block has siblings, reward distribution is more complex
boolean previousBrokenSelectionRule = provider.getBrokenSelectionRule();
this.payWithSiblings(processingBlockHeader, fullBlockReward, siblings, previousBrokenSelectionRule);
boolean brokenSelectionRule = SelectionRule.isBrokenSelectionRule(processingBlockHeader, siblings);
provider.setBrokenSelectionRule(brokenSelectionRule);
} else {
if (provider.getBrokenSelectionRule()) {
// broken selection rule, apply punishment, ie burn part of the reward.
Coin punishment = fullBlockReward.divide(BigInteger.valueOf(remascConstants.getPunishmentDivisor()));
fullBlockReward = fullBlockReward.subtract(punishment);
provider.setBurnedBalance(provider.getBurnedBalance().add(punishment));
}
feesPayer.payMiningFees(processingBlockHeader.getHash().getBytes(), fullBlockReward, processingBlockHeader.getCoinbase(), logs);
provider.setBrokenSelectionRule(Boolean.FALSE);
}
this.removeUsedSiblings(processingBlockHeader);
}
use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class Remasc method addNewSiblings.
/**
* Saves uncles of the current block into the siblings map to use in the future for fee distribution
*/
private void addNewSiblings() {
// Add uncles of the execution block to the siblings map
List<BlockHeader> uncles = executionBlock.getUncleList();
if (CollectionUtils.isNotEmpty(uncles)) {
for (BlockHeader uncleHeader : uncles) {
List<Sibling> siblings = provider.getSiblings().get(uncleHeader.getNumber());
if (siblings == null) {
siblings = new ArrayList<>();
}
siblings.add(new Sibling(uncleHeader, executionBlock.getHeader().getCoinbase(), executionBlock.getNumber()));
provider.getSiblings().put(uncleHeader.getNumber(), siblings);
}
}
}
Aggregations