-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package llo | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"math/big" | ||
|
||
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" | ||
"github.com/smartcontractkit/libocr/offchainreporting2/types" | ||
) | ||
|
||
var _ ReportCodec = JSONReportCodec{} | ||
|
||
// JSONReportCodec is a chain-agnostic reference implementation | ||
|
||
type JSONReportCodec struct{} | ||
|
||
func (cdc JSONReportCodec) Encode(r Report) ([]byte, error) { | ||
return json.Marshal(r) | ||
} | ||
|
||
func (cdc JSONReportCodec) Decode(b []byte) (r Report, err error) { | ||
type decode struct { | ||
ConfigDigest string | ||
ChainSelector uint64 | ||
SeqNr uint64 | ||
ChannelID commontypes.ChannelID | ||
ValidAfterSeconds uint32 | ||
ValidUntilSeconds uint32 | ||
Values []*big.Int | ||
Specimen bool | ||
} | ||
d := decode{} | ||
err = json.Unmarshal(b, &d) | ||
cdBytes, err := hex.DecodeString(d.ConfigDigest) | ||
if err != nil { | ||
return r, fmt.Errorf("invalid ConfigDigest; %w", err) | ||
} | ||
cd, err := types.BytesToConfigDigest(cdBytes) | ||
if err != nil { | ||
return r, fmt.Errorf("invalid ConfigDigest; %w", err) | ||
} | ||
|
||
return Report{ | ||
ConfigDigest: cd, | ||
ChainSelector: d.ChainSelector, | ||
SeqNr: d.SeqNr, | ||
ChannelID: d.ChannelID, | ||
ValidAfterSeconds: d.ValidAfterSeconds, | ||
ValidUntilSeconds: d.ValidUntilSeconds, | ||
Values: d.Values, | ||
Specimen: d.Specimen, | ||
}, err | ||
} |
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,39 @@ | ||
package llo | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" | ||
"github.com/smartcontractkit/libocr/offchainreporting2/types" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_JSONCodec(t *testing.T) { | ||
t.Run("Encode=>Decode", func(t *testing.T) { | ||
r := Report{ | ||
ConfigDigest: types.ConfigDigest([32]byte{1, 2, 3}), | ||
ChainSelector: 42, | ||
SeqNr: 43, | ||
ChannelID: commontypes.ChannelID([32]byte{'f', 'o', 'o'}), | ||
ValidAfterSeconds: 44, | ||
ValidUntilSeconds: 45, | ||
Values: []*big.Int{big.NewInt(1), big.NewInt(2)}, | ||
Specimen: true, | ||
} | ||
|
||
cdc := JSONReportCodec{} | ||
|
||
encoded, err := cdc.Encode(r) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, `{"ConfigDigest":"0102030000000000000000000000000000000000000000000000000000000000","ChainSelector":42,"SeqNr":43,"ChannelID":"666f6f0000000000000000000000000000000000000000000000000000000000","ValidAfterSeconds":44,"ValidUntilSeconds":45,"Values":[1,2],"Specimen":true}`, string(encoded)) | ||
|
||
decoded, err := cdc.Decode(encoded) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, r, decoded) | ||
}) | ||
} |
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
Oops, something went wrong.