Browse Source

:gear: ledger: do not call genesis if we have already a chain

Ettore Di Giacinto 3 years ago
parent
commit
cf14509541
2 changed files with 6 additions and 1 deletions
  1. 3 1
      pkg/blockchain/ledger.go
  2. 3 0
      pkg/blockchain/store_memory.go

+ 3 - 1
pkg/blockchain/ledger.go

@@ -49,7 +49,9 @@ type Store interface {
 // New returns a new ledger which writes to the writer
 func New(w io.Writer, s Store) *Ledger {
 	c := &Ledger{channel: w, blockchain: s}
-	c.newGenesis()
+	if s.Len() == 0 {
+		c.newGenesis()
+	}
 	return c
 }
 

+ 3 - 0
pkg/blockchain/store_memory.go

@@ -31,6 +31,9 @@ func (m *MemoryStore) Add(b Block) {
 func (m *MemoryStore) Len() int {
 	m.Lock()
 	defer m.Unlock()
+	if m.block == nil {
+		return 0
+	}
 	return m.block.Index
 }