code.vegaprotocol.io/vega@v0.79.0/core/datasource/definition/definition_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package definition_test
    17  
    18  import (
    19  	"errors"
    20  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/core/datasource/common"
    24  	"code.vegaprotocol.io/vega/core/datasource/definition"
    25  	ethcallcommon "code.vegaprotocol.io/vega/core/datasource/external/ethcall/common"
    26  	"code.vegaprotocol.io/vega/core/datasource/external/signedoracle"
    27  	"code.vegaprotocol.io/vega/core/datasource/internal/timetrigger"
    28  	"code.vegaprotocol.io/vega/core/datasource/internal/vegatime"
    29  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    30  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
    31  
    32  	"github.com/stretchr/testify/assert"
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  func TestNewDefinitionWith(t *testing.T) {
    37  	t.Run("oracle", func(t *testing.T) {
    38  		t.Run("empty", func(t *testing.T) {
    39  			nds := definition.NewWith(nil)
    40  			assert.NotNil(t, nds)
    41  			assert.IsType(t, &definition.Definition{}, nds)
    42  			assert.Nil(t, nds.Content())
    43  		})
    44  		// Non-empty cases are tested implicitly by TestDefinitionIntoProto
    45  	})
    46  }
    47  
    48  func TestNewDefinition(t *testing.T) {
    49  	ds := definition.New(definition.ContentTypeOracle)
    50  	assert.NotNil(t, ds)
    51  	cnt := ds.Content()
    52  	assert.IsType(t, signedoracle.SpecConfiguration{}, cnt)
    53  
    54  	ds = definition.New(definition.ContentTypeEthOracle)
    55  	assert.NotNil(t, ds)
    56  	cnt = ds.Content()
    57  	assert.IsType(t, ethcallcommon.Spec{}, cnt)
    58  
    59  	ds = definition.New(definition.ContentTypeInternalTimeTermination)
    60  	assert.NotNil(t, ds)
    61  	cnt = ds.Content()
    62  	assert.IsType(t, vegatime.SpecConfiguration{}, cnt)
    63  }
    64  
    65  func TestDefinitionIntoProto(t *testing.T) {
    66  	t.Run("empty", func(t *testing.T) {
    67  		ds := &definition.Definition{}
    68  		protoDs := ds.IntoProto()
    69  		assert.IsType(t, &vegapb.DataSourceDefinition{}, protoDs)
    70  		assert.Nil(t, protoDs.SourceType)
    71  	})
    72  
    73  	t.Run("external dataSourceDefinition", func(t *testing.T) {
    74  		t.Run("non-empty but no content", func(t *testing.T) {
    75  			ds := &definition.Definition{}
    76  			protoDs := ds.IntoProto()
    77  			assert.IsType(t, &vegapb.DataSourceDefinition{}, protoDs)
    78  			assert.Nil(t, protoDs.SourceType)
    79  		})
    80  
    81  		t.Run("oracle", func(t *testing.T) {
    82  			ds := definition.NewWith(
    83  				&signedoracle.SpecConfiguration{
    84  					Signers: []*common.Signer{
    85  						{
    86  							Signer: &common.SignerETHAddress{
    87  								ETHAddress: &common.ETHAddress{
    88  									Address: "test-eth-address-0",
    89  								},
    90  							},
    91  						},
    92  						{
    93  							Signer: &common.SignerETHAddress{
    94  								ETHAddress: &common.ETHAddress{
    95  									Address: "test-eth-address-1",
    96  								},
    97  							},
    98  						},
    99  					},
   100  					Filters: []*common.SpecFilter{
   101  						{
   102  							Key: &common.SpecPropertyKey{
   103  								Name: "test-property-key-name-0",
   104  								Type: common.SpecPropertyKeyType(0),
   105  							},
   106  							Conditions: []*common.SpecCondition{
   107  								{
   108  									Operator: common.SpecConditionOperator(0),
   109  									Value:    "12",
   110  								},
   111  							},
   112  						},
   113  					},
   114  				},
   115  			)
   116  
   117  			dsProto := ds.IntoProto()
   118  			assert.NotNil(t, dsProto.SourceType)
   119  			assert.IsType(t, &vegapb.DataSourceDefinition_External{}, dsProto.SourceType)
   120  			o := dsProto.GetExternal().GetOracle()
   121  			assert.NotNil(t, o)
   122  			assert.IsType(t, &vegapb.DataSourceSpecConfiguration{}, o)
   123  			signers := dsProto.GetSigners()
   124  			assert.Equal(t, 2, len(signers))
   125  			assert.IsType(t, &datapb.Signer_EthAddress{}, signers[0].Signer)
   126  			assert.Equal(t, "test-eth-address-0", signers[0].GetEthAddress().Address)
   127  			assert.IsType(t, &datapb.Signer_EthAddress{}, signers[1].Signer)
   128  			assert.Equal(t, "test-eth-address-1", signers[1].GetEthAddress().Address)
   129  			filters := dsProto.GetFilters()
   130  			assert.Equal(t, 1, len(filters))
   131  			assert.IsType(t, &datapb.Filter{}, filters[0])
   132  			assert.IsType(t, &datapb.PropertyKey{}, filters[0].Key)
   133  			assert.Equal(t, "test-property-key-name-0", filters[0].Key.Name)
   134  			assert.Equal(t, datapb.PropertyKey_Type(0), filters[0].Key.Type)
   135  			assert.Equal(t, 1, len(filters[0].Conditions))
   136  			assert.IsType(t, &datapb.Condition{}, filters[0].Conditions[0])
   137  			assert.Equal(t, datapb.Condition_OPERATOR_UNSPECIFIED, filters[0].Conditions[0].Operator)
   138  			assert.Equal(t, "12", filters[0].Conditions[0].Value)
   139  		})
   140  
   141  		t.Run("eth oracle", func(t *testing.T) {
   142  			timeNow := time.Now()
   143  			ds := definition.NewWith(
   144  				&ethcallcommon.Spec{
   145  					Address: "some-eth-address",
   146  					AbiJson: []byte(`
   147  [
   148  	{"inputs":
   149  		[
   150  			{"internalType":"uint256","name":"input","type":"uint256"}
   151  		],
   152  		"name":"get_uint256",
   153  		"outputs":
   154  			[
   155  				{"internalType":"uint256","name":"","type":"uint256"}
   156  			],
   157  		"stateMutability":"pure",
   158  		"type":"function"
   159  	}
   160  ]
   161  `),
   162  					Method: "method",
   163  					Trigger: &ethcallcommon.TimeTrigger{
   164  						Initial: uint64(timeNow.UnixNano()),
   165  						Every:   2,
   166  						Until:   uint64(timeNow.UnixNano()),
   167  					},
   168  					RequiredConfirmations: 256,
   169  					Filters: []*common.SpecFilter{
   170  						{
   171  							Key: &common.SpecPropertyKey{
   172  								Name: "test-key-name-0",
   173  								Type: common.SpecPropertyKeyType(5),
   174  							},
   175  							Conditions: []*common.SpecCondition{
   176  								{
   177  									Operator: common.SpecConditionOperator(0),
   178  									Value:    "12",
   179  								},
   180  							},
   181  						},
   182  					},
   183  				})
   184  			dsProto := ds.IntoProto()
   185  			assert.NotNil(t, dsProto.SourceType)
   186  			assert.IsType(t, &vegapb.DataSourceDefinition_External{}, dsProto.SourceType)
   187  			eo := dsProto.GetExternal().GetEthOracle()
   188  			assert.NotNil(t, eo)
   189  			assert.IsType(t, &vegapb.EthCallSpec{}, eo)
   190  			assert.Equal(t, "some-eth-address", eo.Address)
   191  			assert.IsType(t, "", eo.Abi)
   192  			assert.Equal(t, "method", eo.Method)
   193  			filters := eo.GetFilters()
   194  			assert.Equal(t, 1, len(filters))
   195  			assert.IsType(t, &datapb.PropertyKey{}, filters[0].Key)
   196  			assert.Equal(t, "test-key-name-0", filters[0].Key.Name)
   197  			assert.Equal(t, datapb.PropertyKey_Type(5), filters[0].Key.Type)
   198  			assert.Equal(t, 1, len(filters[0].Conditions))
   199  			assert.IsType(t, &datapb.Condition{}, filters[0].Conditions[0])
   200  			assert.Equal(t, datapb.Condition_OPERATOR_UNSPECIFIED, filters[0].Conditions[0].Operator)
   201  			assert.Equal(t, "12", filters[0].Conditions[0].Value)
   202  			assert.IsType(t, &vegapb.EthCallTrigger{}, eo.Trigger)
   203  			assert.Equal(t, uint64(2), eo.Trigger.GetTimeTrigger().GetEvery())
   204  			assert.Equal(t, uint64(timeNow.UnixNano()), eo.Trigger.GetTimeTrigger().GetInitial())
   205  			assert.Equal(t, uint64(timeNow.UnixNano()), eo.Trigger.GetTimeTrigger().GetUntil())
   206  			assert.Equal(t, uint64(256), eo.RequiredConfirmations)
   207  		})
   208  	})
   209  
   210  	t.Run("internal datasource Definition", func(t *testing.T) {
   211  		t.Run("time termination", func(t *testing.T) {
   212  			ds := definition.NewWith(
   213  				&vegatime.SpecConfiguration{
   214  					Conditions: []*common.SpecCondition{
   215  						{
   216  							Operator: common.SpecConditionOperator(0),
   217  							Value:    "12",
   218  						},
   219  					},
   220  				},
   221  			)
   222  
   223  			dsProto := ds.IntoProto()
   224  			assert.NotNil(t, dsProto.SourceType)
   225  			assert.IsType(t, &vegapb.DataSourceDefinition_Internal{}, dsProto.SourceType)
   226  			cond := dsProto.GetInternal().GetTime()
   227  			assert.NotNil(t, cond)
   228  			assert.IsType(t, &vegapb.DataSourceSpecConfigurationTime{}, cond)
   229  			assert.Equal(t, 1, len(cond.Conditions))
   230  			assert.IsType(t, &datapb.Condition{}, cond.Conditions[0])
   231  			assert.Equal(t, datapb.Condition_OPERATOR_UNSPECIFIED, cond.Conditions[0].Operator)
   232  			assert.Equal(t, "12", cond.Conditions[0].Value)
   233  		})
   234  
   235  		t.Run("time trigger termination", func(t *testing.T) {
   236  			timeNow := time.Now()
   237  			ds := definition.NewWith(
   238  				&timetrigger.SpecConfiguration{
   239  					Conditions: []*common.SpecCondition{
   240  						{
   241  							Operator: common.SpecConditionOperator(0),
   242  							Value:    "12",
   243  						},
   244  						{
   245  							Operator: common.SpecConditionOperator(0),
   246  							Value:    "17",
   247  						},
   248  					},
   249  					Triggers: common.InternalTimeTriggers{
   250  						{
   251  							Initial: &timeNow,
   252  							Every:   int64(15),
   253  						},
   254  					},
   255  				},
   256  			)
   257  
   258  			dsProto := ds.IntoProto()
   259  			assert.NotNil(t, dsProto.SourceType)
   260  			assert.IsType(t, &vegapb.DataSourceDefinition_Internal{}, dsProto.SourceType)
   261  			cond := dsProto.GetInternal().GetTimeTrigger()
   262  			assert.NotNil(t, cond)
   263  			assert.IsType(t, &vegapb.DataSourceSpecConfigurationTimeTrigger{}, cond)
   264  			assert.Equal(t, 2, len(cond.Conditions))
   265  			assert.IsType(t, &datapb.Condition{}, cond.Conditions[0])
   266  			assert.Equal(t, datapb.Condition_OPERATOR_UNSPECIFIED, cond.Conditions[0].Operator)
   267  			assert.Equal(t, "12", cond.Conditions[0].Value)
   268  			assert.IsType(t, &datapb.Condition{}, cond.Conditions[1])
   269  			assert.Equal(t, datapb.Condition_OPERATOR_UNSPECIFIED, cond.Conditions[1].Operator)
   270  			assert.Equal(t, "17", cond.Conditions[1].Value)
   271  			assert.Equal(t, timeNow.Unix(), *cond.Triggers[0].Initial)
   272  			assert.Equal(t, int64(15), cond.Triggers[0].Every)
   273  		})
   274  	})
   275  }
   276  
   277  func TestContent(t *testing.T) {
   278  	t.Run("testContent", func(t *testing.T) {
   279  		t.Run("non-empty content with time termination source", func(t *testing.T) {
   280  			d := definition.NewWith(vegatime.SpecConfiguration{
   281  				Conditions: []*common.SpecCondition{
   282  					{
   283  						Operator: datapb.Condition_OPERATOR_EQUALS,
   284  						Value:    "ext-test-value-0",
   285  					},
   286  					{
   287  						Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   288  						Value:    "ext-test-value-1",
   289  					},
   290  				},
   291  			})
   292  
   293  			c := d.Content()
   294  			assert.NotNil(t, c)
   295  			tp, ok := c.(vegatime.SpecConfiguration)
   296  			assert.True(t, ok)
   297  			assert.Equal(t, 2, len(tp.Conditions))
   298  			assert.Equal(t, "ext-test-value-0", tp.Conditions[0].Value)
   299  			assert.Equal(t, "ext-test-value-1", tp.Conditions[1].Value)
   300  		})
   301  
   302  		t.Run("non-empty content with ethereum oracle source", func(t *testing.T) {
   303  			timeNow := time.Now()
   304  			d := definition.NewWith(ethcallcommon.Spec{
   305  				Address: "some-eth-address",
   306  				AbiJson: []byte("abi-json-test"),
   307  				Method:  "method",
   308  				Trigger: &ethcallcommon.TimeTrigger{
   309  					Initial: uint64(timeNow.UnixNano()),
   310  				},
   311  				RequiredConfirmations: 256,
   312  				Filters: []*common.SpecFilter{
   313  					{
   314  						Key: &common.SpecPropertyKey{
   315  							Name: "test-key-name-0",
   316  							Type: common.SpecPropertyKeyType(5),
   317  						},
   318  					},
   319  				},
   320  			})
   321  
   322  			content := d.Content()
   323  			assert.NotNil(t, content)
   324  			assert.IsType(t, ethcallcommon.Spec{}, content)
   325  			c, ok := content.(ethcallcommon.Spec)
   326  			assert.True(t, ok)
   327  			assert.Equal(t, "some-eth-address", c.Address)
   328  			assert.Equal(t, []byte("abi-json-test"), c.AbiJson)
   329  			assert.Equal(t, "method", c.Method)
   330  			filters := c.Filters
   331  			assert.Equal(t, 1, len(filters))
   332  			assert.IsType(t, &common.SpecPropertyKey{}, filters[0].Key)
   333  			assert.Equal(t, "test-key-name-0", filters[0].Key.Name)
   334  			assert.Equal(t, datapb.PropertyKey_Type(5), filters[0].Key.Type)
   335  			assert.IsType(t, &ethcallcommon.TimeTrigger{}, c.Trigger)
   336  			assert.Equal(t, uint64(256), c.RequiredConfirmations)
   337  		})
   338  
   339  		t.Run("non-empty content with oracle", func(t *testing.T) {
   340  			d := definition.NewWith(signedoracle.SpecConfiguration{
   341  				Signers: []*common.Signer{
   342  					common.CreateSignerFromString("0xSOMEKEYX", common.SignerTypePubKey),
   343  					common.CreateSignerFromString("0xSOMEKEYY", common.SignerTypePubKey),
   344  				},
   345  			})
   346  
   347  			c := d.Content()
   348  			assert.NotNil(t, c)
   349  			tp, ok := c.(signedoracle.SpecConfiguration)
   350  			assert.True(t, ok)
   351  			assert.Equal(t, 0, len(tp.Filters))
   352  			assert.Equal(t, 2, len(tp.Signers))
   353  			assert.Equal(t, "0xSOMEKEYX", tp.Signers[0].GetSignerPubKey().Key)
   354  			assert.Equal(t, "0xSOMEKEYY", tp.Signers[1].GetSignerPubKey().Key)
   355  		})
   356  	})
   357  }
   358  
   359  func TestGetFilters(t *testing.T) {
   360  	t.Run("testGetFiltersExternal", func(t *testing.T) {
   361  		t.Run("NotEmpty Oracle", func(t *testing.T) {
   362  			dsd := definition.NewWith(signedoracle.SpecConfiguration{
   363  				Signers: []*common.Signer{
   364  					common.CreateSignerFromString("0xSOMEKEYX", common.SignerTypePubKey),
   365  					common.CreateSignerFromString("0xSOMEKEYY", common.SignerTypePubKey),
   366  				},
   367  				Filters: []*common.SpecFilter{
   368  					{
   369  						Key: &common.SpecPropertyKey{
   370  							Name: "prices.ETH.value",
   371  							Type: datapb.PropertyKey_TYPE_INTEGER,
   372  						},
   373  						Conditions: []*common.SpecCondition{
   374  							{
   375  								Operator: datapb.Condition_OPERATOR_EQUALS,
   376  								Value:    "ext-test-value-1",
   377  							},
   378  							{
   379  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   380  								Value:    "ext-test-value-2",
   381  							},
   382  						},
   383  					},
   384  					{
   385  						Key: &common.SpecPropertyKey{
   386  							Name: "key-name-string",
   387  							Type: datapb.PropertyKey_TYPE_STRING,
   388  						},
   389  						Conditions: []*common.SpecCondition{
   390  							{
   391  								Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   392  								Value:    "ext-test-value-3",
   393  							},
   394  							{
   395  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   396  								Value:    "ext-test-value-4",
   397  							},
   398  						},
   399  					},
   400  				},
   401  			})
   402  
   403  			filters := dsd.GetFilters()
   404  			assert.Equal(t, 2, len(filters))
   405  			assert.Equal(t, "prices.ETH.value", filters[0].Key.Name)
   406  			assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
   407  			assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
   408  			assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
   409  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   410  			assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
   411  
   412  			assert.Equal(t, "key-name-string", filters[1].Key.Name)
   413  			assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
   414  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
   415  			assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
   416  
   417  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
   418  			assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
   419  		})
   420  
   421  		t.Run("NotEmpty EthOracle", func(t *testing.T) {
   422  			dsd := definition.NewWith(ethcallcommon.Spec{
   423  				Address: "some-eth-address",
   424  				AbiJson: []byte("abi-json-test"),
   425  				Method:  "method",
   426  				Trigger: &ethcallcommon.TimeTrigger{
   427  					Initial: uint64(time.Now().UnixNano()),
   428  				},
   429  				RequiredConfirmations: 256,
   430  				Filters: []*common.SpecFilter{
   431  					{
   432  						Key: &common.SpecPropertyKey{
   433  							Name: "test-key-name-0",
   434  							Type: common.SpecPropertyKeyType(3),
   435  						},
   436  					},
   437  				},
   438  			})
   439  
   440  			filters := dsd.GetFilters()
   441  			assert.Equal(t, 1, len(filters))
   442  			assert.Equal(t, "test-key-name-0", filters[0].Key.Name)
   443  			assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[0].Key.Type)
   444  			assert.Equal(t, 0, len(filters[0].Conditions))
   445  		})
   446  	})
   447  
   448  	t.Run("testGetFiltersInternal", func(t *testing.T) {
   449  		t.Run("NotEmpty", func(t *testing.T) {
   450  			dsd := definition.NewWith(
   451  				vegatime.SpecConfiguration{
   452  					Conditions: []*common.SpecCondition{
   453  						{
   454  							Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   455  							Value:    "int-test-value-1",
   456  						},
   457  						{
   458  							Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   459  							Value:    "int-test-value-2",
   460  						},
   461  					},
   462  				})
   463  
   464  			filters := dsd.GetFilters()
   465  			// Ensure only a single filter has been created, that holds all given conditions
   466  			assert.Equal(t, 1, len(filters))
   467  
   468  			assert.Equal(t, "vegaprotocol.builtin.timestamp", filters[0].Key.Name)
   469  			assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   470  
   471  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   472  			assert.Equal(t, "int-test-value-1", filters[0].Conditions[0].Value)
   473  		})
   474  	})
   475  }
   476  
   477  func TestUpdateFilters(t *testing.T) {
   478  	t.Run("testUpdateFiltersExternal", func(t *testing.T) {
   479  		t.Run("Empty", func(t *testing.T) {
   480  			dsd := &vegapb.DataSourceDefinition{
   481  				SourceType: &vegapb.DataSourceDefinition_External{},
   482  			}
   483  
   484  			dsdt, err := definition.FromProto(dsd, nil)
   485  			assert.NoError(t, err)
   486  
   487  			err = dsdt.(*definition.Definition).UpdateFilters([]*common.SpecFilter{})
   488  			assert.NoError(t, err)
   489  			filters := dsdt.(*definition.Definition).GetFilters()
   490  			assert.Equal(t, 0, len(filters))
   491  
   492  			dsd = &vegapb.DataSourceDefinition{
   493  				SourceType: &vegapb.DataSourceDefinition_External{
   494  					External: &vegapb.DataSourceDefinitionExternal{
   495  						SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
   496  							Oracle: nil,
   497  						},
   498  					},
   499  				},
   500  			}
   501  
   502  			dsdt, err = definition.FromProto(dsd, nil)
   503  			assert.NoError(t, err)
   504  			filters = dsdt.(*definition.Definition).GetFilters()
   505  			assert.Equal(t, 0, len(filters))
   506  		})
   507  
   508  		t.Run("NotEmpty Oracle", func(t *testing.T) {
   509  			dsd := &vegapb.DataSourceDefinition{
   510  				SourceType: &vegapb.DataSourceDefinition_External{
   511  					External: &vegapb.DataSourceDefinitionExternal{
   512  						SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
   513  							Oracle: &vegapb.DataSourceSpecConfiguration{
   514  								Signers: common.SignersIntoProto(
   515  									[]*common.Signer{
   516  										common.CreateSignerFromString("0xSOMEKEYX", common.SignerTypePubKey),
   517  										common.CreateSignerFromString("0xSOMEKEYY", common.SignerTypePubKey),
   518  									}),
   519  							},
   520  						},
   521  					},
   522  				},
   523  			}
   524  
   525  			dsdt, err := definition.FromProto(dsd, nil)
   526  			assert.NoError(t, err)
   527  			dst := definition.NewWith(dsdt)
   528  			err = dst.UpdateFilters(
   529  				[]*common.SpecFilter{
   530  					{
   531  						Key: &common.SpecPropertyKey{
   532  							Name: "prices.ETH.value",
   533  							Type: datapb.PropertyKey_TYPE_INTEGER,
   534  						},
   535  						Conditions: []*common.SpecCondition{
   536  							{
   537  								Operator: datapb.Condition_OPERATOR_EQUALS,
   538  								Value:    "ext-test-value-1",
   539  							},
   540  							{
   541  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   542  								Value:    "ext-test-value-2",
   543  							},
   544  						},
   545  					},
   546  					{
   547  						Key: &common.SpecPropertyKey{
   548  							Name: "key-name-string",
   549  							Type: datapb.PropertyKey_TYPE_STRING,
   550  						},
   551  						Conditions: []*common.SpecCondition{
   552  							{
   553  								Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   554  								Value:    "ext-test-value-3",
   555  							},
   556  							{
   557  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   558  								Value:    "ext-test-value-4",
   559  							},
   560  						},
   561  					},
   562  				},
   563  			)
   564  
   565  			assert.NoError(t, err)
   566  
   567  			filters := dst.GetFilters()
   568  			require.Equal(t, 2, len(filters))
   569  			assert.Equal(t, "prices.ETH.value", filters[0].Key.Name)
   570  			assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
   571  			assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
   572  			assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
   573  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   574  			assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
   575  
   576  			assert.Equal(t, "key-name-string", filters[1].Key.Name)
   577  			assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
   578  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
   579  			assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
   580  
   581  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
   582  			assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
   583  		})
   584  
   585  		t.Run("NotEmpty EthOracle", func(t *testing.T) {
   586  			timeNow := uint64(time.Now().UnixNano())
   587  			dsd := &vegapb.DataSourceDefinition{
   588  				SourceType: &vegapb.DataSourceDefinition_External{
   589  					External: &vegapb.DataSourceDefinitionExternal{
   590  						SourceType: &vegapb.DataSourceDefinitionExternal_EthOracle{
   591  							EthOracle: &vegapb.EthCallSpec{
   592  								Address: "some-eth-address",
   593  								Trigger: &vegapb.EthCallTrigger{
   594  									Trigger: &vegapb.EthCallTrigger_TimeTrigger{
   595  										TimeTrigger: &vegapb.EthTimeTrigger{
   596  											Initial: &timeNow,
   597  										},
   598  									},
   599  								},
   600  
   601  								Filters: []*datapb.Filter{
   602  									{
   603  										Key: &datapb.PropertyKey{
   604  											Name: "test-key-name-0",
   605  											Type: common.SpecPropertyKeyType(5),
   606  										},
   607  									},
   608  								},
   609  							},
   610  						},
   611  					},
   612  				},
   613  			}
   614  
   615  			dsdt, err := definition.FromProto(dsd, nil)
   616  			assert.NoError(t, err)
   617  			dst := definition.NewWith(dsdt)
   618  			err = dst.UpdateFilters(
   619  				[]*common.SpecFilter{
   620  					{
   621  						Key: &common.SpecPropertyKey{
   622  							Name: "eth-spec-new-property-key",
   623  							Type: datapb.PropertyKey_TYPE_INTEGER,
   624  						},
   625  						Conditions: []*common.SpecCondition{
   626  							{
   627  								Operator: datapb.Condition_OPERATOR_EQUALS,
   628  								Value:    "ext-test-value-1",
   629  							},
   630  							{
   631  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   632  								Value:    "ext-test-value-2",
   633  							},
   634  						},
   635  					},
   636  					{
   637  						Key: &common.SpecPropertyKey{
   638  							Name: "key-name-string",
   639  							Type: datapb.PropertyKey_TYPE_STRING,
   640  						},
   641  						Conditions: []*common.SpecCondition{
   642  							{
   643  								Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   644  								Value:    "ext-test-value-3",
   645  							},
   646  							{
   647  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   648  								Value:    "ext-test-value-4",
   649  							},
   650  						},
   651  					},
   652  				},
   653  			)
   654  			assert.NoError(t, err)
   655  
   656  			filters := dst.GetFilters()
   657  			require.Equal(t, 2, len(filters))
   658  			assert.Equal(t, "eth-spec-new-property-key", filters[0].Key.Name)
   659  			assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
   660  			assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
   661  			assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
   662  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   663  			assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
   664  
   665  			assert.Equal(t, "key-name-string", filters[1].Key.Name)
   666  			assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
   667  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
   668  			assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
   669  
   670  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
   671  			assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
   672  		})
   673  	})
   674  
   675  	t.Run("testUpdateFiltersInternal", func(t *testing.T) {
   676  		t.Run("NotEmpty", func(t *testing.T) {
   677  			dsd := &vegapb.DataSourceDefinition{
   678  				SourceType: &vegapb.DataSourceDefinition_Internal{
   679  					Internal: &vegapb.DataSourceDefinitionInternal{
   680  						SourceType: &vegapb.DataSourceDefinitionInternal_Time{
   681  							Time: &vegapb.DataSourceSpecConfigurationTime{},
   682  						},
   683  					},
   684  				},
   685  			}
   686  
   687  			dsdt, err := definition.FromProto(dsd, nil)
   688  			assert.NoError(t, err)
   689  			dst := definition.NewWith(dsdt)
   690  			err = dst.UpdateFilters(
   691  				[]*common.SpecFilter{
   692  					{
   693  						Conditions: []*common.SpecCondition{
   694  							{
   695  								Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   696  								Value:    "int-test-value-1",
   697  							},
   698  							{
   699  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   700  								Value:    "int-test-value-2",
   701  							},
   702  						},
   703  					},
   704  				},
   705  			)
   706  			assert.NoError(t, err)
   707  			filters := dst.GetFilters()
   708  			// Ensure only a single filter has been created, that holds all given conditions
   709  			assert.Equal(t, 1, len(filters))
   710  
   711  			assert.Equal(t, "vegaprotocol.builtin.timestamp", filters[0].Key.Name)
   712  			assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   713  
   714  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   715  			assert.Equal(t, "int-test-value-1", filters[0].Conditions[0].Value)
   716  		})
   717  
   718  		t.Run("NotEmpty timetrigger", func(t *testing.T) {
   719  			dsd := &vegapb.DataSourceDefinition{
   720  				SourceType: &vegapb.DataSourceDefinition_Internal{
   721  					Internal: &vegapb.DataSourceDefinitionInternal{
   722  						SourceType: &vegapb.DataSourceDefinitionInternal_TimeTrigger{
   723  							TimeTrigger: &vegapb.DataSourceSpecConfigurationTimeTrigger{},
   724  						},
   725  					},
   726  				},
   727  			}
   728  
   729  			tn := time.Now()
   730  			dsdt, err := definition.FromProto(dsd, &tn)
   731  			assert.NoError(t, err)
   732  			dst := definition.NewWith(dsdt)
   733  			err = dst.UpdateFilters(
   734  				[]*common.SpecFilter{
   735  					{
   736  						Conditions: []*common.SpecCondition{
   737  							{
   738  								Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   739  								Value:    "int-test-value-1",
   740  							},
   741  							{
   742  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   743  								Value:    "int-test-value-2",
   744  							},
   745  						},
   746  					},
   747  				},
   748  			)
   749  			assert.NoError(t, err)
   750  			filters := dst.GetFilters()
   751  			// Ensure only a single filter has been created, that holds all given conditions
   752  			assert.Equal(t, 1, len(filters))
   753  
   754  			assert.Equal(t, "vegaprotocol.builtin.timetrigger", filters[0].Key.Name)
   755  			assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   756  
   757  			assert.Equal(t, 2, len(filters[0].Conditions))
   758  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
   759  			assert.Equal(t, "int-test-value-1", filters[0].Conditions[0].Value)
   760  			assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
   761  			assert.Equal(t, "int-test-value-2", filters[0].Conditions[1].Value)
   762  		})
   763  
   764  		t.Run("Empty", func(t *testing.T) {
   765  			dsd := &vegapb.DataSourceDefinition{
   766  				SourceType: &vegapb.DataSourceDefinition_Internal{},
   767  			}
   768  
   769  			dsdt, err := definition.FromProto(dsd, nil)
   770  			assert.NoError(t, err)
   771  			dst := definition.NewWith(dsdt)
   772  			err = dst.UpdateFilters(
   773  				[]*common.SpecFilter{},
   774  			)
   775  
   776  			assert.NoError(t, err)
   777  			filters := dsdt.(*definition.Definition).GetFilters()
   778  			assert.Equal(t, 0, len(filters))
   779  		})
   780  	})
   781  }
   782  
   783  func TestGetSigners(t *testing.T) {
   784  	t.Run("empty signers", func(t *testing.T) {
   785  		ds := definition.NewWith(signedoracle.SpecConfiguration{})
   786  
   787  		signers := ds.GetSigners()
   788  		assert.Equal(t, 0, len(signers))
   789  	})
   790  
   791  	t.Run("non-empty list but empty signers", func(t *testing.T) {
   792  		ds := definition.NewWith(signedoracle.SpecConfiguration{
   793  			Signers: []*common.Signer{
   794  				{},
   795  				{},
   796  			},
   797  		})
   798  
   799  		signers := ds.GetSigners()
   800  		assert.Equal(t, 2, len(signers))
   801  	})
   802  
   803  	t.Run("non-empty signers", func(t *testing.T) {
   804  		ds := definition.NewWith(signedoracle.SpecConfiguration{
   805  			Signers: []*common.Signer{
   806  				{
   807  					common.CreateSignerFromString("0xTESTSIGN", common.SignerTypePubKey).Signer,
   808  				},
   809  			},
   810  		})
   811  
   812  		signers := ds.GetSigners()
   813  		assert.Equal(t, 1, len(signers))
   814  		assert.IsType(t, &common.Signer{}, signers[0])
   815  		assert.IsType(t, &common.SignerPubKey{}, signers[0].Signer)
   816  		assert.Equal(t, "0xTESTSIGN", signers[0].GetSignerPubKey().Key)
   817  	})
   818  }
   819  
   820  func TestGetDataSourceSpecConfiguration(t *testing.T) {
   821  	ds := definition.NewWith(
   822  		signedoracle.SpecConfiguration{
   823  			Signers: []*common.Signer{
   824  				{
   825  					common.CreateSignerFromString("0xTESTSIGN", common.SignerTypePubKey).Signer,
   826  				},
   827  			},
   828  		})
   829  
   830  	spec := ds.GetSpecConfiguration()
   831  	assert.NotNil(t, spec)
   832  	assert.IsType(t, signedoracle.SpecConfiguration{}, spec)
   833  
   834  	assert.Equal(t, 1, len(spec.(signedoracle.SpecConfiguration).Signers))
   835  	assert.Equal(t, "0xTESTSIGN", spec.(signedoracle.SpecConfiguration).Signers[0].GetSignerPubKey().Key)
   836  }
   837  
   838  func TestGetEthCallSpec(t *testing.T) {
   839  	ds := definition.NewWith(ethcallcommon.Spec{
   840  		Address: "some-eth-address",
   841  		AbiJson: []byte("abi-json-test"),
   842  		Method:  "method",
   843  		Trigger: &ethcallcommon.TimeTrigger{
   844  			Initial: uint64(time.Now().UnixNano()),
   845  		},
   846  		RequiredConfirmations: 256,
   847  		Filters: []*common.SpecFilter{
   848  			{
   849  				Key: &common.SpecPropertyKey{
   850  					Name: "test-key-name-0",
   851  					Type: common.SpecPropertyKeyType(3),
   852  				},
   853  			},
   854  		},
   855  	})
   856  
   857  	dsSpec := ds.GetEthCallSpec()
   858  	assert.NotNil(t, dsSpec)
   859  	assert.IsType(t, ethcallcommon.Spec{}, dsSpec)
   860  	assert.Equal(t, "some-eth-address", dsSpec.Address)
   861  	assert.Equal(t, []byte("abi-json-test"), dsSpec.AbiJson)
   862  	assert.Equal(t, "method", dsSpec.Method)
   863  	assert.Equal(t, uint64(256), dsSpec.RequiredConfirmations)
   864  	filters := dsSpec.Filters
   865  	assert.Equal(t, 1, len(filters))
   866  	assert.Equal(t, "test-key-name-0", filters[0].Key.Name)
   867  	assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[0].Key.Type)
   868  	assert.Equal(t, 0, len(filters[0].Conditions))
   869  }
   870  
   871  func TestGetDataSourceSpecConfigurationTime(t *testing.T) {
   872  	ds := definition.NewWith(vegatime.SpecConfiguration{
   873  		Conditions: []*common.SpecCondition{
   874  			{
   875  				Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   876  				Value:    "1",
   877  			},
   878  		},
   879  	})
   880  
   881  	spec := ds.GetSpecConfiguration()
   882  	assert.NotNil(t, spec)
   883  	assert.IsType(t, vegatime.SpecConfiguration{}, spec)
   884  
   885  	assert.NotNil(t, spec.(vegatime.SpecConfiguration).Conditions)
   886  	assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, spec.(vegatime.SpecConfiguration).Conditions[0].Operator)
   887  	assert.Equal(t, "1", spec.(vegatime.SpecConfiguration).Conditions[0].Value)
   888  }
   889  
   890  func TestGetDataSourceSpecConfigurationTimeTrigger(t *testing.T) {
   891  	ds := definition.NewWith(timetrigger.SpecConfiguration{
   892  		Conditions: []*common.SpecCondition{
   893  			{
   894  				Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   895  				Value:    "1",
   896  			},
   897  		},
   898  		Triggers: common.InternalTimeTriggers{
   899  			{},
   900  		},
   901  	})
   902  
   903  	spec := ds.GetSpecConfiguration()
   904  	assert.NotNil(t, spec)
   905  	assert.IsType(t, timetrigger.SpecConfiguration{}, spec)
   906  
   907  	assert.NotNil(t, spec.(timetrigger.SpecConfiguration).Conditions)
   908  	assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, spec.(timetrigger.SpecConfiguration).Conditions[0].Operator)
   909  	assert.Equal(t, "1", spec.(timetrigger.SpecConfiguration).Conditions[0].Value)
   910  
   911  	assert.IsType(t, common.InternalTimeTriggers{}, spec.(timetrigger.SpecConfiguration).Triggers)
   912  }
   913  
   914  func TestIsExternal(t *testing.T) {
   915  	dsDef := definition.NewWith(signedoracle.SpecConfiguration{})
   916  
   917  	res, err := dsDef.IsExternal()
   918  	assert.NoError(t, err)
   919  	assert.True(t, res)
   920  
   921  	dsDef = definition.NewWith(ethcallcommon.Spec{})
   922  
   923  	res, err = dsDef.IsExternal()
   924  	assert.NoError(t, err)
   925  	assert.True(t, res)
   926  
   927  	dsDef = definition.NewWith(vegatime.SpecConfiguration{
   928  		Conditions: []*common.SpecCondition{},
   929  	})
   930  
   931  	res, err = dsDef.IsExternal()
   932  	assert.NoError(t, err)
   933  	assert.False(t, res)
   934  
   935  	dsDef = definition.NewWith(timetrigger.SpecConfiguration{
   936  		Conditions: []*common.SpecCondition{},
   937  		Triggers:   common.InternalTimeTriggers{},
   938  	})
   939  
   940  	res, err = dsDef.IsExternal()
   941  	assert.NoError(t, err)
   942  	assert.False(t, res)
   943  
   944  	dsDef = definition.NewWith(nil)
   945  	res, _ = dsDef.IsExternal()
   946  
   947  	assert.Error(t, errors.New("unknown type of data source provided"))
   948  	assert.False(t, res)
   949  }
   950  
   951  func TestSetOracleConfig(t *testing.T) {
   952  	t.Run("non-empty oracle", func(t *testing.T) {
   953  		dsd := definition.NewWith(signedoracle.SpecConfiguration{})
   954  
   955  		udsd := dsd.SetOracleConfig(
   956  			&signedoracle.SpecConfiguration{
   957  				Signers: []*common.Signer{
   958  					common.CreateSignerFromString("0xSOMEKEYX", common.SignerTypePubKey),
   959  					common.CreateSignerFromString("0xSOMEKEYY", common.SignerTypePubKey),
   960  				},
   961  				Filters: []*common.SpecFilter{
   962  					{
   963  						Key: &common.SpecPropertyKey{
   964  							Name: "prices.ETH.value",
   965  							Type: datapb.PropertyKey_TYPE_INTEGER,
   966  						},
   967  						Conditions: []*common.SpecCondition{
   968  							{
   969  								Operator: datapb.Condition_OPERATOR_EQUALS,
   970  								Value:    "ext-test-value-1",
   971  							},
   972  							{
   973  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   974  								Value:    "ext-test-value-2",
   975  							},
   976  						},
   977  					},
   978  					{
   979  						Key: &common.SpecPropertyKey{
   980  							Name: "key-name-string",
   981  							Type: datapb.PropertyKey_TYPE_STRING,
   982  						},
   983  						Conditions: []*common.SpecCondition{
   984  							{
   985  								Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
   986  								Value:    "ext-test-value-3",
   987  							},
   988  							{
   989  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   990  								Value:    "ext-test-value-4",
   991  							},
   992  						},
   993  					},
   994  				},
   995  			},
   996  		)
   997  
   998  		signers := udsd.GetSigners()
   999  		assert.Equal(t, 2, len(signers))
  1000  		assert.Equal(t, "0xSOMEKEYX", signers[0].GetSignerPubKey().Key)
  1001  		assert.Equal(t, "0xSOMEKEYY", signers[1].GetSignerPubKey().Key)
  1002  		filters := udsd.GetFilters()
  1003  		assert.Equal(t, 2, len(filters))
  1004  		assert.Equal(t, "prices.ETH.value", filters[0].Key.Name)
  1005  		assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
  1006  		assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
  1007  		assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
  1008  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
  1009  		assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
  1010  
  1011  		assert.Equal(t, "key-name-string", filters[1].Key.Name)
  1012  		assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
  1013  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
  1014  		assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
  1015  
  1016  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
  1017  		assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
  1018  	})
  1019  
  1020  	t.Run("non-empty eth oracle", func(t *testing.T) {
  1021  		dsd := definition.NewWith(ethcallcommon.Spec{})
  1022  
  1023  		udsd := dsd.SetOracleConfig(
  1024  			&ethcallcommon.Spec{
  1025  				Address: "some-eth-address",
  1026  				AbiJson: []byte("abi-json-test"),
  1027  				Method:  "method",
  1028  				Trigger: &ethcallcommon.TimeTrigger{
  1029  					Initial: uint64(time.Now().UnixNano()),
  1030  				},
  1031  				RequiredConfirmations: 256,
  1032  				Filters: []*common.SpecFilter{
  1033  					{
  1034  						Key: &common.SpecPropertyKey{
  1035  							Name: "prices.ETH.value",
  1036  							Type: datapb.PropertyKey_TYPE_INTEGER,
  1037  						},
  1038  						Conditions: []*common.SpecCondition{
  1039  							{
  1040  								Operator: datapb.Condition_OPERATOR_EQUALS,
  1041  								Value:    "ext-test-value-1",
  1042  							},
  1043  							{
  1044  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1045  								Value:    "ext-test-value-2",
  1046  							},
  1047  						},
  1048  					},
  1049  					{
  1050  						Key: &common.SpecPropertyKey{
  1051  							Name: "key-name-string",
  1052  							Type: datapb.PropertyKey_TYPE_STRING,
  1053  						},
  1054  						Conditions: []*common.SpecCondition{
  1055  							{
  1056  								Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
  1057  								Value:    "ext-test-value-3",
  1058  							},
  1059  							{
  1060  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1061  								Value:    "ext-test-value-4",
  1062  							},
  1063  						},
  1064  					},
  1065  				},
  1066  			},
  1067  		)
  1068  
  1069  		dsSpec := udsd.GetEthCallSpec()
  1070  		assert.NotNil(t, dsSpec)
  1071  		assert.IsType(t, ethcallcommon.Spec{}, dsSpec)
  1072  		assert.Equal(t, "some-eth-address", dsSpec.Address)
  1073  		assert.Equal(t, []byte("abi-json-test"), dsSpec.AbiJson)
  1074  		assert.Equal(t, "method", dsSpec.Method)
  1075  		assert.Equal(t, uint64(256), dsSpec.RequiredConfirmations)
  1076  		filters := udsd.GetFilters()
  1077  		assert.Equal(t, 2, len(filters))
  1078  		assert.Equal(t, "prices.ETH.value", filters[0].Key.Name)
  1079  		assert.Equal(t, datapb.PropertyKey_TYPE_INTEGER, filters[0].Key.Type)
  1080  		assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[0].Operator)
  1081  		assert.Equal(t, "ext-test-value-1", filters[0].Conditions[0].Value)
  1082  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
  1083  		assert.Equal(t, "ext-test-value-2", filters[0].Conditions[1].Value)
  1084  
  1085  		assert.Equal(t, "key-name-string", filters[1].Key.Name)
  1086  		assert.Equal(t, datapb.PropertyKey_TYPE_STRING, filters[1].Key.Type)
  1087  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[1].Conditions[0].Operator)
  1088  		assert.Equal(t, "ext-test-value-3", filters[1].Conditions[0].Value)
  1089  
  1090  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[1].Conditions[1].Operator)
  1091  		assert.Equal(t, "ext-test-value-4", filters[1].Conditions[1].Value)
  1092  	})
  1093  
  1094  	t.Run("try to set oracle config to internal data source", func(t *testing.T) {
  1095  		dsd := definition.NewWith(
  1096  			vegatime.SpecConfiguration{
  1097  				Conditions: []*common.SpecCondition{
  1098  					{
  1099  						Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
  1100  						Value:    "int-test-value-1",
  1101  					},
  1102  					{
  1103  						Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1104  						Value:    "int-test-value-2",
  1105  					},
  1106  				},
  1107  			})
  1108  
  1109  		iudsd := dsd.SetOracleConfig(
  1110  			&signedoracle.SpecConfiguration{
  1111  				Filters: []*common.SpecFilter{
  1112  					{
  1113  						Key: &common.SpecPropertyKey{
  1114  							Name: "prices.ETH.value",
  1115  							Type: datapb.PropertyKey_TYPE_INTEGER,
  1116  						},
  1117  						Conditions: []*common.SpecCondition{
  1118  							{
  1119  								Operator: datapb.Condition_OPERATOR_EQUALS,
  1120  								Value:    "ext-test-value-1",
  1121  							},
  1122  							{
  1123  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1124  								Value:    "ext-test-value-2",
  1125  							},
  1126  						},
  1127  					},
  1128  					{
  1129  						Key: &common.SpecPropertyKey{
  1130  							Name: "key-name-string",
  1131  							Type: datapb.PropertyKey_TYPE_STRING,
  1132  						},
  1133  						Conditions: []*common.SpecCondition{
  1134  							{
  1135  								Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
  1136  								Value:    "ext-test-value-3",
  1137  							},
  1138  							{
  1139  								Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1140  								Value:    "ext-test-value-4",
  1141  							},
  1142  						},
  1143  					},
  1144  				},
  1145  			},
  1146  		)
  1147  
  1148  		filters := iudsd.GetFilters()
  1149  		assert.Equal(t, 1, len(filters))
  1150  
  1151  		assert.Equal(t, "vegaprotocol.builtin.timestamp", filters[0].Key.Name)
  1152  		assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
  1153  
  1154  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
  1155  		assert.Equal(t, "int-test-value-1", filters[0].Conditions[0].Value)
  1156  	})
  1157  }
  1158  
  1159  func TestSetTimeTriggerConditionConfig(t *testing.T) {
  1160  	dsd := definition.NewWith(signedoracle.SpecConfiguration{})
  1161  
  1162  	udsd := dsd.SetTimeTriggerConditionConfig(
  1163  		[]*common.SpecCondition{
  1164  			{
  1165  				Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
  1166  				Value:    "ext-test-value-3",
  1167  			},
  1168  			{
  1169  				Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1170  				Value:    "ext-test-value-4",
  1171  			},
  1172  		},
  1173  	)
  1174  
  1175  	filters := udsd.GetFilters()
  1176  	assert.Equal(t, 0, len(filters))
  1177  
  1178  	t.Run("try to set time config to internal time data source", func(t *testing.T) {
  1179  		dsd := definition.NewWith(
  1180  			vegatime.SpecConfiguration{
  1181  				Conditions: []*common.SpecCondition{
  1182  					{
  1183  						Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
  1184  						Value:    "int-test-value-1",
  1185  					},
  1186  					{
  1187  						Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1188  						Value:    "int-test-value-2",
  1189  					},
  1190  				},
  1191  			})
  1192  
  1193  		iudsd := dsd.SetTimeTriggerConditionConfig(
  1194  			[]*common.SpecCondition{
  1195  				{
  1196  					Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
  1197  					Value:    "int-test-value-3",
  1198  				},
  1199  				{
  1200  					Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1201  					Value:    "int-test-value-4",
  1202  				},
  1203  			},
  1204  		)
  1205  
  1206  		filters := iudsd.GetFilters()
  1207  		assert.Equal(t, 1, len(filters))
  1208  
  1209  		assert.Equal(t, "vegaprotocol.builtin.timestamp", filters[0].Key.Name)
  1210  		assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
  1211  
  1212  		assert.Equal(t, 1, len(filters[0].Conditions))
  1213  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
  1214  		assert.Equal(t, "int-test-value-3", filters[0].Conditions[0].Value)
  1215  	})
  1216  
  1217  	t.Run("try to set time trigger config to internal time data source", func(t *testing.T) {
  1218  		dsd := definition.NewWith(
  1219  			timetrigger.SpecConfiguration{
  1220  				Conditions: []*common.SpecCondition{
  1221  					{
  1222  						Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
  1223  						Value:    "int-test-value-1",
  1224  					},
  1225  					{
  1226  						Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1227  						Value:    "int-test-value-2",
  1228  					},
  1229  				},
  1230  				Triggers: common.InternalTimeTriggers{
  1231  					{},
  1232  				},
  1233  			})
  1234  
  1235  		iudsd := dsd.SetTimeTriggerConditionConfig(
  1236  			[]*common.SpecCondition{
  1237  				{
  1238  					Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL,
  1239  					Value:    "int-test-value-3",
  1240  				},
  1241  				{
  1242  					Operator: datapb.Condition_OPERATOR_GREATER_THAN,
  1243  					Value:    "int-test-value-4",
  1244  				},
  1245  			},
  1246  		)
  1247  
  1248  		filters := iudsd.GetFilters()
  1249  		assert.Equal(t, 1, len(filters))
  1250  
  1251  		assert.Equal(t, "vegaprotocol.builtin.timetrigger", filters[0].Key.Name)
  1252  		assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
  1253  
  1254  		assert.Equal(t, 2, len(filters[0].Conditions))
  1255  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, filters[0].Conditions[0].Operator)
  1256  		assert.Equal(t, "int-test-value-3", filters[0].Conditions[0].Value)
  1257  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[1].Operator)
  1258  		assert.Equal(t, "int-test-value-4", filters[0].Conditions[1].Value)
  1259  	})
  1260  }
  1261  
  1262  func TestType(t *testing.T) {
  1263  	ds := definition.NewWith(&signedoracle.SpecConfiguration{})
  1264  	tp, ext := ds.Type()
  1265  	assert.Equal(t, definition.ContentTypeOracle, tp)
  1266  	assert.True(t, ext)
  1267  
  1268  	ds = definition.NewWith(&ethcallcommon.Spec{})
  1269  	tp, ext = ds.Type()
  1270  	assert.Equal(t, definition.ContentTypeEthOracle, tp)
  1271  	assert.True(t, ext)
  1272  
  1273  	ds = definition.NewWith(&vegatime.SpecConfiguration{})
  1274  	tp, ext = ds.Type()
  1275  	assert.Equal(t, definition.ContentTypeInternalTimeTermination, tp)
  1276  	assert.False(t, ext)
  1277  
  1278  	ds = definition.NewWith(&definition.Definition{})
  1279  	tp, ext = ds.Type()
  1280  	assert.Equal(t, definition.ContentTypeInvalid, tp)
  1281  	assert.False(t, ext)
  1282  }