Skip to content

Commit

Permalink
half-baked attempt at indexing title and description after offchain d…
Browse files Browse the repository at this point in the history
…ownload happens
  • Loading branch information
altergui committed Aug 28, 2024
1 parent 43d2089 commit f72f3d4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions service/offchaindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (vs *VocdoniService) OffChainDataHandler() error {
}
vs.OffChainData = offchaindatahandler.NewOffChainDataHandler(
vs.App,
vs.Indexer,
vs.DataDownloader,
vs.CensusDB,
vs.Config.SkipPreviousOffchainData,
Expand Down
29 changes: 29 additions & 0 deletions vochain/indexer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package indexer
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"go.vocdoni.io/proto/build/go/models"

"go.vocdoni.io/dvote/api"
"go.vocdoni.io/dvote/data"
"go.vocdoni.io/dvote/log"
indexerdb "go.vocdoni.io/dvote/vochain/indexer/db"
"go.vocdoni.io/dvote/vochain/indexer/indexertypes"
Expand Down Expand Up @@ -315,3 +318,29 @@ func (idx *Indexer) updateProcess(ctx context.Context, queries *indexerdb.Querie
}
return nil
}

// updateProcessMetadata synchronize title and description
// with the information fetched over IPFS.
func (idx *Indexer) UpdateProcessMetadata(d data.Storage, pid []byte, uri string) error {
// Try to retrieve the election metadata
if d != nil {
stgCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

// TODO: abstract all of this somewhere else and just call from here
metadataBytes, err := d.Retrieve(stgCtx, uri, MaxOffchainFileSize)
if err != nil {
log.Warnf("cannot get metadata from %s: %v", election.MetadataURL, err)
} else {
// if metadata exists and is not encrypted, add it to the indexed election
// if the metadata is not encrypted, unmarshal it, otherwise store it as bytes
if !election.ElectionMode.EncryptedMetaData {
electionMetadata := api.ElectionMetadata{}
if err := json.Unmarshal(metadataBytes, &electionMetadata); err != nil {
log.Warnf("cannot unmarshal metadata from %s: %v", election.MetadataURL, err)
}
election.Metadata = &electionMetadata
}
}
}
}
11 changes: 7 additions & 4 deletions vochain/offchaindatahandler/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (

// enqueueMetadata enqueue a election metadata for download.
// (safe for concurrent use, simply pushes an item to a channel)
func (d *OffChainDataHandler) enqueueMetadata(uri string) {
if !strings.HasPrefix(uri, d.storage.RemoteStorage.URIprefix()) {
log.Warnf("metadata URI not valid: %s", uri)
func (d *OffChainDataHandler) enqueueMetadata(item importItem) {
if !strings.HasPrefix(item.uri, d.storage.RemoteStorage.URIprefix()) {
log.Warnf("metadata URI not valid: %s", item.uri)
return
}
d.storage.AddToQueue(uri, func(s string, b []byte) {
d.storage.AddToQueue(item.uri, func(s string, b []byte) {
log.Infof("metadata downloaded successfully from %s (%d bytes)", s, len(b))
if item.itemType == itemTypeElectionMetadata && len(item.pid) > 0 {
d.indexer.UpdateProcessMetadata(d.storage.RemoteStorage, item.pid, item.uri)
}
}, true)
}
10 changes: 8 additions & 2 deletions vochain/offchaindatahandler/offchaindatahandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"go.vocdoni.io/dvote/api/censusdb"
"go.vocdoni.io/dvote/data/downloader"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/dvote/util"
"go.vocdoni.io/dvote/vochain"
"go.vocdoni.io/dvote/vochain/indexer"
"go.vocdoni.io/dvote/vochain/state"
"go.vocdoni.io/dvote/vochain/transaction/vochaintx"
"go.vocdoni.io/proto/build/go/models"
Expand All @@ -26,6 +28,7 @@ type importItem struct {
itemType int
uri string
censusRoot string
pid types.HexBytes
}

var itemTypesToString = map[int]string{
Expand All @@ -42,6 +45,7 @@ var itemTypesToString = map[int]string{
// offchain data (usually on IPFS).
type OffChainDataHandler struct {
vochain *vochain.BaseApplication
indexer *indexer.Indexer
census *censusdb.CensusDB
storage *downloader.Downloader
queue []importItem
Expand All @@ -52,11 +56,12 @@ type OffChainDataHandler struct {

// NewOffChainDataHandler creates a new instance of the off chain data downloader daemon.
// It will subscribe to Vochain events and perform data import.
func NewOffChainDataHandler(v *vochain.BaseApplication, d *downloader.Downloader,
func NewOffChainDataHandler(v *vochain.BaseApplication, i *indexer.Indexer, d *downloader.Downloader,
c *censusdb.CensusDB, importOnlyNew bool,
) *OffChainDataHandler {
od := OffChainDataHandler{
vochain: v,
indexer: i,
census: c,
storage: d,
importOnlyNew: importOnlyNew,
Expand Down Expand Up @@ -87,7 +92,7 @@ func (d *OffChainDataHandler) Commit(_ uint32) error {
go d.enqueueOffchainCensus(item.censusRoot, item.uri)
case itemTypeElectionMetadata, itemTypeAccountMetadata:
log.Infow("importing data", "type", itemTypesToString[item.itemType], "uri", item.uri)
go d.enqueueMetadata(item.uri)
go d.enqueueMetadata(item)
default:
log.Errorf("unknown import item %d", item.itemType)
}
Expand All @@ -107,6 +112,7 @@ func (d *OffChainDataHandler) OnProcess(p *models.Process, _ int32) {
if m := p.GetMetadata(); m != "" {
log.Debugf("adding election metadata %s to queue", m)
d.queue = append(d.queue, importItem{
pid: p.GetProcessId(),
uri: m,
itemType: itemTypeElectionMetadata,
})
Expand Down

0 comments on commit f72f3d4

Please sign in to comment.