浏览代码

:ledger: No need for resets anymore

Ettore Di Giacinto 3 年之前
父节点
当前提交
eb7b47bb1b
共有 6 个文件被更改,包括 11 次插入33 次删除
  1. 0 4
      pkg/blockchain/block.go
  2. 5 12
      pkg/blockchain/ledger.go
  3. 5 5
      pkg/blockchain/store_memory.go
  4. 0 2
      pkg/edgevpn/config.go
  5. 1 1
      pkg/edgevpn/edgevpn.go
  6. 0 9
      pkg/edgevpn/options.go

+ 0 - 4
pkg/blockchain/block.go

@@ -21,10 +21,6 @@ type Block struct {
 // Blockchain is a series of validated Blocks
 type Blockchain []Block
 
-func (b Blockchain) IsMoreRecent(bb Blockchain) bool {
-	return len(b) > len(bb) || len(b) == len(bb) && b[len(b)-1].Hash != bb[len(bb)-1].Hash
-}
-
 // make sure block is valid by checking index, and comparing the hash of the previous block
 func (newBlock Block) IsValid(oldBlock Block) bool {
 	if oldBlock.Index+1 != newBlock.Index {

+ 5 - 12
pkg/blockchain/ledger.go

@@ -17,8 +17,7 @@ type Ledger struct {
 	sync.Mutex
 	blockchain store
 
-	maxChainSize int
-	channel      io.Writer
+	channel io.Writer
 
 	onDisk bool
 }
@@ -31,8 +30,8 @@ type store interface {
 }
 
 // New returns a new ledger which writes to the writer
-func New(w io.Writer, maxChainSize int) *Ledger {
-	c := &Ledger{channel: w, maxChainSize: maxChainSize, blockchain: &memory{}}
+func New(w io.Writer, s store) *Ledger {
+	c := &Ledger{channel: w, blockchain: s}
 	c.newGenesis()
 	return c
 }
@@ -60,11 +59,6 @@ func (l *Ledger) Syncronizer(ctx context.Context, t time.Duration) {
 				}
 				l.channel.Write(bytes)
 
-				// Reset blockchain if we exceed chainsize
-				if l.maxChainSize != 0 && l.blockchain.Len() >= l.maxChainSize {
-					l.blockchain.Reset()
-					l.newGenesis()
-				}
 				l.Unlock()
 			case <-ctx.Done():
 				return
@@ -85,9 +79,8 @@ func (l *Ledger) Update(h *hub.Message) (err error) {
 	}
 
 	l.Lock()
-	if (l.maxChainSize == 0 || (l.maxChainSize != 0 && block.Index <= l.maxChainSize)) &&
-		block.Index > l.blockchain.Len() || block.Index == l.blockchain.Len() &&
-		block.Hash != l.blockchain.Last().Hash {
+	if block.Index > l.blockchain.Len() || (block.Index == l.blockchain.Len() &&
+		block.Hash != l.blockchain.Last().Hash) {
 		l.blockchain.Add(*block)
 	}
 	l.Unlock()

+ 5 - 5
pkg/blockchain/store_memory.go

@@ -1,21 +1,21 @@
 package blockchain
 
-type memory struct {
+type MemoryStore struct {
 	block *Block
 }
 
-func (m *memory) Add(b Block) {
+func (m *MemoryStore) Add(b Block) {
 	m.block = &b
 }
 
-func (m *memory) Reset() {
+func (m *MemoryStore) Reset() {
 	m.block = &Block{}
 }
 
-func (m *memory) Len() int {
+func (m *MemoryStore) Len() int {
 	return m.block.Index
 }
 
-func (m *memory) Last() Block {
+func (m *MemoryStore) Last() Block {
 	return *m.block
 }

+ 0 - 2
pkg/edgevpn/config.go

@@ -43,8 +43,6 @@ type Config struct {
 
 	SealKeyLength int
 
-	MaxBlockChainLength int
-
 	NetLinkBootstrap bool
 
 	// Handle is a handle consumed by HumanInterfaces to handle received messages

+ 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, e.config.MaxBlockChainLength)
+	e.ledger = blockchain.New(mw, &blockchain.MemoryStore{})
 	return e.ledger, nil
 }
 

+ 0 - 9
pkg/edgevpn/options.go

@@ -51,13 +51,6 @@ func WithInterfaceMTU(i int) func(cfg *Config) error {
 	}
 }
 
-func WithMaxBlockChainSize(i int) func(cfg *Config) error {
-	return func(cfg *Config) error {
-		cfg.MaxBlockChainLength = i
-		return nil
-	}
-}
-
 func WithPacketMTU(i int) func(cfg *Config) error {
 	return func(cfg *Config) error {
 		cfg.MTU = i
@@ -224,7 +217,6 @@ func (y YAMLConnectionConfig) copy(cfg *Config) {
 	cfg.SealKeyInterval = y.OTP.Crypto.Interval
 	cfg.ServiceDiscovery = []ServiceDiscovery{d, m}
 	cfg.SealKeyLength = y.OTP.Crypto.Length
-	cfg.MaxBlockChainLength = y.MaxBlockChainLength
 	cfg.MaxMessageSize = y.MaxMessageSize
 }
 
@@ -241,7 +233,6 @@ func GenerateNewConnectionData() (*YAMLConnectionConfig, error) {
 	config.OTP.Crypto.Interval = 9000
 	config.OTP.Crypto.Length = 12
 	config.OTP.DHT.Length = 12
-	config.MaxBlockChainLength = 0
 	config.MaxMessageSize = 20 << 20 // 20MB
 
 	return &config, nil