Commit f2c160ac authored by laser's avatar laser
Browse files

feat(json): add JSON marshalers, which enable JSON-RPC support for SortedSectorInfo

parent 641e9ea5
...@@ -2,6 +2,7 @@ package go_sectorbuilder ...@@ -2,6 +2,7 @@ package go_sectorbuilder
import ( import (
"bytes" "bytes"
"encoding/json"
"os" "os"
"runtime" "runtime"
"sort" "sort"
...@@ -53,6 +54,20 @@ func (s *SortedSectorInfo) Values() []SectorInfo { ...@@ -53,6 +54,20 @@ func (s *SortedSectorInfo) Values() []SectorInfo {
return s.f return s.f
} }
// MarshalJSON JSON-encodes and serializes the SortedSectorInfo.
func (s SortedSectorInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(s.f)
}
// UnmarshalJSON parses the JSON-encoded byte slice and stores the result in the
// value pointed to by s.f. Note that this method allows for construction of a
// SortedSectorInfo which violates its invariant (that its SectorInfo are sorted
// in some defined way). Callers should take care to never provide a byte slice
// which would violate this invariant.
func (s *SortedSectorInfo) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &s.f)
}
type SectorInfo struct { type SectorInfo struct {
SectorID uint64 SectorID uint64
CommR [CommitmentBytesLen]byte CommR [CommitmentBytesLen]byte
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"errors" "errors"
"io" "io"
"io/ioutil" "io/ioutil"
"math/big"
"os" "os"
"testing" "testing"
"time" "time"
...@@ -118,6 +119,32 @@ func TestSectorBuilderLifecycle(t *testing.T) { ...@@ -118,6 +119,32 @@ func TestSectorBuilderLifecycle(t *testing.T) {
require.Equal(t, pieceBytes, unsealedPieceBytes) require.Equal(t, pieceBytes, unsealedPieceBytes)
} }
func TestJsonMarshalSymmetry(t *testing.T) {
for i := 0; i < 100; i++ {
xs := make([]sb.SectorInfo, 10)
for j := 0; j < 10; j++ {
var x sb.SectorInfo
_, err := io.ReadFull(rand.Reader, x.CommR[:])
require.NoError(t, err)
n, err := rand.Int(rand.Reader, big.NewInt(500))
require.NoError(t, err)
x.SectorID = n.Uint64()
xs[j] = x
}
toSerialize := sb.NewSortedSectorInfo(xs...)
serialized, err := toSerialize.MarshalJSON()
require.NoError(t, err)
var fromSerialized sb.SortedSectorInfo
err = fromSerialized.UnmarshalJSON(serialized)
require.NoError(t, err)
require.Equal(t, toSerialize, fromSerialized)
}
}
func pollForSectorSealingStatus(ptr unsafe.Pointer, sectorID uint64, targetState sealing_state.State, timeout time.Duration) (status sb.SectorSealingStatus, retErr error) { func pollForSectorSealingStatus(ptr unsafe.Pointer, sectorID uint64, targetState sealing_state.State, timeout time.Duration) (status sb.SectorSealingStatus, retErr error) {
timeoutCh := time.After(timeout) timeoutCh := time.After(timeout)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment