From e4aa13e86eaf51483e7bbd1aa8363b3930b07773 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 9 Aug 2023 15:14:06 +0300 Subject: [PATCH] rpcsrv: do not store nil Oracle service And simplify atomic service value stored by RPC server. It's unwanted to use reflection, thus we can directly check for the typed nil service value. Otherwise `submitoracleresponse` RPC handler doesn't work properly. Signed-off-by: Anna Shaleva --- cli/server/server.go | 9 ++++++++- pkg/services/rpcsrv/server.go | 16 ++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cli/server/server.go b/cli/server/server.go index b55982a4eb..efcbd0c2db 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -359,7 +359,14 @@ func resetDB(ctx *cli.Context) error { return nil } -func mkOracle(config config.OracleConfiguration, magic netmode.Magic, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (*oracle.Oracle, error) { +// OracleService is an interface representing Oracle service with network.Service +// capabilities and ability to submit oracle responses. +type OracleService interface { + rpcsrv.OracleHandler + network.Service +} + +func mkOracle(config config.OracleConfiguration, magic netmode.Magic, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (OracleService, error) { if !config.Enabled { return nil, nil } diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 5fcbf656c2..88934ee307 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -254,7 +254,8 @@ var invalidBlockHeightError = func(index int, height int) *neorpc.Error { return neorpc.NewRPCError("Invalid block height", fmt.Sprintf("param at index %d should be greater than or equal to 0 and less then or equal to current block height, got: %d", index, height)) } -// New creates a new Server struct. +// New creates a new Server struct. Pay attention that orc is expected to be either +// untyped nil or non-nil structure implementing OracleHandler interface. func New(chain Ledger, conf config.RPC, coreServer *network.Server, orc OracleHandler, log *zap.Logger, errChan chan<- error) Server { addrs := conf.GetAddresses() @@ -293,7 +294,7 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server, } var oracleWrapped = new(atomic.Value) if orc != nil { - oracleWrapped.Store(&orc) + oracleWrapped.Store(orc) } var wsOriginChecker func(*http.Request) bool if conf.EnableCORSWorkaround { @@ -445,7 +446,9 @@ func (s *Server) Shutdown() { // SetOracleHandler allows to update oracle handler used by the Server. func (s *Server) SetOracleHandler(orc OracleHandler) { - s.oracle.Store(&orc) + if orc != nil { + s.oracle.Store(orc) + } } func (s *Server) handleHTTPRequest(w http.ResponseWriter, httpRequest *http.Request) { @@ -2461,10 +2464,11 @@ func getRelayResult(err error, hash util.Uint256) (any, *neorpc.Error) { } func (s *Server) submitOracleResponse(ps params.Params) (any, *neorpc.Error) { - oracle := s.oracle.Load().(*OracleHandler) - if oracle == nil || *oracle == nil { + oraclePtr := s.oracle.Load() + if oraclePtr == nil { return nil, neorpc.NewRPCError("Oracle is not enabled", "") } + oracle := oraclePtr.(OracleHandler) var pub *keys.PublicKey pubBytes, err := ps.Value(0).GetBytesBase64() if err == nil { @@ -2489,7 +2493,7 @@ func (s *Server) submitOracleResponse(ps params.Params) (any, *neorpc.Error) { if !pub.Verify(msgSig, hash.Sha256(data).BytesBE()) { return nil, neorpc.NewRPCError("Invalid request signature", "") } - (*oracle).AddResponse(pub, uint64(reqID), txSig) + (oracle).AddResponse(pub, uint64(reqID), txSig) return json.RawMessage([]byte("{}")), nil }