Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

addrset.gno

// Package addrset provides a set of blockchain addresses, backed by a B+ tree.
//
// It is the B+ tree successor to [gno.land/p/nym-moultest123/addrset/v0] (which is backed
// by an AVL tree): a bump to v1 because the backing data structure — and thus
// the on-chain storage layout — changed. The exported API is the same as v0
// (Add/Remove/Has/Size/IterateByOffset/ReverseIterateByOffset) EXCEPT that the
// v0 `Tree() avl.ITree` escape hatch is intentionally removed, so the backing
// store never leaks across realms.
//
// A B+ tree packs many entries per persisted node, so a stored address costs
// roughly ~0.9 KB vs the AVL backing's ~2.0 KB (and inserts spend materially
// less gas). Prefer v1 when the set is part of persisted realm state.
//
// Two behavioral differences from v0, both consequences of the in-place-
// mutating B+ tree backing:
//
//   - do NOT mutate the set (Add/Remove) from inside an iteration callback —
//     the AVL backing's copy-on-write tolerated it, this one does not;
//   - do NOT copy a non-zero Set by value — the copies would share live tree
//     nodes while their roots and sizes diverge (v0's copies were independent
//     snapshots).
//
// Example:
//
//	var set addrset.Set // the zero value is an empty, usable set
//
//	set.Add(addr)    // true (newly added)
//	set.Has(addr)    // true
//	set.Remove(addr) // true (was present)
package addrset

import "gno.land/p/nt/bptree/v0"

// Set stores a set of addresses in sorted order. The zero value is an empty,
// usable set.
type Set struct {
	tree bptree.BPTree
}

// Add inserts an address into the set.
// Returns true if the address was newly added, false if it already existed.
func (s *Set) Add(addr address) bool {
	return !s.tree.Set(string(addr), nil)
}

// Remove deletes an address from the set.
// Returns true if the address was found and removed, false if it didn't exist.
func (s *Set) Remove(addr address) bool {
	_, removed := s.tree.Remove(string(addr))
	return removed
}

// Has checks if an address exists in the set.
func (s *Set) Has(addr address) bool {
	return s.tree.Has(string(addr))
}

// Size returns the number of addresses in the set.
func (s *Set) Size() int {
	return s.tree.Size()
}

// IterateByOffset walks through addresses in sorted order, starting at the
// given offset and visiting up to count addresses. The callback returns true
// to stop iteration. The set must not be modified during iteration.
func (s *Set) IterateByOffset(offset int, count int, cb func(addr address) bool) {
	s.tree.IterateByOffset(offset, count, func(key string, _ any) bool {
		return cb(address(key))
	})
}

// ReverseIterateByOffset walks through addresses in reverse (descending) order,
// starting at the given offset (counted from the end) and visiting up to count
// addresses. The callback returns true to stop iteration. The set must not be
// modified during iteration.
func (s *Set) ReverseIterateByOffset(offset int, count int, cb func(addr address) bool) {
	s.tree.ReverseIterateByOffset(offset, count, func(key string, _ any) bool {
		return cb(address(key))
	})
}

addrset_test.gno

package addrset

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
)

func TestSet(t *testing.T) {
	addr1 := address("addr1")
	addr2 := address("addr2")
	addr3 := address("addr3")

	tests := []struct {
		name    string
		actions func(s *Set)
		size    int
		has     map[address]bool
		addrs   []address // for iteration checks
	}{
		{
			name:    "empty set",
			actions: func(s *Set) {},
			size:    0,
			has:     map[address]bool{addr1: false},
		},
		{
			name: "single address",
			actions: func(s *Set) {
				s.Add(addr1)
			},
			size: 1,
			has: map[address]bool{
				addr1: true,
				addr2: false,
			},
			addrs: []address{addr1},
		},
		{
			name: "multiple addresses",
			actions: func(s *Set) {
				s.Add(addr1)
				s.Add(addr2)
				s.Add(addr3)
			},
			size: 3,
			has: map[address]bool{
				addr1: true,
				addr2: true,
				addr3: true,
			},
			addrs: []address{addr1, addr2, addr3},
		},
		{
			name: "remove address",
			actions: func(s *Set) {
				s.Add(addr1)
				s.Add(addr2)
				s.Remove(addr1)
			},
			size: 1,
			has: map[address]bool{
				addr1: false,
				addr2: true,
			},
			addrs: []address{addr2},
		},
		{
			name: "duplicate adds",
			actions: func(s *Set) {
				uassert.True(t, s.Add(addr1))     // first add returns true
				uassert.False(t, s.Add(addr1))    // second add returns false
				uassert.True(t, s.Remove(addr1))  // remove existing returns true
				uassert.False(t, s.Remove(addr1)) // remove non-existing returns false
			},
			size: 0,
			has: map[address]bool{
				addr1: false,
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var set Set

			// Execute test actions
			tt.actions(&set)

			// Check size
			uassert.Equal(t, tt.size, set.Size())

			// Check existence
			for addr, expected := range tt.has {
				uassert.Equal(t, expected, set.Has(addr))
			}

			// Check iteration if addresses are specified
			if tt.addrs != nil {
				collected := []address{}
				set.IterateByOffset(0, 10, func(addr address) bool {
					collected = append(collected, addr)
					return false
				})

				// Check length
				uassert.Equal(t, len(tt.addrs), len(collected))

				// Check each address
				for i, addr := range tt.addrs {
					uassert.Equal(t, addr, collected[i])
				}
			}
		})
	}
}

func TestSetIterationLimits(t *testing.T) {
	tests := []struct {
		name     string
		addrs    []address
		offset   int
		limit    int
		expected int
	}{
		{
			name:     "zero offset full list",
			addrs:    []address{"a1", "a2", "a3"},
			offset:   0,
			limit:    10,
			expected: 3,
		},
		{
			name:     "offset with limit",
			addrs:    []address{"a1", "a2", "a3", "a4"},
			offset:   1,
			limit:    2,
			expected: 2,
		},
		{
			name:     "offset beyond size",
			addrs:    []address{"a1", "a2"},
			offset:   3,
			limit:    1,
			expected: 0,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var set Set
			for _, addr := range tt.addrs {
				set.Add(addr)
			}

			// Test forward iteration
			count := 0
			set.IterateByOffset(tt.offset, tt.limit, func(addr address) bool {
				count++
				return false
			})
			uassert.Equal(t, tt.expected, count)

			// Test reverse iteration
			count = 0
			set.ReverseIterateByOffset(tt.offset, tt.limit, func(addr address) bool {
				count++
				return false
			})
			uassert.Equal(t, tt.expected, count)
		})
	}
}

gnomod.toml

module = "gno.land/p/nym-moultest123/addrset/v1"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186404
  max_deposit = "5000000ugnot"