use of co.rsk.peg.Bridge in project rskj by rsksmart.
the class Program method callToPrecompiledAddress.
public void callToPrecompiledAddress(MessageCall msg, PrecompiledContract contract) {
if (getCallDeep() == MAX_DEPTH) {
stackPushZero();
this.refundGas(msg.getGas().longValue(), " call deep limit reach");
return;
}
Repository track = getStorage().startTracking();
RskAddress senderAddress = new RskAddress(getOwnerAddress());
RskAddress codeAddress = new RskAddress(msg.getCodeAddress());
RskAddress contextAddress = msg.getType().isStateless() ? senderAddress : codeAddress;
Coin endowment = new Coin(msg.getEndowment().getData());
Coin senderBalance = track.getBalance(senderAddress);
if (senderBalance.compareTo(endowment) < 0) {
stackPushZero();
this.refundGas(msg.getGas().longValue(), "refund gas from message call");
return;
}
byte[] data = this.memoryChunk(msg.getInDataOffs().intValue(), msg.getInDataSize().intValue());
// Charge for endowment - is not reversible by rollback
track.transfer(senderAddress, contextAddress, new Coin(msg.getEndowment().getData()));
if (byTestingSuite()) {
// This keeps track of the calls created for a test
this.getResult().addCallCreate(data, codeAddress.getBytes(), msg.getGas().longValueSafe(), msg.getEndowment().getNoLeadZeroesData());
stackPushOne();
return;
}
long requiredGas = contract.getGasForData(data);
if (requiredGas > msg.getGas().longValue()) {
// matches cpp logic
this.refundGas(0, "call pre-compiled");
this.stackPushZero();
track.rollback();
} else {
this.refundGas(msg.getGas().longValue() - requiredGas, "call pre-compiled");
if (contract instanceof Bridge || contract instanceof RemascContract) {
// CREATE CALL INTERNAL TRANSACTION
InternalTransaction internalTx = addInternalTx(null, getGasLimit(), senderAddress, contextAddress, endowment, EMPTY_BYTE_ARRAY, "call");
Block executionBlock = new Block(getPrevHash().getData(), EMPTY_BYTE_ARRAY, getCoinbase().getData(), EMPTY_BYTE_ARRAY, getDifficulty().getData(), getNumber().longValue(), getGasLimit().getData(), 0, getTimestamp().longValue(), EMPTY_BYTE_ARRAY, EMPTY_BYTE_ARRAY, EMPTY_BYTE_ARRAY, new ArrayList<>(), new ArrayList<>(), null);
contract.init(internalTx, executionBlock, track, this.invoke.getBlockStore(), null, null);
}
byte[] out = contract.execute(data);
this.memorySave(msg.getOutDataOffs().intValue(), out);
this.stackPushOne();
track.commit();
}
}
use of co.rsk.peg.Bridge in project rskj by rsksmart.
the class BridgePerformanceTestCase method execute.
private ExecutionTracker execute(ABIEncoder abiEncoder, BridgeStorageProviderInitializer storageInitializer, TxBuilder txBuilder, HeightProvider heightProvider, int executionIndex) {
ExecutionTracker executionInfo = new ExecutionTracker(thread);
RepositoryImpl repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeStorageProvider storageProvider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
storageInitializer.initialize(storageProvider, track, executionIndex);
try {
storageProvider.save();
} catch (Exception e) {
throw new RuntimeException("Error trying to save the storage after initialization", e);
}
track.commit();
Transaction tx = txBuilder.build(executionIndex);
List<LogInfo> logs = new ArrayList<>();
RepositoryTrackWithBenchmarking benchmarkerTrack = new RepositoryTrackWithBenchmarking(config, repository);
Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
Blockchain blockchain = BlockChainBuilder.ofSizeWithNoTransactionPoolCleaner(heightProvider.getHeight(executionIndex));
bridge.init(tx, blockchain.getBestBlock(), benchmarkerTrack, blockchain.getBlockStore(), null, logs);
// Execute a random method so that bridge support initialization
// does its initial writes to the repo for e.g. genesis block,
// federation, etc, etc. and we don't get
// those recorded in the actual execution.
bridge.execute(Bridge.GET_FEDERATION_SIZE.encode());
benchmarkerTrack.getStatistics().clear();
executionInfo.startTimer();
bridge.execute(abiEncoder.encode(executionIndex));
executionInfo.endTimer();
benchmarkerTrack.commit();
executionInfo.setRepositoryStatistics(benchmarkerTrack.getStatistics());
return executionInfo;
}
Aggregations