blocks.go 1.84 KB
Newer Older
1 2
// package blocks contains the lowest level of ipfs data structures,
// the raw block with a checksum.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
3 4 5
package blocks

import (
Jeromy's avatar
Jeromy committed
6
	"errors"
Jeromy's avatar
Jeromy committed
7 8
	"fmt"

George Antoniadis's avatar
George Antoniadis committed
9
	key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
Jeromy's avatar
Jeromy committed
10

11 12
	mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
Jeromy's avatar
Jeromy committed
13
	cid "gx/ipfs/QmfSc2xehWmWLnwwYR91Y8QF4xdASypTFVknutoKQS3GHp/go-cid"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15
)

16
var ErrWrongHash = errors.New("data did not match given hash!")
17

18 19
type Block interface {
	Multihash() mh.Multihash
Jeromy's avatar
Jeromy committed
20
	RawData() []byte
21 22 23 24 25
	Key() key.Key
	String() string
	Loggable() map[string]interface{}
}

Jeromy's avatar
Jeromy committed
26
// Block is a singular block of data in ipfs
27
type BasicBlock struct {
28 29
	multihash mh.Multihash
	data      []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
32
// NewBlock creates a Block object from opaque data. It will hash the data.
33 34
func NewBlock(data []byte) *BasicBlock {
	return &BasicBlock{data: data, multihash: u.Hash(data)}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36
}

Jeromy's avatar
Jeromy committed
37 38 39
// NewBlockWithHash 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
40
func NewBlockWithHash(data []byte, h mh.Multihash) (*BasicBlock, error) {
Jeromy's avatar
Jeromy committed
41 42 43
	if u.Debug {
		chk := u.Hash(data)
		if string(chk) != string(h) {
44
			return nil, ErrWrongHash
Jeromy's avatar
Jeromy committed
45 46
		}
	}
47
	return &BasicBlock{data: data, multihash: h}, nil
48 49
}

50
func (b *BasicBlock) Multihash() mh.Multihash {
51 52 53
	return b.multihash
}

Jeromy's avatar
Jeromy committed
54
func (b *BasicBlock) RawData() []byte {
55
	return b.data
Jeromy's avatar
Jeromy committed
56 57
}

Jeromy's avatar
Jeromy committed
58 59 60 61
func (b *BasicBlock) Cid() *cid.Cid {
	return cid.NewCidV0(b.multihash)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
62
// Key returns the block's Multihash as a Key value.
63
func (b *BasicBlock) Key() key.Key {
64
	return key.Key(b.multihash)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
}
Jeromy's avatar
Jeromy committed
66

67
func (b *BasicBlock) String() string {
Jeromy's avatar
Jeromy committed
68 69
	return fmt.Sprintf("[Block %s]", b.Key())
}
70

71
func (b *BasicBlock) Loggable() map[string]interface{} {
72 73 74 75
	return map[string]interface{}{
		"block": b.Key().String(),
	}
}