Commit 6e0cfb32 authored by Jeromy's avatar Jeromy

removed error from return type of blocks.NewBlock()

parent 98cde157
......@@ -12,8 +12,8 @@ type Block struct {
}
// NewBlock creates a Block object from opaque data. It will hash the data.
func NewBlock(data []byte) (*Block, error) {
return &Block{Data: data, Multihash: u.Hash(data)}, nil
func NewBlock(data []byte) *Block {
return &Block{Data: data, Multihash: u.Hash(data)}
}
// Key returns the block's Multihash as a Key value.
......
......@@ -6,20 +6,11 @@ func TestBlocksBasic(t *testing.T) {
// Test empty data
empty := []byte{}
_, err := NewBlock(empty)
if err != nil {
t.Fatal(err)
}
NewBlock(empty)
// Test nil case
_, err = NewBlock(nil)
if err != nil {
t.Fatal(err)
}
NewBlock(nil)
// Test some data
_, err = NewBlock([]byte("Hello world!"))
if err != nil {
t.Fatal(err)
}
NewBlock([]byte("Hello world!"))
}
......@@ -35,7 +35,7 @@ func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
if !ok {
return nil, ValueTypeMismatch
}
return blocks.NewBlock(bdata)
return blocks.NewBlock(bdata), nil
}
func (bs *blockstore) Put(block blocks.Block) error {
......
......@@ -5,8 +5,8 @@ import (
"testing"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
blocks "github.com/jbenet/go-ipfs/blocks"
u "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
// TODO(brian): TestGetReturnsNil
......@@ -24,7 +24,7 @@ func TestGetWhenKeyNotPresent(t *testing.T) {
func TestPutThenGetBlock(t *testing.T) {
bs := NewBlockstore(ds.NewMapDatastore())
block := testutil.NewBlockOrFail(t, "some data")
block := blocks.NewBlock("some data")
err := bs.Put(block)
if err != nil {
......@@ -41,7 +41,7 @@ func TestPutThenGetBlock(t *testing.T) {
}
func TestValueTypeMismatch(t *testing.T) {
block := testutil.NewBlockOrFail(t, "some data")
block := blocks.NewBlock("some data")
datastore := ds.NewMapDatastore()
datastore.Put(block.Key().DsKey(), "data that isn't a block!")
......
......@@ -9,7 +9,7 @@ import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
"github.com/jbenet/go-ipfs/blocks"
blocks "github.com/jbenet/go-ipfs/blocks"
bstore "github.com/jbenet/go-ipfs/blockstore"
exchange "github.com/jbenet/go-ipfs/exchange"
notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications"
......@@ -18,7 +18,6 @@ import (
peer "github.com/jbenet/go-ipfs/peer"
mock "github.com/jbenet/go-ipfs/routing/mock"
util "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
func TestGetBlockTimeout(t *testing.T) {
......@@ -30,7 +29,7 @@ func TestGetBlockTimeout(t *testing.T) {
self := g.Next()
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
block := testutil.NewBlockOrFail(t, "block")
block := blocks.NewBlock([]byte("block"))
_, err := self.exchange.Block(ctx, block.Key())
if err != context.DeadlineExceeded {
......@@ -44,7 +43,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
rs := mock.VirtualRoutingServer()
g := NewSessionGenerator(net, rs)
block := testutil.NewBlockOrFail(t, "block")
block := blocks.NewBlock([]byte("block"))
rs.Announce(&peer.Peer{}, block.Key()) // but not on network
solo := g.Next()
......@@ -63,15 +62,15 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
net := tn.VirtualNetwork()
rs := mock.VirtualRoutingServer()
block := testutil.NewBlockOrFail(t, "block")
block := blocks.NewBlock([]byte("block"))
g := NewSessionGenerator(net, rs)
hasBlock := g.Next()
if err := hasBlock.blockstore.Put(block); err != nil {
if err := hasBlock.blockstore.Put(*block); err != nil {
t.Fatal(err)
}
if err := hasBlock.exchange.HasBlock(context.Background(), block); err != nil {
if err := hasBlock.exchange.HasBlock(context.Background(), *block); err != nil {
t.Fatal(err)
}
......@@ -93,7 +92,7 @@ func TestSwarm(t *testing.T) {
net := tn.VirtualNetwork()
rs := mock.VirtualRoutingServer()
sg := NewSessionGenerator(net, rs)
bg := NewBlockGenerator(t)
bg := NewBlockGenerator()
t.Log("Create a ton of instances, and just a few blocks")
......@@ -154,7 +153,7 @@ func TestSendToWantingPeer(t *testing.T) {
net := tn.VirtualNetwork()
rs := mock.VirtualRoutingServer()
sg := NewSessionGenerator(net, rs)
bg := NewBlockGenerator(t)
bg := NewBlockGenerator()
me := sg.Next()
w := sg.Next()
......@@ -212,20 +211,17 @@ func TestSendToWantingPeer(t *testing.T) {
}
}
func NewBlockGenerator(t *testing.T) BlockGenerator {
return BlockGenerator{
T: t,
}
func NewBlockGenerator() BlockGenerator {
return BlockGenerator{}
}
type BlockGenerator struct {
*testing.T // b/c block generation can fail
seq int
seq int
}
func (bg *BlockGenerator) Next() blocks.Block {
bg.seq++
return testutil.NewBlockOrFail(bg.T, string(bg.seq))
return *blocks.NewBlock([]byte(string(bg.seq)))
}
func (bg *BlockGenerator) Blocks(n int) []*blocks.Block {
......
......@@ -32,19 +32,16 @@ func New() *message {
return new(message)
}
func newMessageFromProto(pbm PBMessage) (BitSwapMessage, error) {
func newMessageFromProto(pbm PBMessage) BitSwapMessage {
m := New()
for _, s := range pbm.GetWantlist() {
m.AppendWanted(u.Key(s))
}
for _, d := range pbm.GetBlocks() {
b, err := blocks.NewBlock(d)
if err != nil {
return nil, err
}
b := blocks.NewBlock(d)
m.AppendBlock(*b)
}
return m, nil
return m
}
// TODO(brian): convert these into keys
......@@ -70,10 +67,7 @@ func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) {
if err := proto.Unmarshal(nmsg.Data(), pb); err != nil {
return nil, err
}
m, err := newMessageFromProto(*pb)
if err != nil {
return nil, err
}
m := newMessageFromProto(*pb)
return m, nil
}
......
......@@ -4,9 +4,9 @@ import (
"bytes"
"testing"
"github.com/jbenet/go-ipfs/blocks"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
func TestAppendWanted(t *testing.T) {
......@@ -26,10 +26,7 @@ func TestNewMessageFromProto(t *testing.T) {
if !contains(protoMessage.Wantlist, str) {
t.Fail()
}
m, err := newMessageFromProto(*protoMessage)
if err != nil {
t.Fatal(err)
}
m := newMessageFromProto(*protoMessage)
if !contains(m.ToProto().GetWantlist(), str) {
t.Fail()
}
......@@ -43,8 +40,8 @@ func TestAppendBlock(t *testing.T) {
m := New()
for _, str := range strs {
block := testutil.NewBlockOrFail(t, str)
m.AppendBlock(block)
block := blocks.NewBlock([]byte(str))
m.AppendBlock(*block)
}
// assert strings are in proto message
......@@ -134,10 +131,10 @@ func TestToNetFromNetPreservesWantList(t *testing.T) {
func TestToAndFromNetMessage(t *testing.T) {
original := New()
original.AppendBlock(testutil.NewBlockOrFail(t, "W"))
original.AppendBlock(testutil.NewBlockOrFail(t, "E"))
original.AppendBlock(testutil.NewBlockOrFail(t, "F"))
original.AppendBlock(testutil.NewBlockOrFail(t, "M"))
original.AppendBlock(*blocks.NewBlock([]byte("W")))
original.AppendBlock(*blocks.NewBlock([]byte("E")))
original.AppendBlock(*blocks.NewBlock([]byte("F")))
original.AppendBlock(*blocks.NewBlock([]byte("M")))
p := &peer.Peer{ID: []byte("X")}
netmsg, err := original.ToNet(p)
......
......@@ -6,25 +6,23 @@ import (
"time"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
testutil "github.com/jbenet/go-ipfs/util/testutil"
blocks "github.com/jbenet/go-ipfs/blocks"
)
func TestPublishSubscribe(t *testing.T) {
blockSent := testutil.NewBlockOrFail(t, "Greetings from The Interval")
blockSent := blocks.NewBlock([]byte("Greetings from The Interval"))
n := New()
defer n.Shutdown()
ch := n.Subscribe(context.Background(), blockSent.Key())
n.Publish(blockSent)
n.Publish(*blockSent)
blockRecvd, ok := <-ch
if !ok {
t.Fail()
}
assertBlocksEqual(t, blockRecvd, blockSent)
assertBlocksEqual(t, blockRecvd, *blockSent)
}
......@@ -35,7 +33,7 @@ func TestCarryOnWhenDeadlineExpires(t *testing.T) {
n := New()
defer n.Shutdown()
block := testutil.NewBlockOrFail(t, "A Missed Connection")
block := blocks.NewBlock([]byte("A Missed Connection"))
blockChannel := n.Subscribe(fastExpiringCtx, block.Key())
assertBlockChannelNil(t, blockChannel)
......
......@@ -4,9 +4,9 @@ import (
"strings"
"testing"
blocks "github.com/jbenet/go-ipfs/blocks"
message "github.com/jbenet/go-ipfs/exchange/bitswap/message"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
type peerAndStrategist struct {
......@@ -30,7 +30,7 @@ func TestConsistentAccounting(t *testing.T) {
m := message.New()
content := []string{"this", "is", "message", "i"}
m.AppendBlock(testutil.NewBlockOrFail(t, strings.Join(content, " ")))
m.AppendBlock(*blocks.NewBlock([]byte(strings.Join(content, " "))))
sender.MessageSent(receiver.Peer, m)
receiver.MessageReceived(sender.Peer, m)
......@@ -57,7 +57,7 @@ func TestBlockRecordedAsWantedAfterMessageReceived(t *testing.T) {
beggar := newPeerAndStrategist("can't be chooser")
chooser := newPeerAndStrategist("chooses JIF")
block := testutil.NewBlockOrFail(t, "data wanted by beggar")
block := blocks.NewBlock([]byte("data wanted by beggar"))
messageFromBeggarToChooser := message.New()
messageFromBeggarToChooser.AppendWanted(block.Key())
......
......@@ -5,10 +5,10 @@ import (
"testing"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
blocks "github.com/jbenet/go-ipfs/blocks"
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
func TestSendRequestToCooperativePeer(t *testing.T) {
......@@ -33,7 +33,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
// TODO test contents of incoming message
m := bsmsg.New()
m.AppendBlock(testutil.NewBlockOrFail(t, expectedStr))
m.AppendBlock(*blocks.NewBlock([]byte(expectedStr)))
return from, m
}))
......@@ -41,7 +41,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
t.Log("Build a message and send a synchronous request to recipient")
message := bsmsg.New()
message.AppendBlock(testutil.NewBlockOrFail(t, "data"))
message.AppendBlock(*blocks.NewBlock([]byte("data")))
response, err := initiator.SendRequest(
context.Background(), &peer.Peer{ID: idOfRecipient}, message)
if err != nil {
......@@ -77,7 +77,7 @@ func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
*peer.Peer, bsmsg.BitSwapMessage) {
msgToWaiter := bsmsg.New()
msgToWaiter.AppendBlock(testutil.NewBlockOrFail(t, expectedStr))
msgToWaiter.AppendBlock(*blocks.NewBlock([]byte(expectedStr)))
return fromWaiter, msgToWaiter
}))
......@@ -105,7 +105,7 @@ func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
}))
messageSentAsync := bsmsg.New()
messageSentAsync.AppendBlock(testutil.NewBlockOrFail(t, "data"))
messageSentAsync.AppendBlock(*blocks.NewBlock([]byte("data")))
errSending := waiter.SendMessage(
context.Background(), &peer.Peer{ID: idOfResponder}, messageSentAsync)
if errSending != nil {
......
......@@ -5,8 +5,8 @@ import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
blocks "github.com/jbenet/go-ipfs/blocks"
u "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
func TestBlockReturnsErr(t *testing.T) {
......@@ -20,8 +20,8 @@ func TestBlockReturnsErr(t *testing.T) {
func TestHasBlockReturnsNil(t *testing.T) {
off := NewOfflineExchange()
block := testutil.NewBlockOrFail(t, "data")
err := off.HasBlock(context.Background(), block)
block := blocks.NewBlock([]byte("data"))
err := off.HasBlock(context.Background(), *block)
if err != nil {
t.Fatal("")
}
......
......@@ -46,14 +46,14 @@ func (dm *DagModifier) WriteAt(b []byte, offset uint64) (int, error) {
return 0, errors.New("Attempted to perform write starting past end of file")
}
// First need to find where we are writing at
end := uint64(len(b)) + offset
// This shouldnt be necessary if we do subblocks sizes properly
newsize := dm.pbdata.GetFilesize()
if uint64(len(b))+offset > dm.pbdata.GetFilesize() {
newsize = uint64(len(b)) + offset
if end > dm.pbdata.GetFilesize() {
newsize = end
}
// First need to find where we are writing at
end := uint64(len(b)) + offset
zeroblocklen := uint64(len(dm.pbdata.Data))
origlen := len(b)
......
......@@ -110,7 +110,7 @@ func BenchmarkDagWriter(b *testing.B) {
dag := &mdag.DAGService{bserv}
b.ResetTimer()
nbytes := int64(b.N)
nbytes := int64(100000)
for i := 0; i < b.N; i++ {
b.SetBytes(nbytes)
dw := NewDagWriter(dag, &imp.SizeSplitter{4096})
......
......@@ -3,6 +3,8 @@ package merkledag
import (
"fmt"
u "github.com/jbenet/go-ipfs/util"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
)
......@@ -76,6 +78,7 @@ func (n *Node) Encoded(force bool) ([]byte, error) {
if err != nil {
return []byte{}, err
}
n.cached = u.Hash(n.encoded)
}
return n.encoded, nil
......
......@@ -24,6 +24,8 @@ type Node struct {
// cache encoded/marshaled value
encoded []byte
cached mh.Multihash
}
// Link represents an IPFS Merkle DAG Link between Nodes.
......@@ -122,12 +124,12 @@ func (n *Node) Size() (uint64, error) {
// Multihash hashes the encoded data of this node.
func (n *Node) Multihash() (mh.Multihash, error) {
b, err := n.Encoded(false)
_, err := n.Encoded(false)
if err != nil {
return nil, err
}
return u.Hash(b), nil
return n.cached, nil
}
// Key returns the Multihash as a key, for maps.
......@@ -183,7 +185,9 @@ func (n *DAGService) Add(nd *Node) (u.Key, error) {
return "", err
}
b, err := blocks.NewBlock(d)
b := new(blocks.Block)
b.Data = d
b.Multihash, err = nd.Multihash()
if err != nil {
return "", err
}
......
package testutil
import (
"testing"
blocks "github.com/jbenet/go-ipfs/blocks"
)
// NewBlockOrFail returns a block created from msgData. Signals test failure if
// creation fails.
//
// NB: NewBlockOrFail accepts a msgData parameter to avoid non-determinism in
// tests. Generating random block data could potentially result in unexpected
// behavior in tests. Thus, it is left up to the caller to select the msgData
// that will determine the blocks key.
func NewBlockOrFail(t *testing.T, msgData string) blocks.Block {
block, blockCreationErr := blocks.NewBlock([]byte(msgData))
if blockCreationErr != nil {
t.Fatal(blockCreationErr)
}
return *block
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment