code.vegaprotocol.io/vega@v0.79.0/core/datasource/external/ethcall/common/spec_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 common_test 17 18 import ( 19 "errors" 20 "fmt" 21 "strconv" 22 "testing" 23 "time" 24 25 dscommon "code.vegaprotocol.io/vega/core/datasource/common" 26 "code.vegaprotocol.io/vega/core/datasource/external/ethcall/common" 27 vegapb "code.vegaprotocol.io/vega/protos/vega" 28 v1 "code.vegaprotocol.io/vega/protos/vega/data/v1" 29 30 "github.com/stretchr/testify/assert" 31 "google.golang.org/protobuf/types/known/structpb" 32 ) 33 34 func TestEthCallSpecFromProto(t *testing.T) { 35 t.Run("empty", func(t *testing.T) { 36 s, err := common.SpecFromProto(nil) 37 assert.Error(t, errors.New("ethereum call spec proto is nil"), err) 38 39 assert.NotNil(t, s) 40 assert.IsType(t, common.Spec{}, s) 41 42 assert.Equal(t, "", s.Address) 43 assert.Equal(t, []byte(nil), s.AbiJson) 44 assert.Nil(t, nil, s.Method) 45 assert.Nil(t, s.ArgsJson) 46 assert.Equal(t, 0, len(s.ArgsJson)) 47 assert.Nil(t, s.Filters) 48 assert.Nil(t, s.Trigger) 49 }) 50 51 t.Run("non-empty with empty lists", func(t *testing.T) { 52 protoSource := &vegapb.EthCallSpec{ 53 Abi: "", 54 Args: nil, 55 Filters: nil, 56 } 57 s, err := common.SpecFromProto(protoSource) 58 assert.Error(t, errors.New(" error unmarshalling trigger: trigger proto is nil"), err) 59 60 assert.NotNil(t, s) 61 assert.IsType(t, common.Spec{}, s) 62 63 assert.Equal(t, "", s.Address) 64 assert.Equal(t, []byte(nil), s.AbiJson) 65 assert.Nil(t, nil, s.Method) 66 assert.Nil(t, s.ArgsJson) 67 assert.Equal(t, 0, len(s.ArgsJson)) 68 assert.Nil(t, s.Filters) 69 assert.Nil(t, s.Trigger) 70 }) 71 72 t.Run("non-empty with error", func(t *testing.T) { 73 timeNow := uint64(time.Now().UnixNano()) 74 every := uint64(2) 75 protoSource := &vegapb.EthCallSpec{ 76 Address: "test-eth-address", 77 Abi: "5", 78 Method: "test-method", 79 Args: []*structpb.Value{ 80 {}, 81 }, 82 Trigger: &vegapb.EthCallTrigger{ 83 Trigger: &vegapb.EthCallTrigger_TimeTrigger{ 84 TimeTrigger: &vegapb.EthTimeTrigger{ 85 Initial: &timeNow, 86 Every: &every, 87 Until: &timeNow, 88 }, 89 }, 90 }, 91 Filters: []*v1.Filter{ 92 { 93 Key: &v1.PropertyKey{ 94 Name: "test-key", 95 Type: v1.PropertyKey_Type(1), 96 }, 97 }, 98 }, 99 } 100 101 ds, err := common.SpecFromProto(protoSource) 102 assert.Error(t, errors.New("error marshalling arg: proto: google.protobuf.Value: none of the oneof fields is set"), err) 103 assert.IsType(t, common.Spec{}, ds) 104 105 assert.Equal(t, "", ds.Address) 106 assert.Equal(t, []byte(nil), ds.AbiJson) 107 assert.Nil(t, nil, ds.Method) 108 assert.Nil(t, ds.ArgsJson) 109 assert.Equal(t, 0, len(ds.ArgsJson)) 110 assert.Nil(t, ds.Filters) 111 assert.Nil(t, ds.Trigger) 112 113 protoSource.Args = nil 114 ds, err = common.SpecFromProto(protoSource) 115 assert.Nil(t, err) 116 assert.IsType(t, common.Spec{}, ds) 117 118 assert.Equal(t, "test-eth-address", ds.Address) 119 assert.Equal(t, []byte("5"), ds.AbiJson) 120 assert.Equal(t, "test-method", ds.Method) 121 assert.NotNil(t, ds.ArgsJson) 122 assert.Equal(t, 0, len(ds.ArgsJson)) 123 assert.NotNil(t, ds.Filters) 124 assert.Equal(t, 1, len(ds.Filters)) 125 assert.Equal(t, "test-key", ds.Filters[0].Key.Name) 126 assert.Equal(t, dscommon.SpecPropertyKeyType(1), ds.Filters[0].Key.Type) 127 assert.NotNil(t, ds.Trigger) 128 tr := ds.Trigger.IntoTriggerProto().Trigger 129 assert.IsType(t, &vegapb.EthCallTrigger_TimeTrigger{}, tr) 130 assert.Equal(t, fmt.Sprintf("initial(%s) every(2) until(%s)", strconv.FormatUint(timeNow, 10), strconv.FormatUint(timeNow, 10)), ds.Trigger.String()) 131 }) 132 133 t.Run("non-empty", func(t *testing.T) { 134 timeNow := uint64(time.Now().UnixNano()) 135 protoSource := &vegapb.EthCallSpec{ 136 Address: "test-eth-address", 137 Abi: "5", 138 Method: "test-method", 139 Args: []*structpb.Value{ 140 structpb.NewStringValue("test-arg-value"), 141 }, 142 Trigger: &vegapb.EthCallTrigger{ 143 Trigger: &vegapb.EthCallTrigger_TimeTrigger{ 144 TimeTrigger: &vegapb.EthTimeTrigger{ 145 Initial: &timeNow, 146 }, 147 }, 148 }, 149 Filters: []*v1.Filter{ 150 { 151 Key: &v1.PropertyKey{ 152 Name: "test-key", 153 Type: v1.PropertyKey_Type(1), 154 }, 155 }, 156 }, 157 } 158 159 ds, err := common.SpecFromProto(protoSource) 160 assert.Nil(t, err) 161 assert.IsType(t, common.Spec{}, ds) 162 163 assert.Equal(t, "test-eth-address", ds.Address) 164 assert.Equal(t, []byte("5"), ds.AbiJson) 165 assert.Equal(t, "test-method", ds.Method) 166 assert.Equal(t, []string{"\"test-arg-value\""}, ds.ArgsJson) 167 filters := ds.Filters 168 assert.Equal(t, 1, len(filters)) 169 assert.Equal(t, 0, len(filters[0].Conditions)) 170 assert.Equal(t, "test-key", filters[0].Key.Name) 171 assert.Equal(t, v1.PropertyKey_Type(1), filters[0].Key.Type) 172 assert.NotNil(t, ds.Trigger) 173 assert.IsType(t, &vegapb.EthCallTrigger_TimeTrigger{}, ds.Trigger.IntoTriggerProto().Trigger) 174 }) 175 } 176 177 func TestEthCallSpecIntoProto(t *testing.T) { 178 // Implicitly tested with TestDataSourceDefinitionIntoProto. 179 } 180 181 func TestEthCallSpecToDataSourceDefinitionProto(t *testing.T) { 182 // Implicitly tested with TestDataSourceDefinitionIntoProto. 183 }