Skip to content

Commit

Permalink
Merge pull request #1497 from rsksmart/bridge-methods-refactor
Browse files Browse the repository at this point in the history
Bridge new methods
  • Loading branch information
ajlopezrsk authored Apr 14, 2021
2 parents c71670d + 397b91c commit be45938
Show file tree
Hide file tree
Showing 11 changed files with 795 additions and 309 deletions.
127 changes: 82 additions & 45 deletions rskj-core/src/main/java/co/rsk/peg/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,10 @@ public byte[] execute(byte[] data) throws VMException {

// If this is not a local call, then first check whether the function
// allows for non-local calls
if (activations.isActive(ConsensusRule.RSKIP88) && !isLocalCall() && bridgeParsedData.bridgeMethod.onlyAllowsLocalCalls()) {
if (activations.isActive(ConsensusRule.RSKIP88) &&
!isLocalCall() &&
bridgeParsedData.bridgeMethod.onlyAllowsLocalCalls(this, bridgeParsedData.args)) {

String errorMessage = String.format("Non-local-call to %s. Returning without execution.", bridgeParsedData.bridgeMethod.getFunction().name);
logger.info(errorMessage);
throw new BridgeIllegalArgumentException(errorMessage);
Expand Down Expand Up @@ -565,6 +568,10 @@ public Integer getBtcBlockchainBestChainHeight(Object[] args) throws VMException
}
}

public boolean getBtcBlockchainBestChainHeightOnlyAllowsLocalCalls(Object[] args) {
return !activations.isActive(ConsensusRule.RSKIP220);
}

public Integer getBtcBlockchainInitialBlockHeight(Object[] args) throws VMException {
logger.trace("getBtcBlockchainInitialBlockHeight");

Expand Down Expand Up @@ -640,8 +647,7 @@ public int getBtcTransactionConfirmations(Object[] args) throws VMException {
}
}

public Long getMinimumLockTxValue(Object[] args)
{
public Long getMinimumLockTxValue(Object[] args) {
logger.trace("getMinimumLockTxValue");
return bridgeSupport.getMinimumLockTxValue().getValue();
}
Expand Down Expand Up @@ -670,29 +676,25 @@ public Long getBtcTxHashProcessedHeight(Object[] args) throws VMException {
}
}

public String getFederationAddress(Object[] args)
{
public String getFederationAddress(Object[] args) {
logger.trace("getFederationAddress");

return bridgeSupport.getFederationAddress().toBase58();
}

public Integer getFederationSize(Object[] args)
{
public Integer getFederationSize(Object[] args) {
logger.trace("getFederationSize");

return bridgeSupport.getFederationSize();
}

public Integer getFederationThreshold(Object[] args)
{
public Integer getFederationThreshold(Object[] args) {
logger.trace("getFederationThreshold");

return bridgeSupport.getFederationThreshold();
}

public byte[] getFederatorPublicKey(Object[] args)
{
public byte[] getFederatorPublicKey(Object[] args) {
logger.trace("getFederatorPublicKey");

int index = ((BigInteger) args[0]).intValue();
Expand All @@ -715,8 +717,7 @@ public byte[] getFederatorPublicKeyOfType(Object[] args) throws VMException {
return bridgeSupport.getFederatorPublicKeyOfType(index, keyType);
}

public Long getFederationCreationTime(Object[] args)
{
public Long getFederationCreationTime(Object[] args) {
logger.trace("getFederationCreationTime");

// Return the creation time in milliseconds from the epoch
Expand All @@ -728,8 +729,7 @@ public long getFederationCreationBlockNumber(Object[] args) {
return bridgeSupport.getFederationCreationBlockNumber();
}

public String getRetiringFederationAddress(Object[] args)
{
public String getRetiringFederationAddress(Object[] args) {
logger.trace("getRetiringFederationAddress");

Address address = bridgeSupport.getRetiringFederationAddress();
Expand All @@ -742,22 +742,19 @@ public String getRetiringFederationAddress(Object[] args)
return address.toBase58();
}

public Integer getRetiringFederationSize(Object[] args)
{
public Integer getRetiringFederationSize(Object[] args) {
logger.trace("getRetiringFederationSize");

return bridgeSupport.getRetiringFederationSize();
}

public Integer getRetiringFederationThreshold(Object[] args)
{
public Integer getRetiringFederationThreshold(Object[] args) {
logger.trace("getRetiringFederationThreshold");

return bridgeSupport.getRetiringFederationThreshold();
}

public byte[] getRetiringFederatorPublicKey(Object[] args)
{
public byte[] getRetiringFederatorPublicKey(Object[] args) {
logger.trace("getRetiringFederatorPublicKey");

int index = ((BigInteger) args[0]).intValue();
Expand Down Expand Up @@ -794,8 +791,7 @@ public byte[] getRetiringFederatorPublicKeyOfType(Object[] args) throws VMExcept
return publicKey;
}

public Long getRetiringFederationCreationTime(Object[] args)
{
public Long getRetiringFederationCreationTime(Object[] args) {
logger.trace("getRetiringFederationCreationTime");

Instant creationTime = bridgeSupport.getRetiringFederationCreationTime();
Expand Down Expand Up @@ -879,8 +875,7 @@ public Integer rollbackFederation(Object[] args) throws BridgeIllegalArgumentExc
);
}

public byte[] getPendingFederationHash(Object[] args)
{
public byte[] getPendingFederationHash(Object[] args) {
logger.trace("getPendingFederationHash");

byte[] hash = bridgeSupport.getPendingFederationHash();
Expand All @@ -893,15 +888,13 @@ public byte[] getPendingFederationHash(Object[] args)
return hash;
}

public Integer getPendingFederationSize(Object[] args)
{
public Integer getPendingFederationSize(Object[] args) {
logger.trace("getPendingFederationSize");

return bridgeSupport.getPendingFederationSize();
}

public byte[] getPendingFederatorPublicKey(Object[] args)
{
public byte[] getPendingFederatorPublicKey(Object[] args) {
logger.trace("getPendingFederatorPublicKey");

int index = ((BigInteger) args[0]).intValue();
Expand Down Expand Up @@ -938,15 +931,13 @@ public byte[] getPendingFederatorPublicKeyOfType(Object[] args) throws VMExcepti
return publicKey;
}

public Integer getLockWhitelistSize(Object[] args)
{
public Integer getLockWhitelistSize(Object[] args) {
logger.trace("getLockWhitelistSize");

return bridgeSupport.getLockWhitelistSize();
}

public String getLockWhitelistAddress(Object[] args)
{
public String getLockWhitelistAddress(Object[] args) {
logger.trace("getLockWhitelistAddress");

int index = ((BigInteger) args[0]).intValue();
Expand All @@ -960,8 +951,7 @@ public String getLockWhitelistAddress(Object[] args)
return entry.address().toBase58();
}

public long getLockWhitelistEntryByAddress(Object[] args)
{
public long getLockWhitelistEntryByAddress(Object[] args) {
logger.trace("getLockWhitelistEntryByAddress");

String addressBase58;
Expand All @@ -984,8 +974,7 @@ public long getLockWhitelistEntryByAddress(Object[] args)
LOCK_WHITELIST_UNLIMITED_MODE_CODE;
}

public Integer addOneOffLockWhitelistAddress(Object[] args)
{
public Integer addOneOffLockWhitelistAddress(Object[] args) {
logger.trace("addOneOffLockWhitelistAddress");

String addressBase58;
Expand All @@ -1001,8 +990,7 @@ public Integer addOneOffLockWhitelistAddress(Object[] args)
return bridgeSupport.addOneOffLockWhitelistAddress(rskTx, addressBase58, maxTransferValue);
}

public Integer addUnlimitedLockWhitelistAddress(Object[] args)
{
public Integer addUnlimitedLockWhitelistAddress(Object[] args) {
logger.trace("addUnlimitedLockWhitelistAddress");

String addressBase58;
Expand All @@ -1016,8 +1004,7 @@ public Integer addUnlimitedLockWhitelistAddress(Object[] args)
return bridgeSupport.addUnlimitedLockWhitelistAddress(rskTx, addressBase58);
}

public Integer removeLockWhitelistAddress(Object[] args)
{
public Integer removeLockWhitelistAddress(Object[] args) {
logger.trace("removeLockWhitelistAddress");

String addressBase58;
Expand All @@ -1037,8 +1024,7 @@ public Integer setLockWhitelistDisableBlockDelay(Object[] args) throws IOExcepti
return bridgeSupport.setLockWhitelistDisableBlockDelay(rskTx, lockWhitelistDisableBlockDelay);
}

public Integer voteFeePerKbChange(Object[] args)
{
public Integer voteFeePerKbChange(Object[] args) {
logger.trace("voteFeePerKbChange");

Coin feePerKb;
Expand All @@ -1052,8 +1038,7 @@ public Integer voteFeePerKbChange(Object[] args)
return bridgeSupport.voteFeePerKbChange(rskTx, feePerKb);
}

public long getFeePerKb(Object[] args)
{
public long getFeePerKb(Object[] args) {
logger.trace("getFeePerKb");

return bridgeSupport.getFeePerKb().getValue();
Expand Down Expand Up @@ -1145,6 +1130,58 @@ public long registerFastBridgeBtcTransaction(Object[] args) {
}
}

public byte[] getBtcBlockchainBestBlockHeader(Object[] args) {
logger.trace("getBtcBlockchainBestBlockHeader");

try {
return this.bridgeSupport.getBtcBlockchainBestBlockHeader();
} catch (Exception e) {
logger.warn("Exception in getBtcBlockchainBestBlockHeader", e);
return ByteUtil.EMPTY_BYTE_ARRAY;
}
}

public byte[] getBtcBlockchainBlockHeaderByHash(Object[] args) {
logger.trace("getBtcBlockchainBlockHeaderByHash");

try {
byte[] hashBytes = (byte[])args[0];
Sha256Hash hash = Sha256Hash.wrap(hashBytes);

return this.bridgeSupport.getBtcBlockchainBlockHeaderByHash(hash);
} catch (Exception e) {
logger.warn("Exception in getBtcBlockchainBlockHeaderByHash", e);
return ByteUtil.EMPTY_BYTE_ARRAY;
}
}

public byte[] getBtcBlockchainBlockHeaderByHeight(Object[] args) {
logger.trace("getBtcBlockchainBlockHeaderByHeight");

try {
int height = ((BigInteger) args[0]).intValue();

return this.bridgeSupport.getBtcBlockchainBlockHeaderByHeight(height);
} catch (Exception e) {
logger.warn("Exception in getBtcBlockchainBlockHeaderByHeight", e);
return ByteUtil.EMPTY_BYTE_ARRAY;
}
}

public byte[] getBtcBlockchainParentBlockHeaderByHash(Object[] args) {
logger.trace("getBtcBlockchainParentBlockHeaderByHash");

try {
byte[] hashBytes = (byte[])args[0];
Sha256Hash hash = Sha256Hash.wrap(hashBytes);

return this.bridgeSupport.getBtcBlockchainParentBlockHeaderByHash(hash);
} catch (Exception e) {
logger.warn("Exception in getBtcBlockchainParentBlockHeaderByHash", e);
return ByteUtil.EMPTY_BYTE_ARRAY;
}
}

public static BridgeMethods.BridgeMethodExecutor activeAndRetiringFederationOnly(BridgeMethods.BridgeMethodExecutor decoratee, String funcName) {
return (self, args) -> {
Federation retiringFederation = self.bridgeSupport.getRetiringFederation();
Expand Down
Loading

0 comments on commit be45938

Please # to comment.