code.vegaprotocol.io/vega@v0.79.0/core/datasource/external/signedoracle/signedoracle_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 signedoracle_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/datasource"
    22  	"code.vegaprotocol.io/vega/core/datasource/common"
    23  	"code.vegaprotocol.io/vega/core/datasource/external/signedoracle"
    24  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    25  	v1 "code.vegaprotocol.io/vega/protos/vega/data/v1"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestSpecConfigurationIntoProto(t *testing.T) {
    31  	t.Run("non-empty oracle with empty lists", func(t *testing.T) {
    32  		ds := datasource.NewDefinitionWith(signedoracle.SpecConfiguration{})
    33  		protoDs := ds.IntoProto()
    34  		assert.IsType(t, &vegapb.DataSourceDefinition{}, protoDs)
    35  		assert.NotNil(t, protoDs.SourceType)
    36  		ext := protoDs.GetExternal()
    37  		assert.NotNil(t, ext)
    38  		o := ext.GetOracle()
    39  		assert.Equal(t, 0, len(o.Signers))
    40  		assert.Equal(t, 0, len(o.Filters))
    41  	})
    42  
    43  	t.Run("non-empty oracle with data", func(t *testing.T) {
    44  		ds := datasource.NewDefinitionWith(signedoracle.SpecConfiguration{
    45  			Signers: []*common.Signer{
    46  				{},
    47  			},
    48  			Filters: []*common.SpecFilter{
    49  				{
    50  					Key: &common.SpecPropertyKey{
    51  						Name: "test-name",
    52  						Type: common.SpecPropertyKeyType(0),
    53  					},
    54  				},
    55  			},
    56  		})
    57  
    58  		protoDs := ds.IntoProto()
    59  		assert.IsType(t, &vegapb.DataSourceDefinition{}, protoDs)
    60  		assert.NotNil(t, protoDs.SourceType)
    61  		ext := protoDs.GetExternal()
    62  		assert.NotNil(t, ext)
    63  		o := ext.GetOracle()
    64  		assert.Equal(t, 1, len(o.Signers))
    65  		assert.Nil(t, o.Signers[0].Signer)
    66  		assert.Equal(t, 1, len(o.Filters))
    67  		assert.NotNil(t, o.Filters[0].Conditions)
    68  		assert.NotNil(t, o.Filters[0].Key)
    69  	})
    70  }
    71  
    72  func TestSpecConfigurationFromProto(t *testing.T) {
    73  	t.Run("empty", func(t *testing.T) {
    74  		s := signedoracle.SpecConfigurationFromProto(nil)
    75  
    76  		assert.NotNil(t, s)
    77  		assert.IsType(t, signedoracle.SpecConfiguration{}, s)
    78  
    79  		assert.Nil(t, s.Signers)
    80  		assert.Nil(t, s.Filters)
    81  	})
    82  
    83  	t.Run("non-empty with empty lists", func(t *testing.T) {
    84  		protoSource := &vegapb.DataSourceSpecConfiguration{
    85  			Signers: nil,
    86  			Filters: nil,
    87  		}
    88  		s := signedoracle.SpecConfigurationFromProto(protoSource)
    89  
    90  		assert.NotNil(t, s)
    91  		assert.NotNil(t, &signedoracle.SpecConfiguration{}, s)
    92  
    93  		assert.NotNil(t, s.Signers)
    94  		assert.Equal(t, 0, len(s.Signers))
    95  		assert.NotNil(t, s.Filters)
    96  		assert.Equal(t, 0, len(s.Filters))
    97  	})
    98  
    99  	t.Run("non-empty", func(t *testing.T) {
   100  		protoSource := &vegapb.DataSourceSpecConfiguration{
   101  			Signers: []*v1.Signer{
   102  				{
   103  					Signer: &v1.Signer_EthAddress{
   104  						EthAddress: &v1.ETHAddress{
   105  							Address: "some-address",
   106  						},
   107  					},
   108  				},
   109  			},
   110  			Filters: []*v1.Filter{
   111  				{
   112  					Key: &v1.PropertyKey{
   113  						Name: "test-key",
   114  						Type: v1.PropertyKey_Type(1),
   115  					},
   116  				},
   117  			},
   118  		}
   119  
   120  		ds := signedoracle.SpecConfigurationFromProto(protoSource)
   121  		assert.NotNil(t, ds)
   122  		assert.Equal(t, 1, len(ds.Signers))
   123  		assert.IsType(t, &common.SignerETHAddress{}, ds.Signers[0].Signer)
   124  		assert.Equal(t, "some-address", ds.Signers[0].GetSignerETHAddress().Address)
   125  		assert.Equal(t, 1, len(ds.Filters))
   126  		assert.Equal(t, "test-key", ds.Filters[0].Key.Name)
   127  		assert.Equal(t, common.SpecPropertyKeyType(1), ds.Filters[0].Key.Type)
   128  	})
   129  }
   130  
   131  func TestSpecConfigurationString(t *testing.T) {
   132  	t.Run("non-empty oracle with empty lists", func(t *testing.T) {
   133  		ds := datasource.NewDefinitionWith(signedoracle.SpecConfiguration{}).String()
   134  
   135  		assert.Equal(t, "signers() filters()", ds)
   136  	})
   137  
   138  	t.Run("non-empty oracle with data", func(t *testing.T) {
   139  		ds := datasource.NewDefinitionWith(signedoracle.SpecConfiguration{
   140  			Signers: []*common.Signer{
   141  				{},
   142  			},
   143  			Filters: []*common.SpecFilter{
   144  				{
   145  					Key: &common.SpecPropertyKey{
   146  						Name: "test-name",
   147  						Type: common.SpecPropertyKeyType(0),
   148  					},
   149  					Conditions: []*common.SpecCondition{
   150  						{
   151  							Operator: 8,
   152  							Value:    "12",
   153  						},
   154  					},
   155  				},
   156  			},
   157  		}).String()
   158  
   159  		assert.Equal(t, "signers(nil) filters(key(name(test-name) type(TYPE_UNSPECIFIED) decimals()) conditions([value(12) operator(8)]))", ds)
   160  	})
   161  }
   162  
   163  func TestToDataSourceDefinitionProto(t *testing.T) {}
   164  
   165  func TestSpecConfigurationGetTimeTriggers(t *testing.T) {
   166  	ds := datasource.NewDefinitionWith(
   167  		signedoracle.SpecConfiguration{
   168  			Signers: []*common.Signer{
   169  				{},
   170  			},
   171  			Filters: []*common.SpecFilter{
   172  				{
   173  					Key: &common.SpecPropertyKey{
   174  						Name: "test-name",
   175  						Type: common.SpecPropertyKeyType(0),
   176  					},
   177  					Conditions: []*common.SpecCondition{
   178  						{
   179  							Operator: 8,
   180  							Value:    "12",
   181  						},
   182  					},
   183  				},
   184  			},
   185  		})
   186  
   187  	triggers := ds.GetTimeTriggers()
   188  	assert.NotNil(t, triggers)
   189  	assert.Equal(t, 1, len(triggers))
   190  	assert.IsType(t, &common.InternalTimeTrigger{}, triggers[0])
   191  	assert.Nil(t, triggers[0])
   192  }