use of org.aion.util.math.FixedPoint in project aion by aionnetwork.
the class StakingBlockTimeStampRule method validate.
@Override
public boolean validate(BlockHeader header, BlockHeader dependency, List<RuleError> errors, Object stake) {
if (!(header instanceof StakingBlockHeader)) {
BlockHeaderValidatorUtil.addError("Invalid header type", this.getClass(), errors);
return false;
}
if (dependency instanceof StakingBlockHeader) {
BlockHeaderValidatorUtil.addError("Invalid parent header type", this.getClass(), errors);
return false;
}
if (stake == null) {
BlockHeaderValidatorUtil.addError("The stake can not be null", this.getClass(), errors);
return false;
}
if (!(stake instanceof BigInteger)) {
BlockHeaderValidatorUtil.addError("Invalid stake object type", this.getClass(), errors);
return false;
}
long parentTimeStamp = dependency.getTimestamp();
BigInteger stakes = (BigInteger) stake;
if (stakes.signum() < 1) {
return false;
}
long timeStamp = header.getTimestamp();
BigInteger blockDifficulty = header.getDifficultyBI();
byte[] seed = ((StakingBlockHeader) header).getSeedOrProof();
if (seed.length == StakingBlockHeader.PROOF_LENGTH) {
seed = VRF_Ed25519.generateProofHash(seed);
}
BigInteger dividend = new BigInteger(1, HashUtil.h256(seed));
FixedPoint logDifference = logBoundary.subtract(LogApproximator.log(dividend));
BigInteger delta = logDifference.multiplyInteger(blockDifficulty).toBigInteger().divide(stakes);
long offset = max(delta.longValueExact(), 1);
if (timeStamp != (parentTimeStamp + offset)) {
BlockHeaderValidatorUtil.addError(formatError(timeStamp, parentTimeStamp, offset), this.getClass(), errors);
return false;
}
return true;
}
use of org.aion.util.math.FixedPoint in project aion by aionnetwork.
the class StakingDeltaCalculator method calculateDelta.
// returns how long a block producer would have to wait, which is
// (difficulty * log(2^256 / hash(seed))) / stake
public static long calculateDelta(byte[] seed, BigInteger difficulty, BigInteger stake) {
BigInteger dividend = new BigInteger(1, HashUtil.h256(seed));
FixedPoint logDifference = logBoundary.subtract(LogApproximator.log(dividend));
BigInteger delta = logDifference.multiplyInteger(difficulty).toBigInteger().divide(stake);
return max(delta.longValueExact(), 1);
}
Aggregations