Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b9ff3906e | ||
|
|
f7cb4c91eb | ||
|
|
85cd30874e | ||
|
|
12e2623f35 | ||
|
|
d683e9243c | ||
|
|
18af217de6 | ||
|
|
9830f823b5 | ||
|
|
d110f73c43 | ||
|
|
95a7ed4233 | ||
|
|
8dd6fe2778 | ||
|
|
a25d68f3e4 | ||
|
|
7c82f3b81c | ||
|
|
75caa6bdbd | ||
|
|
c1c89c20c1 | ||
|
|
628a0123ed | ||
|
|
266e76d591 | ||
|
|
7d7cb88d78 | ||
|
|
723b4ab105 | ||
|
|
9cb3848e51 | ||
|
|
f458e04107 | ||
|
|
72cd3d39d7 | ||
|
|
c1b740035a |
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: Getting Help on IPFS
|
||||||
|
url: https://ipfs.io/help
|
||||||
|
about: All information about how and where to get help on IPFS.
|
||||||
|
- name: IPFS Official Forum
|
||||||
|
url: https://discuss.ipfs.io
|
||||||
|
about: Please post general questions, support requests, and discussions here.
|
||||||
19
.github/ISSUE_TEMPLATE/open_an_issue.md
vendored
Normal file
19
.github/ISSUE_TEMPLATE/open_an_issue.md
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
---
|
||||||
|
name: Open an issue
|
||||||
|
about: Only for actionable issues relevant to this repository.
|
||||||
|
title: ''
|
||||||
|
labels: need/triage
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
<!--
|
||||||
|
Hello! To ensure this issue is correctly addressed as soon as possible by the IPFS team, please try to make sure:
|
||||||
|
|
||||||
|
- This issue is relevant to this repository's topic or codebase.
|
||||||
|
|
||||||
|
- A clear description is provided. It should includes as much relevant information as possible and clear scope for the issue to be actionable.
|
||||||
|
|
||||||
|
FOR GENERAL DISCUSSION, HELP OR QUESTIONS, please see the options at https://ipfs.io/help or head directly to https://discuss.ipfs.io.
|
||||||
|
|
||||||
|
(you can delete this section after reading)
|
||||||
|
-->
|
||||||
68
.github/config.yml
vendored
Normal file
68
.github/config.yml
vendored
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
# Configuration for welcome - https://github.com/behaviorbot/welcome
|
||||||
|
|
||||||
|
# Configuration for new-issue-welcome - https://github.com/behaviorbot/new-issue-welcome
|
||||||
|
# Comment to be posted to on first time issues
|
||||||
|
newIssueWelcomeComment: >
|
||||||
|
Thank you for submitting your first issue to this repository! A maintainer
|
||||||
|
will be here shortly to triage and review.
|
||||||
|
|
||||||
|
In the meantime, please double-check that you have provided all the
|
||||||
|
necessary information to make this process easy! Any information that can
|
||||||
|
help save additional round trips is useful! We currently aim to give
|
||||||
|
initial feedback within **two business days**. If this does not happen, feel
|
||||||
|
free to leave a comment.
|
||||||
|
|
||||||
|
Please keep an eye on how this issue will be labeled, as labels give an
|
||||||
|
overview of priorities, assignments and additional actions requested by the
|
||||||
|
maintainers:
|
||||||
|
|
||||||
|
- "Priority" labels will show how urgent this is for the team.
|
||||||
|
- "Status" labels will show if this is ready to be worked on, blocked, or in progress.
|
||||||
|
- "Need" labels will indicate if additional input or analysis is required.
|
||||||
|
|
||||||
|
Finally, remember to use https://discuss.ipfs.io if you just need general
|
||||||
|
support.
|
||||||
|
|
||||||
|
# Configuration for new-pr-welcome - https://github.com/behaviorbot/new-pr-welcome
|
||||||
|
# Comment to be posted to on PRs from first time contributors in your repository
|
||||||
|
newPRWelcomeComment: >
|
||||||
|
Thank you for submitting this PR!
|
||||||
|
|
||||||
|
A maintainer will be here shortly to review it.
|
||||||
|
|
||||||
|
We are super grateful, but we are also overloaded! Help us by making sure
|
||||||
|
that:
|
||||||
|
|
||||||
|
* The context for this PR is clear, with relevant discussion, decisions
|
||||||
|
and stakeholders linked/mentioned.
|
||||||
|
|
||||||
|
* Your contribution itself is clear (code comments, self-review for the
|
||||||
|
rest) and in its best form. Follow the [code contribution
|
||||||
|
guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md#code-contribution-guidelines)
|
||||||
|
if they apply.
|
||||||
|
|
||||||
|
Getting other community members to do a review would be great help too on
|
||||||
|
complex PRs (you can ask in the chats/forums). If you are unsure about
|
||||||
|
something, just leave us a comment.
|
||||||
|
|
||||||
|
Next steps:
|
||||||
|
|
||||||
|
* A maintainer will triage and assign priority to this PR, commenting on
|
||||||
|
any missing things and potentially assigning a reviewer for high
|
||||||
|
priority items.
|
||||||
|
|
||||||
|
* The PR gets reviews, discussed and approvals as needed.
|
||||||
|
|
||||||
|
* The PR is merged by maintainers when it has been approved and comments addressed.
|
||||||
|
|
||||||
|
We currently aim to provide initial feedback/triaging within **two business
|
||||||
|
days**. Please keep an eye on any labelling actions, as these will indicate
|
||||||
|
priorities and status of your contribution.
|
||||||
|
|
||||||
|
We are very grateful for your contribution!
|
||||||
|
|
||||||
|
|
||||||
|
# Configuration for first-pr-merge - https://github.com/behaviorbot/first-pr-merge
|
||||||
|
# Comment to be posted to on pull requests merged by a first time user
|
||||||
|
# Currently disabled
|
||||||
|
#firstPRMergeComment: ""
|
||||||
@@ -1 +0,0 @@
|
|||||||
0.9.3: QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN
|
|
||||||
@@ -23,7 +23,6 @@ script:
|
|||||||
|
|
||||||
cache:
|
cache:
|
||||||
directories:
|
directories:
|
||||||
- $GOPATH/src/gx
|
|
||||||
- $GOPATH/pkg/mod
|
- $GOPATH/pkg/mod
|
||||||
- $HOME/.cache/go-build
|
- $HOME/.cache/go-build
|
||||||
|
|
||||||
|
|||||||
13
Makefile
13
Makefile
@@ -1,16 +1,5 @@
|
|||||||
all: deps
|
all: deps
|
||||||
|
|
||||||
gx:
|
deps:
|
||||||
go get github.com/whyrusleeping/gx
|
|
||||||
go get github.com/whyrusleeping/gx-go
|
|
||||||
|
|
||||||
covertools:
|
|
||||||
go get github.com/mattn/goveralls
|
go get github.com/mattn/goveralls
|
||||||
go get golang.org/x/tools/cmd/cover
|
go get golang.org/x/tools/cmd/cover
|
||||||
|
|
||||||
deps: gx covertools
|
|
||||||
gx --verbose install --global
|
|
||||||
gx-go rewrite
|
|
||||||
|
|
||||||
publish:
|
|
||||||
gx-go rewrite --undo
|
|
||||||
|
|||||||
256
cid.go
256
cid.go
@@ -22,29 +22,21 @@ package cid
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding"
|
"encoding"
|
||||||
"encoding/binary"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
mbase "github.com/multiformats/go-multibase"
|
mbase "github.com/multiformats/go-multibase"
|
||||||
mh "github.com/multiformats/go-multihash"
|
mh "github.com/multiformats/go-multihash"
|
||||||
|
varint "github.com/multiformats/go-varint"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnsupportedVersionString just holds an error message
|
// UnsupportedVersionString just holds an error message
|
||||||
const UnsupportedVersionString = "<unsupported cid version>"
|
const UnsupportedVersionString = "<unsupported cid version>"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrVarintBuffSmall means that a buffer passed to the cid parser was not
|
|
||||||
// long enough, or did not contain an invalid cid
|
|
||||||
ErrVarintBuffSmall = errors.New("reading varint: buffer too small")
|
|
||||||
|
|
||||||
// ErrVarintTooBig means that the varint in the given cid was above the
|
|
||||||
// limit of 2^64
|
|
||||||
ErrVarintTooBig = errors.New("reading varint: varint bigger than 64bits" +
|
|
||||||
" and not supported")
|
|
||||||
|
|
||||||
// ErrCidTooShort means that the cid passed to decode was not long
|
// ErrCidTooShort means that the cid passed to decode was not long
|
||||||
// enough to be a valid Cid
|
// enough to be a valid Cid
|
||||||
ErrCidTooShort = errors.New("cid too short")
|
ErrCidTooShort = errors.New("cid too short")
|
||||||
@@ -66,75 +58,81 @@ const (
|
|||||||
|
|
||||||
GitRaw = 0x78
|
GitRaw = 0x78
|
||||||
|
|
||||||
EthBlock = 0x90
|
EthBlock = 0x90
|
||||||
EthBlockList = 0x91
|
EthBlockList = 0x91
|
||||||
EthTxTrie = 0x92
|
EthTxTrie = 0x92
|
||||||
EthTx = 0x93
|
EthTx = 0x93
|
||||||
EthTxReceiptTrie = 0x94
|
EthTxReceiptTrie = 0x94
|
||||||
EthTxReceipt = 0x95
|
EthTxReceipt = 0x95
|
||||||
EthStateTrie = 0x96
|
EthStateTrie = 0x96
|
||||||
EthAccountSnapshot = 0x97
|
EthAccountSnapshot = 0x97
|
||||||
EthStorageTrie = 0x98
|
EthStorageTrie = 0x98
|
||||||
BitcoinBlock = 0xb0
|
BitcoinBlock = 0xb0
|
||||||
BitcoinTx = 0xb1
|
BitcoinTx = 0xb1
|
||||||
ZcashBlock = 0xc0
|
ZcashBlock = 0xc0
|
||||||
ZcashTx = 0xc1
|
ZcashTx = 0xc1
|
||||||
DecredBlock = 0xe0
|
DecredBlock = 0xe0
|
||||||
DecredTx = 0xe1
|
DecredTx = 0xe1
|
||||||
DashBlock = 0xf0
|
DashBlock = 0xf0
|
||||||
DashTx = 0xf1
|
DashTx = 0xf1
|
||||||
|
FilCommitmentUnsealed = 0xf101
|
||||||
|
FilCommitmentSealed = 0xf102
|
||||||
)
|
)
|
||||||
|
|
||||||
// Codecs maps the name of a codec to its type
|
// Codecs maps the name of a codec to its type
|
||||||
var Codecs = map[string]uint64{
|
var Codecs = map[string]uint64{
|
||||||
"v0": DagProtobuf,
|
"v0": DagProtobuf,
|
||||||
"raw": Raw,
|
"raw": Raw,
|
||||||
"protobuf": DagProtobuf,
|
"protobuf": DagProtobuf,
|
||||||
"cbor": DagCBOR,
|
"cbor": DagCBOR,
|
||||||
"libp2p-key": Libp2pKey,
|
"libp2p-key": Libp2pKey,
|
||||||
"git-raw": GitRaw,
|
"git-raw": GitRaw,
|
||||||
"eth-block": EthBlock,
|
"eth-block": EthBlock,
|
||||||
"eth-block-list": EthBlockList,
|
"eth-block-list": EthBlockList,
|
||||||
"eth-tx-trie": EthTxTrie,
|
"eth-tx-trie": EthTxTrie,
|
||||||
"eth-tx": EthTx,
|
"eth-tx": EthTx,
|
||||||
"eth-tx-receipt-trie": EthTxReceiptTrie,
|
"eth-tx-receipt-trie": EthTxReceiptTrie,
|
||||||
"eth-tx-receipt": EthTxReceipt,
|
"eth-tx-receipt": EthTxReceipt,
|
||||||
"eth-state-trie": EthStateTrie,
|
"eth-state-trie": EthStateTrie,
|
||||||
"eth-account-snapshot": EthAccountSnapshot,
|
"eth-account-snapshot": EthAccountSnapshot,
|
||||||
"eth-storage-trie": EthStorageTrie,
|
"eth-storage-trie": EthStorageTrie,
|
||||||
"bitcoin-block": BitcoinBlock,
|
"bitcoin-block": BitcoinBlock,
|
||||||
"bitcoin-tx": BitcoinTx,
|
"bitcoin-tx": BitcoinTx,
|
||||||
"zcash-block": ZcashBlock,
|
"zcash-block": ZcashBlock,
|
||||||
"zcash-tx": ZcashTx,
|
"zcash-tx": ZcashTx,
|
||||||
"decred-block": DecredBlock,
|
"decred-block": DecredBlock,
|
||||||
"decred-tx": DecredTx,
|
"decred-tx": DecredTx,
|
||||||
"dash-block": DashBlock,
|
"dash-block": DashBlock,
|
||||||
"dash-tx": DashTx,
|
"dash-tx": DashTx,
|
||||||
|
"fil-commitment-unsealed": FilCommitmentUnsealed,
|
||||||
|
"fil-commitment-sealed": FilCommitmentSealed,
|
||||||
}
|
}
|
||||||
|
|
||||||
// CodecToStr maps the numeric codec to its name
|
// CodecToStr maps the numeric codec to its name
|
||||||
var CodecToStr = map[uint64]string{
|
var CodecToStr = map[uint64]string{
|
||||||
Raw: "raw",
|
Raw: "raw",
|
||||||
DagProtobuf: "protobuf",
|
DagProtobuf: "protobuf",
|
||||||
DagCBOR: "cbor",
|
DagCBOR: "cbor",
|
||||||
GitRaw: "git-raw",
|
GitRaw: "git-raw",
|
||||||
EthBlock: "eth-block",
|
EthBlock: "eth-block",
|
||||||
EthBlockList: "eth-block-list",
|
EthBlockList: "eth-block-list",
|
||||||
EthTxTrie: "eth-tx-trie",
|
EthTxTrie: "eth-tx-trie",
|
||||||
EthTx: "eth-tx",
|
EthTx: "eth-tx",
|
||||||
EthTxReceiptTrie: "eth-tx-receipt-trie",
|
EthTxReceiptTrie: "eth-tx-receipt-trie",
|
||||||
EthTxReceipt: "eth-tx-receipt",
|
EthTxReceipt: "eth-tx-receipt",
|
||||||
EthStateTrie: "eth-state-trie",
|
EthStateTrie: "eth-state-trie",
|
||||||
EthAccountSnapshot: "eth-account-snapshot",
|
EthAccountSnapshot: "eth-account-snapshot",
|
||||||
EthStorageTrie: "eth-storage-trie",
|
EthStorageTrie: "eth-storage-trie",
|
||||||
BitcoinBlock: "bitcoin-block",
|
BitcoinBlock: "bitcoin-block",
|
||||||
BitcoinTx: "bitcoin-tx",
|
BitcoinTx: "bitcoin-tx",
|
||||||
ZcashBlock: "zcash-block",
|
ZcashBlock: "zcash-block",
|
||||||
ZcashTx: "zcash-tx",
|
ZcashTx: "zcash-tx",
|
||||||
DecredBlock: "decred-block",
|
DecredBlock: "decred-block",
|
||||||
DecredTx: "decred-tx",
|
DecredTx: "decred-tx",
|
||||||
DashBlock: "dash-block",
|
DashBlock: "dash-block",
|
||||||
DashTx: "dash-tx",
|
DashTx: "dash-tx",
|
||||||
|
FilCommitmentUnsealed: "fil-commitment-unsealed",
|
||||||
|
FilCommitmentSealed: "fil-commitment-sealed",
|
||||||
}
|
}
|
||||||
|
|
||||||
// tryNewCidV0 tries to convert a multihash into a CIDv0 CID and returns an
|
// tryNewCidV0 tries to convert a multihash into a CIDv0 CID and returns an
|
||||||
@@ -173,9 +171,9 @@ func NewCidV0(mhash mh.Multihash) Cid {
|
|||||||
func NewCidV1(codecType uint64, mhash mh.Multihash) Cid {
|
func NewCidV1(codecType uint64, mhash mh.Multihash) Cid {
|
||||||
hashlen := len(mhash)
|
hashlen := len(mhash)
|
||||||
// two 8 bytes (max) numbers plus hash
|
// two 8 bytes (max) numbers plus hash
|
||||||
buf := make([]byte, 2*binary.MaxVarintLen64+hashlen)
|
buf := make([]byte, 1+varint.UvarintSize(codecType)+hashlen)
|
||||||
n := binary.PutUvarint(buf, 1)
|
n := varint.PutUvarint(buf, 1)
|
||||||
n += binary.PutUvarint(buf[n:], codecType)
|
n += varint.PutUvarint(buf[n:], codecType)
|
||||||
cn := copy(buf[n:], mhash)
|
cn := copy(buf[n:], mhash)
|
||||||
if cn != hashlen {
|
if cn != hashlen {
|
||||||
panic("copy hash length is inconsistent")
|
panic("copy hash length is inconsistent")
|
||||||
@@ -281,17 +279,6 @@ func ExtractEncoding(v string) (mbase.Encoding, error) {
|
|||||||
return encoding, nil
|
return encoding, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func uvError(read int) error {
|
|
||||||
switch {
|
|
||||||
case read == 0:
|
|
||||||
return ErrVarintBuffSmall
|
|
||||||
case read < 0:
|
|
||||||
return ErrVarintTooBig
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cast takes a Cid data slice, parses it and returns a Cid.
|
// Cast takes a Cid data slice, parses it and returns a Cid.
|
||||||
// For CidV1, the data buffer is in the form:
|
// For CidV1, the data buffer is in the form:
|
||||||
//
|
//
|
||||||
@@ -351,14 +338,14 @@ func (c Cid) Type() uint64 {
|
|||||||
if c.Version() == 0 {
|
if c.Version() == 0 {
|
||||||
return DagProtobuf
|
return DagProtobuf
|
||||||
}
|
}
|
||||||
_, n := uvarint(c.str)
|
_, n, _ := uvarint(c.str)
|
||||||
codec, _ := uvarint(c.str[n:])
|
codec, _, _ := uvarint(c.str[n:])
|
||||||
return codec
|
return codec
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the default string representation of a
|
// String returns the default string representation of a
|
||||||
// Cid. Currently, Base58 is used as the encoding for the
|
// Cid. Currently, Base32 is used for CIDV1 as the encoding for the
|
||||||
// multibase string.
|
// multibase string, Base58 is used for CIDV0.
|
||||||
func (c Cid) String() string {
|
func (c Cid) String() string {
|
||||||
switch c.Version() {
|
switch c.Version() {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -414,9 +401,9 @@ func (c Cid) Hash() mh.Multihash {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// skip version length
|
// skip version length
|
||||||
_, n1 := binary.Uvarint(bytes)
|
_, n1, _ := varint.FromUvarint(bytes)
|
||||||
// skip codec length
|
// skip codec length
|
||||||
_, n2 := binary.Uvarint(bytes[n1:])
|
_, n2, _ := varint.FromUvarint(bytes[n1:])
|
||||||
|
|
||||||
return mh.Multihash(bytes[n1+n2:])
|
return mh.Multihash(bytes[n1+n2:])
|
||||||
}
|
}
|
||||||
@@ -428,6 +415,30 @@ func (c Cid) Bytes() []byte {
|
|||||||
return []byte(c.str)
|
return []byte(c.str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ByteLen returns the length of the CID in bytes.
|
||||||
|
// It's equivalent to `len(c.Bytes())`, but works without an allocation,
|
||||||
|
// and should therefore be preferred.
|
||||||
|
//
|
||||||
|
// (See also the WriteTo method for other important operations that work without allocation.)
|
||||||
|
func (c Cid) ByteLen() int {
|
||||||
|
return len(c.str)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteBytes writes the CID bytes to the given writer.
|
||||||
|
// This method works without incurring any allocation.
|
||||||
|
//
|
||||||
|
// (See also the ByteLen method for other important operations that work without allocation.)
|
||||||
|
func (c Cid) WriteBytes(w io.Writer) (int, error) {
|
||||||
|
n, err := io.WriteString(w, c.str)
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
if n != len(c.str) {
|
||||||
|
return n, fmt.Errorf("failed to write entire cid string")
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalBinary is equivalent to Bytes(). It implements the
|
// MarshalBinary is equivalent to Bytes(). It implements the
|
||||||
// encoding.BinaryMarshaler interface.
|
// encoding.BinaryMarshaler interface.
|
||||||
func (c Cid) MarshalBinary() ([]byte, error) {
|
func (c Cid) MarshalBinary() ([]byte, error) {
|
||||||
@@ -507,12 +518,29 @@ func (c Cid) Loggable() map[string]interface{} {
|
|||||||
|
|
||||||
// Prefix builds and returns a Prefix out of a Cid.
|
// Prefix builds and returns a Prefix out of a Cid.
|
||||||
func (c Cid) Prefix() Prefix {
|
func (c Cid) Prefix() Prefix {
|
||||||
dec, _ := mh.Decode(c.Hash()) // assuming we got a valid multiaddr, this will not error
|
if c.Version() == 0 {
|
||||||
|
return Prefix{
|
||||||
|
MhType: mh.SHA2_256,
|
||||||
|
MhLength: 32,
|
||||||
|
Version: 0,
|
||||||
|
Codec: DagProtobuf,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := 0
|
||||||
|
version, n, _ := uvarint(c.str[offset:])
|
||||||
|
offset += n
|
||||||
|
codec, n, _ := uvarint(c.str[offset:])
|
||||||
|
offset += n
|
||||||
|
mhtype, n, _ := uvarint(c.str[offset:])
|
||||||
|
offset += n
|
||||||
|
mhlen, _, _ := uvarint(c.str[offset:])
|
||||||
|
|
||||||
return Prefix{
|
return Prefix{
|
||||||
MhType: dec.Code,
|
MhType: mhtype,
|
||||||
MhLength: dec.Length,
|
MhLength: int(mhlen),
|
||||||
Version: c.Version(),
|
Version: version,
|
||||||
Codec: c.Type(),
|
Codec: codec,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -562,34 +590,42 @@ func (p Prefix) Sum(data []byte) (Cid, error) {
|
|||||||
//
|
//
|
||||||
// <version><codec><mh-type><mh-length>
|
// <version><codec><mh-type><mh-length>
|
||||||
func (p Prefix) Bytes() []byte {
|
func (p Prefix) Bytes() []byte {
|
||||||
buf := make([]byte, 4*binary.MaxVarintLen64)
|
size := varint.UvarintSize(p.Version)
|
||||||
n := binary.PutUvarint(buf, p.Version)
|
size += varint.UvarintSize(p.Codec)
|
||||||
n += binary.PutUvarint(buf[n:], p.Codec)
|
size += varint.UvarintSize(p.MhType)
|
||||||
n += binary.PutUvarint(buf[n:], uint64(p.MhType))
|
size += varint.UvarintSize(uint64(p.MhLength))
|
||||||
n += binary.PutUvarint(buf[n:], uint64(p.MhLength))
|
|
||||||
return buf[:n]
|
buf := make([]byte, size)
|
||||||
|
n := varint.PutUvarint(buf, p.Version)
|
||||||
|
n += varint.PutUvarint(buf[n:], p.Codec)
|
||||||
|
n += varint.PutUvarint(buf[n:], p.MhType)
|
||||||
|
n += varint.PutUvarint(buf[n:], uint64(p.MhLength))
|
||||||
|
if n != size {
|
||||||
|
panic("size mismatch")
|
||||||
|
}
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrefixFromBytes parses a Prefix-byte representation onto a
|
// PrefixFromBytes parses a Prefix-byte representation onto a
|
||||||
// Prefix.
|
// Prefix.
|
||||||
func PrefixFromBytes(buf []byte) (Prefix, error) {
|
func PrefixFromBytes(buf []byte) (Prefix, error) {
|
||||||
r := bytes.NewReader(buf)
|
r := bytes.NewReader(buf)
|
||||||
vers, err := binary.ReadUvarint(r)
|
vers, err := varint.ReadUvarint(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Prefix{}, err
|
return Prefix{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
codec, err := binary.ReadUvarint(r)
|
codec, err := varint.ReadUvarint(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Prefix{}, err
|
return Prefix{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mhtype, err := binary.ReadUvarint(r)
|
mhtype, err := varint.ReadUvarint(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Prefix{}, err
|
return Prefix{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mhlen, err := binary.ReadUvarint(r)
|
mhlen, err := varint.ReadUvarint(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Prefix{}, err
|
return Prefix{}, err
|
||||||
}
|
}
|
||||||
@@ -616,8 +652,8 @@ func CidFromBytes(data []byte) (int, Cid, error) {
|
|||||||
return 34, Cid{string(h)}, nil
|
return 34, Cid{string(h)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
vers, n := binary.Uvarint(data)
|
vers, n, err := varint.FromUvarint(data)
|
||||||
if err := uvError(n); err != nil {
|
if err != nil {
|
||||||
return 0, Undef, err
|
return 0, Undef, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -625,8 +661,8 @@ func CidFromBytes(data []byte) (int, Cid, error) {
|
|||||||
return 0, Undef, fmt.Errorf("expected 1 as the cid version number, got: %d", vers)
|
return 0, Undef, fmt.Errorf("expected 1 as the cid version number, got: %d", vers)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, cn := binary.Uvarint(data[n:])
|
_, cn, err := varint.FromUvarint(data[n:])
|
||||||
if err := uvError(cn); err != nil {
|
if err != nil {
|
||||||
return 0, Undef, err
|
return 0, Undef, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
47
cid_test.go
47
cid_test.go
@@ -16,28 +16,30 @@ import (
|
|||||||
// https://github.com/multiformats/go-multihash/blob/7aa9f26a231c6f34f4e9fad52bf580fd36627285/multihash_test.go#L13
|
// https://github.com/multiformats/go-multihash/blob/7aa9f26a231c6f34f4e9fad52bf580fd36627285/multihash_test.go#L13
|
||||||
// Makes it so changing the table accidentally has to happen twice.
|
// Makes it so changing the table accidentally has to happen twice.
|
||||||
var tCodecs = map[uint64]string{
|
var tCodecs = map[uint64]string{
|
||||||
Raw: "raw",
|
Raw: "raw",
|
||||||
DagProtobuf: "protobuf",
|
DagProtobuf: "protobuf",
|
||||||
DagCBOR: "cbor",
|
DagCBOR: "cbor",
|
||||||
Libp2pKey: "libp2p-key",
|
Libp2pKey: "libp2p-key",
|
||||||
GitRaw: "git-raw",
|
GitRaw: "git-raw",
|
||||||
EthBlock: "eth-block",
|
EthBlock: "eth-block",
|
||||||
EthBlockList: "eth-block-list",
|
EthBlockList: "eth-block-list",
|
||||||
EthTxTrie: "eth-tx-trie",
|
EthTxTrie: "eth-tx-trie",
|
||||||
EthTx: "eth-tx",
|
EthTx: "eth-tx",
|
||||||
EthTxReceiptTrie: "eth-tx-receipt-trie",
|
EthTxReceiptTrie: "eth-tx-receipt-trie",
|
||||||
EthTxReceipt: "eth-tx-receipt",
|
EthTxReceipt: "eth-tx-receipt",
|
||||||
EthStateTrie: "eth-state-trie",
|
EthStateTrie: "eth-state-trie",
|
||||||
EthAccountSnapshot: "eth-account-snapshot",
|
EthAccountSnapshot: "eth-account-snapshot",
|
||||||
EthStorageTrie: "eth-storage-trie",
|
EthStorageTrie: "eth-storage-trie",
|
||||||
BitcoinBlock: "bitcoin-block",
|
BitcoinBlock: "bitcoin-block",
|
||||||
BitcoinTx: "bitcoin-tx",
|
BitcoinTx: "bitcoin-tx",
|
||||||
ZcashBlock: "zcash-block",
|
ZcashBlock: "zcash-block",
|
||||||
ZcashTx: "zcash-tx",
|
ZcashTx: "zcash-tx",
|
||||||
DecredBlock: "decred-block",
|
DecredBlock: "decred-block",
|
||||||
DecredTx: "decred-tx",
|
DecredTx: "decred-tx",
|
||||||
DashBlock: "dash-block",
|
DashBlock: "dash-block",
|
||||||
DashTx: "dash-tx",
|
DashTx: "dash-tx",
|
||||||
|
FilCommitmentUnsealed: "fil-commitment-unsealed",
|
||||||
|
FilCommitmentSealed: "fil-commitment-sealed",
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertEqual(t *testing.T, a, b Cid) {
|
func assertEqual(t *testing.T, a, b Cid) {
|
||||||
@@ -554,6 +556,7 @@ func BenchmarkStringV1(b *testing.B) {
|
|||||||
func TestReadCidsFromBuffer(t *testing.T) {
|
func TestReadCidsFromBuffer(t *testing.T) {
|
||||||
cidstr := []string{
|
cidstr := []string{
|
||||||
"bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm",
|
"bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm",
|
||||||
|
"k2cwueckqkibutvhkr4p2ln2pjcaxaakpd9db0e7j7ax1lxhhxy3ekpv",
|
||||||
"Qmf5Qzp6nGBku7CEn2UQx4mgN8TW69YUok36DrGa6NN893",
|
"Qmf5Qzp6nGBku7CEn2UQx4mgN8TW69YUok36DrGa6NN893",
|
||||||
"zb2rhZi1JR4eNc2jBGaRYJKYM8JEB4ovenym8L1CmFsRAytkz",
|
"zb2rhZi1JR4eNc2jBGaRYJKYM8JEB4ovenym8L1CmFsRAytkz",
|
||||||
}
|
}
|
||||||
|
|||||||
5
go.mod
5
go.mod
@@ -1,8 +1,9 @@
|
|||||||
module github.com/ipfs/go-cid
|
module github.com/ipfs/go-cid
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/multiformats/go-multibase v0.0.1
|
github.com/multiformats/go-multibase v0.0.3
|
||||||
github.com/multiformats/go-multihash v0.0.10
|
github.com/multiformats/go-multihash v0.0.13
|
||||||
|
github.com/multiformats/go-varint v0.0.5
|
||||||
)
|
)
|
||||||
|
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|||||||
20
go.sum
20
go.sum
@@ -4,18 +4,18 @@ github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbV
|
|||||||
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
|
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
|
||||||
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
|
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
|
||||||
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||||
github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78=
|
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
|
||||||
github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
||||||
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
|
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
|
||||||
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
|
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
|
||||||
github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmrJR+ubhT9qA=
|
github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4=
|
||||||
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
|
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
|
||||||
github.com/multiformats/go-multihash v0.0.8 h1:wrYcW5yxSi3dU07n5jnuS5PrNwyHy0zRHGVoUugWvXg=
|
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
|
||||||
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
|
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
|
||||||
github.com/multiformats/go-multihash v0.0.9 h1:aoijQXYYl7Xtb2pUUP68R+ys1TlnlR3eX6wmozr0Hp4=
|
github.com/multiformats/go-multihash v0.0.13 h1:06x+mk/zj1FoMsgNejLpy6QTvJqlSt/BhLEy87zidlc=
|
||||||
github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
|
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
|
||||||
github.com/multiformats/go-multihash v0.0.10 h1:lMoNbh2Ssd9PUF74Nz008KGzGPlfeV6wH3rit5IIGCM=
|
github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg=
|
||||||
github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
|
github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
|
||||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
|||||||
30
package.json
30
package.json
@@ -1,30 +0,0 @@
|
|||||||
{
|
|
||||||
"author": "whyrusleeping",
|
|
||||||
"bugs": {
|
|
||||||
"url": "https://github.com/ipfs/go-cid"
|
|
||||||
},
|
|
||||||
"gx": {
|
|
||||||
"dvcsimport": "github.com/ipfs/go-cid"
|
|
||||||
},
|
|
||||||
"gxDependencies": [
|
|
||||||
{
|
|
||||||
"author": "whyrusleeping",
|
|
||||||
"hash": "QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW",
|
|
||||||
"name": "go-multihash",
|
|
||||||
"version": "1.0.9"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"author": "whyrusleeping",
|
|
||||||
"hash": "QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd",
|
|
||||||
"name": "go-multibase",
|
|
||||||
"version": "0.3.0"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"gxVersion": "0.8.0",
|
|
||||||
"language": "go",
|
|
||||||
"license": "MIT",
|
|
||||||
"name": "go-cid",
|
|
||||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
|
||||||
"version": "0.9.3"
|
|
||||||
}
|
|
||||||
|
|
||||||
14
varint.go
14
varint.go
@@ -1,5 +1,9 @@
|
|||||||
package cid
|
package cid
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/multiformats/go-varint"
|
||||||
|
)
|
||||||
|
|
||||||
// Version of varint function that work with a string rather than
|
// Version of varint function that work with a string rather than
|
||||||
// []byte to avoid unnecessary allocation
|
// []byte to avoid unnecessary allocation
|
||||||
|
|
||||||
@@ -15,7 +19,7 @@ package cid
|
|||||||
// n < 0: value larger than 64 bits (overflow)
|
// n < 0: value larger than 64 bits (overflow)
|
||||||
// and -n is the number of bytes read
|
// and -n is the number of bytes read
|
||||||
//
|
//
|
||||||
func uvarint(buf string) (uint64, int) {
|
func uvarint(buf string) (uint64, int, error) {
|
||||||
var x uint64
|
var x uint64
|
||||||
var s uint
|
var s uint
|
||||||
// we have a binary string so we can't use a range loope
|
// we have a binary string so we can't use a range loope
|
||||||
@@ -23,12 +27,14 @@ func uvarint(buf string) (uint64, int) {
|
|||||||
b := buf[i]
|
b := buf[i]
|
||||||
if b < 0x80 {
|
if b < 0x80 {
|
||||||
if i > 9 || i == 9 && b > 1 {
|
if i > 9 || i == 9 && b > 1 {
|
||||||
return 0, -(i + 1) // overflow
|
return 0, 0, varint.ErrOverflow
|
||||||
|
} else if b == 0 && i > 0 {
|
||||||
|
return 0, 0, varint.ErrNotMinimal
|
||||||
}
|
}
|
||||||
return x | uint64(b)<<s, i + 1
|
return x | uint64(b)<<s, i + 1, nil
|
||||||
}
|
}
|
||||||
x |= uint64(b&0x7f) << s
|
x |= uint64(b&0x7f) << s
|
||||||
s += 7
|
s += 7
|
||||||
}
|
}
|
||||||
return 0, 0
|
return 0, 0, varint.ErrUnderflow
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,25 @@
|
|||||||
package cid
|
package cid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/multiformats/go-varint"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUvarintRoundTrip(t *testing.T) {
|
func TestUvarintRoundTrip(t *testing.T) {
|
||||||
testCases := []uint64{0, 1, 2, 127, 128, 129, 255, 256, 257, 1<<63 - 1}
|
testCases := []uint64{0, 1, 2, 127, 128, 129, 255, 256, 257, 1<<63 - 1}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
t.Log("testing", tc)
|
||||||
buf := make([]byte, 16)
|
buf := make([]byte, 16)
|
||||||
binary.PutUvarint(buf, tc)
|
varint.PutUvarint(buf, tc)
|
||||||
v, l1 := uvarint(string(buf))
|
v, l1, err := uvarint(string(buf))
|
||||||
_, l2 := binary.Uvarint(buf)
|
if err != nil {
|
||||||
|
t.Fatalf("%v: %s", buf, err)
|
||||||
|
}
|
||||||
|
_, l2, err := varint.FromUvarint(buf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
if tc != v {
|
if tc != v {
|
||||||
t.Errorf("roundtrip failed expected %d but got %d", tc, v)
|
t.Errorf("roundtrip failed expected %d but got %d", tc, v)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user