Skip to content

Commit

Permalink
refactor: remove Codecs table
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Mar 24, 2022
1 parent da55247 commit 6ca2617
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 131 deletions.
75 changes: 0 additions & 75 deletions cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,9 @@ const (
DagJSON = 0x0129 // https://ipld.io/docs/codecs/known/dag-json/
Libp2pKey = 0x72 // https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#peer-ids

// generic, non-dag variants
Protobuf = 0x50
CBOR = 0x51
JSON = 0x0200

// other
GitRaw = 0x78
DagJOSE = 0x85 // https://ipld.io/specs/codecs/dag-jose/spec/
DagCOSE = 0x86
EthBlock = 0x90
EthBlockList = 0x91
EthTxTrie = 0x92
Expand All @@ -88,75 +82,6 @@ const (
FilCommitmentSealed = 0xf102
)

// Codecs maps the name of a codec to its type
var Codecs = map[string]uint64{
"v0": DagProtobuf, // TODO: remove?
"raw": Raw,
"dag-pb": DagProtobuf,
"dag-cbor": DagCBOR,
"dag-json": DagJSON,
"libp2p-key": Libp2pKey,
"protobuf": Protobuf,
"cbor": CBOR,
"json": JSON,
"git-raw": GitRaw,
"eth-block": EthBlock,
"eth-block-list": EthBlockList,
"eth-tx-trie": EthTxTrie,
"eth-tx": EthTx,
"eth-tx-receipt-trie": EthTxReceiptTrie,
"eth-tx-receipt": EthTxReceipt,
"eth-state-trie": EthStateTrie,
"eth-account-snapshot": EthAccountSnapshot,
"eth-storage-trie": EthStorageTrie,
"bitcoin-block": BitcoinBlock,
"bitcoin-tx": BitcoinTx,
"zcash-block": ZcashBlock,
"zcash-tx": ZcashTx,
"decred-block": DecredBlock,
"decred-tx": DecredTx,
"dash-block": DashBlock,
"dash-tx": DashTx,
"fil-commitment-unsealed": FilCommitmentUnsealed,
"fil-commitment-sealed": FilCommitmentSealed,
"dag-jose": DagJOSE,
"dag-cose": DagCOSE,
}

// CodecToStr maps the numeric codec to its name
var CodecToStr = map[uint64]string{
Raw: "raw",
DagProtobuf: "dag-pb",
DagCBOR: "dag-cbor",
DagJSON: "dag-json",
Libp2pKey: "libp2p-key",
Protobuf: "protobuf",
CBOR: "cbor",
JSON: "json",
GitRaw: "git-raw",
EthBlock: "eth-block",
EthBlockList: "eth-block-list",
EthTxTrie: "eth-tx-trie",
EthTx: "eth-tx",
EthTxReceiptTrie: "eth-tx-receipt-trie",
EthTxReceipt: "eth-tx-receipt",
EthStateTrie: "eth-state-trie",
EthAccountSnapshot: "eth-account-snapshot",
EthStorageTrie: "eth-storage-trie",
BitcoinBlock: "bitcoin-block",
BitcoinTx: "bitcoin-tx",
ZcashBlock: "zcash-block",
ZcashTx: "zcash-tx",
DecredBlock: "decred-block",
DecredTx: "decred-tx",
DashBlock: "dash-block",
DashTx: "dash-tx",
FilCommitmentUnsealed: "fil-commitment-unsealed",
FilCommitmentSealed: "fil-commitment-sealed",
DagJOSE: "dag-jose",
DagCOSE: "dag-cose",
}

// tryNewCidV0 tries to convert a multihash into a CIDv0 CID and returns an
// error on failure.
func tryNewCidV0(mhash mh.Multihash) (Cid, error) {
Expand Down
56 changes: 0 additions & 56 deletions cid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,6 @@ import (
mh "github.com/multiformats/go-multihash"
)

// Copying the "silly test" idea from
// https://github.com/multiformats/go-multihash/blob/7aa9f26a231c6f34f4e9fad52bf580fd36627285/multihash_test.go#L13
// Makes it so changing the table accidentally has to happen twice.
var tCodecs = map[uint64]string{
Raw: "raw",
DagProtobuf: "dag-pb",
DagCBOR: "dag-cbor",
DagJSON: "dag-json",
Libp2pKey: "libp2p-key",
Protobuf: "protobuf",
CBOR: "cbor",
JSON: "json",
GitRaw: "git-raw",
EthBlock: "eth-block",
EthBlockList: "eth-block-list",
EthTxTrie: "eth-tx-trie",
EthTx: "eth-tx",
EthTxReceiptTrie: "eth-tx-receipt-trie",
EthTxReceipt: "eth-tx-receipt",
EthStateTrie: "eth-state-trie",
EthAccountSnapshot: "eth-account-snapshot",
EthStorageTrie: "eth-storage-trie",
BitcoinBlock: "bitcoin-block",
BitcoinTx: "bitcoin-tx",
ZcashBlock: "zcash-block",
ZcashTx: "zcash-tx",
DecredBlock: "decred-block",
DecredTx: "decred-tx",
DashBlock: "dash-block",
DashTx: "dash-tx",
FilCommitmentUnsealed: "fil-commitment-unsealed",
FilCommitmentSealed: "fil-commitment-sealed",
DagJOSE: "dag-jose",
DagCOSE: "dag-cose",
}

func assertEqual(t *testing.T, a, b Cid) {
if a.Type() != b.Type() {
t.Fatal("mismatch on type")
Expand All @@ -65,26 +29,6 @@ func assertEqual(t *testing.T, a, b Cid) {
}
}

func TestTable(t *testing.T) {
if len(tCodecs) != len(Codecs)-1 {
t.Errorf("Item count mismatch in the Table of Codec. Should be %d, got %d", len(tCodecs)+1, len(Codecs))
}

for k, v := range tCodecs {
if Codecs[v] != k {
t.Errorf("Table mismatch: 0x%x %s", k, v)
}
}
}

// The table returns cid.DagProtobuf for "v0"
// so we test it apart
func TestTableForV0(t *testing.T) {
if Codecs["v0"] != DagProtobuf {
t.Error("Table mismatch: Codecs[\"v0\"] should resolve to DagProtobuf (0x70)")
}
}

func TestPrefixSum(t *testing.T) {
// Test creating CIDs both manually and with Prefix.
// Tests: https://github.com/ipfs/go-cid/issues/83
Expand Down

0 comments on commit 6ca2617

Please sign in to comment.