1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 09:34:03 +08:00

fix(blockstore, bitswap) enforce threadsafety in blockstore

fixes data race detected in a testnet test
This commit is contained in:
Brian Tiger Chow
2014-10-24 16:14:12 -07:00
parent bd5a1c0c0f
commit 715f5f4a19
4 changed files with 9 additions and 7 deletions

View File

@ -16,14 +16,14 @@ type Blockstore interface {
Put(*blocks.Block) error
}
func NewBlockstore(d ds.Datastore) Blockstore {
func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
return &blockstore{
datastore: d,
}
}
type blockstore struct {
datastore ds.Datastore
datastore ds.ThreadSafeDatastore
}
func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {

View File

@ -5,6 +5,7 @@ import (
"testing"
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"
u "github.com/jbenet/go-ipfs/util"
)
@ -12,7 +13,7 @@ import (
// TODO(brian): TestGetReturnsNil
func TestGetWhenKeyNotPresent(t *testing.T) {
bs := NewBlockstore(ds.NewMapDatastore())
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
_, err := bs.Get(u.Key("not present"))
if err != nil {
@ -23,7 +24,7 @@ func TestGetWhenKeyNotPresent(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"))
err := bs.Put(block)
@ -46,7 +47,7 @@ func TestValueTypeMismatch(t *testing.T) {
datastore := ds.NewMapDatastore()
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())
if err != ValueTypeMismatch {

View File

@ -22,7 +22,7 @@ var log = u.Logger("bitswap")
// provided NetMessage service
func NetMessageSession(parent context.Context, p peer.Peer,
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)
bs := &bitswap{

View File

@ -9,6 +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/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"
bstore "github.com/jbenet/go-ipfs/blockstore"
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)
htc := rs.Client(p)
blockstore := bstore.NewBlockstore(ds.NewMapDatastore())
blockstore := bstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
const alwaysSendToPeer = true
bs := &bitswap{
blockstore: blockstore,