Created special PTM for public-only mode. (#968)

This commit is contained in:
Peter Fox 2020-05-12 20:55:21 +01:00 committed by GitHub
parent d6c2ee46e9
commit 17f98b117b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 128 additions and 33 deletions

16
private/cache/cache.go vendored Normal file
View File

@ -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)
}

View File

@ -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"
}

View File

@ -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'")
}

View File

@ -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 &notinuse.PrivateTransactionManager{}
}
return privatetransactionmanager.MustNew(cfgPath)
}

24
private/private_test.go Normal file
View File

@ -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(&notinuse.PrivateTransactionManager{}).String()
actualType := reflect.TypeOf(ptm).String()
t.Errorf("got wrong tx manager type. Expected '%s' but got '%s'", expectedType, actualType)
}
}

View File

@ -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))