@@ -2,7 +2,6 @@ package database
22
33import (
44 "context"
5- "flare-ftso-indexer/logger"
65 "sync"
76 "time"
87
@@ -64,7 +63,13 @@ func (s *DBStates) updateStates(newStates map[string]*State) {
6463
6564func (s * DBStates ) updateIndex (name string , newIndex , blockTimestamp uint64 ) {
6665 s .mu .Lock ()
67- s .States [name ].updateIndex (newIndex , blockTimestamp )
66+ state := s .States [name ]
67+ if state == nil {
68+ state = & State {Name : name }
69+ s .States [name ] = state
70+ }
71+
72+ state .updateIndex (newIndex , blockTimestamp )
6873 s .mu .Unlock ()
6974}
7075
@@ -76,50 +81,32 @@ func (s *DBStates) updateDB(db *gorm.DB, name string) error {
7681}
7782
7883func (s * DBStates ) Update (db * gorm.DB , name string , newIndex , blockTimestamp uint64 ) error {
79- s .mu .RLock ()
80- state := s .States [name ]
81- s .mu .RUnlock ()
82-
83- if state == nil {
84- return errors .Errorf ("state %s not found" , name )
85- }
86-
8784 s .updateIndex (name , newIndex , blockTimestamp )
8885 return s .updateDB (db , name )
8986}
9087
9188func (s * DBStates ) UpdateAtStart (
92- db * gorm.DB , startIndex , startBlockTimestamp , lastChainIndex , lastBlockTimestamp , stopIndex uint64 ,
93- ) (uint64 , uint64 , error ) {
94- var err error
95-
89+ db * gorm.DB , startIndex , startBlockTimestamp , lastChainIndex , lastBlockTimestamp uint64 ,
90+ ) error {
9691 s .mu .RLock ()
97- firstIndex := s .States [FirstDatabaseIndexState ].Index
98- lastIndex := s .States [LastDatabaseIndexState ].Index
92+ _ , firstDatabaseIndexSet := s .States [FirstDatabaseIndexState ]
9993 s .mu .RUnlock ()
10094
101- if startIndex >= firstIndex && startIndex <= lastIndex {
102- logger .Info ("Data from blocks %d to %d already in the database" , startIndex , lastIndex )
103- startIndex = lastIndex + 1
104- } else {
105- logger .Warn ("Data from blocks %d to %d not in the database, starting from %d" , startIndex , lastIndex , firstIndex )
106-
107- // if startIndex is set before existing data in the DB or a break among saved blocks
108- // in the DB is created, then we change the guaranties about the starting block
109- err = s .Update (db , FirstDatabaseIndexState , startIndex , startBlockTimestamp )
95+ // Set the first database index state only if it does not exist yet
96+ if ! firstDatabaseIndexSet {
97+ err := s .Update (db , FirstDatabaseIndexState , startIndex , startBlockTimestamp )
11098 if err != nil {
111- return 0 , 0 , errors .Wrap (err , "states.Update" )
99+ return errors .Wrap (err , "states.Update(FirstDatabaseIndexState) " )
112100 }
113101 }
114102
115- err = s .Update (db , LastChainIndexState , lastChainIndex , lastBlockTimestamp )
103+ // Set the state for the current latest chain index
104+ err := s .Update (db , LastChainIndexState , lastChainIndex , lastBlockTimestamp )
116105 if err != nil {
117- return 0 , 0 , errors .Wrap (err , "states.Update" )
106+ return errors .Wrap (err , "states.Update(LastChainIndexState) " )
118107 }
119108
120- lastIndex = min (stopIndex , lastChainIndex )
121-
122- return startIndex , lastIndex , nil
109+ return nil
123110}
124111
125112func UpdateDBStates (ctx context.Context , db * gorm.DB ) (* DBStates , error ) {
@@ -148,12 +135,7 @@ func getDBStates(ctx context.Context, db *gorm.DB) (map[string]*State, error) {
148135 return errors .Wrap (err , "db.Where" )
149136 }
150137
151- // If the state is not found, create a new one.
152- state = & State {Name : name , Updated : time .Now ()}
153- err := db .Create (state ).Error
154- if err != nil {
155- return errors .Wrap (err , "db.Create" )
156- }
138+ return nil
157139 }
158140
159141 mu .Lock ()
0 commit comments