-
Notifications
You must be signed in to change notification settings - Fork 6
/
ingest_request.go
85 lines (71 loc) · 2.59 KB
/
ingest_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package model
import (
"encoding/json"
"fmt"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/record"
"github.com/multiformats/go-multihash"
)
// IgenstRequest is a request to store a single multihash. This is
// intentionally limited to one multihash as bulk ingestion should be done via
// advertisement ingestion method.
type IngestRequest struct {
Multihash multihash.Multihash
ProviderID peer.ID
ContextID []byte
Metadata []byte
Addrs []string
Seq uint64
}
// IngestRequestEnvelopeDomain is the domain string used for ingest requests contained in a Envelope.
const IngestRequestEnvelopeDomain = "indexer-ingest-request-record"
// IngestRequestEnvelopePayloadType is the type hint used to identify IngestRequest records in a Envelope.
var IngestRequestEnvelopePayloadType = []byte("indexer-ingest-request")
func init() {
record.RegisterType(&IngestRequest{})
}
// Domain is used when signing and validating IngestRequest records contained in Envelopes
func (r *IngestRequest) Domain() string {
return IngestRequestEnvelopeDomain
}
// Codec is a binary identifier for the IngestRequest type
func (r *IngestRequest) Codec() []byte {
return IngestRequestEnvelopePayloadType
}
// UnmarshalRecord parses an IngestRequest from a byte slice.
func (r *IngestRequest) UnmarshalRecord(data []byte) error {
if r == nil {
return fmt.Errorf("cannot unmarshal IngestRequest to nil receiver")
}
return json.Unmarshal(data, r)
}
// MarshalRecord serializes an IngestRequesr to a byte slice.
func (r *IngestRequest) MarshalRecord() ([]byte, error) {
return json.Marshal(r)
}
// MakeIngestRequest creates a signed IngestRequest and marshals it into bytes
func MakeIngestRequest(providerID peer.ID, privateKey crypto.PrivKey, m multihash.Multihash, contextID []byte, metadata []byte, addrs []string) ([]byte, error) {
req := &IngestRequest{
Multihash: m,
ProviderID: providerID,
ContextID: contextID,
Metadata: metadata,
Addrs: addrs,
Seq: peer.TimestampSeq(),
}
return makeRequestEnvelop(req, privateKey)
}
// ReadIngestRequest unmarshals an IngestRequest from bytes, verifies the
// signature, and returns the IngestRequest
func ReadIngestRequest(data []byte) (*IngestRequest, error) {
_, untypedRecord, err := record.ConsumeEnvelope(data, IngestRequestEnvelopeDomain)
if err != nil {
return nil, fmt.Errorf("cannot consume register request envelope: %s", err)
}
rec, ok := untypedRecord.(*IngestRequest)
if !ok {
return nil, fmt.Errorf("unmarshaled request is not a *IngestRequest")
}
return rec, nil
}