This repository has been archived by the owner on Aug 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move validation to validation service package * Add Transaction Solidifier * Update Transaction Validator * Update Unit Tests * Remove tip field from solidify stage * Move broadcast queue retreival to solidify stage * Undo auto-formatting * More autoformatting * Re-remove refillBroadcast * Move propagation logic to inner class * Remove unused imports * Add comment to propagator * Remove unused txSolidifier private field * Remove separate thread logic, check solidity from milestone solidifier * LinkedHashSet -> ArrayDeque in checkSolidity * Update maxProcessedTransactions value determination * Make Transaction Propagator private * Typo correction Co-authored-by: Gal Rogozinski <[email protected]>
- Loading branch information
Showing
22 changed files
with
1,037 additions
and
548 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
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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/iota/iri/network/pipeline/SolidifyPayload.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,39 @@ | ||
package com.iota.iri.network.pipeline; | ||
|
||
import com.iota.iri.controllers.TransactionViewModel; | ||
import com.iota.iri.network.neighbor.Neighbor; | ||
|
||
/** | ||
* Defines a payload which gets submitted to the {@link SolidifyStage}. | ||
*/ | ||
public class SolidifyPayload extends Payload { | ||
private Neighbor originNeighbor; | ||
private TransactionViewModel tvm; | ||
|
||
/** | ||
* Constructor for solidification payload. | ||
* | ||
* @param originNeighbor The originating point of a received transaction | ||
* @param tvm The transaction that needs to be solidified | ||
*/ | ||
public SolidifyPayload(Neighbor originNeighbor, TransactionViewModel tvm){ | ||
this.originNeighbor = originNeighbor; | ||
this.tvm = tvm; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Neighbor getOriginNeighbor(){ | ||
return originNeighbor; | ||
} | ||
|
||
/** | ||
* Fetches the transaction from the payload. | ||
* @return The transaction stored in the payload. | ||
*/ | ||
public TransactionViewModel getTransaction(){ | ||
return tvm; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/com/iota/iri/network/pipeline/SolidifyStage.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,94 @@ | ||
package com.iota.iri.network.pipeline; | ||
|
||
import com.iota.iri.controllers.TipsViewModel; | ||
import com.iota.iri.controllers.TransactionViewModel; | ||
import com.iota.iri.model.Hash; | ||
import com.iota.iri.service.validation.TransactionSolidifier; | ||
import com.iota.iri.storage.Tangle; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static com.iota.iri.controllers.TransactionViewModel.fromHash; | ||
|
||
/** | ||
* The {@link SolidifyStage} is used to process newly received transaction for solidity. Once a transaction has been | ||
* passed from the {@link ReceivedStage} it will be placed into this stage to have the {@link TransactionSolidifier} | ||
* check the solidity of the transaction. If the transaction is found to be solid, it will be passed forward to the | ||
* {@link BroadcastStage}. If it is found to be unsolid, it is put through the solidity check so that missing reference | ||
* transactions get requested. If the transaction is unsolid, a random solid tip is broadcast instead to keep the | ||
* requests transmitting to neighbors. | ||
*/ | ||
public class SolidifyStage implements Stage { | ||
private static final Logger log = LoggerFactory.getLogger(SolidifyStage.class); | ||
|
||
private TransactionSolidifier txSolidifier; | ||
private TipsViewModel tipsViewModel; | ||
private Tangle tangle; | ||
|
||
/** | ||
* Constructor for the {@link SolidifyStage}. | ||
* | ||
* @param txSolidifier Transaction solidifier implementation for determining the validity of a transaction | ||
* @param tipsViewModel Used for broadcasting random solid tips if the subject transaction is unsolid | ||
* @param tangle A reference to the nodes DB | ||
*/ | ||
public SolidifyStage(TransactionSolidifier txSolidifier, TipsViewModel tipsViewModel, Tangle tangle){ | ||
this.txSolidifier = txSolidifier; | ||
this.tipsViewModel = tipsViewModel; | ||
this.tangle = tangle; | ||
} | ||
|
||
/** | ||
* Processes the payload of the {@link ProcessingContext} as a {@link SolidifyPayload}. First the transaction will | ||
* be checked for solidity and validity. If the transaction is already solid or can be set solid quickly by the | ||
* transaction solidifier, the transaction is passed to the {@link BroadcastStage}. If not, a random solid tip is | ||
* pulled form the {@link TipsViewModel} to be broadcast instead. | ||
* | ||
* @param ctx The context to process | ||
* @return The output context, in most cases a {@link BroadcastPayload}. | ||
*/ | ||
@Override | ||
public ProcessingContext process(ProcessingContext ctx){ | ||
try { | ||
SolidifyPayload payload = (SolidifyPayload) ctx.getPayload(); | ||
TransactionViewModel tvm = payload.getTransaction(); | ||
|
||
if (tvm.isSolid() || txSolidifier.quickSetSolid(tvm)) { | ||
// If the transaction is in the solidifier broadcast queue, remove it as it will be broadcast now | ||
txSolidifier.clearFromBroadcastQueue(tvm); | ||
ctx.setNextStage(TransactionProcessingPipeline.Stage.BROADCAST); | ||
ctx.setPayload(new BroadcastPayload(payload.getOriginNeighbor(), payload.getTransaction())); | ||
return ctx; | ||
} | ||
|
||
return broadcastTip(ctx, payload); | ||
}catch (Exception e){ | ||
log.error("Failed to process transaction for solidification", e); | ||
ctx.setNextStage(TransactionProcessingPipeline.Stage.ABORT); | ||
return ctx; | ||
} | ||
|
||
} | ||
|
||
private ProcessingContext broadcastTip(ProcessingContext ctx, SolidifyPayload payload) throws Exception{ | ||
// First check if there is a transaction available to broadcast from the broadcast queue | ||
TransactionViewModel tip = txSolidifier.getNextTxInBroadcastQueue(); | ||
|
||
// If there is not a transaction available from the broadcast queue, instead try to send a solid tip | ||
if (tip == null) { | ||
Hash tipHash = tipsViewModel.getRandomSolidTipHash(); | ||
|
||
if (tipHash == null) { | ||
ctx.setNextStage(TransactionProcessingPipeline.Stage.FINISH); | ||
return ctx; | ||
} | ||
|
||
tip = fromHash(tangle, tipHash); | ||
} | ||
|
||
ctx.setNextStage(TransactionProcessingPipeline.Stage.BROADCAST); | ||
ctx.setPayload(new BroadcastPayload(payload.getOriginNeighbor(), tip)); | ||
|
||
return ctx; | ||
} | ||
} |
Oops, something went wrong.