feat(schema/indexer): add address codec param (#21361)
This commit is contained in:
parent
da27d8b9a1
commit
08a3da44b1
8
schema/addressutil/codec.go
Normal file
8
schema/addressutil/codec.go
Normal file
@ -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
schema/addressutil/hex.go
Normal file
24
schema/addressutil/hex.go
Normal file
@ -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
schema/addressutil/hex_test.go
Normal file
54
schema/addressutil/hex_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@ package indexer
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cosmossdk.io/schema/addressutil"
|
||||
"cosmossdk.io/schema/appdata"
|
||||
"cosmossdk.io/schema/logutil"
|
||||
"cosmossdk.io/schema/view"
|
||||
@ -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.
|
||||
|
||||
@ -3,6 +3,7 @@ package indexer
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cosmossdk.io/schema/addressutil"
|
||||
"cosmossdk.io/schema/appdata"
|
||||
"cosmossdk.io/schema/decoding"
|
||||
"cosmossdk.io/schema/logutil"
|
||||
@ -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.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user