-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert deprecation and go-libipfs/blocks stub types
- Loading branch information
Showing
5 changed files
with
195 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,82 @@ | ||
// Deprecated: Use github.com/ipfs/go-libipfs/blocks instead. | ||
// Package blocks contains the lowest level of IPLD data structures. | ||
// A block is raw data accompanied by a CID. The CID contains the multihash | ||
// corresponding to the block. | ||
package blocks | ||
|
||
import ( | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-libipfs/blocks" | ||
"errors" | ||
"fmt" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
u "github.com/ipfs/go-ipfs-util" | ||
mh "github.com/multiformats/go-multihash" | ||
) | ||
|
||
// Deprecated: Use blocks.ErrWrongHash instead. | ||
var ErrWrongHash = blocks.ErrWrongHash | ||
// ErrWrongHash is returned when the Cid of a block is not the expected | ||
// according to the contents. It is currently used only when debugging. | ||
var ErrWrongHash = errors.New("data did not match given hash") | ||
|
||
// Block provides abstraction for blocks implementations. | ||
type Block interface { | ||
RawData() []byte | ||
Cid() cid.Cid | ||
String() string | ||
Loggable() map[string]interface{} | ||
} | ||
|
||
// A BasicBlock is a singular block of data in ipfs. It implements the Block | ||
// interface. | ||
type BasicBlock struct { | ||
cid cid.Cid | ||
data []byte | ||
} | ||
|
||
// NewBlock creates a Block object from opaque data. It will hash the data. | ||
func NewBlock(data []byte) *BasicBlock { | ||
// TODO: fix assumptions | ||
return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))} | ||
} | ||
|
||
// NewBlockWithCid creates a new block when the hash of the data | ||
// is already known, this is used to save time in situations where | ||
// we are able to be confident that the data is correct. | ||
func NewBlockWithCid(data []byte, c cid.Cid) (*BasicBlock, error) { | ||
if u.Debug { | ||
chkc, err := c.Prefix().Sum(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !chkc.Equals(c) { | ||
return nil, ErrWrongHash | ||
} | ||
} | ||
return &BasicBlock{data: data, cid: c}, nil | ||
} | ||
|
||
// Deprecated: Use blocks.Block instead. | ||
type Block = blocks.Block | ||
// Multihash returns the hash contained in the block CID. | ||
func (b *BasicBlock) Multihash() mh.Multihash { | ||
return b.cid.Hash() | ||
} | ||
|
||
// Deprecated: Use blocks.BasicBlock instead. | ||
type BasicBlock = blocks.BasicBlock | ||
// RawData returns the block raw contents as a byte slice. | ||
func (b *BasicBlock) RawData() []byte { | ||
return b.data | ||
} | ||
|
||
// Cid returns the content identifier of the block. | ||
func (b *BasicBlock) Cid() cid.Cid { | ||
return b.cid | ||
} | ||
|
||
// Deprecated: Use blocks.NewBlock instead. | ||
func NewBlock(data []byte) *blocks.BasicBlock { | ||
return blocks.NewBlock(data) | ||
// String provides a human-readable representation of the block CID. | ||
func (b *BasicBlock) String() string { | ||
return fmt.Sprintf("[Block %s]", b.Cid()) | ||
} | ||
|
||
// Deprecated: Use blocks.NewBlockWithCid instead. | ||
func NewBlockWithCid(data []byte, c cid.Cid) (*blocks.BasicBlock, error) { | ||
return blocks.NewBlockWithCid(data, c) | ||
// Loggable returns a go-log loggable item. | ||
func (b *BasicBlock) Loggable() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"block": b.Cid().String(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package blocks | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
u "github.com/ipfs/go-ipfs-util" | ||
mh "github.com/multiformats/go-multihash" | ||
) | ||
|
||
func TestBlocksBasic(t *testing.T) { | ||
|
||
// Test empty data | ||
empty := []byte{} | ||
NewBlock(empty) | ||
|
||
// Test nil case | ||
NewBlock(nil) | ||
|
||
// Test some data | ||
NewBlock([]byte("Hello world!")) | ||
} | ||
|
||
func TestData(t *testing.T) { | ||
data := []byte("some data") | ||
block := NewBlock(data) | ||
|
||
if !bytes.Equal(block.RawData(), data) { | ||
t.Error("data is wrong") | ||
} | ||
} | ||
|
||
func TestHash(t *testing.T) { | ||
data := []byte("some other data") | ||
block := NewBlock(data) | ||
|
||
hash, err := mh.Sum(data, mh.SHA2_256, -1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(block.Multihash(), hash) { | ||
t.Error("wrong multihash") | ||
} | ||
} | ||
|
||
func TestCid(t *testing.T) { | ||
data := []byte("yet another data") | ||
block := NewBlock(data) | ||
c := block.Cid() | ||
|
||
if !bytes.Equal(block.Multihash(), c.Hash()) { | ||
t.Error("key contains wrong data") | ||
} | ||
} | ||
|
||
func TestManualHash(t *testing.T) { | ||
oldDebugState := u.Debug | ||
defer (func() { | ||
u.Debug = oldDebugState | ||
})() | ||
|
||
data := []byte("I can't figure out more names .. data") | ||
hash, err := mh.Sum(data, mh.SHA2_256, -1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
c := cid.NewCidV0(hash) | ||
|
||
u.Debug = false | ||
block, err := NewBlockWithCid(data, c) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(block.Multihash(), hash) { | ||
t.Error("wrong multihash") | ||
} | ||
|
||
data[5] = byte((uint32(data[5]) + 5) % 256) // Transfrom hash to be different | ||
block, err = NewBlockWithCid(data, c) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(block.Multihash(), hash) { | ||
t.Error("wrong multihash") | ||
} | ||
|
||
u.Debug = true | ||
|
||
_, err = NewBlockWithCid(data, c) | ||
if err != ErrWrongHash { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,32 @@ | ||
github.com/ipfs/go-cid v0.3.2 h1:OGgOd+JCFM+y1DjWPmVH+2/4POtpDzwcr7VgnB7mZXc= | ||
github.com/ipfs/go-cid v0.3.2/go.mod h1:gQ8pKqT/sUxGY+tIwy1RPpAojYu7jAyCp5Tz1svoupw= | ||
github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY= | ||
github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= | ||
github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8= | ||
github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ= | ||
github.com/ipfs/go-libipfs v0.3.0 h1:YvzFWGcl88eiz2tjOheNqaeQseH+dW3fUKrSaHOG/dU= | ||
github.com/ipfs/go-libipfs v0.3.0/go.mod h1:pSUHZ5qPJTAidsxe9bAeHp3KIiw2ODEW2a2kM3v+iXI= | ||
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= | ||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= | ||
github.com/klauspost/cpuid/v2 v2.1.1 h1:t0wUqjowdm8ezddV5k0tLWVklVuvLJpoHeb4WBdydm0= | ||
github.com/klauspost/cpuid/v2 v2.1.1/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= | ||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g= | ||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= | ||
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo= | ||
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= | ||
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= | ||
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= | ||
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= | ||
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc= | ||
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= | ||
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= | ||
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= | ||
github.com/multiformats/go-base32 v0.1.0 h1:pVx9xoSPqEIQG8o+UbAe7DNi51oej1NtK+aGkbLYxPE= | ||
github.com/multiformats/go-base32 v0.1.0/go.mod h1:Kj3tFY6zNr+ABYMqeUNeGvkIC/UYgtWibDcT0rExnbI= | ||
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-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= | ||
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= | ||
github.com/multiformats/go-multibase v0.1.1 h1:3ASCDsuLX8+j4kx58qnJ4YFq/JWTJpCyDW27ztsVTOI= | ||
github.com/multiformats/go-multibase v0.1.1/go.mod h1:ZEjHE+IsUrgp5mhlEAYjMtZwK1k4haNkcaPg9aoe1a8= | ||
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= | ||
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= | ||
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= | ||
github.com/multiformats/go-multihash v0.2.1 h1:aem8ZT0VA2nCHHk7bPJ1BjUbHNciqZC/d16Vve9l108= | ||
github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc= | ||
github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I= | ||
github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= | ||
github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg= | ||
github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= | ||
github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY= | ||
github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= | ||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= | ||
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-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU= | ||
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM= | ||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0= | ||
lukechampine.com/blake3 v1.1.7/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA= |