Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base grpctest #76

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 90 additions & 15 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ 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"
"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/ipfs/go-libipfs/blocks"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/connmgr"
Expand Down Expand Up @@ -117,6 +124,21 @@ type P2P interface {
// Close p2p
Close() error

// NewBitSwapBlock creates block data of data
NewBitSwapBlock(data []byte) (cid.Cid, error)

// SaveDataToBlock saves data to block data
SaveDataToBlock(data []byte) (cid.Cid, error)

// GetDataFromBlock get data from block
GetDataFromBlock(wantCid string) ([]byte, error)

// NewBitSwapBlockWithCid creates block data of data and cidstr
NewBitSwapBlockWithCid(data []byte, cidstr cid.Cid) (*blocks.BasicBlock, error)

// SaveAndNotifyBlock save and notify block
SaveAndNotifyBlock(block *blocks.BasicBlock) error

//
GetDiscoveredPeers() <-chan *routing.QueryEvent

Expand All @@ -138,20 +160,14 @@ type P2P interface {
// PoisMinerCommitGenChall
PoisMinerCommitGenChall(
addr string,
accountKey []byte,
commit *pb.Commits,
minerPoisInfo *pb.MinerPoisInfo,
minerSign []byte,
commitGenChall *pb.RequestMinerCommitGenChall,
timeout time.Duration,
) (*pb.Challenge, error)

// PoisVerifyCommitProof
PoisVerifyCommitProof(
addr string,
accountKey []byte,
commitProofGroup *pb.CommitProofGroup,
accProof *pb.AccProof,
minerSign []byte,
verifyCommitAndAccProof *pb.RequestVerifyCommitAndAccProof,
timeout time.Duration,
) (*pb.ResponseVerifyCommitOrDeletionProof, error)

Expand Down Expand Up @@ -219,17 +235,13 @@ type P2P interface {

PoisMinerCommitGenChallP2P(
peerid peer.ID,
accountKey []byte,
commit *pb.Commits,
commitGenChall *pb.RequestMinerCommitGenChall,
timeout time.Duration,
) (*pb.Challenge, error)

PoisVerifyCommitProofP2P(
peerid peer.ID,
accountKey []byte,
commitProofGroup *pb.CommitProofGroup,
accProof *pb.AccProof,
minerSign []byte,
verifyCommitAndAccProof *pb.RequestVerifyCommitAndAccProof,
timeout time.Duration,
) (*pb.ResponseVerifyCommitOrDeletionProof, error)

Expand Down Expand Up @@ -297,6 +309,8 @@ 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
Expand Down Expand Up @@ -390,13 +404,17 @@ func NewBasicNode(
libp2p.ConnectionManager(cmgr),
libp2p.DefaultTransports,
libp2p.DefaultSecurity,
libp2p.NATPortMap(),
libp2p.ProtocolVersion(protocolPrefix+p2pProtocolVer),
libp2p.DefaultMuxers,
libp2p.AddrsFactory(addressFactory),
libp2p.DefaultEnableRelay,
libp2p.DisableMetrics(),
libp2p.ResourceManager(rm),
libp2p.NATPortMap(),
libp2p.EnableNATService(),
libp2p.EnableRelay(),
libp2p.EnableRelayService(),
libp2p.EnableHolePunching(),
)

bhost, err := libp2p.New(opts...)
Expand Down Expand Up @@ -449,11 +467,68 @@ func NewBasicNode(
return nil, err
}

network := bsnet.NewFromIpfsHost(n.host, n.RoutingDiscovery)
// blockData := blocks.NewBlock([]byte("123456"))
// fmt.Println("Generate a cid: ", blockData.Cid().String())
n.bstore = blockstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
n.bswap = bitswap.New(n.ctxQueryFromCtxCancel, network, n.bstore)

n.initProtocol(protocolPrefix)

return n, nil
}

// NewBitSwapBlock creates block data of data
func (n *Node) NewBitSwapBlock(data []byte) (cid.Cid, error) {
if len(data) <= 0 {
return cid.Cid{}, errors.New("[NewBitSwapBlock] empty data")
}
blockData := blocks.NewBlock(data)
return blockData.Cid(), nil
}

// NewBitSwapBlockWithCid creates block data of data and cidstr
func (n *Node) NewBitSwapBlockWithCid(data []byte, cidstr cid.Cid) (*blocks.BasicBlock, error) {
if len(data) <= 0 {
return nil, errors.New("[NewBitSwapBlockWithCid] empty data")
}
return blocks.NewBlockWithCid(data, cidstr)
}

// SaveAndNotifyBlock save and notify block
func (n *Node) SaveAndNotifyBlock(block *blocks.BasicBlock) error {
err := n.bstore.Put(n.ctxQueryFromCtxCancel, block)
if err != nil {
return err
}
err = n.bswap.NotifyNewBlocks(n.ctxQueryFromCtxCancel, block)
n.bswap.GetWantHaves()
return err
}

// SaveDataToBlock saves data to block data
func (n *Node) SaveDataToBlock(data []byte) (cid.Cid, error) {
if len(data) <= 0 {
return cid.Cid{}, errors.New("[SaveDataToBlock] empty data")
}
blockData := blocks.NewBlock(data)
err := n.bstore.Put(n.ctxQueryFromCtxCancel, blockData)
return blockData.Cid(), err
}

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

// DHTFindPeer searches for a peer with given ID.
func (n *Node) DHTFindPeer(peerid string) (peer.AddrInfo, error) {
id, err := peer.Decode(peerid)
Expand Down
24 changes: 4 additions & 20 deletions core/pois.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ func (n *Node) PoisGetMinerInitParam(addr string, accountKey []byte, timeout tim

func (n *Node) PoisMinerCommitGenChall(
addr string,
accountKey []byte,
commit *pb.Commits,
minerPoisInfo *pb.MinerPoisInfo,
minerSign []byte,
commitGenChall *pb.RequestMinerCommitGenChall,
timeout time.Duration,
) (*pb.Challenge, error) {
conn, err := grpc.Dial(
Expand All @@ -64,21 +61,13 @@ func (n *Node) PoisMinerCommitGenChall(
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

result, err := c.RequestMinerCommitGenChall(ctx, &pb.RequestMinerCommitGenChall{
MinerId: accountKey,
Commit: commit,
PoisInfo: minerPoisInfo,
MinerSign: minerSign,
})
result, err := c.RequestMinerCommitGenChall(ctx, commitGenChall)
return result, err
}

func (n *Node) PoisVerifyCommitProof(
addr string,
accountKey []byte,
commitProofGroup *pb.CommitProofGroup,
accProof *pb.AccProof,
minerSign []byte,
verifyCommitAndAccProof *pb.RequestVerifyCommitAndAccProof,
timeout time.Duration,
) (*pb.ResponseVerifyCommitOrDeletionProof, error) {
conn, err := grpc.Dial(
Expand All @@ -94,12 +83,7 @@ func (n *Node) PoisVerifyCommitProof(
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

result, err := c.RequestVerifyCommitProof(ctx, &pb.RequestVerifyCommitAndAccProof{
CommitProofGroup: commitProofGroup,
AccProof: accProof,
MinerId: accountKey,
MinerSign: minerSign,
})
result, err := c.RequestVerifyCommitProof(ctx, verifyCommitAndAccProof)
return result, err
}

Expand Down
23 changes: 8 additions & 15 deletions core/pois_p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func (n *Node) PoisGetMinerInitParamP2P(peerid peer.ID, accountKey []byte, timeo
return result, err
}

func (n *Node) PoisMinerCommitGenChallP2P(peerid peer.ID, accountKey []byte, commit *pb.Commits, timeout time.Duration) (*pb.Challenge, error) {
func (n *Node) PoisMinerCommitGenChallP2P(
peerid peer.ID,
commitGenChall *pb.RequestMinerCommitGenChall,
timeout time.Duration,
) (*pb.Challenge, error) {
opts := []grpc.DialOption{grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials())}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
Expand All @@ -38,19 +42,13 @@ func (n *Node) PoisMinerCommitGenChallP2P(peerid peer.ID, accountKey []byte, com
defer conn.Close()
c := pb.NewPoisApiClient(conn)

result, err := c.RequestMinerCommitGenChall(ctx, &pb.RequestMinerCommitGenChall{
MinerId: accountKey,
Commit: commit,
})
result, err := c.RequestMinerCommitGenChall(ctx, commitGenChall)
return result, err
}

func (n *Node) PoisVerifyCommitProofP2P(
peerid peer.ID,
accountKey []byte,
commitProofGroup *pb.CommitProofGroup,
accProof *pb.AccProof,
minerSign []byte,
verifyCommitAndAccProof *pb.RequestVerifyCommitAndAccProof,
timeout time.Duration,
) (*pb.ResponseVerifyCommitOrDeletionProof, error) {
opts := []grpc.DialOption{grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials())}
Expand All @@ -63,12 +61,7 @@ func (n *Node) PoisVerifyCommitProofP2P(
defer conn.Close()
c := pb.NewPoisApiClient(conn)

result, err := c.RequestVerifyCommitProof(ctx, &pb.RequestVerifyCommitAndAccProof{
CommitProofGroup: commitProofGroup,
AccProof: accProof,
MinerId: accountKey,
MinerSign: minerSign,
})
result, err := c.RequestVerifyCommitProof(ctx, verifyCommitAndAccProof)
return result, err
}

Expand Down
38 changes: 25 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ 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.8.0
github.com/ipfs/go-cid v0.4.0
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ipfs-blockstore v1.3.0
github.com/ipfs/go-libipfs v0.4.0
github.com/libp2p/go-libp2p v0.26.3
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.8.0
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pkg/errors v0.9.1
golang.org/x/sys v0.7.0
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
golang.org/x/sys v0.10.0
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.30.0
)
Expand All @@ -25,13 +30,14 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/cgroups v1.0.4 // 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.1.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
Expand All @@ -41,14 +47,19 @@ require (
github.com/google/pprof v0.0.0-20221203041831-ce31453925ec // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/huin/goupnp v1.0.3 // indirect
github.com/ipfs/boxo v0.8.0 // indirect
github.com/ipfs/go-cid v0.4.0 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-block-format v0.1.2 // 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/go-ipfs-util v0.0.2 // indirect
github.com/ipfs/go-ipld-format v0.4.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.20.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 @@ -100,19 +111,20 @@ require (
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
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.10.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/fx v1.18.2 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gonum.org/v1/gonum v0.11.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
Expand Down
Loading