-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
165 changed files
with
5,487 additions
and
4,871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/n2p5/loci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// loci is a single purpose datastore keyed by the caller's address. It has two | ||
// functions: Set and Get. loci is plural for locus, which is a central or core | ||
// place where something is found or from which it originates. In this case, | ||
// it's a simple key-value store where an address (the key) can store exactly | ||
// one value (in the form of a byte slice). Only the caller can set the value | ||
// for their address, but anyone can retrieve the value for any address. | ||
package loci | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/p/demo/avl" | ||
) | ||
|
||
// LociStore is a simple key-value store that uses | ||
// an AVL tree to store the data. | ||
type LociStore struct { | ||
internal *avl.Tree | ||
} | ||
|
||
// New creates a reference to a new LociStore. | ||
func New() *LociStore { | ||
return &LociStore{ | ||
internal: avl.NewTree(), | ||
} | ||
} | ||
|
||
// Set stores a byte slice in the AVL tree using the `std.PrevRealm().Addr()` | ||
// string as the key. | ||
func (s *LociStore) Set(value []byte) { | ||
key := string(std.PrevRealm().Addr()) | ||
s.internal.Set(key, value) | ||
} | ||
|
||
// Get retrieves a byte slice from the AVL tree using the provided address. | ||
// The return values are the byte slice value and a boolean indicating | ||
// whether the value exists. | ||
func (s *LociStore) Get(addr std.Address) []byte { | ||
value, exists := s.internal.Get(string(addr)) | ||
if !exists { | ||
return nil | ||
} | ||
return value.([]byte) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package loci | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestLociStore(t *testing.T) { | ||
t.Parallel() | ||
|
||
u1 := testutils.TestAddress("u1") | ||
u2 := testutils.TestAddress("u1") | ||
|
||
t.Run("TestSet", func(t *testing.T) { | ||
t.Parallel() | ||
store := New() | ||
u1 := testutils.TestAddress("u1") | ||
|
||
m1 := []byte("hello") | ||
m2 := []byte("world") | ||
std.TestSetOrigCaller(u1) | ||
|
||
// Ensure that the value is nil before setting it. | ||
r1 := store.Get(u1) | ||
if r1 != nil { | ||
t.Errorf("expected value to be nil, got '%s'", r1) | ||
} | ||
store.Set(m1) | ||
// Ensure that the value is correct after setting it. | ||
r2 := store.Get(u1) | ||
if string(r2) != "hello" { | ||
t.Errorf("expected value to be 'hello', got '%s'", r2) | ||
} | ||
store.Set(m2) | ||
// Ensure that the value is correct after overwriting it. | ||
r3 := store.Get(u1) | ||
if string(r3) != "world" { | ||
t.Errorf("expected value to be 'world', got '%s'", r3) | ||
} | ||
}) | ||
t.Run("TestGet", func(t *testing.T) { | ||
t.Parallel() | ||
store := New() | ||
u1 := testutils.TestAddress("u1") | ||
u2 := testutils.TestAddress("u2") | ||
u3 := testutils.TestAddress("u3") | ||
u4 := testutils.TestAddress("u4") | ||
|
||
m1 := []byte("hello") | ||
m2 := []byte("world") | ||
m3 := []byte("goodbye") | ||
|
||
std.TestSetOrigCaller(u1) | ||
store.Set(m1) | ||
std.TestSetOrigCaller(u2) | ||
store.Set(m2) | ||
std.TestSetOrigCaller(u3) | ||
store.Set(m3) | ||
|
||
// Ensure that the value is correct after setting it. | ||
r0 := store.Get(u4) | ||
if r0 != nil { | ||
t.Errorf("expected value to be nil, got '%s'", r0) | ||
} | ||
// Ensure that the value is correct after setting it. | ||
r1 := store.Get(u1) | ||
if string(r1) != "hello" { | ||
t.Errorf("expected value to be 'hello', got '%s'", r1) | ||
} | ||
// Ensure that the value is correct after setting it. | ||
r2 := store.Get(u2) | ||
if string(r2) != "world" { | ||
t.Errorf("expected value to be 'world', got '%s'", r2) | ||
} | ||
// Ensure that the value is correct after setting it. | ||
r3 := store.Get(u3) | ||
if string(r3) != "goodbye" { | ||
t.Errorf("expected value to be 'goodbye', got '%s'", r3) | ||
} | ||
}) | ||
|
||
} |
Oops, something went wrong.