2019-04-15 16:28:23 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package ipfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2019-04-18 12:39:37 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-04-15 16:28:23 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
)
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
const payloadChanBufferSize = 800 // 1/10th max eth sub buffer size
|
2019-04-15 16:28:23 +00:00
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Indexer is an interface for streaming, converting to IPLDs, publishing, and indexing all Ethereum data
|
|
|
|
// This is the top-level interface used by the syncAndPublish command
|
2019-04-15 16:28:23 +00:00
|
|
|
type Indexer interface {
|
2019-04-18 12:39:37 +00:00
|
|
|
Index(wg *sync.WaitGroup) error
|
2019-04-15 16:28:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// ipfsIndexer is the underlying struct for the Indexer interface
|
|
|
|
// we are not exporting this to enforce proper initialization through the NewIPFSIndexer function
|
|
|
|
type ipfsIndexer struct {
|
2019-04-18 12:39:37 +00:00
|
|
|
Streamer Streamer
|
|
|
|
Converter Converter
|
|
|
|
Publisher Publisher
|
|
|
|
Repository Repository
|
2019-04-15 16:28:23 +00:00
|
|
|
PayloadChan chan statediff.Payload
|
|
|
|
QuitChan chan bool
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// NewIPFSIndexer creates a new Indexer interface using an underlying ipfsIndexer struct
|
|
|
|
func NewIPFSIndexer(ipfsPath string, db *postgres.DB, ethClient core.EthClient, rpcClient core.RpcClient, qc chan bool) (Indexer, error) {
|
2019-04-15 16:28:23 +00:00
|
|
|
publisher, err := NewIPLDPublisher(ipfsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
return &ipfsIndexer{
|
2019-04-18 12:39:37 +00:00
|
|
|
Streamer: NewStateDiffSyncer(rpcClient),
|
|
|
|
Repository: NewCIDRepository(db),
|
|
|
|
Converter: NewPayloadConverter(ethClient),
|
|
|
|
Publisher: publisher,
|
2019-04-15 16:28:23 +00:00
|
|
|
PayloadChan: make(chan statediff.Payload, payloadChanBufferSize),
|
2019-04-18 12:39:37 +00:00
|
|
|
QuitChan: qc,
|
2019-04-15 16:28:23 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Index is the main processing loop
|
2019-04-18 12:39:37 +00:00
|
|
|
func (i *ipfsIndexer) Index(wg *sync.WaitGroup) error {
|
2019-04-17 18:26:48 +00:00
|
|
|
sub, err := i.Streamer.Stream(i.PayloadChan)
|
2019-04-15 16:28:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case payload := <-i.PayloadChan:
|
2019-04-18 12:39:37 +00:00
|
|
|
if payload.Err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-04-15 16:28:23 +00:00
|
|
|
ipldPayload, err := i.Converter.Convert(payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
cidPayload, err := i.Publisher.Publish(ipldPayload)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
err = i.Repository.Index(cidPayload)
|
2019-04-15 16:28:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
case err = <-sub.Err():
|
|
|
|
log.Error(err)
|
|
|
|
case <-i.QuitChan:
|
|
|
|
log.Info("quiting IPFSIndexer")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|