-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise BlockHeader Validators for compatible with staking block heade…
…r rules
- Loading branch information
Showing
11 changed files
with
179 additions
and
60 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
34 changes: 26 additions & 8 deletions
34
modAionImpl/src/org/aion/zero/impl/valid/BlockHeaderValidator.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
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
48 changes: 36 additions & 12 deletions
48
modAionImpl/src/org/aion/zero/impl/valid/GrandParentBlockHeaderValidator.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 |
---|---|---|
@@ -1,28 +1,52 @@ | ||
package org.aion.zero.impl.valid; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.aion.mcf.blockchain.BlockHeader; | ||
import org.slf4j.Logger; | ||
|
||
public class GrandParentBlockHeaderValidator | ||
extends AbstractBlockHeaderValidator { | ||
public class GrandParentBlockHeaderValidator extends AbstractBlockHeaderValidator { | ||
|
||
private List<GrandParentDependantBlockHeaderRule> rules; | ||
private Map<Byte, List<GrandParentDependantBlockHeaderRule>> chainRules; | ||
|
||
public GrandParentBlockHeaderValidator(List<GrandParentDependantBlockHeaderRule> rules) { | ||
this.rules = rules; | ||
public GrandParentBlockHeaderValidator( | ||
Map<Byte, List<GrandParentDependantBlockHeaderRule>> rules) { | ||
if (rules == null) { | ||
throw new NullPointerException(); | ||
} | ||
chainRules = rules; | ||
} | ||
|
||
public boolean validate(BlockHeader grandParent, BlockHeader parent, BlockHeader current, Logger logger) { | ||
List<RuleError> errors = new LinkedList<>(); | ||
public boolean validate( | ||
BlockHeader grandParent, BlockHeader parent, BlockHeader current, Logger logger) { | ||
if (parent == null) { | ||
RuleError err = new RuleError(this.getClass(),"the input parent header is null"); | ||
logErrors(logger, Collections.singletonList(err)); | ||
return false; | ||
} | ||
|
||
if (current == null) { | ||
RuleError err = new RuleError(this.getClass(),"the input header is null"); | ||
logErrors(logger, Collections.singletonList(err)); | ||
return false; | ||
} | ||
|
||
List<GrandParentDependantBlockHeaderRule> rules = chainRules.get(current.getSealType().getSealId()); | ||
|
||
for (GrandParentDependantBlockHeaderRule rule : rules) { | ||
if (!rule.validate(grandParent, parent, current, errors)) { | ||
if (logger != null) logErrors(logger, errors); | ||
return false; | ||
if (rules == null) { | ||
return false; | ||
} else { | ||
List<RuleError> errors = new LinkedList<>(); | ||
for (GrandParentDependantBlockHeaderRule rule : rules) { | ||
if (!rule.validate(grandParent, parent, current, errors)) { | ||
if (logger != null) logErrors(logger, errors); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
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
50 changes: 40 additions & 10 deletions
50
modAionImpl/src/org/aion/zero/impl/valid/ParentBlockHeaderValidator.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 |
---|---|---|
@@ -1,29 +1,59 @@ | ||
package org.aion.zero.impl.valid; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.aion.mcf.blockchain.BlockHeader; | ||
import org.slf4j.Logger; | ||
|
||
/** validation rules depending on parent's block header */ | ||
public class ParentBlockHeaderValidator | ||
extends AbstractBlockHeaderValidator { | ||
public class ParentBlockHeaderValidator extends AbstractBlockHeaderValidator { | ||
|
||
private List<DependentBlockHeaderRule> rules; | ||
private Map<Byte, List<DependentBlockHeaderRule>> chainRules; | ||
|
||
public ParentBlockHeaderValidator(List<DependentBlockHeaderRule> rules) { | ||
this.rules = rules; | ||
public ParentBlockHeaderValidator(Map<Byte, List<DependentBlockHeaderRule>> rules) { | ||
if (rules == null) { | ||
throw new NullPointerException(); | ||
} | ||
chainRules = rules; | ||
} | ||
|
||
public boolean validate(BlockHeader header, BlockHeader parent, Logger logger) { | ||
List<RuleError> errors = new LinkedList<>(); | ||
return validate(header, parent, logger, null); | ||
} | ||
|
||
public boolean validate( | ||
BlockHeader header, BlockHeader parent, Logger logger, Object extraArg) { | ||
if (header == null) { | ||
RuleError err = new RuleError(this.getClass(),"the input header is null"); | ||
logErrors(logger, Collections.singletonList(err)); | ||
return false; | ||
} | ||
|
||
for (DependentBlockHeaderRule rule : rules) { | ||
if (!rule.validate(header, parent, errors)) { | ||
if (logger != null) logErrors(logger, errors); | ||
return false; | ||
if (parent == null) { | ||
RuleError err = new RuleError(this.getClass(),"the input parent header is null"); | ||
logErrors(logger, Collections.singletonList(err)); | ||
return false; | ||
} | ||
|
||
List<DependentBlockHeaderRule> rules = chainRules.get(header.getSealType().getSealId()); | ||
|
||
if (rules == null) { | ||
return false; | ||
} else { | ||
List<RuleError> errors = new LinkedList<>(); | ||
for (DependentBlockHeaderRule rule : rules) { | ||
if (!rule.validate(header, parent, errors, extraArg)) { | ||
if (logger != null) { | ||
logErrors(logger, errors); | ||
} | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.