github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/private/constellation/constellation.go (about)

     1  package constellation
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/patrickmn/go-cache"
    12  )
    13  
    14  type Constellation struct {
    15  	node                    *Client
    16  	c                       *cache.Cache
    17  	isConstellationNotInUse bool
    18  }
    19  
    20  var (
    21  	errPrivateTransactionManagerNotUsed = errors.New("private transaction manager not in use")
    22  )
    23  
    24  func (g *Constellation) Send(data []byte, from string, to []string) (out []byte, err error) {
    25  	if g.isConstellationNotInUse {
    26  		return nil, errPrivateTransactionManagerNotUsed
    27  	}
    28  	out, err = g.node.SendPayload(data, from, to)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	g.c.Set(string(out), data, cache.DefaultExpiration)
    33  	return out, nil
    34  }
    35  
    36  func (g *Constellation) SendSignedTx(data []byte, to []string) (out []byte, err error) {
    37  	if g.isConstellationNotInUse {
    38  		return nil, errPrivateTransactionManagerNotUsed
    39  	}
    40  	out, err = g.node.SendSignedPayload(data, to)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	return out, nil
    45  }
    46  
    47  func (g *Constellation) Receive(data []byte) ([]byte, error) {
    48  	if g.isConstellationNotInUse {
    49  		return nil, nil
    50  	}
    51  	if len(data) == 0 {
    52  		return data, nil
    53  	}
    54  	// Ignore this error since not being a recipient of
    55  	// a payload isn't an error.
    56  	// TODO: Return an error if it's anything OTHER than
    57  	// 'you are not a recipient.'
    58  	dataStr := string(data)
    59  	x, found := g.c.Get(dataStr)
    60  	if found {
    61  		return x.([]byte), nil
    62  	}
    63  	pl, _ := g.node.ReceivePayload(data)
    64  	g.c.Set(dataStr, pl, cache.DefaultExpiration)
    65  	return pl, nil
    66  }
    67  
    68  func New(path string) (*Constellation, error) {
    69  	info, err := os.Lstat(path)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	// We accept either the socket or a configuration file that points to
    74  	// a socket.
    75  	isSocket := info.Mode()&os.ModeSocket != 0
    76  	if !isSocket {
    77  		cfg, err := LoadConfig(path)
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  		path = filepath.Join(cfg.WorkDir, cfg.Socket)
    82  	}
    83  	err = RunNode(path)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	n, err := NewClient(path)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	return &Constellation{
    92  		node:                    n,
    93  		c:                       cache.New(5*time.Minute, 5*time.Minute),
    94  		isConstellationNotInUse: false,
    95  	}, nil
    96  }
    97  
    98  func MustNew(path string) *Constellation {
    99  	if strings.EqualFold(path, "ignore") {
   100  		return &Constellation{
   101  			node:                    nil,
   102  			c:                       nil,
   103  			isConstellationNotInUse: true,
   104  		}
   105  	}
   106  	g, err := New(path)
   107  	if err != nil {
   108  		panic(fmt.Sprintf("MustNew: Failed to connect to Constellation (%s): %v", path, err))
   109  	}
   110  	return g
   111  }