-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(state): JSON marshaling for sdk Address wrapper (#2348)
While debugging #2322 , I noticed that it is not only an RPC issue, but also a broken API one. All RPC methods that used the type `state.Address` were unusable, both via the client and via raw JSON calls. This is because the server was unable to marshal the address string value back into the interface type. To circumvent this, I have embedded the sdk.Address type in the same way that we embed the fraud proof type, to allow us to unmarshal it back into a concrete type. I have also added unit tests to fix this. In addition, it fixes the RPC parsing - so it closes #2322 .
- Loading branch information
1 parent
c157db6
commit 55db897
Showing
8 changed files
with
104 additions
and
22 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
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,57 @@ | ||
package state | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAddressMarshalling(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
addressString string | ||
addressFromStr func(string) (interface{}, error) | ||
marshalJSON func(interface{}) ([]byte, error) | ||
unmarshalJSON func([]byte) (interface{}, error) | ||
}{ | ||
{ | ||
name: "Account Address", | ||
addressString: "celestia1377k5an3f94v6wyaceu0cf4nq6gk2jtpc46g7h", | ||
addressFromStr: func(s string) (interface{}, error) { return sdk.AccAddressFromBech32(s) }, | ||
marshalJSON: func(addr interface{}) ([]byte, error) { return addr.(sdk.AccAddress).MarshalJSON() }, | ||
unmarshalJSON: func(b []byte) (interface{}, error) { | ||
var addr sdk.AccAddress | ||
err := addr.UnmarshalJSON(b) | ||
return addr, err | ||
}, | ||
}, | ||
{ | ||
name: "Validator Address", | ||
addressString: "celestiavaloper1q3v5cugc8cdpud87u4zwy0a74uxkk6u4q4gx4p", | ||
addressFromStr: func(s string) (interface{}, error) { return sdk.ValAddressFromBech32(s) }, | ||
marshalJSON: func(addr interface{}) ([]byte, error) { return addr.(sdk.ValAddress).MarshalJSON() }, | ||
unmarshalJSON: func(b []byte) (interface{}, error) { | ||
var addr sdk.ValAddress | ||
err := addr.UnmarshalJSON(b) | ||
return addr, err | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
addr, err := tc.addressFromStr(tc.addressString) | ||
require.NoError(t, err) | ||
|
||
addrBytes, err := tc.marshalJSON(addr) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte("\""+tc.addressString+"\""), addrBytes) | ||
|
||
addrUnmarshalled, err := tc.unmarshalJSON(addrBytes) | ||
assert.NoError(t, err) | ||
assert.Equal(t, addr, addrUnmarshalled) | ||
}) | ||
} | ||
} |
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