mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
fix(blockstore, bitswap) enforce threadsafety in blockstore
fixes data race detected in a testnet test
This commit is contained in:
@ -16,14 +16,14 @@ type Blockstore interface {
|
|||||||
Put(*blocks.Block) error
|
Put(*blocks.Block) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlockstore(d ds.Datastore) Blockstore {
|
func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
|
||||||
return &blockstore{
|
return &blockstore{
|
||||||
datastore: d,
|
datastore: d,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type blockstore struct {
|
type blockstore struct {
|
||||||
datastore ds.Datastore
|
datastore ds.ThreadSafeDatastore
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
|
func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||||
|
ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
@ -12,7 +13,7 @@ import (
|
|||||||
// TODO(brian): TestGetReturnsNil
|
// TODO(brian): TestGetReturnsNil
|
||||||
|
|
||||||
func TestGetWhenKeyNotPresent(t *testing.T) {
|
func TestGetWhenKeyNotPresent(t *testing.T) {
|
||||||
bs := NewBlockstore(ds.NewMapDatastore())
|
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
||||||
_, err := bs.Get(u.Key("not present"))
|
_, err := bs.Get(u.Key("not present"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -23,7 +24,7 @@ func TestGetWhenKeyNotPresent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPutThenGetBlock(t *testing.T) {
|
func TestPutThenGetBlock(t *testing.T) {
|
||||||
bs := NewBlockstore(ds.NewMapDatastore())
|
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
||||||
block := blocks.NewBlock([]byte("some data"))
|
block := blocks.NewBlock([]byte("some data"))
|
||||||
|
|
||||||
err := bs.Put(block)
|
err := bs.Put(block)
|
||||||
@ -46,7 +47,7 @@ func TestValueTypeMismatch(t *testing.T) {
|
|||||||
datastore := ds.NewMapDatastore()
|
datastore := ds.NewMapDatastore()
|
||||||
datastore.Put(block.Key().DsKey(), "data that isn't a block!")
|
datastore.Put(block.Key().DsKey(), "data that isn't a block!")
|
||||||
|
|
||||||
blockstore := NewBlockstore(datastore)
|
blockstore := NewBlockstore(ds_sync.MutexWrap(datastore))
|
||||||
|
|
||||||
_, err := blockstore.Get(block.Key())
|
_, err := blockstore.Get(block.Key())
|
||||||
if err != ValueTypeMismatch {
|
if err != ValueTypeMismatch {
|
||||||
|
@ -22,7 +22,7 @@ var log = u.Logger("bitswap")
|
|||||||
// provided NetMessage service
|
// provided NetMessage service
|
||||||
func NetMessageSession(parent context.Context, p peer.Peer,
|
func NetMessageSession(parent context.Context, p peer.Peer,
|
||||||
net inet.Network, srv inet.Service, directory bsnet.Routing,
|
net inet.Network, srv inet.Service, directory bsnet.Routing,
|
||||||
d ds.Datastore, nice bool) exchange.Interface {
|
d ds.ThreadSafeDatastore, nice bool) exchange.Interface {
|
||||||
|
|
||||||
networkAdapter := bsnet.NetMessageAdapter(srv, net, nil)
|
networkAdapter := bsnet.NetMessageAdapter(srv, net, nil)
|
||||||
bs := &bitswap{
|
bs := &bitswap{
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
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/go-datastore"
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||||
|
ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||||
bstore "github.com/jbenet/go-ipfs/blockstore"
|
bstore "github.com/jbenet/go-ipfs/blockstore"
|
||||||
exchange "github.com/jbenet/go-ipfs/exchange"
|
exchange "github.com/jbenet/go-ipfs/exchange"
|
||||||
@ -279,7 +280,7 @@ func session(net tn.Network, rs mock.RoutingServer, id peer.ID) instance {
|
|||||||
adapter := net.Adapter(p)
|
adapter := net.Adapter(p)
|
||||||
htc := rs.Client(p)
|
htc := rs.Client(p)
|
||||||
|
|
||||||
blockstore := bstore.NewBlockstore(ds.NewMapDatastore())
|
blockstore := bstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
||||||
const alwaysSendToPeer = true
|
const alwaysSendToPeer = true
|
||||||
bs := &bitswap{
|
bs := &bitswap{
|
||||||
blockstore: blockstore,
|
blockstore: blockstore,
|
||||||
|
Reference in New Issue
Block a user