Skip to content

Commit

Permalink
cancel bitswap (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode authored Oct 27, 2023
1 parent fcec11a commit 3276296
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 154 deletions.
213 changes: 107 additions & 106 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
libp2pgrpc "github.com/drgomesp/go-libp2p-grpc"
ggio "github.com/gogo/protobuf/io"
"github.com/gogo/protobuf/proto"
bitswap "github.com/ipfs/boxo/bitswap"
bsnet "github.com/ipfs/boxo/bitswap/network"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
ds_sync "github.com/ipfs/go-datastore/sync"
blockstore "github.com/ipfs/go-ipfs-blockstore"

//bsnet "github.com/ipfs/boxo/bitswap/network"
//blocks "github.com/ipfs/go-block-format"

//ds_sync "github.com/ipfs/go-datastore/sync"

"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/connmgr"
Expand All @@ -44,7 +44,8 @@ import (
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/mr-tron/base58"
ma "github.com/multiformats/go-multiaddr"
mh "github.com/multiformats/go-multihash"

//mh "github.com/multiformats/go-multihash"
"github.com/pbnjay/memory"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -124,26 +125,26 @@ type P2P interface {
// Close p2p
Close() error

//
GetBlockstore() blockstore.Blockstore
// //
// GetBlockstore() blockstore.Blockstore

//
GetBitSwap() *bitswap.Bitswap
// //
// GetBitSwap() *bitswap.Bitswap

//
FidToCid(fid string) (string, error)
// //
// FidToCid(fid string) (string, error)

//
SaveAndNotifyDataBlock(buf []byte) (cid.Cid, error)
// //
// SaveAndNotifyDataBlock(buf []byte) (cid.Cid, error)

//
NotifyData(buf []byte) error
// //
// NotifyData(buf []byte) error

// GetDataFromBlock get data from block
GetDataFromBlock(ctx context.Context, wantCid string) ([]byte, error)
// // GetDataFromBlock get data from block
// GetDataFromBlock(ctx context.Context, wantCid string) ([]byte, error)

//
GetLocalDataFromBlock(wantCid string) ([]byte, error)
// //
// GetLocalDataFromBlock(wantCid string) ([]byte, error)

//
GetDiscoveredPeers() <-chan *routing.QueryEvent
Expand Down Expand Up @@ -305,21 +306,21 @@ type Node struct {
discoveredPeerCh <-chan *routing.QueryEvent
host host.Host
libp2pgrpcCli *libp2pgrpc.Client
bstore blockstore.Blockstore
bswap *bitswap.Bitswap
dir DataDirs
peerPublickey []byte
workspace string
privatekeyPath string
idleTee atomic.Value
serviceTee atomic.Value
serviceTagDataCh chan string
protocolVersion string
dhtProtocolVersion string
rendezvousVersion string
grpcProtocolVersion string
protocolPrefix string
bootstrap []string
//bstore blockstore.Blockstore
//bswap *bitswap.Bitswap
dir DataDirs
peerPublickey []byte
workspace string
privatekeyPath string
idleTee atomic.Value
serviceTee atomic.Value
serviceTagDataCh chan string
protocolVersion string
dhtProtocolVersion string
rendezvousVersion string
grpcProtocolVersion string
protocolPrefix string
bootstrap []string
*dht.IpfsDHT
*drouting.RoutingDiscovery
*protocols
Expand Down Expand Up @@ -462,83 +463,83 @@ func NewBasicNode(
return nil, err
}

network := bsnet.NewFromIpfsHost(n.host, n.RoutingDiscovery)
fsdatastore, err := NewDatastore(filepath.Join(n.workspace, FileBlockDir))
if err != nil {
return nil, err
}
// network := bsnet.NewFromIpfsHost(n.host, n.RoutingDiscovery)
// fsdatastore, err := NewDatastore(filepath.Join(n.workspace, FileBlockDir))
// if err != nil {
// return nil, err
// }

n.bstore = blockstore.NewBlockstore(ds_sync.MutexWrap(fsdatastore))
n.bswap = bitswap.New(
n.ctxQueryFromCtxCancel,
network,
n.bstore,
)
// n.bstore = blockstore.NewBlockstore(ds_sync.MutexWrap(fsdatastore))
// n.bswap = bitswap.New(
// n.ctxQueryFromCtxCancel,
// network,
// n.bstore,
// )

n.initProtocol(protocolPrefix)

return n, nil
}

func (n *Node) GetBlockstore() blockstore.Blockstore {
return n.bstore
}

func (n *Node) GetBitSwap() *bitswap.Bitswap {
return n.bswap
}

// SaveAndNotifyDataBlock
func (n *Node) SaveAndNotifyDataBlock(buf []byte) (cid.Cid, error) {
blockData := blocks.NewBlock(buf)
err := n.bstore.Put(n.ctxQueryFromCtxCancel, blockData)
if err != nil {
return blockData.Cid(), err
}
err = n.bswap.NotifyNewBlocks(n.ctxQueryFromCtxCancel, blockData)
return blockData.Cid(), err
}

// NotifyData notify data
func (n *Node) NotifyData(buf []byte) error {
blockData := blocks.NewBlock(buf)
return n.bswap.NotifyNewBlocks(n.ctxQueryFromCtxCancel, blockData)
}

// GetDataFromBlock get data from block
func (n *Node) GetDataFromBlock(ctx context.Context, wantCid string) ([]byte, error) {
wantcid, err := cid.Decode(wantCid)
if err != nil {
return nil, err
}
block, err := n.bswap.GetBlock(ctx, wantcid)
if err != nil {
return nil, err
}
return block.RawData(), err
}

// GetLocalDataFromBlock get local data from block
func (n *Node) GetLocalDataFromBlock(wantCid string) ([]byte, error) {
wantcid, err := cid.Decode(wantCid)
if err != nil {
return nil, err
}
block, err := n.bstore.Get(n.ctxQueryFromCtxCancel, wantcid)
if err != nil {
return nil, err
}
return block.RawData(), err
}

// FidToCid
func (n *Node) FidToCid(fid string) (string, error) {
mhash, err := mh.FromHexString("1220" + fid)
if err != nil {
return "", err
}
return cid.NewCidV0(mhash).String(), nil
}
// func (n *Node) GetBlockstore() blockstore.Blockstore {
// return n.bstore
// }

// func (n *Node) GetBitSwap() *bitswap.Bitswap {
// return n.bswap
// }

// // SaveAndNotifyDataBlock
// func (n *Node) SaveAndNotifyDataBlock(buf []byte) (cid.Cid, error) {
// blockData := blocks.NewBlock(buf)
// err := n.bstore.Put(n.ctxQueryFromCtxCancel, blockData)
// if err != nil {
// return blockData.Cid(), err
// }
// err = n.bswap.NotifyNewBlocks(n.ctxQueryFromCtxCancel, blockData)
// return blockData.Cid(), err
// }

// // NotifyData notify data
// func (n *Node) NotifyData(buf []byte) error {
// blockData := blocks.NewBlock(buf)
// return n.bswap.NotifyNewBlocks(n.ctxQueryFromCtxCancel, blockData)
// }

// // GetDataFromBlock get data from block
// func (n *Node) GetDataFromBlock(ctx context.Context, wantCid string) ([]byte, error) {
// wantcid, err := cid.Decode(wantCid)
// if err != nil {
// return nil, err
// }
// block, err := n.bswap.GetBlock(ctx, wantcid)
// if err != nil {
// return nil, err
// }
// return block.RawData(), err
// }

// // GetLocalDataFromBlock get local data from block
// func (n *Node) GetLocalDataFromBlock(wantCid string) ([]byte, error) {
// wantcid, err := cid.Decode(wantCid)
// if err != nil {
// return nil, err
// }
// block, err := n.bstore.Get(n.ctxQueryFromCtxCancel, wantcid)
// if err != nil {
// return nil, err
// }
// return block.RawData(), err
// }

// // FidToCid
// func (n *Node) FidToCid(fid string) (string, error) {
// mhash, err := mh.FromHexString("1220" + fid)
// if err != nil {
// return "", err
// }
// return cid.NewCidV0(mhash).String(), nil
// }

// DHTFindPeer searches for a peer with given ID.
func (n *Node) DHTFindPeer(peerid string) (peer.AddrInfo, error) {
Expand Down
18 changes: 3 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ require (
github.com/drgomesp/go-libp2p-grpc v0.1.0
github.com/gogo/protobuf v1.3.2
github.com/google/uuid v1.3.0
github.com/ipfs/boxo v0.12.0
github.com/ipfs/go-block-format v0.1.2
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ipfs-blockstore v1.3.0
github.com/libp2p/go-libp2p v0.30.0
github.com/libp2p/go-libp2p-kad-dht v0.23.0
github.com/libp2p/go-msgio v0.3.0
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-multiaddr v0.11.0
github.com/multiformats/go-multihash v0.2.3
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pkg/errors v0.9.1
golang.org/x/sys v0.11.0
Expand All @@ -31,7 +26,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cskr/pubsub v1.0.2 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
Expand All @@ -50,18 +44,12 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/huin/goupnp v1.2.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-ipfs-delay v0.0.1 // indirect
github.com/ipfs/go-ipfs-ds-help v1.1.0 // indirect
github.com/ipfs/go-ipfs-pq v0.0.3 // indirect
github.com/ipfs/boxo v0.12.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
github.com/ipfs/go-ipld-format v0.5.0 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
github.com/ipfs/go-peertaskqueue v0.8.1 // indirect
github.com/ipld/go-ipld-prime v0.21.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
Expand Down Expand Up @@ -93,6 +81,7 @@ require (
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
Expand All @@ -114,7 +103,6 @@ require (
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
Loading

0 comments on commit 3276296

Please sign in to comment.