提交 01539884 作者: Juan Batiz-Benet

block get returned.

上级 a5241b76
package block
import (
"github.com/jbenet/go-multihash"
)
type Block struct {
Multihash []byte
Data []byte
}
...@@ -39,9 +39,10 @@ func NewBlockService(d ds.Datastore) (*BlockService, error) { ...@@ -39,9 +39,10 @@ func NewBlockService(d ds.Datastore) (*BlockService, error) {
return &BlockService{Datastore: d}, nil return &BlockService{Datastore: d}, nil
} }
func (s *BlockService) AddBlock(b *Block) error { func (s *BlockService) AddBlock(b *Block) (u.Key, error) {
dsk := ds.NewKey(string(b.Key())) k := b.Key()
return s.Datastore.Put(dsk, b.Data) dsk := ds.NewKey(string(k))
return k, s.Datastore.Put(dsk, b.Data)
} }
func (s *BlockService) GetBlock(k u.Key) (*Block, error) { func (s *BlockService) GetBlock(k u.Key) (*Block, error) {
......
...@@ -37,12 +37,16 @@ func TestBlocks(t *testing.T) { ...@@ -37,12 +37,16 @@ func TestBlocks(t *testing.T) {
t.Error("Block key and data multihash key not equal") t.Error("Block key and data multihash key not equal")
} }
err = bs.AddBlock(b) k, err := bs.AddBlock(b)
if err != nil { if err != nil {
t.Error("failed to add block to BlockService", err) t.Error("failed to add block to BlockService", err)
return return
} }
if k != b.Key() {
t.Error("returned key is not equal to block key", err)
}
b2, err := bs.GetBlock(b.Key()) b2, err := bs.GetBlock(b.Key())
if err != nil { if err != nil {
t.Error("failed to retrieve block from BlockService", err) t.Error("failed to retrieve block from BlockService", err)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论