-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from rsksmart/avoid-signing-same-pegout-integ…
…ration Avoid Pegnatories From Signing Multiple Times The Same Peg-out
- Loading branch information
Showing
8 changed files
with
599 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/co/rsk/federate/btcreleaseclient/cache/PegoutSignedCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package co.rsk.federate.btcreleaseclient.cache; | ||
|
||
import co.rsk.crypto.Keccak256; | ||
|
||
public interface PegoutSignedCache { | ||
|
||
/** | ||
* Checks if the specified RSK transaction hash for pegout creation has already | ||
* been signed. | ||
* | ||
* @param pegoutCreationRskTxHash The Keccak256 hash of the RSK transaction for | ||
* pegout creation. | ||
* @return {@code true} if the hash of the transaction has already been signed, | ||
* {@code false} otherwise. | ||
*/ | ||
boolean hasAlreadyBeenSigned(Keccak256 pegoutCreationRskTxHash); | ||
|
||
/** | ||
* Stores the specified RSK transaction hash for pegout creation along with its | ||
* timestamp in the cache if absent. | ||
* | ||
* @param pegoutCreationRskTxHash The Keccak256 hash of the RSK transaction for | ||
* pegout creation to be stored. | ||
*/ | ||
void putIfAbsent(Keccak256 pegoutCreationRskTxHash); | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/co/rsk/federate/btcreleaseclient/cache/PegoutSignedCacheImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package co.rsk.federate.btcreleaseclient.cache; | ||
|
||
import co.rsk.crypto.Keccak256; | ||
import java.time.Clock; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class PegoutSignedCacheImpl implements PegoutSignedCache { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(PegoutSignedCacheImpl.class); | ||
private static final Integer CLEANUP_INTERVAL_IN_HOURS = 1; | ||
|
||
private final Map<Keccak256, Instant> cache = new ConcurrentHashMap<>(); | ||
private final ScheduledExecutorService cleanupScheduler = Executors.newSingleThreadScheduledExecutor(); | ||
private final Duration ttl; | ||
private final Clock clock; | ||
|
||
public PegoutSignedCacheImpl(Duration ttl, Clock clock) { | ||
validateTtl(ttl); | ||
|
||
this.ttl = ttl; | ||
this.clock = clock; | ||
|
||
// Start a background thread for periodic cleanup | ||
cleanupScheduler.scheduleAtFixedRate( | ||
this::performCleanup, | ||
CLEANUP_INTERVAL_IN_HOURS, // initial delay | ||
CLEANUP_INTERVAL_IN_HOURS, // period | ||
TimeUnit.HOURS | ||
); | ||
} | ||
|
||
@Override | ||
public boolean hasAlreadyBeenSigned(Keccak256 pegoutCreationRskTxHash) { | ||
return Optional.ofNullable(pegoutCreationRskTxHash) | ||
.map(cache::get) | ||
.map(this::isValidTimestamp) | ||
.orElse(false); | ||
} | ||
|
||
@Override | ||
public void putIfAbsent(Keccak256 pegoutCreationRskTxHash) { | ||
if (pegoutCreationRskTxHash == null) { | ||
throw new IllegalArgumentException( | ||
"The pegoutCreationRskTxHash argument must not be null"); | ||
} | ||
|
||
Optional.of(pegoutCreationRskTxHash) | ||
.ifPresent(rskTxHash -> cache.putIfAbsent(rskTxHash, clock.instant())); | ||
} | ||
|
||
void performCleanup() { | ||
logger.trace( | ||
"[performCleanup] Pegouts signed cache before cleanup: {}", cache.keySet()); | ||
cache.entrySet().removeIf( | ||
entry -> !isValidTimestamp(entry.getValue())); | ||
logger.trace( | ||
"[performCleanup] Pegouts signed cache after cleanup: {}", cache.keySet()); | ||
} | ||
|
||
private boolean isValidTimestamp(Instant timestampInCache) { | ||
return Optional.ofNullable(timestampInCache) | ||
.map(timestamp -> clock.instant().toEpochMilli() - timestamp.toEpochMilli()) | ||
.map(timeCachedInMillis -> timeCachedInMillis <= ttl.toMillis()) | ||
.orElse(false); | ||
} | ||
|
||
private static void validateTtl(Duration ttl) { | ||
if (ttl == null || ttl.isNegative() || ttl.isZero()) { | ||
Long ttlInMinutes = ttl != null ? ttl.toMinutes() : null; | ||
String message = String.format( | ||
"Invalid pegouts signed cache TTL value in minutes supplied: %d", ttlInMinutes); | ||
logger.error("[validateTtl] {}", message); | ||
|
||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.