|
9 | 9 | "time" |
10 | 10 |
|
11 | 11 | "github.com/go-resty/resty/v2" |
| 12 | + cfgnet "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/config/network" |
| 13 | + "github.com/smartcontractkit/chainlink-deployments-framework/pkg/logger" |
12 | 14 | "github.com/stretchr/testify/assert" |
13 | 15 | "github.com/stretchr/testify/require" |
14 | 16 | ) |
@@ -126,3 +128,102 @@ func Test_AnvilClient_SendTransaction(t *testing.T) { |
126 | 128 | }) |
127 | 129 | } |
128 | 130 | } |
| 131 | + |
| 132 | +func Test_isPublicRPC(t *testing.T) { |
| 133 | + t.Parallel() |
| 134 | + tests := []struct { |
| 135 | + url string |
| 136 | + want bool |
| 137 | + }{ |
| 138 | + {"http://rpcs.cldev.sh/", false}, |
| 139 | + {"https://rpcs.cldev.sh/", false}, |
| 140 | + {"https://rpcs.cldev.sh/anything", false}, |
| 141 | + {"https://gap-rpcs.stage.cldev.sh/anything", false}, |
| 142 | + {"https://gap-rpcs.prod.cldev.sh/anything", false}, |
| 143 | + {"https://gap-other.prod.cldev.sh/anything", false}, |
| 144 | + {"https://gap-other.stage.cldev.sh/anything", false}, |
| 145 | + {"https://gap-other.stage.cldev.sh/anything", false}, |
| 146 | + {"https://gap-grpc-job-distributor.public.main.prod.cldev.sh/", false}, |
| 147 | + {"https://gap-ws-job-distributor.public.main.prod.cldev.sh/", false}, |
| 148 | + {"https://gap-rpc-proxy.public.main.prod.cldev.sh/", false}, |
| 149 | + {"https://gap-grpc-job-distributor.public.main.stage.cldev.sh/", false}, |
| 150 | + {"https://gap-ws-job-distributor.public.main.stage.cldev.sh/", false}, |
| 151 | + {"https://gap-grpc-chainlink-catalog.public.main.stage.cldev.sh/", false}, |
| 152 | + {"", true}, |
| 153 | + {"http://", true}, |
| 154 | + {"https://", true}, |
| 155 | + {"https://rpcs.cldev.sh", true}, |
| 156 | + {"https://rpcs.prod.cldev.sh/anything", true}, |
| 157 | + {"https://rpcs.stage.cldev.sh/anything", true}, |
| 158 | + {"https://gap.stage.cldev.sh/anything", true}, |
| 159 | + } |
| 160 | + for _, tt := range tests { |
| 161 | + t.Run(tt.url, func(t *testing.T) { |
| 162 | + t.Parallel() |
| 163 | + require.Equal(t, tt.want, isPublicRPC(tt.url)) |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func Test_selectPublicRPC(t *testing.T) { |
| 169 | + t.Parallel() |
| 170 | + |
| 171 | + lggr := logger.Test(t) |
| 172 | + tests := []struct { |
| 173 | + name string |
| 174 | + metadata *cfgnet.EVMMetadata |
| 175 | + chainSelector uint64 |
| 176 | + rpcs []cfgnet.RPC |
| 177 | + want *cfgnet.EVMMetadata |
| 178 | + wantErr string |
| 179 | + }{ |
| 180 | + { |
| 181 | + name: "success: metadata has url", |
| 182 | + metadata: &cfgnet.EVMMetadata{AnvilConfig: &cfgnet.AnvilConfig{ |
| 183 | + ArchiveHTTPURL: "http://metadata.url", |
| 184 | + }}, |
| 185 | + rpcs: []cfgnet.RPC{ |
| 186 | + {HTTPURL: "http://other.url"}, |
| 187 | + }, |
| 188 | + want: &cfgnet.EVMMetadata{AnvilConfig: &cfgnet.AnvilConfig{ |
| 189 | + ArchiveHTTPURL: "http://metadata.url", |
| 190 | + }}, |
| 191 | + }, |
| 192 | + { |
| 193 | + name: "success: private rpc in metadata is replaced public url from parameters", |
| 194 | + metadata: &cfgnet.EVMMetadata{AnvilConfig: &cfgnet.AnvilConfig{ |
| 195 | + ArchiveHTTPURL: "http://gap-rpc.prod.cldev.sh/ethereum/sepolia", |
| 196 | + }}, |
| 197 | + rpcs: []cfgnet.RPC{ |
| 198 | + {HTTPURL: "http://rpcs.cldev.sh/ethereum/sepolia"}, |
| 199 | + {HTTPURL: "http://public.rpc.url"}, |
| 200 | + }, |
| 201 | + want: &cfgnet.EVMMetadata{AnvilConfig: &cfgnet.AnvilConfig{ |
| 202 | + ArchiveHTTPURL: "http://public.rpc.url", |
| 203 | + }}, |
| 204 | + }, |
| 205 | + { |
| 206 | + name: "failure: no public rpcs found", |
| 207 | + metadata: &cfgnet.EVMMetadata{AnvilConfig: &cfgnet.AnvilConfig{ |
| 208 | + ArchiveHTTPURL: "http://gap-rpc.prod.cldev.sh/ethereum/sepolia", |
| 209 | + }}, |
| 210 | + rpcs: []cfgnet.RPC{ |
| 211 | + {HTTPURL: "http://rpcs.cldev.sh/ethereum/sepolia"}, |
| 212 | + }, |
| 213 | + wantErr: "no public RPCs found for chain 0", |
| 214 | + }, |
| 215 | + } |
| 216 | + for _, tt := range tests { |
| 217 | + t.Run(tt.name, func(t *testing.T) { |
| 218 | + t.Parallel() |
| 219 | + |
| 220 | + err := selectPublicRPC(lggr, tt.metadata, tt.chainSelector, tt.rpcs) |
| 221 | + if tt.wantErr == "" { |
| 222 | + require.NoError(t, err) |
| 223 | + require.Equal(t, tt.want, tt.metadata) |
| 224 | + } else { |
| 225 | + require.ErrorContains(t, err, tt.wantErr) |
| 226 | + } |
| 227 | + }) |
| 228 | + } |
| 229 | +} |
0 commit comments