• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang blocks.NewBlock函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/ipfs/go-ipfs/blocks.NewBlock函数的典型用法代码示例。如果您正苦于以下问题:Golang NewBlock函数的具体用法?Golang NewBlock怎么用?Golang NewBlock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewBlock函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestToAndFromNetMessage

func TestToAndFromNetMessage(t *testing.T) {

	original := New(true)
	original.AddBlock(blocks.NewBlock([]byte("W")))
	original.AddBlock(blocks.NewBlock([]byte("E")))
	original.AddBlock(blocks.NewBlock([]byte("F")))
	original.AddBlock(blocks.NewBlock([]byte("M")))

	buf := new(bytes.Buffer)
	if err := original.ToNet(buf); err != nil {
		t.Fatal(err)
	}

	m2, err := FromNet(buf)
	if err != nil {
		t.Fatal(err)
	}

	keys := make(map[key.Key]bool)
	for _, b := range m2.Blocks() {
		keys[b.Key()] = true
	}

	for _, b := range original.Blocks() {
		if _, ok := keys[b.Key()]; !ok {
			t.Fail()
		}
	}
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:29,代码来源:message_test.go


示例2: TestPutManyAddsToBloom

func TestPutManyAddsToBloom(t *testing.T) {
	bs := NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))

	ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
	cachedbs, err := testBloomCached(bs, ctx)

	select {
	case <-cachedbs.rebuildChan:
	case <-ctx.Done():
		t.Fatalf("Timeout wating for rebuild: %d", cachedbs.bloom.ElementsAdded())
	}

	block1 := blocks.NewBlock([]byte("foo"))
	block2 := blocks.NewBlock([]byte("bar"))

	cachedbs.PutMany([]blocks.Block{block1})
	has, err := cachedbs.Has(block1.Cid())
	if err != nil {
		t.Fatal(err)
	}
	if has == false {
		t.Fatal("added block is reported missing")
	}

	has, err = cachedbs.Has(block2.Cid())
	if err != nil {
		t.Fatal(err)
	}
	if has == true {
		t.Fatal("not added block is reported to be in blockstore")
	}
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:32,代码来源:bloom_cache_test.go


示例3: TestHasIsBloomCached

func TestHasIsBloomCached(t *testing.T) {
	cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()}
	bs := NewBlockstore(syncds.MutexWrap(cd))

	for i := 0; i < 1000; i++ {
		bs.Put(blocks.NewBlock([]byte(fmt.Sprintf("data: %d", i))))
	}
	ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
	cachedbs, err := testBloomCached(bs, ctx)
	if err != nil {
		t.Fatal(err)
	}

	select {
	case <-cachedbs.rebuildChan:
	case <-ctx.Done():
		t.Fatalf("Timeout wating for rebuild: %d", cachedbs.bloom.ElementsAdded())
	}

	cacheFails := 0
	cd.SetFunc(func() {
		cacheFails++
	})

	for i := 0; i < 1000; i++ {
		cachedbs.Has(blocks.NewBlock([]byte(fmt.Sprintf("data: %d", i+2000))).Key())
	}

	if float64(cacheFails)/float64(1000) > float64(0.05) {
		t.Fatal("Bloom filter has cache miss rate of more than 5%")
	}
}
开发者ID:yuanwr,项目名称:go-ipfs,代码行数:32,代码来源:bloom_cache_test.go


示例4: TestHasIsBloomCached

func TestHasIsBloomCached(t *testing.T) {
	cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()}
	bs := NewBlockstore(syncds.MutexWrap(cd))

	for i := 0; i < 1000; i++ {
		bs.Put(blocks.NewBlock([]byte(fmt.Sprintf("data: %d", i))))
	}
	ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
	cachedbs, err := testBloomCached(bs, ctx)
	if err != nil {
		t.Fatal(err)
	}

	select {
	case <-cachedbs.rebuildChan:
	case <-ctx.Done():
		t.Fatalf("Timeout wating for rebuild: %d", cachedbs.bloom.ElementsAdded())
	}

	cacheFails := 0
	cd.SetFunc(func() {
		cacheFails++
	})

	for i := 0; i < 1000; i++ {
		cachedbs.Has(blocks.NewBlock([]byte(fmt.Sprintf("data: %d", i+2000))).Cid())
	}

	if float64(cacheFails)/float64(1000) > float64(0.05) {
		t.Fatal("Bloom filter has cache miss rate of more than 5%")
	}

	cacheFails = 0
	block := blocks.NewBlock([]byte("newBlock"))

	cachedbs.PutMany([]blocks.Block{block})
	if cacheFails != 2 {
		t.Fatalf("expected two datastore hits: %d", cacheFails)
	}
	cachedbs.Put(block)
	if cacheFails != 3 {
		t.Fatalf("expected datastore hit: %d", cacheFails)
	}

	if has, err := cachedbs.Has(block.Cid()); !has || err != nil {
		t.Fatal("has gave wrong response")
	}

	bl, err := cachedbs.Get(block.Cid())
	if bl.String() != block.String() {
		t.Fatal("block data doesn't match")
	}

	if err != nil {
		t.Fatal("there should't be an error")
	}
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:57,代码来源:bloom_cache_test.go


示例5: TestSendMessageAsyncButWaitForResponse

func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
	net := VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
	responderPeer := testutil.RandIdentityOrFatal(t)
	waiter := net.Adapter(testutil.RandIdentityOrFatal(t))
	responder := net.Adapter(responderPeer)

	var wg sync.WaitGroup

	wg.Add(1)

	expectedStr := "received async"

	responder.SetDelegate(lambda(func(
		ctx context.Context,
		fromWaiter peer.ID,
		msgFromWaiter bsmsg.BitSwapMessage) {

		msgToWaiter := bsmsg.New(true)
		msgToWaiter.AddBlock(blocks.NewBlock([]byte(expectedStr)))
		waiter.SendMessage(ctx, fromWaiter, msgToWaiter)
	}))

	waiter.SetDelegate(lambda(func(
		ctx context.Context,
		fromResponder peer.ID,
		msgFromResponder bsmsg.BitSwapMessage) {

		// TODO assert that this came from the correct peer and that the message contents are as expected
		ok := false
		for _, b := range msgFromResponder.Blocks() {
			if string(b.Data) == expectedStr {
				wg.Done()
				ok = true
			}
		}

		if !ok {
			t.Fatal("Message not received from the responder")
		}
	}))

	messageSentAsync := bsmsg.New(true)
	messageSentAsync.AddBlock(blocks.NewBlock([]byte("data")))
	errSending := waiter.SendMessage(
		context.Background(), responderPeer.ID(), messageSentAsync)
	if errSending != nil {
		t.Fatal(errSending)
	}

	wg.Wait() // until waiter delegate function is executed
}
开发者ID:kalmi,项目名称:go-ipfs,代码行数:51,代码来源:network_test.go


示例6: BenchmarkHandle10KBlocks

func BenchmarkHandle10KBlocks(b *testing.B) {
	bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
	var testdata []*blocks.Block
	for i := 0; i < 10000; i++ {
		testdata = append(testdata, blocks.NewBlock([]byte(string(i))))
	}
	b.ResetTimer()
	b.SetBytes(10000)
	for i := 0; i < b.N; i++ {

		b.StopTimer()
		w := NewWorker(offline.Exchange(bstore), Config{
			NumWorkers:       1,
			ClientBufferSize: 0,
			WorkerBufferSize: 0,
		})
		b.StartTimer()

		for _, block := range testdata {
			if err := w.HasBlock(block); err != nil {
				b.Fatal(err)
			}
		}

		b.StopTimer()
		w.Close()
		b.StartTimer()

	}
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:30,代码来源:bench_worker_test.go


示例7: Get

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))
	}
}
开发者ID:yuanwr,项目名称:go-ipfs,代码行数:28,代码来源:blockstore.go


示例8: BenchmarkRandomBlockWrites

func BenchmarkRandomBlockWrites(n *core.IpfsNode, cfg *BenchCfg) error {
	buf := make([]byte, cfg.Blocksize)
	read := randbo.New()

	var keys []key.Key
	f := func(b *testing.B) {
		b.SetBytes(cfg.Blocksize)
		for i := 0; i < b.N; i++ {
			read.Read(buf)
			blk := blocks.NewBlock(buf)
			k, err := n.Blocks.AddBlock(blk)
			if err != nil {
				b.Fatal(err)
			}

			keys = append(keys, k)
		}
	}

	br := testing.Benchmark(f)
	fmt.Printf("RandomBlockWrites:\t%s\n", br)

	// clean up
	for _, k := range keys {
		err := n.Blocks.DeleteBlock(k)
		if err != nil {
			return err
		}
	}

	return nil
}
开发者ID:whyrusleeping,项目名称:repobench,代码行数:32,代码来源:main.go


示例9: BenchmarkBlockRewrites

