Search in sources :

Example 61 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class BridgeUtils method isFreeBridgeTx.

public static boolean isFreeBridgeTx(SystemProperties config, Transaction rskTx, long blockNumber) {
    BlockchainNetConfig blockchainConfig = config.getBlockchainConfig();
    RskAddress receiveAddress = rskTx.getReceiveAddress();
    if (receiveAddress.equals(RskAddress.nullAddress())) {
        return false;
    }
    BridgeConstants bridgeConstants = blockchainConfig.getCommonConstants().getBridgeConstants();
    // Once the original federation changes, txs are always paid.
    return PrecompiledContracts.BRIDGE_ADDR.equals(receiveAddress) && blockchainConfig.getConfigForBlock(blockNumber).areBridgeTxsFree() && rskTx.acceptTransactionSignature(config.getBlockchainConfig().getCommonConstants().getChainId()) && (isFromFederateMember(rskTx, bridgeConstants.getGenesisFederation()) || isFromFederationChangeAuthorizedSender(rskTx, bridgeConstants) || isFromLockWhitelistChangeAuthorizedSender(rskTx, bridgeConstants) || isFromFeePerKbChangeAuthorizedSender(rskTx, bridgeConstants));
}
Also used : RskAddress(co.rsk.core.RskAddress) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) BridgeConstants(co.rsk.config.BridgeConstants)

Example 62 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class SamplePrecompiledContract method AddBalance.

public void AddBalance(Object... args) {
    RskAddress addr = new RskAddress("cd2a3d9f938e13cd947ec05abc7fe734df8dd826");
    Coin balance = Coin.valueOf(50000);
    repository.addBalance(addr, balance);
}
Also used : Coin(co.rsk.core.Coin) RskAddress(co.rsk.core.RskAddress)

Example 63 with RskAddress

use of co.rsk.core.RskAddress 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 64 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class Sibling method create.

public static Sibling create(byte[] data) {
    ArrayList<RLPElement> params = RLP.decode2(data);
    RLPList sibling = (RLPList) params.get(0);
    byte[] hash = sibling.get(0).getRLPData();
    RskAddress coinbase = RLP.parseRskAddress(sibling.get(1).getRLPData());
    RskAddress includedBlockCoinbase = RLP.parseRskAddress(sibling.get(2).getRLPData());
    Coin paidFees = RLP.parseCoin(sibling.get(3).getRLPData());
    byte[] bytesIncludedHeight = sibling.get(4).getRLPData();
    RLPElement uncleCountElement = sibling.get(5);
    byte[] bytesUncleCount = uncleCountElement != null ? uncleCountElement.getRLPData() : null;
    long includedHeight = bytesIncludedHeight == null ? 0 : BigIntegers.fromUnsignedByteArray(bytesIncludedHeight).longValue();
    int uncleCount = bytesUncleCount == null ? 0 : BigIntegers.fromUnsignedByteArray(bytesUncleCount).intValue();
    return new Sibling(hash, coinbase, includedBlockCoinbase, paidFees, includedHeight, uncleCount);
}
Also used : Coin(co.rsk.core.Coin) RLPElement(org.ethereum.util.RLPElement) RskAddress(co.rsk.core.RskAddress) RLPList(org.ethereum.util.RLPList)

Example 65 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class TxPoolModuleImpl method serializeTransactions.

private JsonNode serializeTransactions(Map<RskAddress, Map<BigInteger, List<Transaction>>> groupedTransactions, Function<Transaction, JsonNode> txSerializer) {
    Map<String, JsonNode> senderProps = new HashMap<>();
    for (Map.Entry<RskAddress, Map<BigInteger, List<Transaction>>> entrySender : groupedTransactions.entrySet()) {
        Map<String, JsonNode> nonceProps = new HashMap<>();
        for (Map.Entry<BigInteger, List<Transaction>> entryNonce : entrySender.getValue().entrySet()) {
            ArrayNode txsNodes = jsonNodeFactory.arrayNode();
            for (Transaction tx : entryNonce.getValue()) {
                txsNodes.add(txSerializer.apply(tx));
            }
            nonceProps.put(entryNonce.getKey().toString(), txsNodes);
        }
        senderProps.put(entrySender.getKey().toString(), jsonNodeFactory.objectNode().setAll(nonceProps));
    }
    return jsonNodeFactory.objectNode().setAll(senderProps);
}
Also used : Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

RskAddress (co.rsk.core.RskAddress)174 Test (org.junit.Test)102 Repository (org.ethereum.core.Repository)60 BigInteger (java.math.BigInteger)47 Coin (co.rsk.core.Coin)38 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 DataWord (org.ethereum.vm.DataWord)27 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)24 RepositoryImpl (co.rsk.db.RepositoryImpl)16 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)15 Transaction (org.ethereum.core.Transaction)15 Program (org.ethereum.vm.program.Program)15 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 AccountState (org.ethereum.core.AccountState)12 HashMapDB (org.ethereum.datasource.HashMapDB)11 ArrayList (java.util.ArrayList)10 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)10 BridgeConstants (co.rsk.config.BridgeConstants)8 RskSystemProperties (co.rsk.config.RskSystemProperties)8 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)8