whitepaper: 352/169 — wave4 MEDIUM (hadoop/hbase/nova/neutron/openstack) + fix odl-0002 dup
This commit is contained in:
parent
9934133dcf
commit
835ae73b0f
82 changed files with 5931 additions and 6 deletions
96
defects/substrate/unit/IsExposedAlgorithm.java
Normal file
96
defects/substrate/unit/IsExposedAlgorithm.java
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
package unit;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* substrate-0001: CWE-407 is_exposed_in_era full table scan
|
||||
*
|
||||
* Simulates:
|
||||
* Slow: iter_prefix(era).any(|(validator, exposure)| exposure.others.iter().any(|i| i.who == who))
|
||||
* Fast: ErasValidatorPrefs::contains_key (validator fast-path) + targeted scan
|
||||
*/
|
||||
public class IsExposedAlgorithm {
|
||||
|
||||
static class Result {
|
||||
final long ops;
|
||||
final boolean exposed;
|
||||
Result(long ops, boolean exposed) { this.ops = ops; this.exposed = exposed; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Slow: scan all validators, scan all nominators per validator.
|
||||
* Mirrors: ErasStakers::iter_prefix(era).any(|(v, exp)| v==who || exp.others.iter().any(...))
|
||||
*/
|
||||
static Result slowIsExposed(String who, String[] validators, Map<String, String[]> nominators) {
|
||||
long ops = 0;
|
||||
for (String validator : validators) {
|
||||
ops++;
|
||||
if (validator.equals(who)) return new Result(ops, true);
|
||||
String[] noms = nominators.getOrDefault(validator, new String[0]);
|
||||
for (String nom : noms) {
|
||||
ops++;
|
||||
if (nom.equals(who)) return new Result(ops, true);
|
||||
}
|
||||
}
|
||||
return new Result(ops, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast: O(1) validator set check, then targeted nominator scan.
|
||||
* Mirrors: ErasValidatorPrefs::contains_key(era, who) fast path
|
||||
* + nominator reverse index lookup.
|
||||
*/
|
||||
static Result fastIsExposed(String who, Set<String> validatorSet,
|
||||
Map<String, String[]> nominators,
|
||||
Map<String, List<String>> nominatorToValidators) {
|
||||
long ops = 1; // O(1) set lookup
|
||||
if (validatorSet.contains(who)) return new Result(ops, true);
|
||||
|
||||
// Nominator reverse-index lookup: O(K) where K = validators who nominated
|
||||
List<String> validatorsForNominator = nominatorToValidators.getOrDefault(who, Collections.emptyList());
|
||||
ops += validatorsForNominator.size();
|
||||
return new Result(ops, !validatorsForNominator.isEmpty());
|
||||
}
|
||||
|
||||
static void bench() {
|
||||
int N_VALIDATORS = 300;
|
||||
int N_NOMINATORS_PER = 256;
|
||||
// Target nominator is the last entry of the last validator (worst case slow path)
|
||||
String who = "nominator_" + (N_VALIDATORS - 1) + "_" + (N_NOMINATORS_PER - 1);
|
||||
|
||||
String[] validators = new String[N_VALIDATORS];
|
||||
Map<String, String[]> nominators = new HashMap<>();
|
||||
Set<String> validatorSet = new HashSet<>();
|
||||
Map<String, List<String>> reverseIndex = new HashMap<>();
|
||||
|
||||
for (int v = 0; v < N_VALIDATORS; v++) {
|
||||
validators[v] = "validator_" + v;
|
||||
validatorSet.add(validators[v]);
|
||||
String[] noms = new String[N_NOMINATORS_PER];
|
||||
for (int n = 0; n < N_NOMINATORS_PER; n++) {
|
||||
noms[n] = "nominator_" + v + "_" + n;
|
||||
reverseIndex.computeIfAbsent(noms[n], k -> new ArrayList<>()).add(validators[v]);
|
||||
}
|
||||
nominators.put(validators[v], noms);
|
||||
}
|
||||
|
||||
Result slow = slowIsExposed(who, validators, nominators);
|
||||
Result fast = fastIsExposed(who, validatorSet, nominators, reverseIndex);
|
||||
|
||||
System.out.println("is_exposed_in_era N_VALIDATORS=" + N_VALIDATORS + " N_NOMINATORS_PER=" + N_NOMINATORS_PER);
|
||||
System.out.println(" slow ops: " + slow.ops + " exposed=" + slow.exposed);
|
||||
System.out.println(" fast ops: " + fast.ops + " exposed=" + fast.exposed);
|
||||
|
||||
double speedup = (double) slow.ops / fast.ops;
|
||||
System.out.printf(" speedup: %.1fx%n", speedup);
|
||||
|
||||
assert slow.exposed == fast.exposed : "result mismatch";
|
||||
assert slow.ops > fast.ops * 100 : "expected >100x speedup, got " + speedup;
|
||||
|
||||
System.out.println("1/1 PASS");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
bench();
|
||||
}
|
||||
}
|
||||
71
defects/substrate/unit/IsMemberAlgorithm.java
Normal file
71
defects/substrate/unit/IsMemberAlgorithm.java
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package unit;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* substrate-0002: CWE-407 Aura/BABE/BEEFY is_member linear authority scan
|
||||
*
|
||||
* Simulates:
|
||||
* Slow: authorities.iter().any(|id| id == authority_id) — O(A)
|
||||
* Fast: authorities.binary_search(authority_id).is_ok() — O(log A)
|
||||
*/
|
||||
public class IsMemberAlgorithm {
|
||||
|
||||
static class Result {
|
||||
final long ops;
|
||||
final boolean found;
|
||||
Result(long ops, boolean found) { this.ops = ops; this.found = found; }
|
||||
}
|
||||
|
||||
/** Slow: linear scan. Mirrors .iter().any() */
|
||||
static Result slowIsMember(int[] authorities, int target) {
|
||||
long ops = 0;
|
||||
for (int id : authorities) {
|
||||
ops++;
|
||||
if (id == target) return new Result(ops, true);
|
||||
}
|
||||
return new Result(ops, false);
|
||||
}
|
||||
|
||||
/** Fast: binary search. Mirrors .binary_search() on sorted BoundedVec */
|
||||
static Result fastIsMember(int[] sortedAuthorities, int target) {
|
||||
int lo = 0, hi = sortedAuthorities.length - 1;
|
||||
long ops = 0;
|
||||
while (lo <= hi) {
|
||||
ops++;
|
||||
int mid = (lo + hi) >>> 1;
|
||||
if (sortedAuthorities[mid] == target) return new Result(ops, true);
|
||||
else if (sortedAuthorities[mid] < target) lo = mid + 1;
|
||||
else hi = mid - 1;
|
||||
}
|
||||
return new Result(ops, false);
|
||||
}
|
||||
|
||||
static void bench() {
|
||||
int N_AUTH = 1000;
|
||||
int[] authorities = new int[N_AUTH];
|
||||
for (int i = 0; i < N_AUTH; i++) authorities[i] = i * 2; // even IDs, sorted
|
||||
|
||||
// Worst case: target not present (or last element)
|
||||
int target = N_AUTH * 2 - 1; // odd, not in list → full scan
|
||||
|
||||
Result slow = slowIsMember(authorities, target);
|
||||
Result fast = fastIsMember(authorities, target);
|
||||
|
||||
System.out.println("is_member N_AUTH=" + N_AUTH);
|
||||
System.out.println(" slow ops: " + slow.ops + " found=" + slow.found);
|
||||
System.out.println(" fast ops: " + fast.ops + " found=" + fast.found);
|
||||
|
||||
double speedup = (double) slow.ops / fast.ops;
|
||||
System.out.printf(" speedup: %.1fx%n", speedup);
|
||||
|
||||
assert slow.found == fast.found : "result mismatch";
|
||||
assert slow.ops > fast.ops * 10 : "expected >10x speedup, got " + speedup;
|
||||
|
||||
System.out.println("1/1 PASS");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
bench();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue