github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/bft/privval/utils.go (about)

     1  package privval
     2  
     3  import (
     4  	"fmt"
     5  	"log/slog"
     6  	"net"
     7  
     8  	"github.com/gnolang/gno/tm2/pkg/crypto/ed25519"
     9  	"github.com/gnolang/gno/tm2/pkg/errors"
    10  	osm "github.com/gnolang/gno/tm2/pkg/os"
    11  )
    12  
    13  // IsConnTimeout returns a boolean indicating whether the error is known to
    14  // report that a connection timeout occurred. This detects both fundamental
    15  // network timeouts, as well as ErrConnTimeout errors.
    16  func IsConnTimeout(err error) bool {
    17  	switch errors.Cause(err).(type) {
    18  	case EndpointTimeoutError:
    19  		return true
    20  	case timeoutError:
    21  		return true
    22  	default:
    23  		return false
    24  	}
    25  }
    26  
    27  // NewSignerListener creates a new SignerListenerEndpoint using the corresponding listen address
    28  func NewSignerListener(listenAddr string, logger *slog.Logger) (*SignerListenerEndpoint, error) {
    29  	var listener net.Listener
    30  
    31  	protocol, address := osm.ProtocolAndAddress(listenAddr)
    32  	ln, err := net.Listen(protocol, address)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	switch protocol {
    37  	case "unix":
    38  		listener = NewUnixListener(ln)
    39  	case "tcp":
    40  		// TODO: persist this key so external signer can actually authenticate us
    41  		listener = NewTCPListener(ln, ed25519.GenPrivKey())
    42  	default:
    43  		return nil, fmt.Errorf(
    44  			"wrong listen address: expected either 'tcp' or 'unix' protocols, got %s",
    45  			protocol,
    46  		)
    47  	}
    48  
    49  	pve := NewSignerListenerEndpoint(logger.With("module", "privval"), listener)
    50  
    51  	return pve, nil
    52  }