Skip to content

Commit ef8e117

Browse files
committed
Revert deprecation and go-libipfs/blocks stub types
This reverts commits: - e61d558 - c3d0e42 - a6dbd28
1 parent e61d558 commit ef8e117

File tree

5 files changed

+195
-57
lines changed

5 files changed

+195
-57
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# ⚠️ This repository has been moved to https://github.com/ipfs/go-libipfs/tree/main/blocks.
2-
31
go-block-format
42
==================
53

blocks.go

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,82 @@
1-
// Deprecated: Use github.com/ipfs/go-libipfs/blocks instead.
1+
// Package blocks contains the lowest level of IPLD data structures.
2+
// A block is raw data accompanied by a CID. The CID contains the multihash
3+
// corresponding to the block.
24
package blocks
35

46
import (
5-
"github.com/ipfs/go-cid"
6-
"github.com/ipfs/go-libipfs/blocks"
7+
"errors"
8+
"fmt"
9+
10+
cid "github.com/ipfs/go-cid"
11+
u "github.com/ipfs/go-ipfs-util"
12+
mh "github.com/multiformats/go-multihash"
713
)
814

9-
// Deprecated: Use blocks.ErrWrongHash instead.
10-
var ErrWrongHash = blocks.ErrWrongHash
15+
// ErrWrongHash is returned when the Cid of a block is not the expected
16+
// according to the contents. It is currently used only when debugging.
17+
var ErrWrongHash = errors.New("data did not match given hash")
18+
19+
// Block provides abstraction for blocks implementations.
20+
type Block interface {
21+
RawData() []byte
22+
Cid() cid.Cid
23+
String() string
24+
Loggable() map[string]interface{}
25+
}
26+
27+
// A BasicBlock is a singular block of data in ipfs. It implements the Block
28+
// interface.
29+
type BasicBlock struct {
30+
cid cid.Cid
31+
data []byte
32+
}
33+
34+
// NewBlock creates a Block object from opaque data. It will hash the data.
35+
func NewBlock(data []byte) *BasicBlock {
36+
// TODO: fix assumptions
37+
return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))}
38+
}
39+
40+
// NewBlockWithCid creates a new block when the hash of the data
41+
// is already known, this is used to save time in situations where
42+
// we are able to be confident that the data is correct.
43+
func NewBlockWithCid(data []byte, c cid.Cid) (*BasicBlock, error) {
44+
if u.Debug {
45+
chkc, err := c.Prefix().Sum(data)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
if !chkc.Equals(c) {
51+
return nil, ErrWrongHash
52+
}
53+
}
54+
return &BasicBlock{data: data, cid: c}, nil
55+
}
1156

12-
// Deprecated: Use blocks.Block instead.
13-
type Block = blocks.Block
57+
// Multihash returns the hash contained in the block CID.
58+
func (b *BasicBlock) Multihash() mh.Multihash {
59+
return b.cid.Hash()
60+
}
1461

15-
// Deprecated: Use blocks.BasicBlock instead.
16-
type BasicBlock = blocks.BasicBlock
62+
// RawData returns the block raw contents as a byte slice.
63+
func (b *BasicBlock) RawData() []byte {
64+
return b.data
65+
}
66+
67+
// Cid returns the content identifier of the block.
68+
func (b *BasicBlock) Cid() cid.Cid {
69+
return b.cid
70+
}
1771

