portbook.gno
8.36 Kb · 344 lines
1// Package portbook is the on-chain Solidity -> Gno Migration Registry & Request Board
2// powered by Smelt (https://smeltcli.xyz).
3package portbook
4
5import (
6 "chain"
7 "chain/runtime"
8 "strconv"
9 "strings"
10
11 "gno.land/p/nt/avl/v0"
12)
13
14// PortEntry records a Solidity contract transpiled and deployed to Gno.land.
15type PortEntry struct {
16 ID uint64
17 Name string
18 Category string
19 SourceURL string
20 SolHash string
21 RealmPath string
22 Mode string // "strict", "idiomatic", or "ai-assisted"
23 CleanFuncs int
24 TotalFuncs int
25 Warns int
26 Holes int
27 Summary string
28 Author address
29 CreatedAt int64
30 Upvotes uint64
31}
32
33// PortRequest records a community request to port a Solidity contract to Gno.land.
34type PortRequest struct {
35 ID uint64
36 ContractName string
37 Category string
38 SourceURL string
39 Notes string
40 Requester address
41 CreatedAt int64
42 Votes uint64
43 FulfilledPortID uint64
44}
45
46var (
47 admin address
48 nextPortID uint64
49 nextReqID uint64
50 totalVotes uint64
51 openReqCount uint64
52
53 portsByID *avl.Tree // key: idKey(id) -> *PortEntry
54 requestsByID *avl.Tree // key: idKey(id) -> *PortRequest
55 portUpvoters *avl.Tree // key: idKey(id) + ":" + string(addr) -> true
56 reqVoters *avl.Tree // key: idKey(id) + ":" + string(addr) -> true
57 contributors *avl.Tree // key: string(addr) -> true
58)
59
60func init(cur realm) {
61 admin = cur.Previous().Address()
62 portsByID = avl.NewTree()
63 requestsByID = avl.NewTree()
64 portUpvoters = avl.NewTree()
65 reqVoters = avl.NewTree()
66 contributors = avl.NewTree()
67}
68
69// RegisterPort publishes a Solidity -> Gno migration record on-chain.
70func RegisterPort(
71 cur realm,
72 name string,
73 category string,
74 sourceURL string,
75 solHash string,
76 realmPath string,
77 mode string,
78 cleanFuncs int,
79 totalFuncs int,
80 warns int,
81 holes int,
82 summary string,
83) uint64 {
84 name = strings.TrimSpace(name)
85 category = normalizeCategory(category)
86 sourceURL = strings.TrimSpace(sourceURL)
87 solHash = strings.TrimSpace(solHash)
88 realmPath = strings.TrimSpace(realmPath)
89 mode = normalizeMode(mode)
90 summary = strings.TrimSpace(summary)
91
92 if name == "" {
93 panic("portbook: name is required")
94 }
95 if !strings.HasPrefix(realmPath, "gno.land/r/") && !strings.HasPrefix(realmPath, "gno.land/p/") {
96 panic("portbook: realmPath must start with gno.land/r/ or gno.land/p/")
97 }
98 if cleanFuncs < 0 || totalFuncs < 0 || cleanFuncs > totalFuncs {
99 panic("portbook: invalid cleanFuncs/totalFuncs counts")
100 }
101 if warns < 0 || holes < 0 {
102 panic("portbook: warns and holes must be non-negative")
103 }
104
105 caller := cur.Previous().Address()
106 nextPortID++
107 entry := &PortEntry{
108 ID: nextPortID,
109 Name: name,
110 Category: category,
111 SourceURL: sourceURL,
112 SolHash: solHash,
113 RealmPath: realmPath,
114 Mode: mode,
115 CleanFuncs: cleanFuncs,
116 TotalFuncs: totalFuncs,
117 Warns: warns,
118 Holes: holes,
119 Summary: summary,
120 Author: caller,
121 CreatedAt: runtime.ChainHeight(),
122 Upvotes: 0,
123 }
124
125 portsByID.Set(idKey(entry.ID), entry)
126 contributors.Set(string(caller), true)
127
128 chain.Emit(
129 "PortRegistered",
130 "id", strconv.FormatUint(entry.ID, 10),
131 "name", entry.Name,
132 "realmPath", entry.RealmPath,
133 "mode", entry.Mode,
134 "author", string(caller),
135 )
136 return entry.ID
137}
138
139// UpvotePort casts a single upvote per address on a registered port.
140func UpvotePort(cur realm, portID uint64) {
141 raw := portsByID.Get(idKey(portID))
142 if raw == nil {
143 panic("portbook: port not found")
144 }
145 caller := cur.Previous().Address()
146 voteKey := idKey(portID) + ":" + string(caller)
147 if portUpvoters.Has(voteKey) {
148 panic("portbook: already upvoted this port")
149 }
150
151 entry := raw.(*PortEntry)
152 entry.Upvotes++
153 totalVotes++
154 portUpvoters.Set(voteKey, true)
155 contributors.Set(string(caller), true)
156
157 chain.Emit(
158 "PortUpvoted",
159 "portID", strconv.FormatUint(portID, 10),
160 "voter", string(caller),
161 "upvotes", strconv.FormatUint(entry.Upvotes, 10),
162 )
163}
164
165// RequestPort submits a Solidity contract that the community wants ported to Gno.land.
166// Automatically casts the requester's initial vote.
167func RequestPort(
168 cur realm,
169 contractName string,
170 category string,
171 sourceURL string,
172 notes string,
173) uint64 {
174 contractName = strings.TrimSpace(contractName)
175 category = normalizeCategory(category)
176 sourceURL = strings.TrimSpace(sourceURL)
177 notes = strings.TrimSpace(notes)
178
179 if contractName == "" {
180 panic("portbook: contractName is required")
181 }
182
183 caller := cur.Previous().Address()
184 nextReqID++
185 req := &PortRequest{
186 ID: nextReqID,
187 ContractName: contractName,
188 Category: category,
189 SourceURL: sourceURL,
190 Notes: notes,
191 Requester: caller,
192 CreatedAt: runtime.ChainHeight(),
193 Votes: 1,
194 FulfilledPortID: 0,
195 }
196
197 requestsByID.Set(idKey(req.ID), req)
198 reqVoters.Set(idKey(req.ID)+":"+string(caller), true)
199 contributors.Set(string(caller), true)
200 openReqCount++
201 totalVotes++
202
203 chain.Emit(
204 "PortRequested",
205 "id", strconv.FormatUint(req.ID, 10),
206 "contractName", req.ContractName,
207 "category", req.Category,
208 "requester", string(caller),
209 )
210 return req.ID
211}
212
213// VoteRequest casts a single vote per address on an open port request.
214func VoteRequest(cur realm, requestID uint64) {
215 raw := requestsByID.Get(idKey(requestID))
216 if raw == nil {
217 panic("portbook: request not found")
218 }
219 req := raw.(*PortRequest)
220 if req.FulfilledPortID != 0 {
221 panic("portbook: request is already fulfilled")
222 }
223
224 caller := cur.Previous().Address()
225 voteKey := idKey(requestID) + ":" + string(caller)
226 if reqVoters.Has(voteKey) {
227 panic("portbook: already voted on this request")
228 }
229
230 req.Votes++
231 totalVotes++
232 reqVoters.Set(voteKey, true)
233 contributors.Set(string(caller), true)
234
235 chain.Emit(
236 "RequestVoted",
237 "requestID", strconv.FormatUint(requestID, 10),
238 "voter", string(caller),
239 "votes", strconv.FormatUint(req.Votes, 10),
240 )
241}
242
243// FulfillRequest links an open community request to a registered PortEntry.
244// Callable by the port author, the requester, or the realm admin.
245func FulfillRequest(cur realm, requestID uint64, portID uint64) {
246 rawReq := requestsByID.Get(idKey(requestID))
247 if rawReq == nil {
248 panic("portbook: request not found")
249 }
250 rawPort := portsByID.Get(idKey(portID))
251 if rawPort == nil {
252 panic("portbook: port not found")
253 }
254
255 req := rawReq.(*PortRequest)
256 if req.FulfilledPortID != 0 {
257 panic("portbook: request already fulfilled")
258 }
259
260 port := rawPort.(*PortEntry)
261 caller := cur.Previous().Address()
262 if caller != port.Author && caller != req.Requester && caller != admin {
263 panic("portbook: only port author, requester, or admin can fulfill")
264 }
265
266 req.FulfilledPortID = portID
267 if openReqCount > 0 {
268 openReqCount--
269 }
270
271 chain.Emit(
272 "RequestFulfilled",
273 "requestID", strconv.FormatUint(requestID, 10),
274 "portID", strconv.FormatUint(portID, 10),
275 "by", string(caller),
276 )
277}
278
279// Stats returns aggregate registry counters for indexers, tests, and cross-realm reads.
280func Stats() (ports uint64, requests uint64, openRequests uint64, votes uint64, uniqueContributors int) {
281 return nextPortID, nextReqID, openReqCount, totalVotes, contributors.Size()
282}
283
284// GetPort returns a registered port by ID, or nil if it does not exist.
285func GetPort(id uint64) *PortEntry {
286 raw := portsByID.Get(idKey(id))
287 if raw == nil {
288 return nil
289 }
290 return raw.(*PortEntry)
291}
292
293// GetRequest returns a community port request by ID, or nil if it does not exist.
294func GetRequest(id uint64) *PortRequest {
295 raw := requestsByID.Get(idKey(id))
296 if raw == nil {
297 return nil
298 }
299 return raw.(*PortRequest)
300}
301
302// DeterministicPct returns the integer percentage (0..100) of clean deterministic functions.
303func (p *PortEntry) DeterministicPct() int {
304 if p.TotalFuncs == 0 {
305 if p.Holes == 0 {
306 return 100
307 }
308 return 0
309 }
310 pct := (p.CleanFuncs * 100) / p.TotalFuncs
311 if p.Holes > 0 && pct == 100 {
312 return 99
313 }
314 return pct
315}
316
317func idKey(id uint64) string {
318 s := strconv.FormatUint(id, 10)
319 if len(s) >= 20 {
320 return s
321 }
322 return strings.Repeat("0", 20-len(s)) + s
323}
324
325func normalizeMode(mode string) string {
326 switch strings.ToLower(strings.TrimSpace(mode)) {
327 case "", "strict", "deterministic":
328 return "strict"
329 case "idiomatic":
330 return "idiomatic"
331 case "ai-assisted", "hybrid", "assist":
332 return "ai-assisted"
333 default:
334 panic("portbook: mode must be 'strict', 'idiomatic', or 'ai-assisted'")
335 }
336}
337
338func normalizeCategory(cat string) string {
339 cat = strings.TrimSpace(cat)
340 if cat == "" {
341 return "Utility"
342 }
343 return cat
344}