Fix Marshaling of TipSetKey

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2019-11-14 21:33:56 +01:00
parent c20aaf3856
commit 9f3b5bfb6e
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
2 changed files with 11 additions and 10 deletions

View File

@ -77,7 +77,7 @@ func (k TipSetKey) Bytes() []byte {
return []byte(k.value)
}
func (k *TipSetKey) MarshalJSON() ([]byte, error) {
func (k TipSetKey) MarshalJSON() ([]byte, error) {
return json.Marshal(k.Cids())
}

View File

@ -1,6 +1,7 @@
package types
import (
"encoding/json"
"fmt"
"testing"
@ -11,7 +12,7 @@ import (
)
func TestTipSetKey(t *testing.T) {
cb := cid.V1Builder{Codec: cid.DagCBOR, MhType: multihash.BLAKE2B_MIN+31}
cb := cid.V1Builder{Codec: cid.DagCBOR, MhType: multihash.BLAKE2B_MIN + 31}
c1, _ := cb.Sum([]byte("a"))
c2, _ := cb.Sum([]byte("b"))
c3, _ := cb.Sum([]byte("c"))
@ -42,7 +43,7 @@ func TestTipSetKey(t *testing.T) {
})
t.Run("encoding", func(t *testing.T) {
keys := []TipSetKey {
keys := []TipSetKey{
NewTipSetKey(),
NewTipSetKey(c1),
NewTipSetKey(c1, c2, c3),
@ -62,21 +63,21 @@ func TestTipSetKey(t *testing.T) {
k0 := NewTipSetKey()
verifyJson(t, "[]", k0)
k3 := NewTipSetKey(c1, c2, c3)
verifyJson(t, `[` +
`{"/":"bafy2bzacecesrkxghscnq7vatble2hqdvwat6ed23vdu4vvo3uuggsoaya7ki"},` +
`{"/":"bafy2bzacebxfyh2fzoxrt6kcgc5dkaodpcstgwxxdizrww225vrhsizsfcg4g"},` +
`{"/":"bafy2bzacedwviarjtjraqakob5pslltmuo5n3xev3nt5zylezofkbbv5jclyu"}` +
verifyJson(t, `[`+
`{"/":"bafy2bzacecesrkxghscnq7vatble2hqdvwat6ed23vdu4vvo3uuggsoaya7ki"},`+
`{"/":"bafy2bzacebxfyh2fzoxrt6kcgc5dkaodpcstgwxxdizrww225vrhsizsfcg4g"},`+
`{"/":"bafy2bzacedwviarjtjraqakob5pslltmuo5n3xev3nt5zylezofkbbv5jclyu"}`+
`]`, k3)
})
}
func verifyJson(t *testing.T, expected string, k TipSetKey) {
bytes, err := k.MarshalJSON()
bytes, err := json.Marshal(k)
require.NoError(t, err)
assert.Equal(t, expected, string(bytes))
var rehydrated TipSetKey
err = rehydrated.UnmarshalJSON(bytes)
err = json.Unmarshal(bytes, &rehydrated)
require.NoError(t, err)
assert.Equal(t, k, rehydrated)
}
}