use of co.rsk.core.Coin in project rskj by rsksmart.
the class RemascProcessMinerFeesTest method processMinersFeesFromTxThatIsNotTheLatestTx.
@Test
public void processMinersFeesFromTxThatIsNotTheLatestTx() throws IOException, BlockStoreException {
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
Block blockWithOneTx = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, null, minerFee, 0, txValue, cowKey);
blocks.add(blockWithOneTx);
blocks.addAll(createSimpleBlocks(blockWithOneTx, 9));
BlockExecutor blockExecutor = new BlockExecutor(config, blockchain.getRepository(), null, blockchain.getBlockStore(), null);
for (Block b : blocks) {
blockExecutor.executeAndFillAll(b, blockchain.getBestBlock());
blockchain.tryToConnect(b);
}
Repository repository = blockchain.getRepository();
assertEquals(cowInitialBalance.subtract(Coin.valueOf(minerFee + txValue)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
assertEquals(Coin.valueOf(minerFee), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
assertNull(repository.getAccountState(coinbaseA));
assertNull(repository.getAccountState(remascConfig.getRskLabsAddress()));
RemascStorageProvider remasceStorageProvider = getRemascStorageProvider(blockchain);
assertEquals(Coin.ZERO, remasceStorageProvider.getRewardBalance());
assertEquals(Coin.ZERO, remasceStorageProvider.getBurnedBalance());
assertEquals(0, remasceStorageProvider.getSiblings().size());
// A hacker trying to screw the system creates a tx to remasc and a fool/accomplice miner includes that tx in a block
Transaction tx = new Transaction(Coin.valueOf(1).getBytes(), Coin.valueOf(1).getBytes(), Coin.valueOf(minerFee).getBytes(), PrecompiledContracts.REMASC_ADDR.getBytes(), Coin.valueOf(txValue * 2).getBytes(), null, config.getBlockchainConfig().getCommonConstants().getChainId());
tx.sign(cowKey.getPrivKeyBytes());
Block newblock = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), null, null, tx);
blockExecutor.executeAndFillAll(newblock, blockchain.getBestBlock());
blockchain.tryToConnect(newblock);
repository = blockchain.getRepository();
// Check "hack" tx makes no changes to the remasc state, sender pays fees, and value is added to remasc account balance
assertEquals(cowInitialBalance.subtract(Coin.valueOf(minerFee + txValue + minerFee)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
long blockReward = minerFee / remascConfig.getSyntheticSpan();
long originalBlockReward = blockReward;
assertEquals(Coin.valueOf(minerFee + minerFee - blockReward), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
long rskReward = blockReward / remascConfig.getRskLabsDivisor();
assertEquals(Coin.valueOf(rskReward), repository.getAccountState(remascConfig.getRskLabsAddress()).getBalance());
blockReward -= rskReward;
long federationReward = blockReward / remascConfig.getFederationDivisor();
assertEquals(33, federationReward);
blockReward -= federationReward;
assertEquals(Coin.valueOf(blockReward), repository.getAccountState(coinbaseA).getBalance());
Coin expectedRewardBalance = Coin.valueOf(minerFee - originalBlockReward);
this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), expectedRewardBalance, Coin.ZERO, 0L);
this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward);
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class RemascProcessMinerFeesTest method siblingThatBreaksSelectionRuleGetsPunished.
@Test
public void siblingThatBreaksSelectionRuleGetsPunished() throws IOException, BlockStoreException {
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
final long NUMBER_OF_TXS_WITH_FEES = 3;
List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
Block blockAtHeightThree = blocks.get(blocks.size() - 1);
Block blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blockAtHeightThree, PegTestUtils.createHash3(), coinbaseA, null, minerFee, 0, txValue, cowKey, 2l);
blocks.add(blockWithOneTxA);
Block blockWithOneTxC = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseC, null, minerFee, 1, txValue, cowKey, 2l);
blocks.add(blockWithOneTxC);
Block blockWithOneTxD = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseD, null, minerFee, 1, txValue, cowKey, 2l);
Block blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blockAtHeightThree, PegTestUtils.createHash3(), coinbaseB, null, 3 * minerFee, 0, txValue, cowKey, 2l);
Block blockThatIncludesUnclesE = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxC, PegTestUtils.createHash3(), coinbaseE, Lists.newArrayList(blockWithOneTxB.getHeader(), blockWithOneTxD.getHeader()), minerFee, 2, txValue, cowKey);
blocks.add(blockThatIncludesUnclesE);
blocks.addAll(createSimpleBlocks(blockThatIncludesUnclesE, 7));
BlockExecutor blockExecutor = new BlockExecutor(config, blockchain.getRepository(), null, blockchain.getBlockStore(), null);
for (Block b : blocks) {
blockExecutor.executeAndFillAll(b, blockchain.getBestBlock());
blockchain.tryToConnect(b);
}
// validate that the blockchain's and REMASC's initial states are correct
Coin cowRemainingBalance = cowInitialBalance.subtract(Coin.valueOf(minerFee * NUMBER_OF_TXS_WITH_FEES + txValue * NUMBER_OF_TXS_WITH_FEES));
List<Long> otherAccountsBalance = new ArrayList<>(Arrays.asList(null, null, null, null));
this.validateAccountsCurrentBalanceIsCorrect(blockchain.getRepository(), cowRemainingBalance, minerFee * NUMBER_OF_TXS_WITH_FEES, null, this.getAccountsWithExpectedBalance(otherAccountsBalance));
this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), Coin.ZERO, Coin.ZERO, 2L);
// add block to pay fees of blocks on blockchain's height 4
Block blockToPayFeesOnHeightFour = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), null, minerFee, 3, txValue, cowKey);
blockExecutor.executeAndFillAll(blockToPayFeesOnHeightFour, blockchain.getBestBlock());
blockchain.tryToConnect(blockToPayFeesOnHeightFour);
Repository repository = blockchain.getRepository();
// -- After executing REMASC's contract for paying height 4 block
// validate that account's balances are correct
cowRemainingBalance = cowRemainingBalance.subtract(Coin.valueOf(minerFee + txValue));
long minerRewardOnHeightFour = minerFee / remascConfig.getSyntheticSpan();
long burnBalanceLevelFour = minerRewardOnHeightFour;
long remascCurrentBalance = minerFee * 4 - burnBalanceLevelFour;
long rskCurrentBalance = minerRewardOnHeightFour / remascConfig.getRskLabsDivisor();
minerRewardOnHeightFour -= rskCurrentBalance;
long federationReward = minerRewardOnHeightFour / remascConfig.getFederationDivisor();
minerRewardOnHeightFour -= federationReward;
long publishersFee = minerRewardOnHeightFour / remascConfig.getPublishersDivisor();
minerRewardOnHeightFour -= minerRewardOnHeightFour / remascConfig.getPublishersDivisor();
minerRewardOnHeightFour /= 2;
long siblingPunishmentLvlFour = (long) (minerRewardOnHeightFour * 0.05);
long siblingReward = minerRewardOnHeightFour - siblingPunishmentLvlFour;
HashMap<byte[], Coin> otherAccountsBalanceOnHeightFour = this.getAccountsWithExpectedBalance(new ArrayList<>(Arrays.asList(minerRewardOnHeightFour, siblingReward, null, null)));
otherAccountsBalanceOnHeightFour.put(coinbaseE.getBytes(), Coin.valueOf(publishersFee));
remascCurrentBalance += siblingPunishmentLvlFour;
// TODO review one unit burned?
this.validateAccountsCurrentBalanceIsCorrect(repository, cowRemainingBalance, remascCurrentBalance + 1, rskCurrentBalance, otherAccountsBalanceOnHeightFour);
this.validateFederatorsBalanceIsCorrect(repository, federationReward);
// validate that REMASC's state is correct
long blockRewardOnHeightFour = minerFee / remascConfig.getSyntheticSpan();
Coin expectedRewardBalance = Coin.valueOf(minerFee - blockRewardOnHeightFour);
Coin expectedBurnedBalance = Coin.valueOf(siblingPunishmentLvlFour);
// TODO review one more burned unit
this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), expectedRewardBalance, expectedBurnedBalance.add(Coin.valueOf(1)), 1L);
// add block to pay fees of blocks on blockchain's height 5
Block blockToPayFeesOnHeightFive = RemascTestRunner.createBlock(this.genesisBlock, blockToPayFeesOnHeightFour, PegTestUtils.createHash3(), TestUtils.randomAddress(), null, minerFee, 4, txValue, cowKey);
blockExecutor.executeAndFillAll(blockToPayFeesOnHeightFive, blockchain.getBestBlock());
blockchain.tryToConnect(blockToPayFeesOnHeightFive);
repository = blockchain.getRepository();
// -- After executing REMASC's contract for paying height 5 block
// validate that account's balances are correct
cowRemainingBalance = cowRemainingBalance.subtract(Coin.valueOf(minerFee + txValue));
long rewardBalance = minerFee - blockRewardOnHeightFour;
rewardBalance += minerFee;
long blockRewardOnHeightFive = rewardBalance / remascConfig.getSyntheticSpan();
remascCurrentBalance += minerFee - blockRewardOnHeightFive;
long rskReward = blockRewardOnHeightFive / remascConfig.getRskLabsDivisor();
rskCurrentBalance += rskReward;
blockRewardOnHeightFive -= rskReward;
long federationReward2 = blockRewardOnHeightFive / remascConfig.getFederationDivisor();
blockRewardOnHeightFive -= federationReward2;
long publishersFeeOnHeightFive = blockRewardOnHeightFive / remascConfig.getPublishersDivisor();
blockRewardOnHeightFive -= publishersFeeOnHeightFive;
long numberOfSiblingsOnHeightFive = 2;
blockRewardOnHeightFive /= numberOfSiblingsOnHeightFive;
long punishmentFee = blockRewardOnHeightFive / remascConfig.getPunishmentDivisor();
blockRewardOnHeightFive -= punishmentFee;
remascCurrentBalance += (numberOfSiblingsOnHeightFive * punishmentFee);
HashMap<byte[], Coin> otherAccountsBalanceOnHeightFive = this.getAccountsWithExpectedBalance(new ArrayList<>(Arrays.asList(minerRewardOnHeightFour, siblingReward, blockRewardOnHeightFive, blockRewardOnHeightFive)));
otherAccountsBalanceOnHeightFive.put(coinbaseE.getBytes(), Coin.valueOf(publishersFee + publishersFeeOnHeightFive));
// TODO review value 1
this.validateAccountsCurrentBalanceIsCorrect(repository, cowRemainingBalance, remascCurrentBalance + 1, rskCurrentBalance, otherAccountsBalanceOnHeightFive);
// validate that REMASC's state is correct
blockRewardOnHeightFive = (2 * minerFee - blockRewardOnHeightFour) / remascConfig.getSyntheticSpan();
expectedRewardBalance = Coin.valueOf(minerFee * 2 - blockRewardOnHeightFour - blockRewardOnHeightFive);
expectedBurnedBalance = Coin.valueOf((2 * punishmentFee) + siblingPunishmentLvlFour);
// TODO review value + 1
this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), expectedRewardBalance, expectedBurnedBalance.add(Coin.valueOf(1)), 0L);
this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward + federationReward2);
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class AccountBuilderTest method createAccountWithBalanceAndCode.
@Test
public void createAccountWithBalanceAndCode() {
World world = new World();
byte[] code = new byte[] { 0x01, 0x02, 0x03 };
Coin balance = Coin.valueOf(10);
Account account = new AccountBuilder(world).name("acc1").balance(balance).code(code).build();
Assert.assertNotNull(account);
Assert.assertTrue(account.getEcKey().hasPrivKey());
Assert.assertEquals(balance, world.getRepository().getBalance(account.getAddress()));
Assert.assertArrayEquals(code, world.getRepository().getCode(account.getAddress()));
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class VMComplexTest method test8.
// sha3_memSizeQuadraticCost31
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test8() {
int expectedGas = 354;
DataWord key1 = new DataWord(999);
DataWord value1 = new DataWord(3);
// Set contract into Database
String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
String code = "60016103c020600055";
RskAddress contractAddrB = new RskAddress(contractAddr);
RskAddress callerAddrB = new RskAddress(callerAddr);
byte[] codeB = Hex.decode(code);
byte[] codeKey = HashUtil.keccak256(codeB);
AccountState accountState = new AccountState();
accountState.setCodeHash(codeKey);
ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
pi.setOwnerAddress(contractAddrB);
Repository repository = pi.getRepository();
repository.createAccount(callerAddrB);
final BigInteger value = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639935");
repository.addBalance(callerAddrB, new Coin(value));
repository.createAccount(contractAddrB);
repository.saveCode(contractAddrB, codeB);
repository.addStorageRow(contractAddrB, key1, value1);
// Play the program
VM vm = getSubject();
Program program = getProgram(codeB, pi);
try {
while (!program.isStopped()) vm.step(program);
} catch (RuntimeException e) {
program.setRuntimeFailure(e);
}
System.out.println();
System.out.println("============ Results ============");
Coin balance = repository.getBalance(callerAddrB);
System.out.println("*** Used gas: " + program.getResult().getGasUsed());
System.out.println("*** Contract Balance: " + balance);
// todo: assert caller balance after contract exec
repository.close();
assertEquals(expectedGas, program.getResult().getGasUsed());
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class VMComplexTest method test9.
// sha3_memSizeQuadraticCost32
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test9() {
int expectedGas = 356;
DataWord key1 = new DataWord(9999);
DataWord value1 = new DataWord(3);
// Set contract into Database
String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
String code = "60016103e020600055";
RskAddress contractAddrB = new RskAddress(contractAddr);
RskAddress callerAddrB = new RskAddress(callerAddr);
byte[] codeB = Hex.decode(code);
byte[] codeKey = HashUtil.keccak256(codeB);
AccountState accountState = new AccountState();
accountState.setCodeHash(codeKey);
ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
pi.setOwnerAddress(contractAddrB);
Repository repository = pi.getRepository();
repository.createAccount(callerAddrB);
final BigInteger value = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639935");
repository.addBalance(callerAddrB, new Coin(value));
repository.createAccount(contractAddrB);
repository.saveCode(contractAddrB, codeB);
repository.addStorageRow(contractAddrB, key1, value1);
// Play the program
VM vm = getSubject();
Program program = getProgram(codeB, pi);
try {
while (!program.isStopped()) vm.step(program);
} catch (RuntimeException e) {
program.setRuntimeFailure(e);
}
System.out.println();
System.out.println("============ Results ============");
Coin balance = repository.getBalance(callerAddrB);
System.out.println("*** Used gas: " + program.getResult().getGasUsed());
System.out.println("*** Contract Balance: " + balance);
// todo: assert caller balance after contract exec
repository.close();
assertEquals(expectedGas, program.getResult().getGasUsed());
}
Aggregations