-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathschema.go
70 lines (60 loc) · 2.13 KB
/
schema.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
package schema
import (
_ "embed"
"fmt"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
_ "github.com/ipld/go-ipld-prime/codec/dagjson"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-multihash"
)
const (
// MaxContextIDLen specifies the maximum number of bytes accepted as Advertisement.ContextID.
MaxContextIDLen = 64
// MaxMetadataLen specifies the maximum number of bytes an advertisement metadata can contain.
MaxMetadataLen = 1024 // 1KiB
)
var (
// NoEntries is a special value used to explicitly indicate that an
// advertisement does not have any entries. When isRm is true it and serves to
// remove content by context ID, and when isRm is false it serves to update
// metadata only.
NoEntries cidlink.Link
// Linkproto is the ipld.LinkProtocol used for the ingestion protocol.
// Refer to it if you have encoding questions.
Linkproto = cidlink.LinkPrototype{
Prefix: cid.Prefix{
Version: 1,
Codec: uint64(multicodec.DagJson),
MhType: uint64(multicodec.Sha2_256),
MhLength: -1,
},
}
// AdvertisementPrototype represents the IPLD node prototype of Advertisement.
//
// See: bindnode.Prototype.
AdvertisementPrototype schema.TypedPrototype
// EntryChunkPrototype represents the IPLD node prototype of EntryChunk.
//
// See: bindnode.Prototype.
EntryChunkPrototype schema.TypedPrototype
//go:embed schema.ipldsch
schemaBytes []byte
)
func init() {
typeSystem, err := ipld.LoadSchemaBytes(schemaBytes)
if err != nil {
panic(fmt.Errorf("failed to load schema: %w", err))
}
AdvertisementPrototype = bindnode.Prototype((*Advertisement)(nil), typeSystem.TypeByName("Advertisement"))
EntryChunkPrototype = bindnode.Prototype((*EntryChunk)(nil), typeSystem.TypeByName("EntryChunk"))
// Define NoEntries as the CID of a sha256 hash of nil.
m, err := multihash.Sum(nil, multihash.SHA2_256, 16)
if err != nil {
panic(fmt.Errorf("failed to sum NoEntries multihash: %w", err))
}
NoEntries = cidlink.Link{Cid: cid.NewCidV1(cid.Raw, m)}
}