forked from ava-labs/subnet-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
46 lines (37 loc) · 1.46 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// (c) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"context"
"fmt"
"github.com/MetalBlockchain/metalgo/codec"
"github.com/MetalBlockchain/metalgo/ids"
)
// Request represents a Network request type
type Request interface {
// Requests should implement String() for logging.
fmt.Stringer
// Handle allows `Request` to call respective methods on handler to handle
// this particular request type
Handle(ctx context.Context, nodeID ids.NodeID, requestID uint32, handler RequestHandler) ([]byte, error)
}
// BytesToRequest unmarshals the given requestBytes into Request object
func BytesToRequest(codec codec.Manager, requestBytes []byte) (Request, error) {
var request Request
if _, err := codec.Unmarshal(requestBytes, &request); err != nil {
return nil, err
}
return request, nil
}
// RequestToBytes marshals the given request object into bytes
func RequestToBytes(codec codec.Manager, request Request) ([]byte, error) {
return codec.Marshal(Version, &request)
}
// CrossChainRequest represents the interface a cross chain request should implement
type CrossChainRequest interface {
// CrossChainRequest should implement String() for logging.
fmt.Stringer
// Handle allows [CrossChainRequest] to call respective methods on handler to handle
// this particular request type
Handle(ctx context.Context, requestingChainID ids.ID, requestID uint32, handler CrossChainRequestHandler) ([]byte, error)
}