18-
// Deprecated: Use blocks.NewBlock instead.
19-
func NewBlock(data []byte) *blocks.BasicBlock {
20-
return blocks.NewBlock(data)
72+
// String provides a human-readable representation of the block CID.
73+
func (b *BasicBlock) String() string {
74+
return fmt.Sprintf("[Block %s]", b.Cid())
2175
}
2276

23-
// Deprecated: Use blocks.NewBlockWithCid instead.
24-
func NewBlockWithCid(data []byte, c cid.Cid) (*blocks.BasicBlock, error) {
25-
return blocks.NewBlockWithCid(data, c)
77+
// Loggable returns a go-log loggable item.
78+
func (b *BasicBlock) Loggable() map[string]interface{} {
79+
return map[string]interface{}{
80+
"block": b.Cid().String(),
81+
}
2682
}

blocks_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package blocks
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
cid "github.com/ipfs/go-cid"
8+
u "github.com/ipfs/go-ipfs-util"
9+
mh "github.com/multiformats/go-multihash"
10+
)
11+
12+
func TestBlocksBasic(t *testing.T) {
13+
14+
// Test empty data
15+
empty := []byte{}
16+
NewBlock(empty)
17+
18+
// Test nil case
19+
NewBlock(nil)
20+
21+
// Test some data
22+
NewBlock([]byte("Hello world!"))
23+
}
24+
25+
func TestData(t *testing.T) {
26+
data := []byte("some data")
27+
block := NewBlock(data)
28+
29+
if !bytes.Equal(block.RawData(), data) {
30+
t.Error("data is wrong")
31+
}
32+
}
33+
34+
func TestHash(t *testing.T) {
35+
data := []byte("some other data")
36+
block := NewBlock(data)
37+
38+
hash, err := mh.Sum(data, mh.SHA2_256, -1)
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
if !bytes.Equal(block.Multihash(), hash) {
44+
t.Error("wrong multihash")
45+
}
46+
}
47+
48+
func TestCid(t *testing.T) {
49+
data := []byte("yet another data")
50+
block := NewBlock(data)
51+
c := block.Cid()
52+
53+
if !bytes.Equal(block.Multihash(), c.Hash()) {
54+
t.Error("key contains wrong data")
55+
}
56+
}
57+
58+
func TestManualHash(t *testing.T) {
59+
oldDebugState := u.Debug
60+
defer (func() {
61+
u.Debug = oldDebugState
62+
})()
63+
64+
data := []byte("I can't figure out more names .. data")
65+
hash, err := mh.Sum(data, mh.SHA2_256, -1)
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
70+
c := cid.NewCidV0(hash)
71+
72+
u.Debug = false
73+
block, err := NewBlockWithCid(data, c)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
78+
if !bytes.Equal(block.Multihash(), hash) {
79+
t.Error("wrong multihash")
80+
}
81+
82+
data[5] = byte((uint32(data[5]) + 5) % 256) // Transfrom hash to be different
83+
block, err = NewBlockWithCid(data, c)
84+
if err != nil {
85+
t.Fatal(err)
86+
}
87+
88+
if !bytes.Equal(block.Multihash(), hash) {
89+
t.Error("wrong multihash")
90+
}
91+
92+
u.Debug = true
93+
94+
_, err = NewBlockWithCid(data, c)
95+
if err != ErrWrongHash {
96+
t.Fatal(err)
97+
}
98+
}

go.mod

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ module github.com/ipfs/go-block-format
33
go 1.18
44

55
require (
6-
github.com/ipfs/go-cid v0.3.2
7-
github.com/ipfs/go-libipfs v0.3.0
6+
github.com/ipfs/go-cid v0.0.7
7+
github.com/ipfs/go-ipfs-util v0.0.2
8+
github.com/multiformats/go-multihash v0.0.14
89
)
910

1011
require (
11-
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
12-
github.com/klauspost/cpuid/v2 v2.1.1 // indirect
13-
github.com/minio/sha256-simd v1.0.0 // indirect
14-
github.com/mr-tron/base58 v1.2.0 // indirect
15-
github.com/multiformats/go-base32 v0.1.0 // indirect
12+
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
13+
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 // indirect
14+
github.com/mr-tron/base58 v1.1.3 // indirect
15+
github.com/multiformats/go-base32 v0.0.3 // indirect
1616
github.com/multiformats/go-base36 v0.1.0 // indirect
17-
github.com/multiformats/go-multibase v0.1.1 // indirect
18-
github.com/multiformats/go-multihash v0.2.1 // indirect
19-
github.com/multiformats/go-varint v0.0.6 // indirect
17+
github.com/multiformats/go-multibase v0.0.3 // indirect
18+
github.com/multiformats/go-varint v0.0.5 // indirect
2019
github.com/spaolacci/murmur3 v1.1.0 // indirect
21-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
22-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
23-
lukechampine.com/blake3 v1.1.7 // indirect
20+
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 // indirect
21+
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
2422
)

go.sum

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,32 @@
1-
github.com/ipfs/go-cid v0.3.2 h1:OGgOd+JCFM+y1DjWPmVH+2/4POtpDzwcr7VgnB7mZXc=
2-
github.com/ipfs/go-cid v0.3.2/go.mod h1:gQ8pKqT/sUxGY+tIwy1RPpAojYu7jAyCp5Tz1svoupw=
1+
github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY=
2+
github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
33
github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8=
44
github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ=
5-
github.com/ipfs/go-libipfs v0.3.0 h1:YvzFWGcl88eiz2tjOheNqaeQseH+dW3fUKrSaHOG/dU=
6-
github.com/ipfs/go-libipfs v0.3.0/go.mod h1:pSUHZ5qPJTAidsxe9bAeHp3KIiw2ODEW2a2kM3v+iXI=
7-
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
8-
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
9-
github.com/klauspost/cpuid/v2 v2.1.1 h1:t0wUqjowdm8ezddV5k0tLWVklVuvLJpoHeb4WBdydm0=
10-
github.com/klauspost/cpuid/v2 v2.1.1/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
5+
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
116
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
7+
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo=
128
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
13-
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
14-
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
9+
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
10+
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
1511
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
16-
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
17-
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
18-
github.com/multiformats/go-base32 v0.1.0 h1:pVx9xoSPqEIQG8o+UbAe7DNi51oej1NtK+aGkbLYxPE=
19-
github.com/multiformats/go-base32 v0.1.0/go.mod h1:Kj3tFY6zNr+ABYMqeUNeGvkIC/UYgtWibDcT0rExnbI=
12+
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
13+
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
2014
github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4=
2115
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
22-
github.com/multiformats/go-multibase v0.1.1 h1:3ASCDsuLX8+j4kx58qnJ4YFq/JWTJpCyDW27ztsVTOI=
23-
github.com/multiformats/go-multibase v0.1.1/go.mod h1:ZEjHE+IsUrgp5mhlEAYjMtZwK1k4haNkcaPg9aoe1a8=
16+
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
17+
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
2418
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
25-
github.com/multiformats/go-multihash v0.2.1 h1:aem8ZT0VA2nCHHk7bPJ1BjUbHNciqZC/d16Vve9l108=
26-
github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc=
19+
github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I=
20+
github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
21+
github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg=
2722
github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
28-
github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY=
29-
github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
3023
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
3124
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
3225
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
26+
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
3327
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
34-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
35-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
3628
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
3729
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
30+
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
3831
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
39-
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
40-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
41-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4232
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
43-
lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0=
44-
lukechampine.com/blake3 v1.1.7/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA=

0 commit comments

Comments
 (0)