diff --git a/api/client/client.go b/api/client/client.go index 2aaa05579..3a9f37eed 100644 --- a/api/client/client.go +++ b/api/client/client.go @@ -2,12 +2,12 @@ package client import ( "github.com/filecoin-project/go-lotus/api" - "github.com/filecoin-project/go-lotus/rpclib" + "github.com/filecoin-project/go-lotus/jsonrpc" ) // NewRPC creates a new http jsonrpc client. func NewRPC(addr string) api.API { var res api.Struct - rpclib.NewClient(addr, "Filecoin", &res.Internal) + jsonrpc.NewClient(addr, "Filecoin", &res.Internal) return &res } diff --git a/cborrpc/rpc.go b/cborrpc/rpc.go new file mode 100644 index 000000000..94bd3221c --- /dev/null +++ b/cborrpc/rpc.go @@ -0,0 +1,33 @@ +package cborrpc + +import ( + "io" + "io/ioutil" + + cbor "github.com/ipfs/go-ipld-cbor" +) +const MessageSizeLimit = 1 << 20 + +func WriteCborRPC(w io.Writer, obj interface{}) error { + data, err := cbor.DumpObject(obj) + if err != nil { + return err + } + + _, err = w.Write(data) + return err +} + +type ByteReader interface { + io.Reader + io.ByteReader +} + +func ReadCborRPC(r ByteReader, out interface{}) error { + b, err := ioutil.ReadAll(r) + if err != nil { + return err + } + + return cbor.DecodeInto(b, out) +} \ No newline at end of file diff --git a/daemon/rpc.go b/daemon/rpc.go index 78df124c2..b280200d2 100644 --- a/daemon/rpc.go +++ b/daemon/rpc.go @@ -4,11 +4,11 @@ import ( "net/http" "github.com/filecoin-project/go-lotus/api" - "github.com/filecoin-project/go-lotus/rpclib" + "github.com/filecoin-project/go-lotus/jsonrpc" ) func serveRPC(api api.API) error { - rpcServer := rpclib.NewServer() + rpcServer := jsonrpc.NewServer() rpcServer.Register("Filecoin", api) http.Handle("/rpc/v0", rpcServer) return http.ListenAndServe(":1234", http.DefaultServeMux) diff --git a/go.mod b/go.mod index a729ed395..108d679e1 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( github.com/ipfs/go-datastore v0.0.5 github.com/ipfs/go-ipfs-routing v0.1.0 + github.com/ipfs/go-ipld-cbor v0.0.2 // indirect github.com/ipfs/go-log v0.0.2-0.20190703113630-0c3cfb1eccc4 github.com/libp2p/go-libp2p v0.2.0 github.com/libp2p/go-libp2p-circuit v0.1.0 diff --git a/go.sum b/go.sum index 513d9e062..59eea47cc 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,7 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU= @@ -61,6 +62,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo= github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= +github.com/ipfs/go-block-format v0.0.2 h1:qPDvcP19izTjU8rgo6p7gTXZlkMkF5bz5G3fqIsSCPE= +github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY= github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= github.com/ipfs/go-cid v0.0.2 h1:tuuKaZPU1M6HcejsO3AcYWW8sZ8MTvyxfc4uqB4eFE8= github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= @@ -78,6 +81,9 @@ github.com/ipfs/go-ipfs-routing v0.1.0 h1:gAJTT1cEeeLj6/DlLX6t+NxD9fQe2ymTO6qWRD github.com/ipfs/go-ipfs-routing v0.1.0/go.mod h1:hYoUkJLyAUKhF58tysKpids8RNDPO42BVMgK5dNsoqY= github.com/ipfs/go-ipfs-util v0.0.1 h1:Wz9bL2wB2YBJqggkA4dD7oSmqB4cAnpNbGrlHJulv50= github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc= +github.com/ipfs/go-ipld-cbor v0.0.2/go.mod h1:wTBtrQZA3SoFKMVkp6cn6HMRteIB1VsmHA0AQFOn7Nc= +github.com/ipfs/go-ipld-format v0.0.1 h1:HCu4eB/Gh+KD/Q0M8u888RFkorTWNIL3da4oc5dwc80= +github.com/ipfs/go-ipld-format v0.0.1/go.mod h1:kyJtbkDALmFHv3QR6et67i35QzO3S0dCDnkOJhcZkms= github.com/ipfs/go-log v0.0.1 h1:9XTUN/rW64BCG1YhPK9Hoy3q8nr4gOmHHBpgFdfw6Lc= github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM= github.com/ipfs/go-log v0.0.2-0.20190703113630-0c3cfb1eccc4 h1:4GUopYwyu/8kX0UxYB7QDYOUbnt9HCg/j6J0sMqVvNQ= @@ -98,6 +104,7 @@ github.com/jbenet/goprocess v0.1.3 h1:YKyIEECS/XvcfHtBzxtjBBbWK+MbvA6dG8ASiqwvr1 github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= +github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= @@ -262,6 +269,10 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992 h1:bzMe+2coZJYHnhGgVlcQKuRy4FSny4ds8dLQjw5P1XE= +github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992/go.mod h1:uIp+gprXxxrWSjjklXD+mN4wed/tMfjMMmN/9+JsA9o= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa/go.mod h1:2RVY1rIf+2J2o/IM9+vPq9RzmHDSseB7FoXiSNIUsoU= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a h1:/eS3yfGjQKG+9kayBkj0ip1BGhq6zJ3eaVksphxAaek= github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= @@ -272,6 +283,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= +github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc h1:BCPnHtcboadS0DvysUuJXZ4lWVv5Bh5i7+tbIyi+ck4= github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc/go.mod h1:r45hJU7yEoA81k6MWNhpMj/kms0n14dkzkxYHoB96UM= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= diff --git a/rpclib/rpc_client.go b/jsonrpc/rpc_client.go similarity index 99% rename from rpclib/rpc_client.go rename to jsonrpc/rpc_client.go index f8116c825..e9418040b 100644 --- a/rpclib/rpc_client.go +++ b/jsonrpc/rpc_client.go @@ -1,4 +1,4 @@ -package rpclib +package jsonrpc import ( "bytes" diff --git a/rpclib/rpc_server.go b/jsonrpc/rpc_server.go similarity index 99% rename from rpclib/rpc_server.go rename to jsonrpc/rpc_server.go index 5ef1560e7..104380861 100644 --- a/rpclib/rpc_server.go +++ b/jsonrpc/rpc_server.go @@ -1,4 +1,4 @@ -package rpclib +package jsonrpc import ( "bytes" diff --git a/rpclib/rpc_test.go b/jsonrpc/rpc_test.go similarity index 99% rename from rpclib/rpc_test.go rename to jsonrpc/rpc_test.go index 4879a3e3e..d496e25c5 100644 --- a/rpclib/rpc_test.go +++ b/jsonrpc/rpc_test.go @@ -1,4 +1,4 @@ -package rpclib +package jsonrpc import ( "context" diff --git a/node/builder.go b/node/builder.go index 5b94dcae1..349b548cc 100644 --- a/node/builder.go +++ b/node/builder.go @@ -6,6 +6,7 @@ import ( "time" "github.com/ipfs/go-datastore" + logging "github.com/ipfs/go-log" ci "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-peerstore/pstoremem" @@ -13,11 +14,14 @@ import ( "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/build" + "github.com/filecoin-project/go-lotus/node/hello" "github.com/filecoin-project/go-lotus/node/modules" "github.com/filecoin-project/go-lotus/node/modules/helpers" "github.com/filecoin-project/go-lotus/node/modules/lp2p" ) +var log = logging.Logger("builder") + var defaultListenAddrs = []string{ // TODO: better defaults? "/ip4/0.0.0.0/tcp/4001", "/ip6/::/tcp/4001", @@ -42,6 +46,7 @@ func New(ctx context.Context) (api.API, error) { fx.Provide( pstoremem.NewPeerstore, + // libp2p lp2p.DefaultTransports, lp2p.PNet, lp2p.Host, @@ -59,6 +64,10 @@ func New(ctx context.Context) (api.API, error) { lp2p.NatPortMap, lp2p.ConnectionManager(50, 200, 20*time.Second), + + // filecoin protocols + + hello.NewHelloService, ), fx.Invoke( @@ -69,6 +78,8 @@ func New(ctx context.Context) (api.API, error) { fx.Invoke(versionAPI(&resAPI.Internal.Version)), fx.Invoke(idAPI(&resAPI.Internal.ID)), + + fx.Logger(&debugPrinter{log}), ) if err := app.Start(ctx); err != nil { diff --git a/node/fxlog.go b/node/fxlog.go new file mode 100644 index 000000000..3f0e18d64 --- /dev/null +++ b/node/fxlog.go @@ -0,0 +1,17 @@ +package node + +import ( + logging "github.com/ipfs/go-log" + + "go.uber.org/fx" +) + +type debugPrinter struct { + l logging.StandardLogger +} + +func (p *debugPrinter) Printf(f string, a ...interface{}) { + p.l.Debugf(f, a...) +} + +var _ fx.Printer = new(debugPrinter) diff --git a/node/hello/hello.go b/node/hello/hello.go new file mode 100644 index 000000000..bcb36ce3c --- /dev/null +++ b/node/hello/hello.go @@ -0,0 +1,99 @@ +package hello + +import ( + "bufio" + "context" + "github.com/filecoin-project/go-lotus/cborrpc" + "github.com/libp2p/go-libp2p-core/host" + + "github.com/ipfs/go-cid" + cbor "github.com/ipfs/go-ipld-cbor" + logging "github.com/ipfs/go-log" + inet "github.com/libp2p/go-libp2p-core/network" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p-core/protocol" +) + +const ProtocolID = "/fil/hello/1.0.0" +var log = logging.Logger("hello") + +func init() { + cbor.RegisterCborType(Message{}) +} + +type Message struct { + HeaviestTipSet []cid.Cid + HeaviestTipSetWeight uint64 + GenesisHash cid.Cid +} + +type NewStreamFunc func(context.Context, peer.ID, ...protocol.ID) (inet.Stream, error) + +type Service struct { + newStream NewStreamFunc + + //cs *ChainStore + //syncer *Syncer +} + +func NewHelloService(h host.Host) *Service { + return &Service{ + newStream: h.NewStream, + } +} + +func (hs *Service) HandleStream(s inet.Stream) { + defer s.Close() + + log.Debugw("Handling hello") + + var hmsg Message + if err := cborrpc.ReadCborRPC(bufio.NewReader(s), &hmsg); err != nil { + log.Error("failed to read hello message: ", err) + return + } + log.Debugw("heaviest tipset", "tipset", hmsg.HeaviestTipSet) + log.Debugw("got genesis from hello", "hash", hmsg.GenesisHash) + + /*if hmsg.GenesisHash != hs.syncer.genesis.Cids()[0] { + log.Error("other peer has different genesis!") + s.Conn().Close() + return + } + + ts, err := hs.syncer.FetchTipSet(context.Background(), s.Conn().RemotePeer(), hmsg.HeaviestTipSet) + if err != nil { + log.Errorf("failed to fetch tipset from peer during hello: %s", err) + return + } + + hs.syncer.InformNewHead(s.Conn().RemotePeer(), ts)*/ +} + +func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error { + /*s, err := hs.newStream(ctx, pid, ProtocolID) + if err != nil { + return err + } + + hts := hs.cs.GetHeaviestTipSet() + weight := hs.cs.Weight(hts) + gen, err := hs.cs.GetGenesis() + if err != nil { + return err + } + + hmsg := &Message{ + HeaviestTipSet: hts.Cids(), + HeaviestTipSetWeight: weight, + GenesisHash: gen.Cid(), + } + fmt.Println("SENDING HELLO MESSAGE: ", hts.Cids()) + fmt.Println("hello message genesis: ", gen.Cid()) + + if err := WriteCborRPC(s, hmsg); err != nil { + return err + }*/ + + return nil +} \ No newline at end of file diff --git a/node/modules/core.go b/node/modules/core.go index 5bc21fcf5..070cdef99 100644 --- a/node/modules/core.go +++ b/node/modules/core.go @@ -1,10 +1,13 @@ package modules import ( + logging "github.com/ipfs/go-log" "github.com/libp2p/go-libp2p-core/peerstore" record "github.com/libp2p/go-libp2p-record" ) +var log = logging.Logger("modules") + // RecordValidator provides namesys compatible routing record validator func RecordValidator(ps peerstore.Peerstore) record.Validator { return record.NamespacedValidator{ diff --git a/node/modules/hello.go b/node/modules/hello.go new file mode 100644 index 000000000..ec945ee2b --- /dev/null +++ b/node/modules/hello.go @@ -0,0 +1,25 @@ +package modules + +import ( + "github.com/filecoin-project/go-lotus/node/hello" + "github.com/filecoin-project/go-lotus/node/modules/helpers" + "github.com/libp2p/go-libp2p-core/host" + inet "github.com/libp2p/go-libp2p-core/network" + "go.uber.org/fx" +) + +func HandleHello(mctx helpers.MetricsCtx, lc fx.Lifecycle, h host.Host, svc *hello.Service) error { + h.SetStreamHandler(hello.ProtocolID, svc.HandleStream) + + bundle := inet.NotifyBundle{ + ConnectedF: func(_ inet.Network, c inet.Conn) { + go func() { + if err := svc.SayHello(helpers.LifecycleCtx(mctx, lc), c.RemotePeer()); err != nil { + log.Warnw("failed to say hello", "error", err) + return + } + }() + }, + } + h.Network().Notify(&bundle) +}