code.vegaprotocol.io/vega@v0.79.0/protos/vega/spec_definition_test.go (about)

     1  package vega_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	dstypes "code.vegaprotocol.io/vega/core/datasource/common"
     8  	vegapb "code.vegaprotocol.io/vega/protos/vega"
     9  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
    10  	"github.com/stretchr/testify/assert"
    11  	"google.golang.org/protobuf/types/known/structpb"
    12  )
    13  
    14  func TestGetFilters(t *testing.T) {
    15  	t.Run("testGetFiltersExternal", func(t *testing.T) {
    16  		t.Run("NotEmpty Oracle", func(t *testing.T) {
    17  			dsd := &vegapb.DataSourceDefinition{
    18  				SourceType: &vegapb.DataSourceDefinition_External{
    19  					External: &vegapb.DataSourceDefinitionExternal{
    20  						SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
    21  							Oracle: &vegapb.DataSourceSpecConfiguration{
    22  								Signers: []*datapb.Signer{},
    23  								Filters: []*datapb.Filter{
    24  									{
    25  										Key: &datapb.PropertyKey{
    26  											Name: "prices.ETH.value",
    27  											Type: datapb.PropertyKey_TYPE_INTEGER,
    28  										},
    29  										Conditions: []*datapb.Condition{
    30  											{
    31  												Operator: datapb.Condition_OPERATOR_EQUALS,
    32  												Value:    "ext-test-value-1",
    33  											},
    34  											{
    35  												Operator: datapb.Condition_OPERATOR_GREATER_THAN,
    36  												Value:    "ext-test-value-2",
    37  											},
    38  										},
    39  									},
    40  									{
    41  										Key: &datapb.PropertyKey{
    42  											Name: "key-name-string",
    43  											Type: datapb.PropertyKey_TYPE_STRING,
    44  										},
    45  										Conditions: []*datapb.Condition{
    46  											{
    47  												Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
    48  												Value:    "ext-test-value-3",
    49  											},
    50  											{
    51  												Operator: datapb.Condition_OPERATOR_GREATER_THAN,
    52  												Value:    "ext-test-value-4",
    53  											},
    54  										},
    55  									},
    56  								},
    57  							},
    58  						},
    59  					},
    60  				},
    61  			}
    62  
    63  			filters := dsd.GetFilters()
    64  			assert.Equal(t, 2, len(filters))
    65  			assert.Equal(t, "prices.ETH.value", filters[0].Key.Name)
    66  			assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
    67  			assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
    68  			assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
    69  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
    70  			assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
    71  
    72  			assert.Equal(t, "key-name-string", filters[1].Key.Name)
    73  			assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
    74  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
    75  			assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
    76  
    77  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
    78  			assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
    79  		})
    80  
    81  		t.Run("NotEmpty EthOracle", func(t *testing.T) {
    82  			timeNow := uint64(time.Now().UnixNano())
    83  			dsd := &vegapb.DataSourceDefinition{
    84  				SourceType: &vegapb.DataSourceDefinition_External{
    85  					External: &vegapb.DataSourceDefinitionExternal{
    86  						SourceType: &vegapb.DataSourceDefinitionExternal_EthOracle{
    87  							EthOracle: &vegapb.EthCallSpec{
    88  								Address: "some-eth-address",
    89  								Abi:     "{\"string-value\"}",
    90  								Method:  "test-method",
    91  								Args: []*structpb.Value{
    92  									{
    93  										Kind: &structpb.Value_StringValue{
    94  											StringValue: "string-arg",
    95  										},
    96  									},
    97  								},
    98  								Trigger: &vegapb.EthCallTrigger{
    99  									Trigger: &vegapb.EthCallTrigger_TimeTrigger{
   100  										TimeTrigger: &vegapb.EthTimeTrigger{
   101  											Initial: &timeNow,
   102  										},
   103  									},
   104  								},
   105  								RequiredConfirmations: 256,
   106  								Filters: []*datapb.Filter{
   107  									{
   108  										Key: &datapb.PropertyKey{
   109  											Name: "test-key-name-0",
   110  											Type: dstypes.SpecPropertyKeyType(2),
   111  										},
   112  									},
   113  								},
   114  							},
   115  						},
   116  					},
   117  				},
   118  			}
   119  
   120  			filters := dsd.GetFilters()
   121  			assert.Equal(t, 1, len(filters))
   122  			assert.Equal(t, "test-key-name-0", filters[0].Key.Name)
   123  			assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
   124  			assert.Equal(t, 0, len(filters[0].Conditions))
   125  		})
   126  
   127  		t.Run("Empty", func(t *testing.T) {
   128  			dsd := &vegapb.DataSourceDefinition{
   129  				SourceType: &vegapb.DataSourceDefinition_External{},
   130  			}
   131  
   132  			filters := dsd.GetFilters()
   133  			assert.Equal(t, 0, len(filters))
   134  
   135  			dsd = &vegapb.DataSourceDefinition{
   136  				SourceType: &vegapb.DataSourceDefinition_External{
   137  					External: &vegapb.DataSourceDefinitionExternal{
   138  						SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
   139  							Oracle: nil,
   140  						},
   141  					},
   142  				},
   143  			}
   144  
   145  			filters = dsd.GetFilters()
   146  			assert.Equal(t, 0, len(filters))
   147  		})
   148  	})
   149  
   150  	t.Run("testGetFiltersInternal", func(t *testing.T) {
   151  		t.Run("NotEmpty", func(t *testing.T) {
   152  			dsd := &vegapb.DataSourceDefinition{
   153  				SourceType: &vegapb.DataSourceDefinition_Internal{
   154  					Internal: &vegapb.DataSourceDefinitionInternal{
   155  						SourceType: &vegapb.DataSourceDefinitionInternal_Time{
   156  							Time: &vegapb.DataSourceSpecConfigurationTime{
   157  								Conditions: []*datapb.Condition{
   158  									{
   159  										Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   160  										Value:    "int-test-value-1",
   161  									},
   162  									{
   163  										Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   164  										Value:    "int-test-value-2",
   165  									},
   166  								},
   167  							},
   168  						},
   169  					},
   170  				},
   171  			}
   172  
   173  			filters := dsd.GetFilters()
   174  			// Ensure only a single filter has been created, that holds all given conditions
   175  			assert.Equal(t, 1, len(filters))
   176  
   177  			assert.Equal(t, "vegaprotocol.builtin.timestamp", filters[0].Key.Name)
   178  			assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   179  
   180  			assert.Equal(t, 1, len(filters[0].Conditions))
   181  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   182  			assert.Equal(t, "int-test-value-1", filters[0].Conditions[0].Value)
   183  
   184  			dsd = &vegapb.DataSourceDefinition{
   185  				SourceType: &vegapb.DataSourceDefinition_Internal{
   186  					Internal: &vegapb.DataSourceDefinitionInternal{
   187  						SourceType: &vegapb.DataSourceDefinitionInternal_TimeTrigger{
   188  							TimeTrigger: &vegapb.DataSourceSpecConfigurationTimeTrigger{
   189  								Conditions: []*datapb.Condition{
   190  									{
   191  										Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   192  										Value:    "int-test-value-1",
   193  									},
   194  									{
   195  										Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   196  										Value:    "int-test-value-2",
   197  									},
   198  								},
   199  							},
   200  						},
   201  					},
   202  				},
   203  			}
   204  
   205  			filters = dsd.GetFilters()
   206  			// Ensure only a single filter has been created, that holds all given conditions
   207  			assert.Equal(t, 1, len(filters))
   208  
   209  			assert.Equal(t, "vegaprotocol.builtin.timetrigger", filters[0].Key.Name)
   210  			assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   211  
   212  			assert.Equal(t, 2, len(filters[0].Conditions))
   213  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   214  			assert.Equal(t, "int-test-value-1", filters[0].Conditions[0].Value)
   215  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   216  			assert.Equal(t, "int-test-value-2", filters[0].Conditions[1].Value)
   217  		})
   218  
   219  		t.Run("Empty", func(t *testing.T) {
   220  			dsd := &vegapb.DataSourceDefinition{
   221  				SourceType: &vegapb.DataSourceDefinition_Internal{},
   222  			}
   223  
   224  			filters := dsd.GetFilters()
   225  
   226  			assert.Equal(t, 0, len(filters))
   227  
   228  			dsd = &vegapb.DataSourceDefinition{
   229  				SourceType: &vegapb.DataSourceDefinition_Internal{
   230  					Internal: &vegapb.DataSourceDefinitionInternal{
   231  						SourceType: &vegapb.DataSourceDefinitionInternal_Time{
   232  							Time: &vegapb.DataSourceSpecConfigurationTime{
   233  								Conditions: []*datapb.Condition{},
   234  							},
   235  						},
   236  					},
   237  				},
   238  			}
   239  
   240  			filters = dsd.GetFilters()
   241  			assert.Equal(t, 0, len(filters))
   242  		})
   243  	})
   244  }
   245  
   246  func TestSetOracleConfig(t *testing.T) {
   247  	t.Run("empty", func(t *testing.T) {
   248  	})
   249  	t.Run("non-empty oracle", func(t *testing.T) {
   250  		dsd := &vegapb.DataSourceDefinition{
   251  			SourceType: &vegapb.DataSourceDefinition_External{
   252  				External: &vegapb.DataSourceDefinitionExternal{
   253  					SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
   254  						Oracle: nil,
   255  					},
   256  				},
   257  			},
   258  		}
   259  
   260  		udsd := dsd.SetOracleConfig(
   261  			&vegapb.DataSourceDefinitionExternal_Oracle{
   262  				Oracle: &vegapb.DataSourceSpecConfiguration{
   263  					Signers: dstypes.SignersIntoProto(
   264  						[]*dstypes.Signer{
   265  							dstypes.CreateSignerFromString("0xSOMEKEYX", dstypes.SignerTypePubKey),
   266  							dstypes.CreateSignerFromString("0xSOMEKEYY", dstypes.SignerTypePubKey),
   267  						}),
   268  					Filters: []*datapb.Filter{
   269  						{
   270  							Key: &datapb.PropertyKey{
   271  								Name: "prices.ETH.value",
   272  								Type: datapb.PropertyKey_TYPE_INTEGER,
   273  							},
   274  							Conditions: []*datapb.Condition{
   275  								{
   276  									Operator: datapb.Condition_OPERATOR_EQUALS,
   277  									Value:    "ext-test-value-1",
   278  								},
   279  								{
   280  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   281  									Value:    "ext-test-value-2",
   282  								},
   283  							},
   284  						},
   285  						{
   286  							Key: &datapb.PropertyKey{
   287  								Name: "key-name-string",
   288  								Type: datapb.PropertyKey_TYPE_STRING,
   289  							},
   290  							Conditions: []*datapb.Condition{
   291  								{
   292  									Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   293  									Value:    "ext-test-value-3",
   294  								},
   295  								{
   296  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   297  									Value:    "ext-test-value-4",
   298  								},
   299  							},
   300  						},
   301  					},
   302  				},
   303  			},
   304  		)
   305  
   306  		filters := udsd.GetFilters()
   307  		assert.Equal(t, 2, len(filters))
   308  		assert.Equal(t, "prices.ETH.value", filters[0].Key.Name)
   309  		assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
   310  		assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
   311  		assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
   312  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   313  		assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
   314  
   315  		assert.Equal(t, "key-name-string", filters[1].Key.Name)
   316  		assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
   317  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
   318  		assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
   319  
   320  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
   321  		assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
   322  	})
   323  
   324  	t.Run("non-empty eth oracle", func(t *testing.T) {
   325  		dsd := &vegapb.DataSourceDefinition{
   326  			SourceType: &vegapb.DataSourceDefinition_External{
   327  				External: &vegapb.DataSourceDefinitionExternal{
   328  					SourceType: &vegapb.DataSourceDefinitionExternal_EthOracle{
   329  						EthOracle: nil,
   330  					},
   331  				},
   332  			},
   333  		}
   334  		timeNow := uint64(time.Now().UnixNano())
   335  		udsd := dsd.SetOracleConfig(
   336  			&vegapb.DataSourceDefinitionExternal_EthOracle{
   337  				EthOracle: &vegapb.EthCallSpec{
   338  					Address: "some-eth-address",
   339  					Abi:     "{\"string-value\"}",
   340  					Method:  "test-method",
   341  					Args: []*structpb.Value{
   342  						{
   343  							Kind: &structpb.Value_StringValue{
   344  								StringValue: "string-arg",
   345  							},
   346  						},
   347  					},
   348  					Trigger: &vegapb.EthCallTrigger{
   349  						Trigger: &vegapb.EthCallTrigger_TimeTrigger{
   350  							TimeTrigger: &vegapb.EthTimeTrigger{
   351  								Initial: &timeNow,
   352  							},
   353  						},
   354  					},
   355  					RequiredConfirmations: 256,
   356  					Filters: []*datapb.Filter{
   357  						{
   358  							Key: &datapb.PropertyKey{
   359  								Name: "prices.ETH.value",
   360  								Type: datapb.PropertyKey_TYPE_INTEGER,
   361  							},
   362  							Conditions: []*datapb.Condition{
   363  								{
   364  									Operator: datapb.Condition_OPERATOR_EQUALS,
   365  									Value:    "ext-test-value-1",
   366  								},
   367  								{
   368  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   369  									Value:    "ext-test-value-2",
   370  								},
   371  							},
   372  						},
   373  						{
   374  							Key: &datapb.PropertyKey{
   375  								Name: "key-name-string",
   376  								Type: datapb.PropertyKey_TYPE_STRING,
   377  							},
   378  							Conditions: []*datapb.Condition{
   379  								{
   380  									Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   381  									Value:    "ext-test-value-3",
   382  								},
   383  								{
   384  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   385  									Value:    "ext-test-value-4",
   386  								},
   387  							},
   388  						},
   389  					},
   390  				},
   391  			},
   392  		)
   393  
   394  		eo := udsd.GetExternal().GetEthOracle()
   395  		assert.Equal(t, "some-eth-address", eo.Address)
   396  		assert.Equal(t, "{\"string-value\"}", eo.GetAbi())
   397  		assert.Equal(t, "test-method", eo.Method)
   398  		assert.Equal(t, 1, len(eo.GetArgs()))
   399  		assert.Equal(t, "string-arg", eo.GetArgs()[0].GetStringValue())
   400  		assert.Equal(t, &timeNow, eo.GetTrigger().GetTimeTrigger().Initial)
   401  
   402  		filters := eo.GetFilters()
   403  		assert.Equal(t, 2, len(filters))
   404  		assert.Equal(t, "prices.ETH.value", filters[0].Key.Name)
   405  		assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
   406  		assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
   407  		assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
   408  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   409  		assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
   410  
   411  		assert.Equal(t, "key-name-string", filters[1].Key.Name)
   412  		assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
   413  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
   414  		assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
   415  
   416  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
   417  		assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
   418  	})
   419  
   420  	t.Run("try to set oracle config to internal data source", func(t *testing.T) {
   421  		dsd := &vegapb.DataSourceDefinition{
   422  			SourceType: &vegapb.DataSourceDefinition_Internal{
   423  				Internal: &vegapb.DataSourceDefinitionInternal{
   424  					SourceType: &vegapb.DataSourceDefinitionInternal_Time{
   425  						Time: &vegapb.DataSourceSpecConfigurationTime{
   426  							Conditions: []*datapb.Condition{
   427  								{
   428  									Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   429  									Value:    "int-test-value-1",
   430  								},
   431  								{
   432  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   433  									Value:    "int-test-value-2",
   434  								},
   435  							},
   436  						},
   437  					},
   438  				},
   439  			},
   440  		}
   441  
   442  		iudsd := dsd.SetOracleConfig(
   443  			&vegapb.DataSourceDefinitionExternal_Oracle{
   444  				Oracle: &vegapb.DataSourceSpecConfiguration{
   445  					Filters: []*datapb.Filter{
   446  						{
   447  							Key: &datapb.PropertyKey{
   448  								Name: "prices.ETH.value",
   449  								Type: datapb.PropertyKey_TYPE_INTEGER,
   450  							},
   451  							Conditions: []*datapb.Condition{
   452  								{
   453  									Operator: datapb.Condition_OPERATOR_EQUALS,
   454  									Value:    "ext-test-value-1",
   455  								},
   456  								{
   457  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   458  									Value:    "ext-test-value-2",
   459  								},
   460  							},
   461  						},
   462  						{
   463  							Key: &datapb.PropertyKey{
   464  								Name: "key-name-string",
   465  								Type: datapb.PropertyKey_TYPE_STRING,
   466  							},
   467  							Conditions: []*datapb.Condition{
   468  								{
   469  									Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   470  									Value:    "ext-test-value-3",
   471  								},
   472  								{
   473  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   474  									Value:    "ext-test-value-4",
   475  								},
   476  							},
   477  						},
   478  					},
   479  				},
   480  			},
   481  		)
   482  
   483  		filters := iudsd.GetFilters()
   484  		assert.Equal(t, 1, len(filters))
   485  
   486  		assert.Equal(t, "vegaprotocol.builtin.timestamp", filters[0].Key.Name)
   487  		assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   488  
   489  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   490  		assert.Equal(t, "int-test-value-1", filters[0].Conditions[0].Value)
   491  	})
   492  
   493  	t.Run("try to set oracle config to internal time trigger data source", func(t *testing.T) {
   494  		timeNow := time.Now().Unix()
   495  		dsd := &vegapb.DataSourceDefinition{
   496  			SourceType: &vegapb.DataSourceDefinition_Internal{
   497  				Internal: &vegapb.DataSourceDefinitionInternal{
   498  					SourceType: &vegapb.DataSourceDefinitionInternal_TimeTrigger{
   499  						TimeTrigger: &vegapb.DataSourceSpecConfigurationTimeTrigger{
   500  							Conditions: []*datapb.Condition{
   501  								{
   502  									Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   503  									Value:    "int-test-value-1",
   504  								},
   505  								{
   506  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   507  									Value:    "int-test-value-2",
   508  								},
   509  							},
   510  							Triggers: []*datapb.InternalTimeTrigger{
   511  								{
   512  									Initial: &timeNow,
   513  									Every:   int64(19),
   514  								},
   515  							},
   516  						},
   517  					},
   518  				},
   519  			},
   520  		}
   521  
   522  		iudsd := dsd.SetOracleConfig(
   523  			&vegapb.DataSourceDefinitionExternal_Oracle{
   524  				Oracle: &vegapb.DataSourceSpecConfiguration{
   525  					Filters: []*datapb.Filter{
   526  						{
   527  							Key: &datapb.PropertyKey{
   528  								Name: "prices.ETH.value",
   529  								Type: datapb.PropertyKey_TYPE_INTEGER,
   530  							},
   531  							Conditions: []*datapb.Condition{
   532  								{
   533  									Operator: datapb.Condition_OPERATOR_EQUALS,
   534  									Value:    "ext-test-value-1",
   535  								},
   536  								{
   537  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   538  									Value:    "ext-test-value-2",
   539  								},
   540  							},
   541  						},
   542  						{
   543  							Key: &datapb.PropertyKey{
   544  								Name: "key-name-string",
   545  								Type: datapb.PropertyKey_TYPE_STRING,
   546  							},
   547  							Conditions: []*datapb.Condition{
   548  								{
   549  									Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   550  									Value:    "ext-test-value-3",
   551  								},
   552  								{
   553  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   554  									Value:    "ext-test-value-4",
   555  								},
   556  							},
   557  						},
   558  					},
   559  				},
   560  			},
   561  		)
   562  
   563  		filters := iudsd.GetFilters()
   564  		assert.Equal(t, 1, len(filters))
   565  
   566  		assert.Equal(t, "vegaprotocol.builtin.timetrigger", filters[0].Key.Name)
   567  		assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   568  
   569  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   570  		assert.Equal(t, "int-test-value-1", filters[0].Conditions[0].Value)
   571  	})
   572  }
   573  
   574  func TestSetTimeTriggerConditionConfig(t *testing.T) {
   575  	dsd := &vegapb.DataSourceDefinition{
   576  		SourceType: &vegapb.DataSourceDefinition_External{
   577  			External: &vegapb.DataSourceDefinitionExternal{
   578  				SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
   579  					Oracle: nil,
   580  				},
   581  			},
   582  		},
   583  	}
   584  
   585  	udsd := dsd.SetTimeTriggerConditionConfig(
   586  		[]*datapb.Condition{
   587  			{
   588  				Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   589  				Value:    "ext-test-value-3",
   590  			},
   591  			{
   592  				Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   593  				Value:    "ext-test-value-4",
   594  			},
   595  		},
   596  	)
   597  
   598  	filters := udsd.GetFilters()
   599  	assert.Equal(t, 0, len(filters))
   600  
   601  	t.Run("non-empty internal data source", func(t *testing.T) {
   602  		dsd := &vegapb.DataSourceDefinition{
   603  			SourceType: &vegapb.DataSourceDefinition_Internal{
   604  				Internal: &vegapb.DataSourceDefinitionInternal{
   605  					SourceType: &vegapb.DataSourceDefinitionInternal_Time{
   606  						Time: &vegapb.DataSourceSpecConfigurationTime{
   607  							Conditions: []*datapb.Condition{
   608  								{
   609  									Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   610  									Value:    "int-test-value-1",
   611  								},
   612  								{
   613  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   614  									Value:    "int-test-value-2",
   615  								},
   616  							},
   617  						},
   618  					},
   619  				},
   620  			},
   621  		}
   622  
   623  		iudsd := dsd.SetTimeTriggerConditionConfig(
   624  			[]*datapb.Condition{
   625  				{
   626  					Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   627  					Value:    "int-test-value-3",
   628  				},
   629  				{
   630  					Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   631  					Value:    "int-test-value-4",
   632  				},
   633  			},
   634  		)
   635  
   636  		filters := iudsd.GetFilters()
   637  		assert.Equal(t, 1, len(filters))
   638  
   639  		assert.Equal(t, "vegaprotocol.builtin.timestamp", filters[0].Key.Name)
   640  		assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   641  
   642  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   643  		assert.Equal(t, "int-test-value-3", filters[0].Conditions[0].Value)
   644  	})
   645  
   646  	t.Run("non-empty internal time trigger data source", func(t *testing.T) {
   647  		dsd := &vegapb.DataSourceDefinition{
   648  			SourceType: &vegapb.DataSourceDefinition_Internal{
   649  				Internal: &vegapb.DataSourceDefinitionInternal{
   650  					SourceType: &vegapb.DataSourceDefinitionInternal_TimeTrigger{
   651  						TimeTrigger: &vegapb.DataSourceSpecConfigurationTimeTrigger{
   652  							Conditions: []*datapb.Condition{
   653  								{
   654  									Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   655  									Value:    "int-test-value-1",
   656  								},
   657  								{
   658  									Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   659  									Value:    "int-test-value-2",
   660  								},
   661  							},
   662  						},
   663  					},
   664  				},
   665  			},
   666  		}
   667  
   668  		iudsd := dsd.SetTimeTriggerConditionConfig(
   669  			[]*datapb.Condition{
   670  				{
   671  					Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   672  					Value:    "int-test-value-3",
   673  				},
   674  				{
   675  					Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   676  					Value:    "int-test-value-4",
   677  				},
   678  			},
   679  		)
   680  
   681  		filters := iudsd.GetFilters()
   682  		assert.Equal(t, 1, len(filters))
   683  
   684  		assert.Equal(t, "vegaprotocol.builtin.timetrigger", filters[0].Key.Name)
   685  		assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   686  
   687  		assert.Equal(t, 2, len(filters[0].Conditions))
   688  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   689  		assert.Equal(t, "int-test-value-3", filters[0].Conditions[0].Value)
   690  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   691  		assert.Equal(t, "int-test-value-4", filters[0].Conditions[1].Value)
   692  	})
   693  }
   694  
   695  func TestContent(t *testing.T) {
   696  	t.Run("testContent", func(t *testing.T) {
   697  		t.Run("empty content", func(t *testing.T) {
   698  			d := &vegapb.DataSourceDefinition{}
   699  			assert.Nil(t, d.Content())
   700  
   701  			d = &vegapb.DataSourceDefinition{
   702  				SourceType: &vegapb.DataSourceDefinition_External{
   703  					External: &vegapb.DataSourceDefinitionExternal{
   704  						SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
   705  							Oracle: nil,
   706  						},
   707  					},
   708  				},
   709  			}
   710  
   711  			assert.Nil(t, d.Content())
   712  
   713  			d = &vegapb.DataSourceDefinition{
   714  				SourceType: &vegapb.DataSourceDefinition_External{
   715  					External: &vegapb.DataSourceDefinitionExternal{
   716  						SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
   717  							Oracle: &vegapb.DataSourceSpecConfiguration{},
   718  						},
   719  					},
   720  				},
   721  			}
   722  
   723  			c := d.Content()
   724  			assert.NotNil(t, c)
   725  			tp, ok := c.(*vegapb.DataSourceSpecConfiguration)
   726  			assert.True(t, ok)
   727  			assert.Equal(t, 0, len(tp.Filters))
   728  			assert.Equal(t, 0, len(tp.Signers))
   729  
   730  			d = &vegapb.DataSourceDefinition{
   731  				SourceType: &vegapb.DataSourceDefinition_Internal{
   732  					Internal: &vegapb.DataSourceDefinitionInternal{
   733  						SourceType: &vegapb.DataSourceDefinitionInternal_Time{
   734  							Time: nil,
   735  						},
   736  					},
   737  				},
   738  			}
   739  
   740  			assert.Nil(t, d.Content())
   741  
   742  			d = &vegapb.DataSourceDefinition{
   743  				SourceType: &vegapb.DataSourceDefinition_Internal{
   744  					Internal: &vegapb.DataSourceDefinitionInternal{
   745  						SourceType: &vegapb.DataSourceDefinitionInternal_Time{
   746  							Time: &vegapb.DataSourceSpecConfigurationTime{},
   747  						},
   748  					},
   749  				},
   750  			}
   751  
   752  			ct := d.Content()
   753  			assert.NotNil(t, ct)
   754  			tpt, ok := ct.(*vegapb.DataSourceSpecConfigurationTime)
   755  			assert.True(t, ok)
   756  			assert.Equal(t, 0, len(tpt.Conditions))
   757  		})
   758  
   759  		t.Run("non-empty content with time termiation source", func(t *testing.T) {
   760  			d := &vegapb.DataSourceDefinition{
   761  				SourceType: &vegapb.DataSourceDefinition_Internal{
   762  					Internal: &vegapb.DataSourceDefinitionInternal{
   763  						SourceType: &vegapb.DataSourceDefinitionInternal_Time{
   764  							Time: &vegapb.DataSourceSpecConfigurationTime{
   765  								Conditions: []*datapb.Condition{
   766  									{
   767  										Operator: datapb.Condition_OPERATOR_EQUALS,
   768  										Value:    "ext-test-value-0",
   769  									},
   770  									{
   771  										Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   772  										Value:    "ext-test-value-1",
   773  									},
   774  								},
   775  							},
   776  						},
   777  					},
   778  				},
   779  			}
   780  
   781  			c := d.Content()
   782  			assert.NotNil(t, c)
   783  			assert.IsType(t, &vegapb.DataSourceSpecConfigurationTime{}, c)
   784  			tp, ok := c.(*vegapb.DataSourceSpecConfigurationTime)
   785  			assert.True(t, ok)
   786  			assert.Equal(t, 2, len(tp.Conditions))
   787  			assert.Equal(t, "ext-test-value-0", tp.Conditions[0].Value)
   788  			assert.Equal(t, "ext-test-value-1", tp.Conditions[1].Value)
   789  		})
   790  
   791  		t.Run("non-empty content with time trigger termiation source", func(t *testing.T) {
   792  			timeNow := time.Now().Unix()
   793  			d := &vegapb.DataSourceDefinition{
   794  				SourceType: &vegapb.DataSourceDefinition_Internal{
   795  					Internal: &vegapb.DataSourceDefinitionInternal{
   796  						SourceType: &vegapb.DataSourceDefinitionInternal_TimeTrigger{
   797  							TimeTrigger: &vegapb.DataSourceSpecConfigurationTimeTrigger{
   798  								Conditions: []*datapb.Condition{
   799  									{
   800  										Operator: datapb.Condition_OPERATOR_EQUALS,
   801  										Value:    "ext-test-value-0",
   802  									},
   803  									{
   804  										Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   805  										Value:    "ext-test-value-1",
   806  									},
   807  								},
   808  								Triggers: []*datapb.InternalTimeTrigger{
   809  									{
   810  										Initial: &timeNow,
   811  										Every:   int64(19),
   812  									},
   813  								},
   814  							},
   815  						},
   816  					},
   817  				},
   818  			}
   819  
   820  			c := d.Content()
   821  			assert.NotNil(t, c)
   822  			assert.IsType(t, &vegapb.DataSourceSpecConfigurationTimeTrigger{}, c)
   823  			tp, ok := c.(*vegapb.DataSourceSpecConfigurationTimeTrigger)
   824  			assert.True(t, ok)
   825  			assert.Equal(t, 2, len(tp.Conditions))
   826  			assert.Equal(t, "ext-test-value-0", tp.Conditions[0].Value)
   827  			assert.Equal(t, "ext-test-value-1", tp.Conditions[1].Value)
   828  			assert.Equal(t, timeNow, *tp.GetTriggers()[0].Initial)
   829  			assert.Equal(t, int64(19), tp.GetTriggers()[0].Every)
   830  		})
   831  
   832  		t.Run("non-empty content with oracle", func(t *testing.T) {
   833  			d := &vegapb.DataSourceDefinition{
   834  				SourceType: &vegapb.DataSourceDefinition_External{
   835  					External: &vegapb.DataSourceDefinitionExternal{
   836  						SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
   837  							Oracle: &vegapb.DataSourceSpecConfiguration{
   838  								Signers: dstypes.SignersIntoProto(
   839  									[]*dstypes.Signer{
   840  										dstypes.CreateSignerFromString("0xSOMEKEYX", dstypes.SignerTypePubKey),
   841  										dstypes.CreateSignerFromString("0xSOMEKEYY", dstypes.SignerTypePubKey),
   842  									}),
   843  							},
   844  						},
   845  					},
   846  				},
   847  			}
   848  			c := d.Content()
   849  			assert.NotNil(t, c)
   850  			assert.IsType(t, &vegapb.DataSourceSpecConfiguration{}, c)
   851  			tp, ok := c.(*vegapb.DataSourceSpecConfiguration)
   852  			assert.True(t, ok)
   853  			assert.Equal(t, 2, len(tp.Signers))
   854  			assert.Equal(t, "0xSOMEKEYX", tp.Signers[0].GetPubKey().GetKey())
   855  		})
   856  
   857  		t.Run("non-empty content with ethereum oracle", func(t *testing.T) {
   858  			timeNow := uint64(time.Now().UnixNano())
   859  			d := &vegapb.DataSourceDefinition{
   860  				SourceType: &vegapb.DataSourceDefinition_External{
   861  					External: &vegapb.DataSourceDefinitionExternal{
   862  						SourceType: &vegapb.DataSourceDefinitionExternal_EthOracle{
   863  							EthOracle: &vegapb.EthCallSpec{
   864  								Address: "some-eth-address",
   865  								Abi:     "{\"string-value\"}",
   866  								Method:  "test-method",
   867  								Args: []*structpb.Value{
   868  									{
   869  										Kind: &structpb.Value_StringValue{
   870  											StringValue: "string-arg",
   871  										},
   872  									},
   873  								},
   874  								Trigger: &vegapb.EthCallTrigger{
   875  									Trigger: &vegapb.EthCallTrigger_TimeTrigger{
   876  										TimeTrigger: &vegapb.EthTimeTrigger{
   877  											Initial: &timeNow,
   878  										},
   879  									},
   880  								},
   881  								RequiredConfirmations: 256,
   882  								Filters: []*datapb.Filter{
   883  									{
   884  										Key: &datapb.PropertyKey{
   885  											Name: "prices.ETH.value",
   886  											Type: datapb.PropertyKey_TYPE_INTEGER,
   887  										},
   888  										Conditions: []*datapb.Condition{
   889  											{
   890  												Operator: datapb.Condition_OPERATOR_EQUALS,
   891  												Value:    "ext-test-value-1",
   892  											},
   893  											{
   894  												Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   895  												Value:    "ext-test-value-2",
   896  											},
   897  										},
   898  									},
   899  									{
   900  										Key: &datapb.PropertyKey{
   901  											Name: "key-name-string",
   902  											Type: datapb.PropertyKey_TYPE_STRING,
   903  										},
   904  										Conditions: []*datapb.Condition{
   905  											{
   906  												Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   907  												Value:    "ext-test-value-3",
   908  											},
   909  											{
   910  												Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   911  												Value:    "ext-test-value-4",
   912  											},
   913  										},
   914  									},
   915  								},
   916  							},
   917  						},
   918  					},
   919  				},
   920  			}
   921  			c := d.Content()
   922  			assert.NotNil(t, c)
   923  			assert.IsType(t, &vegapb.EthCallSpec{}, c)
   924  			eo, ok := c.(*vegapb.EthCallSpec)
   925  			assert.True(t, ok)
   926  			assert.Equal(t, "some-eth-address", eo.Address)
   927  			assert.Equal(t, "{\"string-value\"}", eo.GetAbi())
   928  			assert.Equal(t, "test-method", eo.Method)
   929  			assert.Equal(t, 1, len(eo.GetArgs()))
   930  			assert.Equal(t, "string-arg", eo.GetArgs()[0].GetStringValue())
   931  			assert.Equal(t, &timeNow, eo.GetTrigger().GetTimeTrigger().Initial)
   932  
   933  			filters := eo.GetFilters()
   934  			assert.Equal(t, 2, len(filters))
   935  			assert.Equal(t, "prices.ETH.value", filters[0].Key.Name)
   936  			assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
   937  			assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
   938  			assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
   939  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   940  			assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
   941  
   942  			assert.Equal(t, "key-name-string", filters[1].Key.Name)
   943  			assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
   944  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
   945  			assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
   946  
   947  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
   948  			assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
   949  		})
   950  	})
   951  }
   952  
   953  func TestNewDataSourceDefinition(t *testing.T) {
   954  }