Browse Source

:satellite: Allow to specify a store from EdgeVPN

Ettore Di Giacinto 3 years ago
parent
commit
7e58bf0e62
6 changed files with 22 additions and 10 deletions
  1. 2 1
      cmd/util.go
  2. 3 3
      pkg/blockchain/ledger.go
  3. 5 5
      pkg/blockchain/store_disk.go
  4. 3 0
      pkg/edgevpn/config.go
  5. 1 1
      pkg/edgevpn/edgevpn.go
  6. 8 0
      pkg/edgevpn/options.go

+ 2 - 1
cmd/util.go

@@ -3,6 +3,7 @@ package cmd
 import (
 	"github.com/ipfs/go-log"
 	"github.com/mudler/edgevpn/internal"
+	"github.com/mudler/edgevpn/pkg/blockchain"
 	"github.com/mudler/edgevpn/pkg/edgevpn"
 	"github.com/mudler/edgevpn/pkg/logger"
 	"github.com/songgao/water"
@@ -46,7 +47,7 @@ func cliToOpts(c *cli.Context) []edgevpn.Option {
 		edgevpn.WithPacketMTU(1420),
 		edgevpn.WithInterfaceAddress(address),
 		edgevpn.WithInterfaceName(iface),
-		edgevpn.WithMaxBlockChainSize(1000),
+		edgevpn.WithStore(&blockchain.MemoryStore{}),
 		edgevpn.WithInterfaceType(water.TUN),
 		edgevpn.NetLinkBootstrap(true),
 		edgevpn.FromBase64(token),

+ 3 - 3
pkg/blockchain/ledger.go

@@ -15,14 +15,14 @@ import (
 
 type Ledger struct {
 	sync.Mutex
-	blockchain store
+	blockchain Store
 
 	channel io.Writer
 
 	onDisk bool
 }
 
-type store interface {
+type Store interface {
 	Add(Block)
 	Reset()
 	Len() int
@@ -30,7 +30,7 @@ type store interface {
 }
 
 // New returns a new ledger which writes to the writer
-func New(w io.Writer, s store) *Ledger {
+func New(w io.Writer, s Store) *Ledger {
 	c := &Ledger{channel: w, blockchain: s}
 	c.newGenesis()
 	return c

+ 5 - 5
pkg/blockchain/store_disk.go

@@ -8,22 +8,22 @@ import (
 	"github.com/peterbourgon/diskv"
 )
 
-type disk struct {
+type DiskStore struct {
 	chain *diskv.Diskv
 }
 
-func (m *disk) Add(b Block) {
+func (m *DiskStore) Add(b Block) {
 	bb, _ := json.Marshal(b)
 	m.chain.Write(fmt.Sprint(b.Index), bb)
 	m.chain.Write("index", []byte(fmt.Sprint(b.Index)))
 
 }
 
-func (m *disk) Reset() {
+func (m *DiskStore) Reset() {
 	m.chain.EraseAll()
 }
 
-func (m *disk) Len() int {
+func (m *DiskStore) Len() int {
 	count, err := m.chain.Read("index")
 	if err != nil {
 		return 0
@@ -33,7 +33,7 @@ func (m *disk) Len() int {
 
 }
 
-func (m *disk) Last() Block {
+func (m *DiskStore) Last() Block {
 	b := &Block{}
 
 	count, err := m.chain.Read("index")

+ 3 - 0
pkg/edgevpn/config.go

@@ -9,6 +9,7 @@ import (
 	"github.com/libp2p/go-libp2p-core/host"
 	"github.com/libp2p/go-libp2p-core/network"
 	"github.com/libp2p/go-libp2p-core/protocol"
+	"github.com/mudler/edgevpn/pkg/blockchain"
 	discovery "github.com/mudler/edgevpn/pkg/discovery"
 	hub "github.com/mudler/edgevpn/pkg/hub"
 	"github.com/songgao/water"
@@ -45,6 +46,8 @@ type Config struct {
 
 	NetLinkBootstrap bool
 
+	Store blockchain.Store
+
 	// Handle is a handle consumed by HumanInterfaces to handle received messages
 	Handle                     func(bool, *hub.Message)
 	StreamHandlers             map[protocol.ID]StreamHandler

+ 1 - 1
pkg/edgevpn/edgevpn.go

@@ -62,7 +62,7 @@ func (e *EdgeVPN) Ledger() (*blockchain.Ledger, error) {
 		return nil, err
 	}
 
-	e.ledger = blockchain.New(mw, &blockchain.MemoryStore{})
+	e.ledger = blockchain.New(mw, e.config.Store)
 	return e.ledger, nil
 }
 

+ 8 - 0
pkg/edgevpn/options.go

@@ -7,6 +7,7 @@ import (
 	"github.com/ipfs/go-log"
 	"github.com/libp2p/go-libp2p"
 	"github.com/libp2p/go-libp2p-core/protocol"
+	"github.com/mudler/edgevpn/pkg/blockchain"
 	discovery "github.com/mudler/edgevpn/pkg/discovery"
 	"github.com/mudler/edgevpn/pkg/utils"
 	"github.com/pkg/errors"
@@ -65,6 +66,13 @@ func WithInterfaceType(d water.DeviceType) func(cfg *Config) error {
 	}
 }
 
+func WithStore(s blockchain.Store) func(cfg *Config) error {
+	return func(cfg *Config) error {
+		cfg.Store = s
+		return nil
+	}
+}
+
 func WithInterfaceName(i string) func(cfg *Config) error {
 	return func(cfg *Config) error {
 		cfg.InterfaceName = i