示例#1
0
func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
	if k == "" {
		return nil, ErrNotFound
	}

	maybeData, err := bs.datastore.Get(k.DsKey())
	if err == ds.ErrNotFound {
		return nil, ErrNotFound
	}
	if err != nil {
		return nil, err
	}
	bdata, ok := maybeData.([]byte)
	if !ok {
		return nil, ValueTypeMismatch
	}

	if bs.rehash {
		rb := blocks.NewBlock(bdata)
		if rb.Key() != k {
			return nil, ErrHashMismatch
		} else {
			return rb, nil
		}
	} else {
		return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
	}
}
示例#2
0
func TestRuntimeHashing(t *testing.T) {
	bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
	bl := blocks.NewBlock([]byte("some data"))
	blBad, err := blocks.NewBlockWithHash([]byte("some other data"), bl.Key().ToMultihash())
	if err != nil {
		t.Fatal("Debug is enabled")
	}

	bs.Put(blBad)
	bs.RuntimeHashing(true)

	if _, err := bs.Get(bl.Key()); err != ErrHashMismatch {
		t.Fatalf("Expected '%v' got '%v'\n", ErrHashMismatch, err)
	}
}
示例#3
0
func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
	maybeData, err := bs.datastore.Get(k.DsKey())
	if err == ds.ErrNotFound {
		return nil, ErrNotFound
	}
	if err != nil {
		return nil, err
	}
	bdata, ok := maybeData.([]byte)
	if !ok {
		return nil, ValueTypeMismatch
	}

	return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
}
示例#4
0
// Add adds a node to the dagService, storing the block in the BlockService
func (n *dagService) Add(nd *Node) (key.Key, error) {
	if n == nil { // FIXME remove this assertion. protect with constructor invariant
		return "", fmt.Errorf("dagService is nil")
	}

	d, err := nd.EncodeProtobuf(false)
	if err != nil {
		return "", err
	}

	mh, err := nd.Multihash()
	if err != nil {
		return "", err
	}

	b, _ := blocks.NewBlockWithHash(d, mh)

	return n.Blocks.AddBlock(b)
}
示例#5
0
func (t *Batch) Add(nd *Node) (key.Key, error) {
	d, err := nd.EncodeProtobuf(false)
	if err != nil {
		return "", err
	}

	mh, err := nd.Multihash()
	if err != nil {
		return "", err
	}

	b, _ := blocks.NewBlockWithHash(d, mh)

	k := key.Key(mh)

	t.blocks = append(t.blocks, b)
	t.size += len(b.Data())
	if t.size > t.MaxSize {
		return k, t.Commit()
	}
	return k, nil
}