-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ReportCodec implementation that uses the generic Codec, fallbac…
…k to using the current specific ReportCodec if the generic codec is not provided (#8)
- Loading branch information
Showing
6 changed files
with
325 additions
and
11 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,45 @@ | ||
package median | ||
|
||
import ( | ||
"cmp" | ||
"math/big" | ||
"slices" | ||
|
||
"github.com/smartcontractkit/libocr/commontypes" | ||
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median" | ||
) | ||
|
||
type aggregatedAttributedObservation struct { | ||
Timestamp uint32 | ||
Observers [32]commontypes.OracleID | ||
Observations []*big.Int | ||
JuelsPerFeeCoin *big.Int | ||
} | ||
|
||
func aggregate(observations []median.ParsedAttributedObservation) *aggregatedAttributedObservation { | ||
// defensive copy | ||
n := len(observations) | ||
observations = slices.Clone(observations) | ||
|
||
aggregated := &aggregatedAttributedObservation{Observations: make([]*big.Int, len(observations))} | ||
|
||
slices.SortFunc(observations, func(a, b median.ParsedAttributedObservation) int { | ||
return cmp.Compare(a.Timestamp, b.Timestamp) | ||
}) | ||
aggregated.Timestamp = observations[n/2].Timestamp | ||
|
||
slices.SortFunc(observations, func(a, b median.ParsedAttributedObservation) int { | ||
return a.JuelsPerFeeCoin.Cmp(b.JuelsPerFeeCoin) | ||
}) | ||
aggregated.JuelsPerFeeCoin = observations[n/2].JuelsPerFeeCoin | ||
|
||
slices.SortFunc(observations, func(a, b median.ParsedAttributedObservation) int { | ||
return a.Value.Cmp(b.Value) | ||
}) | ||
|
||
for i, o := range observations { | ||
aggregated.Observers[i] = o.Observer | ||
aggregated.Observations[i] = o.Value | ||
} | ||
return aggregated | ||
} |
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,42 @@ | ||
package median | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median" | ||
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types" | ||
) | ||
|
||
const typeName = "MedianReport" | ||
|
||
type reportCodec struct { | ||
codec types.Codec | ||
} | ||
|
||
var _ median.ReportCodec = &reportCodec{} | ||
|
||
func (r *reportCodec) BuildReport(observations []median.ParsedAttributedObservation) (ocrtypes.Report, error) { | ||
if len(observations) == 0 { | ||
return nil, fmt.Errorf("cannot build report from empty attributed observations") | ||
} | ||
|
||
return r.codec.Encode(context.Background(), aggregate(observations), typeName) | ||
} | ||
|
||
func (r *reportCodec) MedianFromReport(report ocrtypes.Report) (*big.Int, error) { | ||
agg := &aggregatedAttributedObservation{} | ||
if err := r.codec.Decode(context.Background(), report, agg, typeName); err != nil { | ||
return nil, err | ||
} | ||
observations := make([]*big.Int, len(agg.Observations)) | ||
copy(observations, agg.Observations) | ||
medianObservation := len(agg.Observations) / 2 | ||
return agg.Observations[medianObservation], nil | ||
} | ||
|
||
func (r *reportCodec) MaxReportLength(n int) (int, error) { | ||
return r.codec.GetMaxDecodingSize(context.Background(), n, typeName) | ||
} |
Oops, something went wrong.