code.vegaprotocol.io/vega@v0.79.0/datanode/entities/data_source_spec.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 entities 17 18 import ( 19 "encoding/json" 20 "fmt" 21 "time" 22 23 "code.vegaprotocol.io/vega/libs/ptr" 24 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 25 vegapb "code.vegaprotocol.io/vega/protos/vega" 26 ) 27 28 type DataSourceSpec struct { 29 ID SpecID 30 CreatedAt time.Time 31 UpdatedAt time.Time 32 Data *DataSourceDefinition 33 Status DataSourceSpecStatus 34 TxHash TxHash 35 VegaTime time.Time 36 } 37 38 func DataSourceSpecFromProto(protoSpec *vegapb.DataSourceSpec, txHash TxHash, vegaTime time.Time) *DataSourceSpec { 39 spec := &DataSourceSpec{} 40 41 if protoSpec != nil { 42 spec.ID = SpecID(protoSpec.Id) 43 spec.CreatedAt = time.Unix(0, protoSpec.CreatedAt) 44 spec.UpdatedAt = time.Unix(0, protoSpec.UpdatedAt) 45 spec.Data = ptr.From(DataSourceDefinition{}) 46 spec.Status = DataSourceSpecStatus(protoSpec.Status) 47 spec.TxHash = txHash 48 spec.VegaTime = vegaTime 49 50 if protoSpec.Data != nil && protoSpec.Data.SourceType != nil { 51 spec.Data = &DataSourceDefinition{protoSpec.Data} 52 } 53 } 54 55 return spec 56 } 57 58 func (ds *DataSourceSpec) ToProto() *vegapb.DataSourceSpec { 59 protoData := &vegapb.DataSourceSpec{} 60 61 if ds != nil { 62 if ds.Data != nil && *ds.Data != (DataSourceDefinition{}) { 63 protoData.Id = ds.ID.String() 64 protoData.CreatedAt = ds.CreatedAt.UnixNano() 65 protoData.UpdatedAt = ds.UpdatedAt.UnixNano() 66 protoData.Data = &vegapb.DataSourceDefinition{} 67 protoData.Status = vegapb.DataSourceSpec_Status(ds.Status) 68 if ds.Data.SourceType != nil { 69 protoData.Data = ds.Data.DataSourceDefinition 70 } 71 } 72 } 73 74 return protoData 75 } 76 77 func (ds *DataSourceSpec) ToOracleProto() *vegapb.OracleSpec { 78 return &vegapb.OracleSpec{ 79 ExternalDataSourceSpec: &vegapb.ExternalDataSourceSpec{ 80 Spec: ds.ToProto(), 81 }, 82 } 83 } 84 85 func (ds DataSourceSpec) Cursor() *Cursor { 86 return NewCursor(DataSourceSpecCursor{ds.VegaTime, ds.ID}.String()) 87 } 88 89 func (ds DataSourceSpec) ToOracleProtoEdge(_ ...any) (*v2.OracleSpecEdge, error) { 90 return &v2.OracleSpecEdge{ 91 Node: ds.ToOracleProto(), 92 Cursor: ds.Cursor().Encode(), 93 }, nil 94 } 95 96 type ExternalDataSourceSpec struct { 97 Spec *DataSourceSpec 98 } 99 100 func (s *ExternalDataSourceSpec) ToProto() *vegapb.ExternalDataSourceSpec { 101 return &vegapb.ExternalDataSourceSpec{ 102 Spec: s.Spec.ToProto(), 103 } 104 } 105 106 func ExternalDataSourceSpecFromProto(spec *vegapb.ExternalDataSourceSpec, txHash TxHash, vegaTime time.Time) *ExternalDataSourceSpec { 107 if spec != nil { 108 if spec.Spec != nil { 109 return &ExternalDataSourceSpec{ 110 Spec: DataSourceSpecFromProto(spec.Spec, txHash, vegaTime), 111 } 112 } 113 } 114 115 return &ExternalDataSourceSpec{ 116 Spec: &DataSourceSpec{ 117 Data: ptr.From(DataSourceDefinition{}), 118 }, 119 } 120 } 121 122 type DataSourceSpecCursor struct { 123 VegaTime time.Time `json:"vegaTime"` 124 ID SpecID `json:"id"` 125 } 126 127 func (ds DataSourceSpecCursor) String() string { 128 bs, err := json.Marshal(ds) 129 if err != nil { 130 panic(fmt.Errorf("could not marshal oracle spec cursor: %w", err)) 131 } 132 return string(bs) 133 } 134 135 func (ds *DataSourceSpecCursor) Parse(cursorString string) error { 136 if cursorString == "" { 137 return nil 138 } 139 return json.Unmarshal([]byte(cursorString), ds) 140 }