/
config.go
140 lines (105 loc) · 4.79 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package test
import (
"bytes"
"context"
"errors"
"fmt"
libocr "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/stretchr/testify/assert"
"github.com/O1MaGnUmO1/chainlink-common/pkg/types"
)
type staticConfigProvider struct{}
// TODO validate start/Close calls?
func (s staticConfigProvider) Start(ctx context.Context) error { return nil }
func (s staticConfigProvider) Close() error { return nil }
func (s staticConfigProvider) Ready() error { panic("unimplemented") }
func (s staticConfigProvider) Name() string { panic("unimplemented") }
func (s staticConfigProvider) HealthReport() map[string]error { panic("unimplemented") }
func (s staticConfigProvider) OffchainConfigDigester() libocr.OffchainConfigDigester {
return staticOffchainConfigDigester{}
}
func (s staticConfigProvider) ContractConfigTracker() libocr.ContractConfigTracker {
return staticContractConfigTracker{}
}
type staticOffchainConfigDigester struct{}
func (s staticOffchainConfigDigester) ConfigDigest(config libocr.ContractConfig) (libocr.ConfigDigest, error) {
if !assert.ObjectsAreEqual(contractConfig, config) {
return libocr.ConfigDigest{}, fmt.Errorf("expected contract config %v but got %v", configDigest, config)
}
return configDigest, nil
}
func (s staticOffchainConfigDigester) ConfigDigestPrefix() (libocr.ConfigDigestPrefix, error) {
return configDigestPrefix, nil
}
type staticContractConfigTracker struct{}
func (s staticContractConfigTracker) Notify() <-chan struct{} { return nil }
func (s staticContractConfigTracker) LatestConfigDetails(ctx context.Context) (uint64, libocr.ConfigDigest, error) {
return changedInBlock, configDigest, nil
}
func (s staticContractConfigTracker) LatestConfig(ctx context.Context, cib uint64) (libocr.ContractConfig, error) {
if changedInBlock != cib {
return libocr.ContractConfig{}, fmt.Errorf("expected changed in block %d but got %d", changedInBlock, cib)
}
return contractConfig, nil
}
func (s staticContractConfigTracker) LatestBlockHeight(ctx context.Context) (uint64, error) {
return blockHeight, nil
}
type staticContractTransmitter struct{}
func (s staticContractTransmitter) Transmit(ctx context.Context, rc libocr.ReportContext, r libocr.Report, ss []libocr.AttributedOnchainSignature) error {
if !assert.ObjectsAreEqual(reportContext, rc) {
return fmt.Errorf("expected report context %v but got %v", reportContext, report)
}
if !bytes.Equal(report, r) {
return fmt.Errorf("expected report %x but got %x", report, r)
}
if !assert.ObjectsAreEqual(sigs, ss) {
return fmt.Errorf("expected signatures %v but got %v", sigs, ss)
}
return nil
}
func (s staticContractTransmitter) LatestConfigDigestAndEpoch(ctx context.Context) (libocr.ConfigDigest, uint32, error) {
return configDigest, epoch, nil
}
func (s staticContractTransmitter) FromAccount() (libocr.Account, error) {
return account, nil
}
type staticCodec struct{}
func (c staticCodec) GetMaxEncodingSize(ctx context.Context, n int, itemType string) (int, error) {
return 0, errors.New("not used for these test")
}
func (c staticCodec) GetMaxDecodingSize(ctx context.Context, n int, itemType string) (int, error) {
return 0, errors.New("not used for these test")
}
func (c staticCodec) Encode(ctx context.Context, item any, itemType string) ([]byte, error) {
return nil, errors.New("not used for these test")
}
func (c staticCodec) Decode(ctx context.Context, raw []byte, into any, itemType string) error {
return errors.New("not used for these test")
}
type staticChainReader struct{}
func (c staticChainReader) Bind(context.Context, []types.BoundContract) error {
return nil
}
func (c staticChainReader) GetLatestValue(ctx context.Context, cn, method string, params, returnVal any) error {
if !assert.ObjectsAreEqual(cn, contractName) {
return fmt.Errorf("%w: expected report context %v but got %v", types.ErrInvalidType, contractName, cn)
}
if method != medianContractGenericMethod {
return fmt.Errorf("%w: expected generic contract method %v but got %v", types.ErrInvalidType, medianContractGenericMethod, method)
}
gotParams, ok := params.(*map[string]any)
if !ok {
return fmt.Errorf("%w: Invalid parameter type received in GetLatestValue. Expected %T but received %T", types.ErrInvalidEncoding, gotParams, params)
}
if (*gotParams)["param1"] != getLatestValueParams["param1"] || (*gotParams)["param2"] != getLatestValueParams["param2"] {
return fmt.Errorf("%w: Wrong params value received in GetLatestValue. Expected %v but received %v", types.ErrInvalidEncoding, getLatestValueParams, *gotParams)
}
ret, ok := returnVal.(*map[string]any)
if !ok {
return fmt.Errorf("%w: Wrong type passed for retVal param to GetLatestValue impl (expected %T instead of %T", types.ErrInvalidType, ret, returnVal)
}
(*ret)["ret1"] = latestValue["ret1"]
(*ret)["ret2"] = latestValue["ret2"]
return nil
}