@@ -13,6 +13,7 @@ import (
1313 "go.vocdoni.io/dvote/types"
1414 "go.vocdoni.io/dvote/vochain/scrutinizer"
1515 "go.vocdoni.io/dvote/vochain/scrutinizer/indexertypes"
16+ "go.vocdoni.io/proto/build/go/models"
1617)
1718
1819type ProcessArchive struct {
@@ -58,6 +59,21 @@ func (js *jsonStorage) AddProcess(p *Process) error {
5859 return os .WriteFile (filepath .Join (js .datadir , fmt .Sprintf ("%x" , p .ProcessInfo .ID )), data , 0o644 )
5960}
6061
62+ // GetProcess retreives a process from the js storage
63+ func (js * jsonStorage ) GetProcess (pid []byte ) (* Process , error ) {
64+ if len (pid ) != types .ProcessIDsize {
65+ return nil , fmt .Errorf ("process not valid" )
66+ }
67+ js .lock .Lock ()
68+ defer js .lock .Unlock ()
69+ data , err := os .ReadFile (filepath .Join (js .datadir , fmt .Sprintf ("%x" , pid )))
70+ if err != nil {
71+ return nil , err
72+ }
73+ p := & Process {}
74+ return p , json .Unmarshal (data , p )
75+ }
76+
6177// ProcessExist returns true if a process already existin in the storage
6278func (js * jsonStorage ) ProcessExist (pid []byte ) (bool , error ) {
6379 js .lock .Lock ()
@@ -157,14 +173,27 @@ func (pa *ProcessArchive) ProcessScan(fromBlock int) error {
157173 return nil
158174}
159175
160- // OnComputeResults implements the indexer event callback
176+ // OnComputeResults implements the indexer event callback.
177+ // On this event the results are set always and the process info only if it
178+ // does not exist yet in the json storage.
161179func (pa * ProcessArchive ) OnComputeResults (results * indexertypes.Results ,
162180 proc * indexertypes.Process , height uint32 ) {
163- if err := pa .storage .AddProcess (& Process {ProcessInfo : proc , Results : results }); err != nil {
181+ // Get the process (if exist)
182+ jsProc , err := pa .storage .GetProcess (results .ProcessID )
183+ if err != nil {
184+ if os .IsNotExist (err ) { // if it does not exist yet, we create it
185+ jsProc = & Process {ProcessInfo : proc , Results : results }
186+ } else {
187+ log .Errorf ("cannot get json store process: %v" , err )
188+ return
189+ }
190+ }
191+ jsProc .Results = results
192+ if err := pa .storage .AddProcess (jsProc ); err != nil {
164193 log .Errorf ("cannot add json process: %v" , err )
165194 return
166195 }
167- log .Infof ("stored json process %x" , proc .ID )
196+ log .Infof ("stored json process %x for compute results event " , proc .ID )
168197
169198 // send publish signal
170199 log .Debugf ("sending archive publish signal for height %d" , height )
@@ -174,6 +203,44 @@ func (pa *ProcessArchive) OnComputeResults(results *indexertypes.Results,
174203 }
175204}
176205
206+ // OnOracleResults implements the indexer event callback.
207+ // On this event the process status is set to Results.
208+ func (pa * ProcessArchive ) OnOracleResults (oracleResults * models.ProcessResult , pid []byte , height uint32 ) {
209+ jsProc , err := pa .storage .GetProcess (pid )
210+ if err != nil {
211+ if os .IsNotExist (err ) { // if it does not exist yet, we create it
212+ proc , err := pa .indexer .ProcessInfo (pid )
213+ if err != nil {
214+ log .Errorf ("cannot get process info %x from indexer: %v" , pid , err )
215+ return
216+ }
217+ jsProc = & Process {ProcessInfo : proc , Results : nil }
218+ } else {
219+ log .Errorf ("cannot get json store process: %v" , err )
220+ return
221+ }
222+ }
223+ // Ensure the status is set to RESULTS since OnOracleResults event is called on setProcessResultsTx
224+ jsProc .ProcessInfo .Status = int32 (models .ProcessStatus_RESULTS )
225+ jsProc .ProcessInfo .FinalResults = true
226+ // TODO: add signatures from oracles
227+ //jsProc.Results.Signatures = append(jsProc.results.Signatures, oracleResults.Signature)
228+
229+ // Store the process
230+ if err := pa .storage .AddProcess (jsProc ); err != nil {
231+ log .Errorf ("cannot add json process: %v" , err )
232+ return
233+ }
234+ log .Infof ("stored json process %x for oracle results transaction event" , pid )
235+
236+ // Send publish signal
237+ log .Debugf ("sending archive publish signal for height %d" , height )
238+ select {
239+ case pa .publish <- true :
240+ default : // do nothing
241+ }
242+ }
243+
177244// Close closes the process archive
178245func (pa * ProcessArchive ) Close () {
179246 pa .close <- true
0 commit comments