code.vegaprotocol.io/vega@v0.79.0/core/datasource/spec/adaptors/adaptors.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 adaptors 17 18 import ( 19 "errors" 20 21 "code.vegaprotocol.io/vega/core/datasource/common" 22 "code.vegaprotocol.io/vega/core/datasource/spec/validation" 23 "code.vegaprotocol.io/vega/libs/crypto" 24 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 25 ) 26 27 // ErrUnknownOracleSource is used when the input data is originated from an 28 // unknown, unsupported or unspecified oracle source. 29 var ErrUnknownOracleSource = errors.New("unknown oracle source") 30 31 // Adaptor represents a adaptor that consumes and normalises data from 32 // a specific type of source. 33 type Adaptor interface { 34 Normalise(crypto.PublicKey, []byte) (*common.Data, error) 35 } 36 37 // Adaptors normalises the input data into an common.Data according to 38 // its source. 39 type Adaptors struct { 40 // Adaptors holds all the supported Adaptors sorted by source. 41 Adaptors map[commandspb.OracleDataSubmission_OracleSource]Adaptor 42 } 43 44 // New creates an Adaptors with all the supported oracle Adaptor. 45 func New() *Adaptors { 46 return &Adaptors{ 47 Adaptors: map[commandspb.OracleDataSubmission_OracleSource]Adaptor{ 48 commandspb.OracleDataSubmission_ORACLE_SOURCE_OPEN_ORACLE: NewOpenOracleAdaptor(), 49 commandspb.OracleDataSubmission_ORACLE_SOURCE_JSON: NewJSONAdaptor(), 50 }, 51 } 52 } 53 54 // Normalise normalises the input data into an common.Data based on its source. 55 func (a *Adaptors) Normalise(txPubKey crypto.PublicKey, data commandspb.OracleDataSubmission) (*common.Data, error) { 56 adaptor, ok := a.Adaptors[data.Source] 57 if !ok { 58 return nil, ErrUnknownOracleSource 59 } 60 61 oracleData, err := adaptor.Normalise(txPubKey, data.Payload) 62 if err != nil { 63 return nil, err 64 } 65 66 if err = validation.CheckForInternalOracle(oracleData.Data); err != nil { 67 return nil, err 68 } 69 70 return oracleData, err 71 }