Skip to content

Commit

Permalink
chore: improve book readability
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Apr 24, 2024
1 parent 1f52b90 commit 55b863f
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions contribs/gnodev/pkg/address/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import (
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
)

type Entry struct {
crypto.Address
Names []string
}

// Book reference a list of addresses optionally associated with a name
// It is not thread safe.
type Book struct {
addrsToNames map[crypto.Address][]string // address -> []names
namesToAddrs map[string]crypto.Address // name -> address
Expand All @@ -25,12 +22,16 @@ func NewBook() *Book {
}
}

func remove(s []string, i int) []string {
s[len(s)-1], s[i] = s[i], s[len(s)-1]
return s[:len(s)-1]
}

// Add inserts a new address into the address book linked to the specified name.
// An address can be associated with multiple names, yet each name can only
// belong to one address. Hence, if a name is reused, it will replace the
// reference to the previous address.
// Adding an address without a name is permissible.
func (bk *Book) Add(addr crypto.Address, name string) {
if addr.IsZero() {
panic("empty address not allowed")
}

// Check and register address if it wasn't existing
names, ok := bk.addrsToNames[addr]
if !ok {
Expand Down Expand Up @@ -68,6 +69,11 @@ func (bk *Book) Add(addr crypto.Address, name string) {
bk.addrsToNames[addr] = append(names, name)
}

type Entry struct {
crypto.Address
Names []string
}

func (bk Book) List() []Entry {
entries := make([]Entry, 0, len(bk.addrsToNames))
for addr, names := range bk.addrsToNames {
Expand Down Expand Up @@ -131,3 +137,8 @@ func (bk Book) ImportKeybase(path string) error {

return nil
}

func remove(s []string, i int) []string {
s[len(s)-1], s[i] = s[i], s[len(s)-1]
return s[:len(s)-1]
}

0 comments on commit 55b863f

Please sign in to comment.