Browse Source

:gear: ledger: don't store what we want to add

This ensures that we read from the ledger only what was streamed back
mudler 3 years ago
parent
commit
df141ebbe2
1 changed files with 10 additions and 2 deletions
  1. 10 2
      pkg/blockchain/ledger.go

+ 10 - 2
pkg/blockchain/ledger.go

@@ -38,6 +38,8 @@ type Ledger struct {
 	channel io.Writer
 
 	onDisk bool
+
+	wantedBlock *Block
 }
 
 type Store interface {
@@ -71,7 +73,12 @@ func (l *Ledger) Syncronizer(ctx context.Context, t time.Duration) {
 			case <-t.C:
 				l.Lock()
 
-				bytes, err := json.Marshal(l.blockchain.Last())
+				b := l.blockchain.Last()
+				if l.wantedBlock != nil && l.wantedBlock.Index > b.Index {
+					b = *l.wantedBlock
+				}
+
+				bytes, err := json.Marshal(b)
 				if err != nil {
 					log.Println(err)
 				}
@@ -352,7 +359,8 @@ func (l *Ledger) writeData(s map[string]map[string]Data) {
 
 	if newBlock.IsValid(l.blockchain.Last()) {
 		l.Lock()
-		l.blockchain.Add(newBlock)
+		l.wantedBlock = &newBlock
+		//l.blockchain.Add(newBlock)
 		l.Unlock()
 	}