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-11-03 19:00:25 +00:00
|
|
|
|
|
|
|
package retriever
|
|
|
|
|
|
|
|
import (
|
2019-08-02 15:09:59 +00:00
|
|
|
"database/sql"
|
2019-10-15 18:12:17 +00:00
|
|
|
|
2019-10-03 16:17:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/datastore/postgres"
|
2018-11-03 19:00:25 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// BlockRetriever is used to retrieve the first block for a given contract and the most recent block
|
2018-11-03 19:02:31 +00:00
|
|
|
// It requires a vDB synced database with blocks, transactions, receipts, and logs
|
2018-11-04 21:26:39 +00:00
|
|
|
type BlockRetriever interface {
|
2018-11-03 19:00:25 +00:00
|
|
|
RetrieveFirstBlock(contractAddr string) (int64, error)
|
2018-11-04 21:26:39 +00:00
|
|
|
RetrieveMostRecentBlock() (int64, error)
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 21:26:39 +00:00
|
|
|
type blockRetriever struct {
|
2018-11-23 18:12:24 +00:00
|
|
|
db *postgres.DB
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// NewBlockRetriever returns a new BlockRetriever
|
|
|
|
func NewBlockRetriever(db *postgres.DB) BlockRetriever {
|
2018-11-04 21:26:39 +00:00
|
|
|
return &blockRetriever{
|
2018-11-23 18:12:24 +00:00
|
|
|
db: db,
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// RetrieveFirstBlock fetches the block number for the earliest block in the db
|
|
|
|
// Tries both methods of finding the first block, with the receipt method taking precedence
|
2018-11-04 21:26:39 +00:00
|
|
|
func (r *blockRetriever) RetrieveFirstBlock(contractAddr string) (int64, error) {
|
|
|
|
i, err := r.retrieveFirstBlockFromReceipts(contractAddr)
|
|
|
|
if err != nil {
|
2019-08-02 15:09:59 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
i, err = r.retrieveFirstBlockFromLogs(contractAddr)
|
|
|
|
}
|
|
|
|
return i, err
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// For some contracts the contract creation transaction receipt doesn't have the contract address so this doesn't work (e.g. Sai)
|
2018-11-04 21:26:39 +00:00
|
|
|
func (r *blockRetriever) retrieveFirstBlockFromReceipts(contractAddr string) (int64, error) {
|
2019-08-02 15:09:59 +00:00
|
|
|
var firstBlock int64
|
2019-10-15 18:12:17 +00:00
|
|
|
addressID, getAddressErr := repository.GetOrCreateAddress(r.db, contractAddr)
|
2019-08-02 15:09:59 +00:00
|
|
|
if getAddressErr != nil {
|
|
|
|
return firstBlock, getAddressErr
|
|
|
|
}
|
2018-11-23 18:12:24 +00:00
|
|
|
err := r.db.Get(
|
2018-11-03 19:00:25 +00:00
|
|
|
&firstBlock,
|
2019-06-10 17:17:14 +00:00
|
|
|
`SELECT number FROM eth_blocks
|
2019-03-22 03:43:06 +00:00
|
|
|
WHERE id = (SELECT block_id FROM full_sync_receipts
|
2019-08-02 15:09:59 +00:00
|
|
|
WHERE contract_address_id = $1
|
2018-11-03 19:00:25 +00:00
|
|
|
ORDER BY block_id ASC
|
|
|
|
LIMIT 1)`,
|
2019-10-15 18:12:17 +00:00
|
|
|
addressID,
|
2018-11-03 19:00:25 +00:00
|
|
|
)
|
|
|
|
|
2019-08-02 15:09:59 +00:00
|
|
|
return firstBlock, err
|
|
|
|
}
|
|
|
|
|
2018-11-03 19:02:31 +00:00
|
|
|
// In which case this servers as a heuristic to find the first block by finding the first contract event log
|
2018-11-04 21:26:39 +00:00
|
|
|
func (r *blockRetriever) retrieveFirstBlockFromLogs(contractAddr string) (int64, error) {
|
2018-11-03 19:00:25 +00:00
|
|
|
var firstBlock int
|
2018-11-23 18:12:24 +00:00
|
|
|
err := r.db.Get(
|
2018-11-03 19:00:25 +00:00
|
|
|
&firstBlock,
|
2019-07-18 21:24:25 +00:00
|
|
|
"SELECT block_number FROM full_sync_logs WHERE lower(address) = $1 ORDER BY block_number ASC LIMIT 1",
|
2018-11-03 19:00:25 +00:00
|
|
|
contractAddr,
|
|
|
|
)
|
|
|
|
|
|
|
|
return int64(firstBlock), err
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// RetrieveMostRecentBlock retrieves the most recent block number in vDB
|
2018-11-04 21:26:39 +00:00
|
|
|
func (r *blockRetriever) RetrieveMostRecentBlock() (int64, error) {
|
|
|
|
var lastBlock int64
|
2018-11-23 18:12:24 +00:00
|
|
|
err := r.db.Get(
|
2018-11-04 21:26:39 +00:00
|
|
|
&lastBlock,
|
2019-06-10 17:17:14 +00:00
|
|
|
"SELECT number FROM eth_blocks ORDER BY number DESC LIMIT 1",
|
2018-11-04 21:26:39 +00:00
|
|
|
)
|
2018-11-03 19:00:25 +00:00
|
|
|
|
2018-11-04 21:26:39 +00:00
|
|
|
return lastBlock, err
|
2018-11-03 19:00:25 +00:00
|
|
|
}
|