Skip to content

Commit

Permalink
feat(schema/indexer): add address codec param (cosmos#21361)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc authored Aug 20, 2024
1 parent da27d8b commit 08a3da4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions schema/addressutil/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package addressutil

type AddressCodec interface {
// StringToBytes decodes text to bytes
StringToBytes(text string) ([]byte, error)
// BytesToString encodes bytes to text
BytesToString(bz []byte) (string, error)
}
24 changes: 24 additions & 0 deletions schema/addressutil/hex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package addressutil

import (
"encoding/hex"
"fmt"
)

// HexAddressCodec is a basic address codec that encodes and decodes addresses as hex strings.
// It is intended to be used as a fallback codec when no other codec is provided.
type HexAddressCodec struct{}

func (h HexAddressCodec) StringToBytes(text string) ([]byte, error) {
if len(text) < 2 || text[:2] != "0x" {
return nil, fmt.Errorf("invalid hex address: %s", text)
}

return hex.DecodeString(text[2:])
}

func (h HexAddressCodec) BytesToString(bz []byte) (string, error) {
return fmt.Sprintf("0x%x", bz), nil
}

var _ AddressCodec = HexAddressCodec{}
54 changes: 54 additions & 0 deletions schema/addressutil/hex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package addressutil

import (
"bytes"
"testing"
)

func TestHexAddressCodec(t *testing.T) {
tt := []struct {
text string
bz []byte
err bool
}{
{
text: "0x1234",
bz: []byte{0x12, 0x34},
},
{
text: "0x",
bz: []byte{},
},
{
text: "0x123",
err: true,
},
{
text: "1234",
err: true,
},
}

h := HexAddressCodec{}
for _, tc := range tt {
bz, err := h.StringToBytes(tc.text)
if tc.err && err == nil {
t.Fatalf("expected error, got none")
}
if !tc.err && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !tc.err && !bytes.Equal(bz, tc.bz) {
t.Fatalf("expected %v, got %v", tc.bz, bz)
}

// check address rendering if no error
if !tc.err {
if str, err := h.BytesToString(tc.bz); err != nil {
t.Fatalf("unexpected error: %v", err)
} else if str != tc.text {
t.Fatalf("expected %s, got %s", tc.text, str)
}
}
}
}
5 changes: 5 additions & 0 deletions schema/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indexer
import (
"context"

"cosmossdk.io/schema/addressutil"
"cosmossdk.io/schema/appdata"
"cosmossdk.io/schema/logutil"
"cosmossdk.io/schema/view"
Expand Down Expand Up @@ -62,6 +63,10 @@ type InitParams struct {
// Logger is a logger the indexer can use to write log messages. It may be nil if the indexer does not need
// to write logs.
Logger logutil.Logger

// AddressCodec is the address codec that the indexer can use to encode and decode addresses. It is
// expected to be non-nil.
AddressCodec addressutil.AddressCodec
}

// InitResult is the indexer initialization result and includes the indexer's listener implementation.
Expand Down
6 changes: 6 additions & 0 deletions schema/indexer/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indexer
import (
"context"

"cosmossdk.io/schema/addressutil"
"cosmossdk.io/schema/appdata"
"cosmossdk.io/schema/decoding"
"cosmossdk.io/schema/logutil"
Expand All @@ -29,6 +30,11 @@ type ManagerOptions struct {
// be used to pass down other parameters to indexers if necessary. If it is omitted, context.Background
// will be used.
Context context.Context

// AddressCodec is the address codec that indexers can use to encode and decode addresses. It should always be
// provided, but if it is omitted, the indexer manager will use a default codec which encodes and decodes addresses
// as hex strings.
AddressCodec addressutil.AddressCodec
}

// ManagerConfig is the configuration of the indexer manager and contains the configuration for each indexer target.
Expand Down

0 comments on commit 08a3da4

Please sign in to comment.