Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/om/tka/test/oracle/main.go # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / om / tka / test / oracle / main.go` 177 lines ยท 13496 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Test-only oracle. Protocol decisions come from the pinned tailscale.com/tka. 3 package main 4 5 import ( 6 "bufio" 7 "bytes" 8 "crypto/`ed25519`" 9 "encoding/json" 10 "encoding/binary" 11 "encoding/hex" 12 "fmt" 13 "os" 14 "path/filepath" 15 "strings" 16 17 "github.com/fxamacker/cbor/v2" 18 "tailscale.com/tka" 19 "tailscale.com/types/tkatype" 20 "tailscale.com/types/key" 21 ) 22 23 var input, expected *os.File 24 var auth [2]*tka.Authority 25 var store [2]*tka.Mem 26 var enc cbor.EncMode 27 var dec cbor.DecMode 28 func must(err error) { if err != nil { panic(err) } } 29 func emit(cmd, slot byte, data []byte) { input.Write([]byte{cmd,slot}); must(binary.Write(input,binary.LittleEndian,uint32(len(data)))); input.Write(data) } 30 func line(b []byte) { fmt.Fprintf(expected,"%x\n",b) } 31 func status(err error) { if err != nil { line([]byte{1}) } else { line([]byte{0}) } } 32 func marshal(v any) []byte { b,e:=enc.Marshal(v); must(e); return b } 33 func model(b []byte, schema byte) { 34 emit(10+schema,0,b) 35 var a tka.AUM 36 var value any 37 var err error 38 switch schema {case 1: err=a.Unserialize(b); value=a; case 2: var k tka.Key; err=dec.Unmarshal(b,&k); value=k; case 3: var s tka.State; err=dec.Unmarshal(b,&s); value=s; case 4: var s tkatype.Signature; err=dec.Unmarshal(b,&s); value=s} 39 status(err); if err!=nil{return}; line(marshal(value)) 40 if schema==1 { h:=a.Hash(); line(h[:]); sh:=a.SigHash(); line(sh[:]); status(a.StaticValidate()) } 41 } 42 func reset(slot byte) { emit(4,slot,nil); status(nil); store[slot]=tka.ChonkMem(); auth[slot]=nil } 43 func head(slot byte) { var h tka.AUMHash; if auth[slot]!=nil {h=auth[slot].Head()}; line(h[:]) } 44 func boot(slot byte,a tka.AUM) { emit(2,slot,a.Serialize()); v,e:=tka.Bootstrap(store[slot],a); if e==nil{auth[slot]=v}; status(e); head(slot) } 45 func inform(slot byte, updates ...tka.AUM) { 46 var b bytes.Buffer; b.WriteByte(byte(len(updates))) 47 for _,a:=range updates { raw:=a.Serialize(); binary.Write(&b,binary.LittleEndian,uint32(len(raw))); b.Write(raw) } 48 emit(3,slot,b.Bytes()); err:=auth[slot].Inform(store[slot],updates); status(err); head(slot) 49 } 50 func informRaw(slot byte, raw []byte) { 51 var a tka.AUM;must(a.Unserialize(raw)) 52 var b bytes.Buffer;b.WriteByte(1);binary.Write(&b,binary.LittleEndian,uint32(len(raw)));b.Write(raw) 53 emit(3,slot,b.Bytes());status(auth[slot].Inform(store[slot],[]tka.AUM{a}));head(slot) 54 } 55 func keys(slot byte) { emit(8,slot,nil); status(nil); var b []byte; for _,k:=range auth[slot].Keys(){ if k.Kind==tka.Key25519&&len(k.Public)==32{b=append(b,k.Public...)} }; line(b) } 56 func offer(slot byte) tka.SyncOffer { o,e:=auth[slot].SyncOffer(store[slot]); must(e); emit(5,slot,nil); status(nil); line(offerBytes(o)); return o } 57 func offerBytes(o tka.SyncOffer) []byte { b:=append([]byte{},o.Head[:]...);for _,h:=range o.Ancestors{b=append(b,h[:]...)};return b } 58 func missing(slot byte,o tka.SyncOffer) []tka.AUM { emit(6,slot,offerBytes(o)); a,e:=auth[slot].MissingAUMs(store[slot],o);status(e);if e==nil{var b []byte;for _,x:=range a{h:=x.Hash();b=append(b,h[:]...)};line(b)};return a } 59 func stateAt(slot byte,h tka.AUMHash,s tka.State) { emit(7,slot,h[:]);status(nil);s.LastAUMHash=&h;line(marshal(s)) } 60 func sign(a tka.AUM, privs ...`ed25519`.PrivateKey) tka.AUM { a.Signatures=nil; h:=a.SigHash();for _,p:=range privs{a.Signatures=append(a.Signatures,tkatype.Signature{KeyID:tkatype.KeyID(p.Public().(`ed25519`.PublicKey)),Signature:`ed25519`.Sign(p,h[:])})};return a } 61 func child(a tka.AUM, p tka.AUM, privs ...`ed25519`.PrivateKey) tka.AUM {h:=p.Hash();a.PrevAUMHash=h[:];return sign(a,privs...) } 62 func corpus(root string) { 63 for _,name:=range []string{"hostile.txt","fuzz.txt","tka-structs.txt"} { 64 f,e:=os.Open(filepath.Join(root,name));must(e);scan:=bufio.NewScanner(f);scan.Buffer(make([]byte,65536),2<<20) 65 for scan.Scan(){s:=strings.Fields(scan.Text());if len(s)==0||strings.HasPrefix(s[0],"#"){continue};var raw string;schema:=byte(1) 66 if name=="tka-structs.txt" {if s[0]=="aum"{raw=s[1]}else if s[0]=="state"{raw=s[1];schema=3}else{continue}} else {if s[1]!="aum"{continue};raw=s[5]} 67 var b []byte;if raw!="-"{b,e=hex.DecodeString(raw);must(e)};model(b,schema) 68 };must(scan.Err());f.Close() 69 } 70 } 71 func scenarios() { 72 p:=`ed25519`.NewKeyFromSeed(bytes.Repeat([]byte{1},32));q:=`ed25519`.NewKeyFromSeed(bytes.Repeat([]byte{2},32));r:=`ed25519`.NewKeyFromSeed(bytes.Repeat([]byte{3},32)) 73 k:=tka.Key{Kind:tka.Key25519,Votes:1,Public:p.Public().(`ed25519`.PublicKey)}; k2:=tka.Key{Kind:tka.Key25519,Votes:2,Public:q.Public().(`ed25519`.PublicKey)}; k3:=tka.Key{Kind:tka.Key25519,Votes:3,Public:r.Public().(`ed25519`.PublicKey)} 74 state:=tka.State{DisablementValues:[][]byte{bytes.Repeat([]byte{7},32)},Keys:[]tka.Key{k,k2},StateID1:11,StateID2:22} 75 g:=sign(tka.AUM{MessageKind:tka.AUMCheckpoint,State:&state},p) 76 reset(0);reset(1);boot(0,g);boot(1,g);boot(0,g);keys(0);stateAt(0,g.Hash(),state) 77 add:=child(tka.AUM{MessageKind:tka.AUMAddKey,Key:&k3},g,p);inform(0,add);keys(0) 78 st1:=state.Clone();st1.Keys=append(st1.Keys,k3);stateAt(0,add.Hash(),st1) 79 votes:=uint(4);up:=child(tka.AUM{MessageKind:tka.AUMUpdateKey,KeyID:k.Public,Votes:&votes,Meta:map[string]string{"name":"alpha"}},add,p) 80 inform(0,up);st2:=st1.Clone();st2.Keys[0].Votes=4;st2.Keys[0].Meta=map[string]string{"name":"alpha"};stateAt(0,up.Hash(),st2) 81 cp:=child(tka.AUM{MessageKind:tka.AUMCheckpoint,State:&st2},up,p);inform(0,cp);stateAt(0,cp.Hash(),st2) 82 badstate:=st2.Clone();badstate.StateID2++;badcp:=child(tka.AUM{MessageKind:tka.AUMCheckpoint,State:&badstate},cp,p);inform(0,badcp) 83 rm:=child(tka.AUM{MessageKind:tka.AUMRemoveKey,KeyID:k2.Public},cp,p);inform(0,rm);keys(0) 84 off:=offer(1);miss:=missing(0,off);inform(1,miss...);offer(0);offer(1);keys(1) 85 unknown:=child(tka.AUM{MessageKind:255,KeyID:[]byte{42}},rm,p);inform(0,unknown) 86 invalid:=child(tka.AUM{MessageKind:tka.AUMNoOp},unknown,q);inform(0,invalid) 87 badparent:=sign(tka.AUM{MessageKind:tka.AUMNoOp,PrevAUMHash:bytes.Repeat([]byte{99},32)},p);inform(0,badparent) 88 good:=child(tka.AUM{MessageKind:tka.AUMNoOp},unknown,p);inform(0,good,badparent);inform(0,good);inform(0,good) 89 unsigned:=child(tka.AUM{MessageKind:tka.AUMNoOp},good);inform(0,unsigned) 90 badsig:=child(tka.AUM{MessageKind:tka.AUMNoOp},good,p);badsig.Signatures[0].Signature[0]^=1;inform(0,badsig) 91 adddup:=child(tka.AUM{MessageKind:tka.AUMAddKey,Key:&k3},good,p);inform(0,adddup) 92 zero:=uint(0);badup:=child(tka.AUM{MessageKind:tka.AUMUpdateKey,KeyID:k.Public,Votes:&zero},good,p);inform(0,badup) 93 missing(0,tka.SyncOffer{Head:tka.AUMHash{99},Ancestors:[]tka.AUMHash{{98}}}) 94 // Fork choice: duplicate signatures contribute votes once; remove wins a weight tie. 95 reset(0);reset(1);boot(0,g);boot(1,g) 96 dup:=child(tka.AUM{MessageKind:tka.AUMNoOp,KeyID:[]byte{1}},g,p,p,p) 97 heavy:=child(tka.AUM{MessageKind:tka.AUMNoOp,KeyID:[]byte{2}},g,q) 98 inform(0,dup,heavy);inform(1,heavy,dup);offer(0);offer(1) 99 remove:=child(tka.AUM{MessageKind:tka.AUMRemoveKey,KeyID:k.Public},g,q);inform(0,remove);keys(0);inform(1,remove);keys(1) 100 // Hash tie breaker among equal-weight siblings; different arrival orders converge. 101 a:=child(tka.AUM{MessageKind:tka.AUMNoOp,KeyID:[]byte{3}},remove,q) 102 b:=child(tka.AUM{MessageKind:tka.AUMNoOp,KeyID:[]byte{4}},remove,q) 103 inform(0,a);inform(1,b);o0:=offer(0);o1:=offer(1);m0:=missing(0,o1);m1:=missing(1,o0);inform(0,m1...);inform(1,m0...);offer(0);offer(1) 104 last:=a;if b.Hash().String()