func BenchmarkBlockRewrites(n *core.IpfsNode, cfg *BenchCfg) error {
	buf := make([]byte, cfg.Blocksize)
	randbo.New().Read(buf)

	blk := blocks.NewBlock(buf)
	// write the block first, before starting the benchmark.
	// we're just looking at the time it takes to write a block thats already
	// been written
	k, err := n.Blocks.AddBlock(blk)
	if err != nil {
		return err
	}

	f := func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			_, err := n.Blocks.AddBlock(blk)
			if err != nil {
				b.Fatal(err)
			}
		}
	}

	br := testing.Benchmark(f)
	fmt.Printf("BlockRewrites:\t\t%s\n", br)

	// clean up
	err = n.Blocks.DeleteBlock(k)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:whyrusleeping,项目名称:repobench,代码行数:33,代码来源:main.go


示例10: TestConsistentAccounting

func TestConsistentAccounting(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	sender := newEngine(ctx, "Ernie")
	receiver := newEngine(ctx, "Bert")

	// Send messages from Ernie to Bert
	for i := 0; i < 1000; i++ {

		m := message.New(false)
		content := []string{"this", "is", "message", "i"}
		m.AddBlock(blocks.NewBlock([]byte(strings.Join(content, " "))))

		sender.Engine.MessageSent(receiver.Peer, m)
		receiver.Engine.MessageReceived(sender.Peer, m)
	}

	// Ensure sender records the change
	if sender.Engine.numBytesSentTo(receiver.Peer) == 0 {
		t.Fatal("Sent bytes were not recorded")
	}

	// Ensure sender and receiver have the same values
	if sender.Engine.numBytesSentTo(receiver.Peer) != receiver.Engine.numBytesReceivedFrom(sender.Peer) {
		t.Fatal("Inconsistent book-keeping. Strategies don't agree")
	}

	// Ensure sender didn't record receving anything. And that the receiver
	// didn't record sending anything
	if receiver.Engine.numBytesSentTo(sender.Peer) != 0 || sender.Engine.numBytesReceivedFrom(receiver.Peer) != 0 {
		t.Fatal("Bert didn't send bytes to Ernie")
	}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:33,代码来源:engine_test.go


示例11: TestGetBlockFromPeerAfterPeerAnnounces

func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {

	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
	block := blocks.NewBlock([]byte("block"))
	g := NewTestSessionGenerator(net)
	defer g.Close()

	peers := g.Instances(2)
	hasBlock := peers[0]
	defer hasBlock.Exchange.Close()

	if err := hasBlock.Exchange.HasBlock(block); err != nil {
		t.Fatal(err)
	}

	wantsBlock := peers[1]
	defer wantsBlock.Exchange.Close()

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	received, err := wantsBlock.Exchange.GetBlock(ctx, block.Key())
	if err != nil {
		t.Log(err)
		t.Fatal("Expected to succeed")
	}

	if !bytes.Equal(block.Data, received.Data) {
		t.Fatal("Data doesn't match")
	}
}
开发者ID:musha68k,项目名称:go-ipfs,代码行数:30,代码来源:bitswap_test.go


示例12: execute

func execute(cmdString string) error {
	//  Check for comment
	if cmdString[0] == '#' {
		return nil
	}

	if strings.Contains(cmdString, "->") {
		return connectCmd(cmdString)
	}

	//  Check for dummy file command, could move some of this into a method in dummyfiles.go
	split := strings.Split(cmdString, " ")
	if split[0] == "create_dummy_files" {
		numfiles, err := strconv.Atoi(split[1])
		if err != nil {
			log.Fatalf("Line %d: Invalid argument for create_dummy_files.", currLine)
		}

		filesize, err := strconv.Atoi(split[2])
		if err != nil {
			log.Fatalf("Line %d: Invalid argument for create_dummy_files.", currLine)
		}

		createDummyFiles(numfiles, filesize)
		return nil
	}

	command := split[1]
	arg := split[2]

	//  Command in form "node# get/put/leave arg"
	nodes := getRange(split[0])
	switch command {
	case "putb":
		return putCmd(nodes, blocks.NewBlock([]byte(arg)))
	case "put":
		return putFileCmd(nodes, arg)
	case "getb":
		return getCmd(nodes, blocks.NewBlock([]byte(arg)))
	case "get":
		return getFileCmd(nodes, arg)
	case "leave":
		return leaveCmd(nodes, arg)
	default:
		return fmt.Errorf("Error on line %d: expected get/put/leave, found %s.", currLine, command)
	}
}
开发者ID:rht,项目名称:bssim,代码行数:47,代码来源:main.go


示例13: partnerWants

func partnerWants(e *Engine, keys []string, partner peer.ID) {
	add := message.New(false)
	for i, letter := range keys {
		block := blocks.NewBlock([]byte(letter))
		add.AddEntry(block.Cid(), math.MaxInt32-i)
	}
	e.MessageReceived(partner, add)
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:8,代码来源:engine_test.go


示例14: partnerCancels

func partnerCancels(e *Engine, keys []string, partner peer.ID) {
	cancels := message.New(false)
	for _, k := range keys {
		block := blocks.NewBlock([]byte(k))
		cancels.Cancel(block.Cid())
	}
	e.MessageReceived(partner, cancels)
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:8,代码来源:engine_test.go


示例15: newMessageFromProto

func newMessageFromProto(pbm pb.Message) BitSwapMessage {
	m := newMsg(pbm.GetWantlist().GetFull())
	for _, e := range pbm.GetWantlist().GetEntries() {
		m.addEntry(key.Key(e.GetBlock()), int(e.GetPriority()), e.GetCancel())
	}
	for _, d := range pbm.GetBlocks() {
		b := blocks.NewBlock(d)
		m.AddBlock(b)
	}
	return m
}
开发者ID:ccsblueboy,项目名称:go-ipfs,代码行数:11,代码来源:message.go


示例16: TestCarryOnWhenDeadlineExpires

func TestCarryOnWhenDeadlineExpires(t *testing.T) {

	impossibleDeadline := time.Nanosecond
	fastExpiringCtx, _ := context.WithTimeout(context.Background(), impossibleDeadline)

	n := New()
	defer n.Shutdown()
	block := blocks.NewBlock([]byte("A Missed Connection"))
	blockChannel := n.Subscribe(fastExpiringCtx, block.Key())

	assertBlockChannelNil(t, blockChannel)
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:12,代码来源:notifications_test.go


示例17: checkHandledInOrder

func checkHandledInOrder(t *testing.T, e *Engine, keys []string) error {
	for _, k := range keys {
		next := <-e.Outbox()
		envelope := <-next
		received := envelope.Block
		expected := blocks.NewBlock([]byte(k))
		if !received.Cid().Equals(expected.Cid()) {
			return errors.New(fmt.Sprintln("received", string(received.RawData()), "expected", string(expected.RawData())))
		}
	}
	return nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:12,代码来源:engine_test.go


示例18: TestPutBlockCmd

func TestPutBlockCmd(t *testing.T) {
	configure("node_count:1", nil)
	net, peers = createTestNetwork()

	testBlock := blocks.NewBlock([]byte("testblock"))
	err := putCmd([]int{0}, testBlock)
	check(err)

	if !checkHasBlock([]int{0}, testBlock.Key()) {
		t.Error("Peer 0 never add block to blockstore.")
	}
}
开发者ID:rht,项目名称:bssim,代码行数:12,代码来源:bssim_test.go


示例19: TestSubscribeMany

func TestSubscribeMany(t *testing.T) {
	e1 := blocks.NewBlock([]byte("1"))
	e2 := blocks.NewBlock([]byte("2"))

	n := New()
	defer n.Shutdown()
	ch := n.Subscribe(context.Background(), e1.Key(), e2.Key())

	n.Publish(e1)
	r1, ok := <-ch
	if !ok {
		t.Fatal("didn't receive first expected block")
	}
	assertBlocksEqual(t, e1, r1)

	n.Publish(e2)
	r2, ok := <-ch
	if !ok {
		t.Fatal("didn't receive second expected block")
	}
	assertBlocksEqual(t, e2, r2)
}
开发者ID:andradeandrey,项目名称:go-ipfs,代码行数:22,代码来源:notifications_test.go


示例20: TestValueTypeMismatch

func TestValueTypeMismatch(t *testing.T) {
	block := blocks.NewBlock([]byte("some data"))

	datastore := ds.NewMapDatastore()
	k := BlockPrefix.Child(block.Key().DsKey())
	datastore.Put(k, "data that isn't a block!")

	blockstore := NewBlockstore(ds_sync.MutexWrap(datastore))

	_, err := blockstore.Get(block.Key())
	if err != ValueTypeMismatch {
		t.Fatal(err)
	}
}
开发者ID:Kubuxu,项目名称:go-ipfs,代码行数:14,代码来源:blockstore_test.go



注:本文中的github.com/ipfs/go-ipfs/blocks.NewBlock函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang blocks.Block类代码示例发布时间:2022-05-23
下一篇:
Golang mount.New函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap