diff --git a/private/cache/cache.go b/private/cache/cache.go new file mode 100644 index 000000000..c7187b9a7 --- /dev/null +++ b/private/cache/cache.go @@ -0,0 +1,16 @@ +package cache + +import ( + "time" + + gocache "github.com/patrickmn/go-cache" +) + +const ( + DefaultExpiration = 5 * time.Minute + CleanupInterval = 5 * time.Minute +) + +func NewDefaultCache() *gocache.Cache { + return gocache.New(DefaultExpiration, CleanupInterval) +} diff --git a/private/engine/notinuse/notInUsePrivateTxManager.go b/private/engine/notinuse/notInUsePrivateTxManager.go new file mode 100644 index 000000000..12de69706 --- /dev/null +++ b/private/engine/notinuse/notInUsePrivateTxManager.go @@ -0,0 +1,30 @@ +package notinuse + +import "errors" + +var ErrPrivateTxManagerNotInUse = errors.New("private transaction manager is not in use") + +// NotInUsePrivateTxManager returns an error for all communication functions, +// stating that no private transaction manager is being used by the node +type PrivateTransactionManager struct{} + +func (ptm *PrivateTransactionManager) Send(data []byte, from string, to []string) ([]byte, error) { + return nil, ErrPrivateTxManagerNotInUse +} + +func (ptm *PrivateTransactionManager) StoreRaw(data []byte, from string) ([]byte, error) { + return nil, ErrPrivateTxManagerNotInUse +} + +func (ptm *PrivateTransactionManager) SendSignedTx(data []byte, to []string) ([]byte, error) { + return nil, ErrPrivateTxManagerNotInUse +} + +func (ptm *PrivateTransactionManager) Receive(data []byte) ([]byte, error) { + //error not thrown here, acts as though no private data to fetch + return nil, nil +} + +func (ptm *PrivateTransactionManager) Name() string { + return "NotInUse" +} diff --git a/private/engine/notinuse/notInUsePrivateTxManager_test.go b/private/engine/notinuse/notInUsePrivateTxManager_test.go new file mode 100644 index 000000000..caf28b0a0 --- /dev/null +++ b/private/engine/notinuse/notInUsePrivateTxManager_test.go @@ -0,0 +1,46 @@ +package notinuse + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestName(t *testing.T) { + ptm := &PrivateTransactionManager{} + name := ptm.Name() + + assert.Equal(t, name, "NotInUse", "got wrong name for NotInUsePrivateTxManager") +} + +func TestSendReturnsError(t *testing.T) { + ptm := &PrivateTransactionManager{} + + _, err := ptm.Send([]byte{}, "", []string{}) + + assert.Equal(t, err, ErrPrivateTxManagerNotInUse, "got wrong error in 'send'") +} + +func TestStoreRawReturnsError(t *testing.T) { + ptm := &PrivateTransactionManager{} + + _, err := ptm.StoreRaw([]byte{}, "") + + assert.Equal(t, err, ErrPrivateTxManagerNotInUse, "got wrong error in 'storeraw'") +} + +func TestReceiveReturnsError(t *testing.T) { + ptm := &PrivateTransactionManager{} + + _, err := ptm.Receive([]byte{}) + + assert.Nil(t, err, "got unexpected error in 'receive'") +} + +func TestSendSignedTxReturnsError(t *testing.T) { + ptm := &PrivateTransactionManager{} + + _, err := ptm.SendSignedTx([]byte{}, []string{}) + + assert.Equal(t, err, ErrPrivateTxManagerNotInUse, "got wrong error in 'SendSignedTx'") +} diff --git a/private/private.go b/private/private.go index 8200382b3..4fc4d294b 100644 --- a/private/private.go +++ b/private/private.go @@ -2,6 +2,9 @@ package private import ( "os" + "strings" + + "github.com/ethereum/go-ethereum/private/engine/notinuse" "github.com/ethereum/go-ethereum/private/privatetransactionmanager" ) @@ -18,6 +21,9 @@ func FromEnvironmentOrNil(name string) PrivateTransactionManager { if cfgPath == "" { return nil } + if strings.EqualFold(cfgPath, "ignore") { + return ¬inuse.PrivateTransactionManager{} + } return privatetransactionmanager.MustNew(cfgPath) } diff --git a/private/private_test.go b/private/private_test.go new file mode 100644 index 000000000..91d2efcc5 --- /dev/null +++ b/private/private_test.go @@ -0,0 +1,24 @@ +package private_test + +import ( + "os" + "reflect" + "testing" + + "github.com/ethereum/go-ethereum/private/engine/notinuse" + + "github.com/ethereum/go-ethereum/private" +) + +func TestDummyPrivateTxManagerUsedWhenIgnoreSpecified(t *testing.T) { + os.Setenv("PRIVATE_CONFIG", "ignore") + + ptm := private.FromEnvironmentOrNil("PRIVATE_CONFIG") + + if _, ok := ptm.(*notinuse.PrivateTransactionManager); !ok { + expectedType := reflect.TypeOf(¬inuse.PrivateTransactionManager{}).String() + actualType := reflect.TypeOf(ptm).String() + + t.Errorf("got wrong tx manager type. Expected '%s' but got '%s'", expectedType, actualType) + } +} diff --git a/private/privatetransactionmanager/tx_manager.go b/private/privatetransactionmanager/tx_manager.go index 3504dac3b..cb3ffd1f1 100644 --- a/private/privatetransactionmanager/tx_manager.go +++ b/private/privatetransactionmanager/tx_manager.go @@ -1,30 +1,20 @@ package privatetransactionmanager import ( - "errors" "fmt" "os" "path/filepath" - "strings" - "time" - "github.com/patrickmn/go-cache" + "github.com/ethereum/go-ethereum/private/cache" + gocache "github.com/patrickmn/go-cache" ) type PrivateTransactionManager struct { - node *Client - c *cache.Cache - isPrivateTransactionManagerNotInUse bool + node *Client + c *gocache.Cache } -var ( - errPrivateTransactionManagerNotUsed = errors.New("private transaction manager not in use") -) - func (g *PrivateTransactionManager) Send(data []byte, from string, to []string) (out []byte, err error) { - if g.isPrivateTransactionManagerNotInUse { - return nil, errPrivateTransactionManagerNotUsed - } out, err = g.node.SendPayload(data, from, to) if err != nil { return nil, err @@ -34,9 +24,6 @@ func (g *PrivateTransactionManager) Send(data []byte, from string, to []string) } func (g *PrivateTransactionManager) StoreRaw(data []byte, from string) (out []byte, err error) { - if g.isPrivateTransactionManagerNotInUse { - return nil, errPrivateTransactionManagerNotUsed - } out, err = g.node.StorePayload(data, from) if err != nil { return nil, err @@ -46,9 +33,6 @@ func (g *PrivateTransactionManager) StoreRaw(data []byte, from string) (out []by } func (g *PrivateTransactionManager) SendSignedTx(data []byte, to []string) (out []byte, err error) { - if g.isPrivateTransactionManagerNotInUse { - return nil, errPrivateTransactionManagerNotUsed - } out, err = g.node.SendSignedPayload(data, to) if err != nil { return nil, err @@ -57,9 +41,6 @@ func (g *PrivateTransactionManager) SendSignedTx(data []byte, to []string) (out } func (g *PrivateTransactionManager) Receive(data []byte) ([]byte, error) { - if g.isPrivateTransactionManagerNotInUse { - return nil, nil - } if len(data) == 0 { return data, nil } @@ -101,20 +82,12 @@ func New(path string) (*PrivateTransactionManager, error) { return nil, err } return &PrivateTransactionManager{ - node: n, - c: cache.New(5*time.Minute, 5*time.Minute), - isPrivateTransactionManagerNotInUse: false, + node: n, + c: cache.NewDefaultCache(), }, nil } func MustNew(path string) *PrivateTransactionManager { - if strings.EqualFold(path, "ignore") { - return &PrivateTransactionManager{ - node: nil, - c: nil, - isPrivateTransactionManagerNotInUse: true, - } - } g, err := New(path) if err != nil { panic(fmt.Sprintf("MustNew: Failed to connect to private transaction manager (%s): %v", path, err))