Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow OutputOwners to be json marshalled without InitCtx #2495

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions vms/secp256k1fx/output_owners.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var (
ErrOutputUnspendable = errors.New("output is unspendable")
ErrOutputUnoptimized = errors.New("output representation should be optimized")
ErrAddrsNotSortedUnique = errors.New("addresses not sorted and unique")
ErrMarshal = errors.New("cannot marshal without ctx")
)

type OutputOwners struct {
Expand All @@ -37,8 +36,8 @@ type OutputOwners struct {
ctx *snow.Context
}

// InitCtx assigns the OutputOwners.ctx object to given [ctx] object
// Must be called at least once for MarshalJSON to work successfully
// InitCtx allows addresses to be formatted into their human readable format
// during json marshalling.
func (out *OutputOwners) InitCtx(ctx *snow.Context) {
out.ctx = ctx
}
Expand All @@ -59,14 +58,7 @@ func (out *OutputOwners) MarshalJSON() ([]byte, error) {
// Fields returns JSON keys in a map that can be used with marshal JSON
// to serialize OutputOwners struct
func (out *OutputOwners) Fields() (map[string]interface{}, error) {
addrsLen := len(out.Addrs)

// we need out.ctx to do this, if its absent, throw error
if addrsLen > 0 && out.ctx == nil {
return nil, ErrMarshal
}

addresses := make([]string, addrsLen)
addresses := make([]string, len(out.Addrs))
for i, addr := range out.Addrs {
// for each [addr] in [Addrs] we attempt to format it given
// the [out.ctx] object
Expand Down Expand Up @@ -138,8 +130,13 @@ func (out *OutputOwners) Sort() {
}

// formatAddress formats a given [addr] into human readable format using
// [ChainID] and [NetworkID] from the provided [ctx].
// [ChainID] and [NetworkID] if a non-nil [ctx] is provided. If [ctx] is not
// provided, the address will be returned in cb58 format.
func formatAddress(ctx *snow.Context, addr ids.ShortID) (string, error) {
if ctx == nil {
return addr.String(), nil
}

chainIDAlias, err := ctx.BCLookup.PrimaryAlias(ctx.ChainID)
if err != nil {
return "", err
Expand Down
17 changes: 3 additions & 14 deletions vms/secp256k1fx/output_owners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,31 +149,20 @@ func TestOutputOwnerEquals(t *testing.T) {
}
}

func TestMarshalJSONRequiresCtxWhenAddrsArePresent(t *testing.T) {
func TestMarshalJSONDoesNotRequireCtx(t *testing.T) {
require := require.New(t)
out := &OutputOwners{
Threshold: 1,
Locktime: 2,
Addrs: []ids.ShortID{
{1},
{0},
},
}

_, err := out.MarshalJSON()
require.ErrorIs(err, ErrMarshal)
}

func TestMarshalJSONDoesNotRequireCtxWhenAddrsAreAbsent(t *testing.T) {
require := require.New(t)
out := &OutputOwners{
Threshold: 1,
Locktime: 2,
Addrs: []ids.ShortID{},
}

b, err := out.MarshalJSON()
require.NoError(err)

jsonData := string(b)
require.Equal(jsonData, "{\"addresses\":[],\"locktime\":2,\"threshold\":1}")
require.Equal(jsonData, `{"addresses":["6HgC8KRBEhXYbF4riJyJFLSHt37UNuRt","111111111111111111116DBWJs"],"locktime":2,"threshold":1}`)
}
Loading