forked from cerc-io/ipld-eth-server
Update vendor directory and make necessary code changes
Fixes for new geth version
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/go-semver/semver"
|
||||
"github.com/libp2p/go-libp2p-core/protocol"
|
||||
)
|
||||
|
||||
// MultistreamSemverMatcher returns a matcher function for a given base protocol.
|
||||
// The matcher function will return a boolean indicating whether a protocol ID
|
||||
// matches the base protocol. A given protocol ID matches the base protocol if
|
||||
// the IDs are the same and if the semantic version of the base protocol is the
|
||||
// same or higher than that of the protocol ID provided.
|
||||
// TODO
|
||||
func MultistreamSemverMatcher(base protocol.ID) (func(string) bool, error) {
|
||||
parts := strings.Split(string(base), "/")
|
||||
vers, err := semver.NewVersion(parts[len(parts)-1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return func(check string) bool {
|
||||
chparts := strings.Split(check, "/")
|
||||
if len(chparts) != len(parts) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i, v := range chparts[:len(chparts)-1] {
|
||||
if parts[i] != v {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
chvers, err := semver.NewVersion(chparts[len(chparts)-1])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return vers.Major == chvers.Major && vers.Minor >= chvers.Minor
|
||||
}, nil
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/libp2p/go-libp2p-core/network"
|
||||
)
|
||||
|
||||
// EOFTimeout is the maximum amount of time to wait to successfully observe an
|
||||
// EOF on the stream. Defaults to 60 seconds.
|
||||
var EOFTimeout = time.Second * 60
|
||||
|
||||
// ErrExpectedEOF is returned when we read data while expecting an EOF.
|
||||
var ErrExpectedEOF = errors.New("read data when expecting EOF")
|
||||
|
||||
// FullClose closes the stream and waits to read an EOF from the other side.
|
||||
//
|
||||
// * If it reads any data *before* the EOF, it resets the stream.
|
||||
// * If it doesn't read an EOF within EOFTimeout, it resets the stream.
|
||||
//
|
||||
// You'll likely want to invoke this as `go FullClose(stream)` to close the
|
||||
// stream in the background.
|
||||
func FullClose(s network.Stream) error {
|
||||
if err := s.Close(); err != nil {
|
||||
s.Reset()
|
||||
return err
|
||||
}
|
||||
return AwaitEOF(s)
|
||||
}
|
||||
|
||||
// AwaitEOF waits for an EOF on the given stream, returning an error if that
|
||||
// fails. It waits at most EOFTimeout (defaults to 1 minute) after which it
|
||||
// resets the stream.
|
||||
func AwaitEOF(s network.Stream) error {
|
||||
// So we don't wait forever
|
||||
s.SetDeadline(time.Now().Add(EOFTimeout))
|
||||
|
||||
// We *have* to observe the EOF. Otherwise, we leak the stream.
|
||||
// Now, technically, we should do this *before*
|
||||
// returning from SendMessage as the message
|
||||
// hasn't really been sent yet until we see the
|
||||
// EOF but we don't actually *know* what
|
||||
// protocol the other side is speaking.
|
||||
n, err := s.Read([]byte{0})
|
||||
if n > 0 || err == nil {
|
||||
s.Reset()
|
||||
return ErrExpectedEOF
|
||||
}
|
||||
if err != io.EOF {
|
||||
s.Reset()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user