Search in sources :

Example 21 with BlockHeader

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));
}
Also used : BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 22 with BlockHeader

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));
}
Also used : BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 23 with BlockHeader

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));
}
Also used : BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 24 with BlockHeader

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);
}
Also used : BridgeSupport(co.rsk.peg.BridgeSupport) Coin(co.rsk.core.Coin) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader)

Example 25 with BlockHeader

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);
        }
    }
}
Also used : BlockHeader(org.ethereum.core.BlockHeader)

Aggregations

BlockHeader (org.ethereum.core.BlockHeader)65 Test (org.junit.Test)36 Block (org.ethereum.core.Block)27 ArrayList (java.util.ArrayList)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)9 BlockDifficulty (co.rsk.core.BlockDifficulty)5 Keccak256 (co.rsk.crypto.Keccak256)5 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)4 BlockBuilder (co.rsk.test.builders.BlockBuilder)4 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 BlockStore (org.ethereum.db.BlockStore)4 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)3 BigInteger (java.math.BigInteger)3 MainNetConfig (org.ethereum.config.net.MainNetConfig)3 DataWord (org.ethereum.vm.DataWord)3 DifficultyCalculator (co.rsk.core.DifficultyCalculator)2 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)2 HashSet (java.util.HashSet)2 Bloom (org.ethereum.core.Bloom)2 Transaction (org.ethereum.core.Transaction)2