SHA256
37 lines
2.0 KiB
Java
37 lines
2.0 KiB
Java
import blockchain.*;
|
|
import blockchain.body.ForkBody;
|
|
import java.nio.file.*;
|
|
import java.util.*;
|
|
/** Parse actual browser-produced FORK pairs with Java. No DB or Solana. */
|
|
public class M5_FORK_CHECK {
|
|
static void check(boolean ok) { if (!ok) throw new AssertionError("FORK check failed"); }
|
|
public static void main(String[] args) throws Exception {
|
|
var rows=Files.readAllLines(Path.of(args[0]));
|
|
var history=Files.readAllLines(Path.of(args[1]));
|
|
var hex=HexFormat.of();
|
|
int count=0;
|
|
for (String row:rows) {
|
|
var f=row.split("\t"); int cutoff=Integer.parseInt(f[0]);
|
|
byte[] raw=Base64.getDecoder().decode(f[1]), owner=Base64.getDecoder().decode(f[2]);
|
|
var b=BchBlockEntry.fromCoreContent(raw);
|
|
check(b.body instanceof ForkBody); var fork=(ForkBody)b.body; fork.check();
|
|
check(BchCryptoVerifier.verifyBlock(b,owner));
|
|
check(!BchCryptoVerifier.verifyBlock(b,fork.parentBlockchainKey32));
|
|
check(Arrays.equals(raw,b.toBytes()));
|
|
check(b.blockNumber==cutoff+1 && fork.forkPointBlockNumber==cutoff);
|
|
check(Arrays.equals(b.prevHash32,hex.parseHex(history.get(cutoff+1).split("\t")[3])));
|
|
check(Arrays.equals(fork.forkPointBlockHash32,b.prevHash32));
|
|
check(fork.parentTipBlockNumber==7 && fork.discardedBlocksCount==7-cutoff);
|
|
check(fork.prevLineNumber==(cutoff==0?0:1));
|
|
check(Arrays.equals(fork.prevLineHash32,hex.parseHex(history.get(fork.prevLineNumber+1).split("\t")[3])));
|
|
check(fork.reasonCode==3 && fork.comment.equals("test\nFORK"));
|
|
check(fork.forkPointTimestampMs==(1700000000L+cutoff)*1000 && fork.parentTipTimestampMs==1700000007000L);
|
|
check(b.getCoreDataItem().hasTag("App","test5595"));
|
|
byte[] bad=raw.clone();bad[bad.length-1]^=1;
|
|
try {BchBlockEntry.fromCoreContent(bad);throw new AssertionError("tampering accepted");} catch(IllegalArgumentException expected){}
|
|
count++;
|
|
}
|
|
check(count==3);System.out.println("PASS: "+count+" browser FORK pairs parsed and validated by Java; tampering rejected");
|
|
}
|
|
}
|