2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2018-05-07 15:41:02 +00:00
|
|
|
package cold_import
|
2018-05-02 16:17:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/ethereum"
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/converters/common"
|
2018-05-02 16:17:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ColdImporter struct {
|
|
|
|
blockRepository datastore.BlockRepository
|
|
|
|
converter common.BlockConverter
|
|
|
|
ethDB ethereum.Database
|
2019-08-05 15:38:37 +00:00
|
|
|
receiptRepository datastore.FullSyncReceiptRepository
|
2018-05-02 16:17:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 15:38:37 +00:00
|
|
|
func NewColdImporter(ethDB ethereum.Database, blockRepository datastore.BlockRepository, receiptRepository datastore.FullSyncReceiptRepository, converter common.BlockConverter) *ColdImporter {
|
2018-05-02 16:17:02 +00:00
|
|
|
return &ColdImporter{
|
|
|
|
blockRepository: blockRepository,
|
|
|
|
converter: converter,
|
|
|
|
ethDB: ethDB,
|
|
|
|
receiptRepository: receiptRepository,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:41:02 +00:00
|
|
|
func (ci *ColdImporter) Execute(startingBlockNumber int64, endingBlockNumber int64, nodeId string) error {
|
|
|
|
missingBlocks := ci.blockRepository.MissingBlockNumbers(startingBlockNumber, endingBlockNumber, nodeId)
|
|
|
|
for _, n := range missingBlocks {
|
|
|
|
hash := ci.ethDB.GetBlockHash(n)
|
2018-05-02 16:17:02 +00:00
|
|
|
|
2018-05-07 15:41:02 +00:00
|
|
|
blockId, err := ci.createBlocksAndTransactions(hash, n)
|
2018-05-02 16:17:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-07 15:41:02 +00:00
|
|
|
err = ci.createReceiptsAndLogs(hash, n, blockId)
|
2018-05-02 16:17:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-05-07 15:41:02 +00:00
|
|
|
ci.blockRepository.SetBlocksStatus(endingBlockNumber)
|
2018-05-02 16:17:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ci *ColdImporter) createBlocksAndTransactions(hash []byte, i int64) (int64, error) {
|
|
|
|
block := ci.ethDB.GetBlock(hash, i)
|
|
|
|
coreBlock, err := ci.converter.ToCoreBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return ci.blockRepository.CreateOrUpdateBlock(coreBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ci *ColdImporter) createReceiptsAndLogs(hash []byte, number int64, blockId int64) error {
|
|
|
|
receipts := ci.ethDB.GetBlockReceipts(hash, number)
|
2019-03-21 19:52:24 +00:00
|
|
|
coreReceipts, err := common.ToCoreReceipts(receipts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-02 16:17:02 +00:00
|
|
|
return ci.receiptRepository.CreateReceiptsAndLogs(blockId, coreReceipts)
|
|
|
|
}
|