Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-sectorbuilder
Commits
f2c160ac
Commit
f2c160ac
authored
Sep 23, 2019
by
laser
Browse files
feat(json): add JSON marshalers, which enable JSON-RPC support for SortedSectorInfo
parent
641e9ea5
Changes
2
Show whitespace changes
Inline
Side-by-side
bindings.go
View file @
f2c160ac
...
@@ -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
...
...
bindings_test.go
View file @
f2c160ac
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment