-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
98 lines (81 loc) · 2.93 KB
/
Copy pathexample_test.go
File metadata and controls
98 lines (81 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package claimgraph_test
import (
"fmt"
"testing"
"time"
"github.com/systemshift/claim-graph/claim"
)
func TestEndToEndWithoutIPFS(t *testing.T) {
// 1. Create two witnesses
witness1, err := claim.GenerateWitness()
if err != nil {
t.Fatalf("Failed to create witness1: %v", err)
}
fmt.Printf("Witness 1: %s\n", witness1.ID[:16]+"...")
witness2, err := claim.GenerateWitness()
if err != nil {
t.Fatalf("Failed to create witness2: %v", err)
}
fmt.Printf("Witness 2: %s\n", witness2.ID[:16]+"...")
// 2. Create a claim
statement := claim.Statement{
Subject: "https://example.com/sports/match-123",
Predicate: "result",
Object: "Manchester United won 2-1",
Domain: "sports",
}
c, err := claim.NewClaim(statement, []string{"evidence-cid-1", "evidence-cid-2"}, "dag-time-event-456")
if err != nil {
t.Fatalf("Failed to create claim: %v", err)
}
fmt.Printf("Claim CID: %s\n", c.ID)
fmt.Printf("Statement: %s %s %s\n", c.Statement.Subject, c.Statement.Predicate, c.Statement.Object)
// 3. Both witnesses attest to the claim
att1, err := witness1.Attest(c)
if err != nil {
t.Fatalf("Witness1 failed to attest: %v", err)
}
if err := c.AddAttestation(att1); err != nil {
t.Fatalf("Failed to add attestation1: %v", err)
}
fmt.Printf("Witness 1 attested at %s\n", att1.Timestamp.Format(time.RFC3339))
att2, err := witness2.Attest(c)
if err != nil {
t.Fatalf("Witness2 failed to attest: %v", err)
}
if err := c.AddAttestation(att2); err != nil {
t.Fatalf("Failed to add attestation2: %v", err)
}
fmt.Printf("Witness 2 attested at %s\n", att2.Timestamp.Format(time.RFC3339))
// 4. Verify all attestations
if err := c.VerifyAllAttestations(); err != nil {
t.Fatalf("Attestation verification failed: %v", err)
}
fmt.Printf("All %d attestations verified\n", len(c.Witnesses))
// 5. Set up reputation tracking
repStore := claim.NewReputationStore()
// Record that both witnesses attested
repStore.RecordAttestation(witness1.ID, "sports")
repStore.RecordAttestation(witness2.ID, "sports")
// Simulate: both agreed with consensus (claim was correct)
repStore.RecordAgreement(witness1.ID, "sports")
repStore.RecordAgreement(witness2.ID, "sports")
// Check reputation
rep1, _ := repStore.GetRecord(witness1.ID)
rep2, _ := repStore.GetRecord(witness2.ID)
fmt.Printf("Witness 1 reputation: %.2f (claims: %d, agreed: %d)\n",
rep1.Score(), rep1.TotalClaims, rep1.AgreedClaims)
fmt.Printf("Witness 2 reputation: %.2f (claims: %d, agreed: %d)\n",
rep2.Score(), rep2.TotalClaims, rep2.AgreedClaims)
// 6. Compute claim confidence
confidence := claim.ClaimConfidence(c, repStore)
fmt.Printf("Claim confidence: %.2f\n", confidence)
// 7. Verify CID hasn't changed (integrity)
if err := claim.VerifyCID(c); err != nil {
t.Fatalf("CID verification failed: %v", err)
}
fmt.Println("CID integrity verified")
// 8. Export reputation for portability
exported := rep1.Export()
fmt.Printf("Exported reputation: %+v\n", exported)
}