diff --git a/cmd/devp2p/internal/v5test/framework.go b/cmd/devp2p/internal/v5test/framework.go index d1f553c851..5759f7cc2a 100644 --- a/cmd/devp2p/internal/v5test/framework.go +++ b/cmd/devp2p/internal/v5test/framework.go @@ -88,7 +88,7 @@ func newConn(dest *enode.Node, log logger) *conn { localNode: ln, remote: dest, remoteAddr: &net.UDPAddr{IP: dest.IP(), Port: dest.UDP()}, - codec: v5wire.NewCodec(ln, key, mclock.System{}), + codec: v5wire.NewCodec(ln, key, mclock.System{}, nil), log: log, } } diff --git a/console/console.go b/console/console.go index e4c83692a1..1b67539a92 100644 --- a/console/console.go +++ b/console/console.go @@ -30,6 +30,7 @@ import ( "github.com/CortexFoundation/CortexTheseus/internal/jsre" "github.com/CortexFoundation/CortexTheseus/internal/jsre/deps" "github.com/CortexFoundation/CortexTheseus/internal/web3ext" + "github.com/CortexFoundation/CortexTheseus/log" "github.com/CortexFoundation/CortexTheseus/rpc" "github.com/dop251/goja" "github.com/mattn/go-colorable" @@ -180,12 +181,20 @@ func (c *Console) initWeb3(bridge *bridge) error { return err } +var defaultAPIs = map[string]string{"ctxc": "1.0", "net": "1.0", "debug": "1.0"} + // initExtensions loads and registers web3.js extensions. func (c *Console) initExtensions() error { + const methodNotFound = -32601 // Compute aliases from server-provided modules. apis, err := c.client.SupportedModules() if err != nil { - return fmt.Errorf("api modules: %v", err) + if rpcErr, ok := err.(rpc.Error); ok && rpcErr.ErrorCode() == methodNotFound { + log.Warn("Server does not support method rpc_modules, using default API list.") + apis = defaultAPIs + } else { + return err + } } aliases := map[string]struct{}{"ctxc": {}, "personal": {}} for api := range apis { diff --git a/go.mod b/go.mod index 5401f7e1bf..d801293848 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.4.1 github.com/CortexFoundation/inference v1.0.2-0.20221114235728-985486629a61 github.com/CortexFoundation/statik v0.0.0-20210315012922-8bb8a7b5dc66 - github.com/CortexFoundation/torrentfs v1.0.35-0.20221121210641-607c9ddacb73 + github.com/CortexFoundation/torrentfs v1.0.35-0.20221126200542-1ab479237bfe github.com/VictoriaMetrics/fastcache v1.12.0 github.com/arsham/figurine v1.2.0 github.com/aws/aws-sdk-go-v2 v1.16.16 diff --git a/go.sum b/go.sum index bc21ba1862..5c7c0d1311 100644 --- a/go.sum +++ b/go.sum @@ -118,8 +118,8 @@ github.com/CortexFoundation/statik v0.0.0-20210315012922-8bb8a7b5dc66/go.mod h1: github.com/CortexFoundation/torrentfs v1.0.13-0.20200623060705-ce027f43f2f8/go.mod h1:Ma+tGhPPvz4CEZHaqEJQMOEGOfHeQBiAoNd1zyc/w3Q= github.com/CortexFoundation/torrentfs v1.0.14-0.20200703071639-3fcabcabf274/go.mod h1:qnb3YlIJmuetVBtC6Lsejr0Xru+1DNmDCdTqnwy7lhk= github.com/CortexFoundation/torrentfs v1.0.20-0.20200810031954-d36d26f82fcc/go.mod h1:N5BsicP5ynjXIi/Npl/SRzlJ630n1PJV2sRj0Z0t2HA= -github.com/CortexFoundation/torrentfs v1.0.35-0.20221121210641-607c9ddacb73 h1:XDBtIn1ymeVpHYi/8+2fpikUZJvo5rv/fTnDzgZRg/M= -github.com/CortexFoundation/torrentfs v1.0.35-0.20221121210641-607c9ddacb73/go.mod h1:WMYeTsLSYxNlt1ecXI8yBl041roC03L4N201nOZZn7k= +github.com/CortexFoundation/torrentfs v1.0.35-0.20221126200542-1ab479237bfe h1:y6h08LLz75+aaOMJqyhM7KH+uPVYH3xQxPMY6In60sY= +github.com/CortexFoundation/torrentfs v1.0.35-0.20221126200542-1ab479237bfe/go.mod h1:WMYeTsLSYxNlt1ecXI8yBl041roC03L4N201nOZZn7k= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/DATA-DOG/go-sqlmock v1.4.1 h1:ThlnYciV1iM/V0OSF/dtkqWb6xo5qITT1TJBG1MRDJM= github.com/HdrHistogram/hdrhistogram-go v1.1.0 h1:6dpdDPTRoo78HxAJ6T1HfMiKSnqhgRRqzCuPshRkQ7I= diff --git a/p2p/discover/common.go b/p2p/discover/common.go index bb0ec703a7..57b09d27c8 100644 --- a/p2p/discover/common.go +++ b/p2p/discover/common.go @@ -35,16 +35,24 @@ type UDPConn interface { LocalAddr() net.Addr } +type V5Config struct { + ProtocolID *[6]byte +} + // Config holds settings for the discovery listener. type Config struct { // These settings are required and configure the UDP listener: PrivateKey *ecdsa.PrivateKey // These settings are optional: - NetRestrict *netutil.Netlist // list of allowed IP networks - Bootnodes []*enode.Node // list of bootstrap nodes - Unhandled chan<- ReadPacket // unhandled packets are sent on this channel - Log log.Logger // if set, log messages go here + NetRestrict *netutil.Netlist // list of allowed IP networks + Bootnodes []*enode.Node // list of bootstrap nodes + Unhandled chan<- ReadPacket // unhandled packets are sent on this channel + Log log.Logger // if set, log messages go here + + // V5ProtocolID configures the discv5 protocol identifier. + V5ProtocolID *[6]byte + ValidSchemes enr.IdentityScheme // allowed identity schemes Clock mclock.Clock } diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go index d318015c7e..8aa5314dd4 100644 --- a/p2p/discover/v5_udp.go +++ b/p2p/discover/v5_udp.go @@ -154,7 +154,7 @@ func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) { callDoneCh: make(chan *callV5), respTimeoutCh: make(chan *callTimeout), // state of dispatch - codec: v5wire.NewCodec(ln, cfg.PrivateKey, cfg.Clock), + codec: v5wire.NewCodec(ln, cfg.PrivateKey, cfg.Clock, cfg.V5ProtocolID), activeCallByNode: make(map[enode.ID]*callV5), activeCallByAuth: make(map[v5wire.Nonce]*callV5), callQueue: make(map[enode.ID][]*callV5), diff --git a/p2p/discover/v5wire/encoding.go b/p2p/discover/v5wire/encoding.go index f3290bbf19..0c060702a5 100644 --- a/p2p/discover/v5wire/encoding.go +++ b/p2p/discover/v5wire/encoding.go @@ -98,7 +98,7 @@ const ( randomPacketMsgSize = 20 ) -var protocolID = [6]byte{'d', 'i', 's', 'c', 'v', '5'} +var DefaultProtocolID = [6]byte{'d', 'i', 's', 'c', 'v', '5'} // Errors. var ( @@ -134,10 +134,11 @@ var ( // Codec encodes and decodes Discovery v5 packets. // This type is not safe for concurrent use. type Codec struct { - sha256 hash.Hash - localnode *enode.LocalNode - privkey *ecdsa.PrivateKey - sc *SessionCache + sha256 hash.Hash + localnode *enode.LocalNode + privkey *ecdsa.PrivateKey + sc *SessionCache + protocolID [6]byte // encoder buffers buf bytes.Buffer // whole packet @@ -150,12 +151,16 @@ type Codec struct { } // NewCodec creates a wire codec. -func NewCodec(ln *enode.LocalNode, key *ecdsa.PrivateKey, clock mclock.Clock) *Codec { +func NewCodec(ln *enode.LocalNode, key *ecdsa.PrivateKey, clock mclock.Clock, protocolID *[6]byte) *Codec { c := &Codec{ - sha256: sha256.New(), - localnode: ln, - privkey: key, - sc: NewSessionCache(1024, clock), + sha256: sha256.New(), + localnode: ln, + privkey: key, + sc: NewSessionCache(1024, clock), + protocolID: DefaultProtocolID, + } + if protocolID != nil { + c.protocolID = *protocolID } return c } @@ -255,7 +260,7 @@ func (c *Codec) makeHeader(toID enode.ID, flag byte, authsizeExtra int) Header { } return Header{ StaticHeader: StaticHeader{ - ProtocolID: protocolID, + ProtocolID: c.protocolID, Version: version, Flag: flag, AuthSize: uint16(authsize), @@ -434,7 +439,7 @@ func (c *Codec) Decode(input []byte, addr string) (src enode.ID, n *enode.Node, c.reader.Reset(staticHeader) binary.Read(&c.reader, binary.BigEndian, &head.StaticHeader) remainingInput := len(input) - sizeofStaticPacketData - if err := head.checkValid(remainingInput); err != nil { + if err := head.checkValid(remainingInput, c.protocolID); err != nil { return enode.ID{}, nil, nil, err } @@ -621,7 +626,7 @@ func (c *Codec) decryptMessage(input, nonce, headerData, readKey []byte) (Packet // checkValid performs some basic validity checks on the header. // The packetLen here is the length remaining after the static header. -func (h *StaticHeader) checkValid(packetLen int) error { +func (h *StaticHeader) checkValid(packetLen int, protocolID [6]byte) error { if h.ProtocolID != protocolID { return errInvalidHeader } diff --git a/p2p/discover/v5wire/encoding_test.go b/p2p/discover/v5wire/encoding_test.go index ff0b3af635..cd7d06b40c 100644 --- a/p2p/discover/v5wire/encoding_test.go +++ b/p2p/discover/v5wire/encoding_test.go @@ -504,8 +504,8 @@ type handshakeTestNode struct { func newHandshakeTest() *handshakeTest { t := new(handshakeTest) - t.nodeA.init(testKeyA, net.IP{127, 0, 0, 1}, &t.clock) - t.nodeB.init(testKeyB, net.IP{127, 0, 0, 1}, &t.clock) + t.nodeA.init(testKeyA, net.IP{127, 0, 0, 1}, &t.clock, DefaultProtocolID) + t.nodeB.init(testKeyB, net.IP{127, 0, 0, 1}, &t.clock, DefaultProtocolID) return t } @@ -514,11 +514,11 @@ func (t *handshakeTest) close() { t.nodeB.ln.Database().Close() } -func (n *handshakeTestNode) init(key *ecdsa.PrivateKey, ip net.IP, clock mclock.Clock) { +func (n *handshakeTestNode) init(key *ecdsa.PrivateKey, ip net.IP, clock mclock.Clock, protocolID [6]byte) { db, _ := enode.OpenDB("") n.ln = enode.NewLocalNode(db, key) n.ln.SetStaticIP(ip) - n.c = NewCodec(n.ln, key, clock) + n.c = NewCodec(n.ln, key, clock, nil) } func (n *handshakeTestNode) encode(t testing.TB, to handshakeTestNode, p Packet) ([]byte, Nonce) { diff --git a/vendor/github.com/CortexFoundation/torrentfs/backend/handler.go b/vendor/github.com/CortexFoundation/torrentfs/backend/handler.go index 7503b3ef89..afe777da73 100644 --- a/vendor/github.com/CortexFoundation/torrentfs/backend/handler.go +++ b/vendor/github.com/CortexFoundation/torrentfs/backend/handler.go @@ -600,7 +600,7 @@ func NewTorrentManager(config *params.Config, fsid uint64, cache, compress bool, activeChan: make(chan *Torrent, torrentChanSize), pendingChan: make(chan *Torrent, torrentChanSize), pendingRemoveChan: make(chan string, torrentChanSize), - droppingChan: make(chan string, torrentChanSize), + droppingChan: make(chan string, 1), mode: config.Mode, //boost: config.Boost, id: fsid, @@ -654,6 +654,8 @@ func NewTorrentManager(config *params.Config, fsid uint64, cache, compress bool, func (tm *TorrentManager) Start() (err error) { tm.startOnce.Do(func() { + tm.wg.Add(1) + go tm.droppingLoop() tm.wg.Add(1) go tm.seedingLoop() tm.wg.Add(1) @@ -825,7 +827,7 @@ func (tm *TorrentManager) pendingLoop() { } } else { log.Error("Meta info marshal failed", "ih", t.infohash, "err", err) - tm.droppingChan <- t.infohash + tm.Drop(t.infohash) return } @@ -840,12 +842,12 @@ func (tm *TorrentManager) pendingLoop() { tm.pendingRemoveChan <- t.infohash } else { log.Error("Write torrent info to file failed", "ih", t.infohash, "err", err) - tm.droppingChan <- t.infohash + tm.Drop(t.infohash) } case <-t.Closed(): case <-tm.closeAll: case <-ctx.Done(): - tm.droppingChan <- t.infohash + tm.Drop(t.infohash) } }() case i := <-tm.pendingRemoveChan: @@ -904,7 +906,7 @@ func (tm *TorrentManager) activeLoop() { case <-timer.C: if t := tm.getTorrent(i); t != nil { //&& t.Ready() { if t.cited <= 0 { - tm.droppingChan <- i + tm.Drop(i) return } else { t.lock.Lock() @@ -996,6 +998,17 @@ func (tm *TorrentManager) seedingLoop() { }() } } + case <-tm.closeAll: + log.Info("Seeding loop closed") + return + } + } +} + +func (tm *TorrentManager) droppingLoop() { + defer tm.wg.Done() + for { + select { case ih := <-tm.droppingChan: if t := tm.getTorrent(ih); t != nil { //&& t.Ready() { t.Torrent.Drop() @@ -1017,7 +1030,7 @@ func (tm *TorrentManager) seedingLoop() { log.Warn("Drop seed not found", "ih", ih) } case <-tm.closeAll: - log.Info("Seeding loop closed") + log.Info("Dropping loop closed") return } } diff --git a/vendor/modules.txt b/vendor/modules.txt index 4724043794..89d00f1d75 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -48,7 +48,7 @@ github.com/CortexFoundation/merkletree # github.com/CortexFoundation/statik v0.0.0-20210315012922-8bb8a7b5dc66 ## explicit; go 1.16 github.com/CortexFoundation/statik -# github.com/CortexFoundation/torrentfs v1.0.35-0.20221121210641-607c9ddacb73 +# github.com/CortexFoundation/torrentfs v1.0.35-0.20221126200542-1ab479237bfe ## explicit; go 1.19 github.com/CortexFoundation/torrentfs github.com/CortexFoundation/torrentfs/backend