Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create processValidationFailure method #2817

Merged
31 changes: 31 additions & 0 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.Repository;
import org.ethereum.vm.DataWord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;

/**
Expand All @@ -40,6 +42,7 @@
* @author Oscar Guindzberg
*/
public class BridgeStorageProvider {
private static final Logger logger = LoggerFactory.getLogger(BridgeStorageProvider.class);

// Dummy value to use when saving key only indexes
private static final byte TRUE_VALUE = (byte) 1;
Expand Down Expand Up @@ -679,6 +682,34 @@ private void saveSvpSpendTxWaitingForSignatures() {
);
}

public void clearSvpValues() {
if (svpFundTxHashUnsigned != null) {
logger.warn("[clearSvpValues] Clearing fund tx hash unsigned {} value.", svpFundTxHashUnsigned);
setSvpFundTxHashUnsigned(null);
}

if (svpFundTxSigned != null) {
logger.warn("[clearSvpValues] Clearing fund tx signed {} value.", svpFundTxSigned.getHash());
setSvpFundTxSigned(null);
}

if (svpSpendTxWaitingForSignatures != null) {
Keccak256 rskCreationHash = svpSpendTxWaitingForSignatures.getKey();
BtcTransaction svpSpendTx = svpSpendTxWaitingForSignatures.getValue();
logger.warn(
"[clearSvpValues] Clearing spend tx waiting for signatures with spend tx {} and rsk creation hash {} value.",
svpSpendTx.getHash(), rskCreationHash
);
setSvpSpendTxWaitingForSignatures(null);
setSvpSpendTxHashUnsigned(null);
}

if (svpSpendTxHashUnsigned != null) {
logger.warn("[clearSvpValues] Clearing spend tx hash unsigned {} value.", svpSpendTxHashUnsigned);
setSvpSpendTxHashUnsigned(null);
}
}

public void save() {
saveBtcTxHashesAlreadyProcessed();

Expand Down
15 changes: 15 additions & 0 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,21 @@ private void logUpdateCollections(Transaction rskTx) {
eventLogger.logUpdateCollections(sender);
}

protected void processSVPFailure(Federation proposedFederation) {
eventLogger.logCommitFederationFailure(rskExecutionBlock, proposedFederation);
logger.warn(
"[processSVPFailure] Proposed federation validation failed at block {}, so federation election will be allowed again.",
rskExecutionBlock.getNumber()
);

allowFederationElectionAgain();
}

private void allowFederationElectionAgain() {
federationSupport.clearProposedFederation();
provider.clearSvpValues();
}

private boolean svpIsOngoing() {
return federationSupport.getProposedFederation()
.map(Federation::getCreationBlockNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public interface FederationSupport {
*/
Optional<byte[]> getProposedFederatorPublicKeyOfType(int index, FederationMember.KeyType keyType);

void clearProposedFederation();

int voteFederationChange(
Transaction tx,
ABICallSpec callSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ public Optional<Instant> getProposedFederationCreationTime() {
.map(Federation::getCreationTime);
}

@Override
public void clearProposedFederation() {
nathanieliov marked this conversation as resolved.
Show resolved Hide resolved
provider.setProposedFederation(null);
}

@Override
public int voteFederationChange(Transaction tx, ABICallSpec callSpec, SignatureCache signatureCache, BridgeEventLogger eventLogger) {
String calledFunction = callSpec.getFunction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,82 @@ void getSvpSpendTxWaitingForSignatures_whenEntryIsCached_shouldReturnTheCachedEn
}
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("clear svp values tests")
class ClearSvpValuesTests {
private BridgeStorageProvider bridgeStorageProvider;

@BeforeEach
void setup() {
Repository repository = createRepository();
bridgeStorageProvider = createBridgeStorageProvider(repository, mainnetBtcParams, activationsAllForks);
}

@Test
void clearSvpValues_whenFundTxHashUnsigned_shouldClearValue() {
// arrange
Sha256Hash svpFundTxHashUnsigned = BitcoinTestUtils.createHash(1);
bridgeStorageProvider.setSvpFundTxHashUnsigned(svpFundTxHashUnsigned);

// act
bridgeStorageProvider.clearSvpValues();

// assert
assertNoSVPValues();
}

@Test
void clearSvpValues_whenFundTxSigned_shouldClearValue() {
// arrange
BtcTransaction svpFundTxSigned = new BtcTransaction(mainnetBtcParams);
bridgeStorageProvider.setSvpFundTxSigned(svpFundTxSigned);

// act
bridgeStorageProvider.clearSvpValues();

// assert
assertNoSVPValues();
}

@Test
void clearSvpValues_whenSpendTxWFS_shouldClearSpendTxValues() {
// arrange
Keccak256 svpSpendTxCreationHash = RskTestUtils.createHash(1);
BtcTransaction svpSpendTx = new BtcTransaction(mainnetBtcParams);
Map.Entry<Keccak256, BtcTransaction> svpSpendTxWFS = new AbstractMap.SimpleEntry<>(svpSpendTxCreationHash, svpSpendTx);
bridgeStorageProvider.setSvpSpendTxWaitingForSignatures(svpSpendTxWFS);

// act
bridgeStorageProvider.clearSvpValues();

// assert
assertNoSVPValues();
}

@Test
void clearSvpValues_whenSpendTxHashUnsigned_shouldClearValue() {
// arrange
Keccak256 svpSpendTxCreationHash = RskTestUtils.createHash(1);
BtcTransaction svpSpendTx = new BtcTransaction(mainnetBtcParams);
Map.Entry<Keccak256, BtcTransaction> svpSpendTxWFS = new AbstractMap.SimpleEntry<>(svpSpendTxCreationHash, svpSpendTx);
bridgeStorageProvider.setSvpSpendTxWaitingForSignatures(svpSpendTxWFS);

// act
bridgeStorageProvider.clearSvpValues();

// assert
assertNoSVPValues();
}

private void assertNoSVPValues() {
assertFalse(bridgeStorageProvider.getSvpFundTxHashUnsigned().isPresent());
assertFalse(bridgeStorageProvider.getSvpFundTxSigned().isPresent());
assertFalse(bridgeStorageProvider.getSvpSpendTxWaitingForSignatures().isPresent());
assertFalse(bridgeStorageProvider.getSvpSpendTxHashUnsigned().isPresent());
}
}

@Test
void getReleaseRequestQueue_before_rskip_146_activation() throws IOException {
Repository repositoryMock = mock(Repository.class);
Expand Down
Loading