use of co.rsk.core.Coin in project rskj by rsksmart.
the class VMComplexTest method test6.
@Ignore
// contractB call itself with code from contractA
@Test
public void test6() {
/**
* #The code will run
* ------------------
*
* contract A: 945304eb96065b2a98b57a48a06ae28d285a71b5
* ---------------
*
* PUSH1 0 CALLDATALOAD SLOAD NOT PUSH1 9 JUMPI STOP
* PUSH1 32 CALLDATALOAD PUSH1 0 CALLDATALOAD SSTORE
*
* contract B: 0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6
* -----------
* { (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
* (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa)
* [[ 0 ]] (CALLSTATELESS 1000000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 64 0)
* }
*/
// Set contract into Database
RskAddress caller_addr = new RskAddress("cd1722f3947def4cf144679da39c4c32bdc35681");
RskAddress contractA_addr = new RskAddress("945304eb96065b2a98b57a48a06ae28d285a71b5");
RskAddress contractB_addr = new RskAddress("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6");
byte[] codeA = Hex.decode("60003554156009570060203560003555");
byte[] codeB = Hex.decode("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6000527faaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa6020526000604060406000601773945304eb96065b2a98b57a48a06ae28d285a71b5620f4240f3600055");
ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
pi.setOwnerAddress(contractB_addr);
pi.setGasLimit(10000000000000l);
Repository repository = pi.getRepository();
repository.createAccount(contractA_addr);
repository.saveCode(contractA_addr, codeA);
repository.addBalance(contractA_addr, Coin.valueOf(23));
repository.createAccount(contractB_addr);
repository.saveCode(contractB_addr, codeB);
final BigInteger value = new BigInteger("1000000000000000000");
repository.addBalance(contractB_addr, new Coin(value));
repository.createAccount(caller_addr);
final BigInteger value1 = new BigInteger("100000000000000000000");
repository.addBalance(caller_addr, new Coin(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 ============");
System.out.println("*** Used gas: " + program.getResult().getGasUsed());
DataWord memValue1 = program.memoryLoad(new DataWord(0));
DataWord memValue2 = program.memoryLoad(new DataWord(32));
DataWord storeValue1 = repository.getStorageValue(contractB_addr, new DataWord(00));
repository.close();
assertEquals("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", memValue1.toString());
assertEquals("aaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa", memValue2.toString());
assertEquals("0x1", storeValue1.shortHex());
// TODO: check that the value pushed after exec is 1
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class VMComplexTest method test1.
// TODO #POC9
@Ignore
// contract call recursive
@Test
public void test1() {
/**
* #The code will run
* ------------------
*
* a = contract.storage[999]
* if a > 0:
* contract.storage[999] = a - 1
*
* # call to contract: 77045e71a7a2c50903d88e564cd72fab11e82051
* send((tx.gas / 10 * 8), 0x77045e71a7a2c50903d88e564cd72fab11e82051, 0)
* else:
* stop
*/
int expectedGas = 436;
DataWord key1 = new DataWord(999);
DataWord value1 = new DataWord(3);
// Set contract into Database
String callerAddr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826";
String contractAddr = "77045e71a7a2c50903d88e564cd72fab11e82051";
String code = "6103e75460005260006000511115630000004c576001600051036103e755600060006000600060007377045e71a7a2c50903d88e564cd72fab11e820516008600a5a0402f1630000004c00565b00";
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("100000000000000000000");
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 SelectionRule method isBrokenSelectionRule.
public static boolean isBrokenSelectionRule(BlockHeader processingBlockHeader, List<Sibling> siblings) {
int maxUncleCount = 0;
for (Sibling sibling : siblings) {
maxUncleCount = Math.max(maxUncleCount, sibling.getUncleCount());
Coin pfm = processingBlockHeader.getPaidFees().multiply(PAID_FEES_MULTIPLIER_CRITERIA);
if (sibling.getPaidFees().compareTo(pfm) > 0) {
return true;
}
Coin blockFeesCriteria = sibling.getPaidFees().multiply(PAID_FEES_MULTIPLIER_CRITERIA);
if (processingBlockHeader.getPaidFees().compareTo(blockFeesCriteria) < 0 && isThisBlockHashSmaller(sibling.getHash(), processingBlockHeader.getHash().getBytes())) {
return true;
}
}
return maxUncleCount > processingBlockHeader.getUncleCount();
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class RepositoryImpl method addBalance.
@Override
public synchronized Coin addBalance(RskAddress addr, Coin value) {
AccountState account = getAccountStateOrCreateNew(addr);
Coin result = account.addToBalance(value);
updateAccountState(addr, account);
return result;
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class MinerUtils method filterTransactions.
public List<org.ethereum.core.Transaction> filterTransactions(List<Transaction> txsToRemove, List<Transaction> txs, Map<RskAddress, BigInteger> accountNonces, Repository originalRepo, Coin minGasPrice) {
List<org.ethereum.core.Transaction> txsResult = new ArrayList<>();
for (org.ethereum.core.Transaction tx : txs) {
try {
Keccak256 hash = tx.getHash();
Coin txValue = tx.getValue();
BigInteger txNonce = new BigInteger(1, tx.getNonce());
RskAddress txSender = tx.getSender();
logger.debug("Examining tx={} sender: {} value: {} nonce: {}", hash, txSender, txValue, txNonce);
BigInteger expectedNonce;
if (accountNonces.containsKey(txSender)) {
expectedNonce = accountNonces.get(txSender).add(BigInteger.ONE);
} else {
expectedNonce = originalRepo.getNonce(txSender);
}
if (!(tx instanceof RemascTransaction) && tx.getGasPrice().compareTo(minGasPrice) < 0) {
logger.warn("Rejected tx={} because of low gas account {}, removing tx from pending state.", hash, txSender);
txsToRemove.add(tx);
continue;
}
if (!expectedNonce.equals(txNonce)) {
logger.warn("Invalid nonce, expected {}, found {}, tx={}", expectedNonce, txNonce, hash);
continue;
}
accountNonces.put(txSender, txNonce);
logger.debug("Accepted tx={} sender: {} value: {} nonce: {}", hash, txSender, txValue, txNonce);
} catch (Exception e) {
// Txs that can't be selected by any reason should be removed from pending state
logger.warn(String.format("Error when processing tx=%s", tx.getHash()), e);
if (txsToRemove != null) {
txsToRemove.add(tx);
} else {
logger.error("Can't remove invalid txs from pending state.");
}
continue;
}
txsResult.add(tx);
}
logger.debug("Ending getTransactions {}", txsResult.size());
return txsResult;
}
Aggregations