Transaction
94323CEC6D175D…AE83132DDC26
Block 25,947 · index 1 · indexed
Summary
- Hash
- 94323CEC6D175D2F107AA18C3BE8C316D8F2922DF5FD32DCAFFCAE83132DDC26
- Block
- 25,947
- Size
- 7558 bytes
- Gas used
- 12,468,515 / 14,962,278
- Fee
- 1000000ugnot
- Memo
- gnopublish
- Status
- success
Messages
Arguments · 9
- #1ratelimit
- #2README.md
- #3# `gno.land/p/moul/x/daily/ratelimit/v0` **Deterministic token-bucket rate limiter** — a port of `golang.org/x/time/rate` with the wall clock replaced by a caller-supplied monotonic tick (on-chain, the block height). A `Limiter` owns a set of per-key token buckets sharing one `rate`/`burst` config. Each bucket refills at `rate` tokens per tick up to `burst`; the caller picks what a key is (typically an address) and passes the current tick on every call. Pure integer/float math over persistent avl state — no chain imports, no ambient state, fully replayable. ```go import "gno.land/p/moul/x/daily/ratelimit/v0" l := ratelimit.New(0.5, 5.0) // 0.5 tokens/tick, capacity 5 l.Allow("g1caller", height) // consume one token; false if empty l.AllowN("g1caller", height, 3) // consume 3 atomically; false if <3 l.Tokens("g1caller", height) // read-only balance (whole tokens) ``` **Live demo:** [`r/moul/x/daily/ratelimitdemo`](https://github.com/moul/gno-contracts/tree/main/r/moul/x/daily/ratelimitdemo/v0) · render it at [`/r/moul/x/daily/ratelimitdemo/v0`](https://gno.land/r/moul/x/daily/ratelimitdemo/v0). <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/moul/gno-contracts)** — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage. **Dependency graph:**  > 🧪 **Highly experimental — potentially vibe-coded.** Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: [DISCLAIMER](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
- #4gnomod.toml
- #5module = "gno.land/p/moul/x/daily/ratelimit/v0" gno = "0.9"
- #6ratelimit.gno
- #7// Package ratelimit is a deterministic token-bucket rate limiter — a port of // golang.org/x/time/rate with the wall clock replaced by a caller-supplied // monotonic tick (on-chain, that tick is the block height). // // It is a pure library: it imports no chain APIs and reads no ambient state. // A [Limiter] owns a set of per-key token buckets sharing one rate/burst // config; the caller decides what a key is (typically an address string) and // supplies the current tick on every call. Between two observations at ticks // `last` and `now`, a bucket gains (now-last)*rate tokens, capped at `burst`. // Everything is integer/float arithmetic over persistent avl state, hence // deterministic and replayable. // // A realm wires it up by holding a *Limiter in a package-level var and feeding // it runtime.ChainHeight() as the tick. For a complete, live example see the // demo realm [r/moul/x/daily/ratelimitdemo](/r/moul/x/daily/ratelimitdemo/v0). package ratelimit import "gno.land/p/nt/avl/v0" // bucket is the per-key state persisted in the avl tree. type bucket struct { tokens float64 // tokens available as of `last` last int64 // tick at which `tokens` was computed } // Limiter is a set of per-key token buckets sharing one rate/burst config. // The zero value is not usable; construct one with [New]. type Limiter struct { rate float64 // tokens replenished per tick burst float64 // bucket capacity (max tokens, largest single burst) buckets *avl.Tree // key string -> *bucket, ordered by key (deterministic) } // New returns a Limiter replenishing `rate` tokens per tick, each bucket capped // at `burst`. rate is clamped to >= 0, burst to >= 1. func New(rate, burst float64) *Limiter { l := &Limiter{buckets: avl.NewTree()} l.SetConfig(rate, burst) return l } // SetConfig updates the shared rate and burst. rate is clamped to >= 0, burst // to >= 1. Existing buckets keep their stored tokens; the new config applies // from the next refill. func (l *Limiter) SetConfig(rate, burst float64) { if rate < 0 { rate = 0 } if burst < 1 { burst = 1 } l.rate = rate l.burst = burst } // Config returns the current rate (tokens/tick) and burst (capacity). func (l *Limiter) Config() (rate, burst float64) { return l.rate, l.burst } // Allow consumes one token for `key` at tick `now` and reports whether the // request is permitted. When the bucket is empty it returns false and consumes // nothing. Equivalent to AllowN(key, now, 1). func (l *Limiter) Allow(key string, now int64) bool { return l.AllowN(key, now, 1) } // AllowN consumes `n` tokens for `key` at tick `now` and reports whether the // request is permitted. When fewer than `n` tokens are available it returns // false and consumes nothing. A key is seen for the first time with a full // bucket of `burst` tokens. func (l *Limiter) AllowN(key string, now int64, n float64) bool { b := l.load(key, now) ok := b.tokens >= n if ok { b.tokens -= n } l.buckets.Set(key, b) return ok } // Tokens is a read-only view of how many whole tokens `key` has available at // tick `now`, without mutating any state. func (l *Limiter) Tokens(key string, now int64) int { if !l.buckets.Has(key) { return tokensToInt(l.burst) } b := l.buckets.Get(key).(*bucket) return tokensToInt(refill(b.tokens, b.last, now, l.rate, l.burst)) } // Len returns the number of keys the limiter has seen. func (l *Limiter) Len() int { return l.buckets.Size() } // Iterate calls fn for every known key in ascending order, passing the whole // tokens available at tick `now` and the last tick the key was observed. // Returning true from fn stops the iteration early; Iterate reports whether it // was stopped that way. func (l *Limiter) Iterate(now int64, fn func(key string, tokens int, last int64) bool) bool { return l.buckets.Iterate("", "", func(key string, value any) bool { b := value.(*bucket) return fn(key, tokensToInt(refill(b.tokens, b.last, now, l.rate, l.burst)), b.last) }) } // load returns the live bucket for key, refilled to `now`, creating a full // bucket the first time a key is seen. The returned bucket is not yet stored; // callers that mutate it must Set it back. func (l *Limiter) load(key string, now int64) *bucket { if l.buckets.Has(key) { b := l.buckets.Get(key).(*bucket) b.tokens = refill(b.tokens, b.last, now, l.rate, l.burst) b.last = now return b } return &bucket{tokens: l.burst, last: now} } // --- pure helpers (unit-tested) ------------------------------------------- // fmin returns the smaller of two float64 values. func fmin(a, b float64) float64 { if a < b { return a } return b } // refill returns the token count at tick `now` given `tokens` observed at tick // `last`, replenishing at `r` tokens/tick up to capacity `cap`. It never // decreases below the stored value and never exceeds `cap`. func refill(tokens float64, last, now int64, r, cap float64) float64 { if now <= last { return fmin(tokens, cap) } elapsed := float64(now - last) return fmin(cap, tokens+elapsed*r) } // tokensToInt floors a token count to a whole, non-negative token. func tokensToInt(t float64) int { if t < 0 { return 0 } return int(t) }
- #8/gno.MemPackageType
- #9 MPUserProd
Result log
msg:0,success:true,log:,events:[]