verifier.gno
2.89 Kb · 113 lines
1package blueprint
2
3import (
4 "chain"
5
6 "gno.land/p/akkadia/v0/accesscontrol"
7 "gno.land/r/akkadia/v0/admin"
8)
9
10const SetChunkVerifierEvent = "SetChunkVerifier"
11
12// SetChunkVerifier stores verifier for a specific chunk key of a blueprint.
13func SetChunkVerifier(cur realm, blueprintID uint32, chunkKey string, verifier string) {
14 assertNotFrozen()
15 accesscontrol.AssertIsAdminOrOperator(0, cur, admin.IsAdmin, admin.IsOperator)
16 blueprintStore.AssertBlueprintExists(blueprintID)
17 assertChunkKey(chunkKey)
18
19 verifierStore.Set(blueprintID, chunkKey, verifier)
20
21 chain.Emit(
22 SetChunkVerifierEvent,
23 "id", formatBlueprintID(blueprintID),
24 "chunkKey", chunkKey,
25 )
26}
27
28// SetChunkVerifiers sets multiple chunk verifiers at once using direct string traversal.
29// chunkKeys and verifiers are comma-separated strings with matching item counts.
30func SetChunkVerifiers(cur realm, blueprintID uint32, chunkKeys string, verifiers string) {
31 assertNotFrozen()
32 accesscontrol.AssertIsAdminOrOperator(0, cur, admin.IsAdmin, admin.IsOperator)
33 blueprintStore.AssertBlueprintExists(blueprintID)
34
35 if chunkKeys == "" {
36 panic("chunkKeys must not be empty")
37 }
38 if verifiers == "" {
39 panic("verifiers must not be empty")
40 }
41
42 keyStart, valStart := 0, 0
43 keyIdx, valIdx := 0, 0
44 count := 0
45
46 for {
47 for keyIdx < len(chunkKeys) && chunkKeys[keyIdx] != ',' {
48 keyIdx++
49 }
50 for valIdx < len(verifiers) && verifiers[valIdx] != ',' {
51 valIdx++
52 }
53
54 key := chunkKeys[keyStart:keyIdx]
55 val := verifiers[valStart:valIdx]
56 if key == "" {
57 panic("empty chunkKey not allowed")
58 }
59 if val == "" {
60 panic("empty verifier not allowed")
61 }
62
63 count++
64 assertBatchLimit("chunkKeys", count)
65 assertChunkKey(key)
66 verifierStore.Set(blueprintID, key, val)
67
68 keyEnd := keyIdx >= len(chunkKeys)
69 valEnd := valIdx >= len(verifiers)
70 if keyEnd != valEnd {
71 panic("chunkKeys and verifiers count mismatch")
72 }
73 if keyEnd {
74 break
75 }
76
77 keyIdx++
78 valIdx++
79 keyStart = keyIdx
80 valStart = valIdx
81 }
82}
83
84// GetChunkVerifier retrieves verifier for a specific chunk key of a blueprint.
85func GetChunkVerifier(blueprintID uint32, chunkKey string) string {
86 assertMigrationStateAvailable()
87 blueprintStore.AssertBlueprintExists(blueprintID)
88 assertChunkKey(chunkKey)
89 verifier, found := verifierStore.Get(blueprintID, chunkKey)
90 if !found {
91 return ""
92 }
93 return verifier
94}
95
96// ListChunkVerifiers retrieves verifiers for multiple chunk keys of a blueprint.
97func ListChunkVerifiers(blueprintID uint32, chunkKeys ...string) []map[string]string {
98 assertMigrationStateAvailable()
99 blueprintStore.AssertBlueprintExists(blueprintID)
100 assertListLimit("chunkKeys", len(chunkKeys))
101 result := []map[string]string{}
102 for _, chunkKey := range chunkKeys {
103 assertChunkKey(chunkKey)
104 verifier, found := verifierStore.Get(blueprintID, chunkKey)
105 if found {
106 result = append(result, map[string]string{
107 "chunkKey": chunkKey,
108 "verifier": verifier,
109 })
110 }
111 }
112 return result
113}