mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
blocks: move block format to it's own repo
We need to reference it from outside of this repo. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
This commit is contained in:
parent
fa5c0d40cd
commit
bccd4d4e8f
@ -1,82 +0,0 @@
|
||||
// Package blocks contains the lowest level of IPFS data structures.
|
||||
// A block is raw data accompanied by a CID. The CID contains the multihash
|
||||
// corresponding to the block.
|
||||
package blocks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
mh "gx/ipfs/QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHw/go-multihash"
|
||||
u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Multihash returns the hash contained in the block CID.
|
||||
func (b *BasicBlock) Multihash() mh.Multihash {
|
||||
return b.cid.Hash()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// String provides a human-readable representation of the block CID.
|
||||
func (b *BasicBlock) String() string {
|
||||
return fmt.Sprintf("[Block %s]", b.Cid())
|
||||
}
|
||||
|
||||
// Loggable returns a go-log loggable item.
|
||||
func (b *BasicBlock) Loggable() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"block": b.Cid().String(),
|
||||
}
|
||||
}
|
||||
@ -1,98 +0,0 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
mh "gx/ipfs/QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHw/go-multihash"
|
||||
u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -3,7 +3,7 @@ package blockstore
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
|
||||
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
|
||||
"gx/ipfs/QmRg1gKTHzc3CZXSKzem8aR4E3TubFhbgXwfVuWnSK5CC5/go-metrics-interface"
|
||||
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
|
||||
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
|
||||
syncds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore/sync"
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"
|
||||
|
||||
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"
|
||||
|
||||
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
|
||||
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
|
||||
"gx/ipfs/QmRg1gKTHzc3CZXSKzem8aR4E3TubFhbgXwfVuWnSK5CC5/go-metrics-interface"
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
|
||||
context "context"
|
||||
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// with Blocks.
|
||||
package blocksutil
|
||||
|
||||
import "github.com/ipfs/go-ipfs/blocks"
|
||||
import "github.com/ipfs/go-block-format"
|
||||
|
||||
// NewBlockGenerator returns an object capable of
|
||||
// producing blocks.
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
exchange "github.com/ipfs/go-ipfs/exchange"
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ package blockservice
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
butil "github.com/ipfs/go-ipfs/blocks/blocksutil"
|
||||
offline "github.com/ipfs/go-ipfs/exchange/offline"
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
. "github.com/ipfs/go-ipfs/blockservice"
|
||||
offline "github.com/ipfs/go-ipfs/exchange/offline"
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
util "github.com/ipfs/go-ipfs/blocks/blockstore/util"
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
core "github.com/ipfs/go-ipfs/core"
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
"github.com/ipfs/go-ipfs/blockservice"
|
||||
"github.com/ipfs/go-ipfs/commands/files"
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
exchange "github.com/ipfs/go-ipfs/exchange"
|
||||
decision "github.com/ipfs/go-ipfs/exchange/bitswap/decision"
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
|
||||
decision "github.com/ipfs/go-ipfs/exchange/bitswap/decision"
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
context "context"
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
|
||||
wl "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
"testing"
|
||||
|
||||
context "context"
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
message "github.com/ipfs/go-ipfs/exchange/bitswap/message"
|
||||
testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
|
||||
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
pb "github.com/ipfs/go-ipfs/exchange/bitswap/message/pb"
|
||||
wantlist "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
|
||||
proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
pb "github.com/ipfs/go-ipfs/exchange/bitswap/message/pb"
|
||||
u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
|
||||
@ -3,7 +3,7 @@ package notifications
|
||||
import (
|
||||
"context"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
|
||||
pubsub "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/briantigerchow/pubsub"
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
)
|
||||
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
context "context"
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
|
||||
bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
|
||||
mockrouting "github.com/ipfs/go-ipfs/routing/mock"
|
||||
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
)
|
||||
|
||||
@ -5,7 +5,7 @@ package offline
|
||||
import (
|
||||
"context"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
exchange "github.com/ipfs/go-ipfs/exchange"
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
"github.com/ipfs/go-ipfs/blocks/blocksutil"
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
context "context"
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
mock "github.com/ipfs/go-ipfs/routing/mock"
|
||||
testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
|
||||
|
||||
@ -10,7 +10,7 @@ package filestore
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
posinfo "github.com/ipfs/go-ipfs/thirdparty/posinfo"
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
pb "github.com/ipfs/go-ipfs/filestore/pb"
|
||||
dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"
|
||||
|
||||
@ -3,7 +3,7 @@ package chunk
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
"gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
bserv "github.com/ipfs/go-ipfs/blockservice"
|
||||
offline "github.com/ipfs/go-ipfs/exchange/offline"
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
blocks "github.com/ipfs/go-ipfs/blocks"
|
||||
blocks "github.com/ipfs/go-block-format"
|
||||
bserv "github.com/ipfs/go-ipfs/blockservice"
|
||||
bstest "github.com/ipfs/go-ipfs/blockservice/test"
|
||||
offline "github.com/ipfs/go-ipfs/exchange/offline"
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package merkledag
|
||||
|
||||
import (
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
|
||||
u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/ipfs/go-ipfs/blocks"
|
||||
"github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-ipfs/core"
|
||||
"github.com/ipfs/go-ipfs/core/mock"
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user