2019-04-15 16:30: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/>.
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
package super_node
|
2019-04-15 16:30:23 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-17 18:26:48 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-04-30 17:48:31 +00:00
|
|
|
"github.com/lib/pq"
|
2019-08-27 19:22:09 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-04-17 18:26:48 +00:00
|
|
|
|
2019-04-15 16:30:23 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2019-08-26 02:13:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
2019-04-15 16:30:23 +00:00
|
|
|
)
|
|
|
|
|
2019-08-05 17:56:15 +00:00
|
|
|
// CIDRepository is an interface for indexing ipfs.CIDPayloads
|
2019-04-30 17:48:31 +00:00
|
|
|
type CIDRepository interface {
|
2019-08-05 17:56:15 +00:00
|
|
|
Index(cidPayload *ipfs.CIDPayload) error
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// Repository is the underlying struct for the CIDRepository interface
|
|
|
|
type Repository struct {
|
2019-04-15 16:30:23 +00:00
|
|
|
db *postgres.DB
|
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// NewCIDRepository creates a new pointer to a Repository which satisfies the CIDRepository interface
|
|
|
|
func NewCIDRepository(db *postgres.DB) *Repository {
|
|
|
|
return &Repository{
|
2019-04-15 16:30:23 +00:00
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// Index indexes a cidPayload in Postgres
|
2019-08-05 17:56:15 +00:00
|
|
|
func (repo *Repository) Index(cidPayload *ipfs.CIDPayload) error {
|
2019-10-08 19:51:38 +00:00
|
|
|
tx, beginErr := repo.db.Beginx()
|
|
|
|
if beginErr != nil {
|
|
|
|
return beginErr
|
2019-06-25 20:31:14 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
headerID, headerErr := repo.indexHeaderCID(tx, cidPayload.HeaderCID, cidPayload.BlockNumber, cidPayload.BlockHash.Hex())
|
|
|
|
if headerErr != nil {
|
2019-08-28 18:41:49 +00:00
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
if rollbackErr != nil {
|
|
|
|
log.Error(rollbackErr)
|
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
return headerErr
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
2019-08-29 19:57:58 +00:00
|
|
|
for uncleHash, cid := range cidPayload.UncleCIDs {
|
2019-10-08 19:51:38 +00:00
|
|
|
uncleErr := repo.indexUncleCID(tx, cid, cidPayload.BlockNumber, uncleHash.Hex())
|
|
|
|
if uncleErr != nil {
|
2019-08-28 18:41:49 +00:00
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
if rollbackErr != nil {
|
|
|
|
log.Error(rollbackErr)
|
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
return uncleErr
|
2019-05-17 15:23:39 +00:00
|
|
|
}
|
2019-05-17 04:08:53 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
trxAndRctErr := repo.indexTransactionAndReceiptCIDs(tx, cidPayload, headerID)
|
|
|
|
if trxAndRctErr != nil {
|
2019-08-28 18:41:49 +00:00
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
if rollbackErr != nil {
|
|
|
|
log.Error(rollbackErr)
|
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
return trxAndRctErr
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
stateAndStorageErr := repo.indexStateAndStorageCIDs(tx, cidPayload, headerID)
|
|
|
|
if stateAndStorageErr != nil {
|
2019-08-28 18:41:49 +00:00
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
if rollbackErr != nil {
|
|
|
|
log.Error(rollbackErr)
|
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
return stateAndStorageErr
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
2019-06-07 02:09:27 +00:00
|
|
|
return tx.Commit()
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
func (repo *Repository) indexHeaderCID(tx *sqlx.Tx, cid, blockNumber, hash string) (int64, error) {
|
2019-04-15 16:30:23 +00:00
|
|
|
var headerID int64
|
2019-10-08 20:31:07 +00:00
|
|
|
err := tx.QueryRowx(`INSERT INTO public.header_cids (block_number, block_hash, cid, uncle) VALUES ($1, $2, $3, $4)
|
|
|
|
ON CONFLICT (block_number, block_hash) DO UPDATE SET (cid, uncle) = ($3, $4)
|
2019-04-17 18:26:48 +00:00
|
|
|
RETURNING id`,
|
2019-10-08 20:31:07 +00:00
|
|
|
blockNumber, hash, cid, false).Scan(&headerID)
|
2019-04-15 16:30:23 +00:00
|
|
|
return headerID, err
|
|
|
|
}
|
|
|
|
|
2019-05-17 04:08:53 +00:00
|
|
|
func (repo *Repository) indexUncleCID(tx *sqlx.Tx, cid, blockNumber, hash string) error {
|
2019-10-08 20:31:07 +00:00
|
|
|
_, err := tx.Exec(`INSERT INTO public.header_cids (block_number, block_hash, cid, uncle) VALUES ($1, $2, $3, $4)
|
|
|
|
ON CONFLICT (block_number, block_hash) DO UPDATE SET (cid, uncle) = ($3, $4)`,
|
|
|
|
blockNumber, hash, cid, true)
|
2019-05-17 04:08:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-05 17:56:15 +00:00
|
|
|
func (repo *Repository) indexTransactionAndReceiptCIDs(tx *sqlx.Tx, payload *ipfs.CIDPayload, headerID int64) error {
|
2019-04-30 17:48:31 +00:00
|
|
|
for hash, trxCidMeta := range payload.TransactionCIDs {
|
2019-04-15 16:30:23 +00:00
|
|
|
var txID int64
|
2019-10-08 19:51:38 +00:00
|
|
|
queryErr := tx.QueryRowx(`INSERT INTO public.transaction_cids (header_id, tx_hash, cid, dst, src) VALUES ($1, $2, $3, $4, $5)
|
2019-05-17 15:23:39 +00:00
|
|
|
ON CONFLICT (header_id, tx_hash) DO UPDATE SET (cid, dst, src) = ($3, $4, $5)
|
2019-04-30 17:48:31 +00:00
|
|
|
RETURNING id`,
|
2019-05-21 19:27:24 +00:00
|
|
|
headerID, hash.Hex(), trxCidMeta.CID, trxCidMeta.Dst, trxCidMeta.Src).Scan(&txID)
|
2019-10-08 19:51:38 +00:00
|
|
|
if queryErr != nil {
|
|
|
|
return queryErr
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
receiptCidMeta, ok := payload.ReceiptCIDs[hash]
|
2019-04-15 16:30:23 +00:00
|
|
|
if ok {
|
2019-10-08 19:51:38 +00:00
|
|
|
rctErr := repo.indexReceiptCID(tx, receiptCidMeta, txID)
|
|
|
|
if rctErr != nil {
|
|
|
|
return rctErr
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 02:09:27 +00:00
|
|
|
return nil
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 17:56:15 +00:00
|
|
|
func (repo *Repository) indexReceiptCID(tx *sqlx.Tx, cidMeta *ipfs.ReceiptMetaData, txID int64) error {
|
2019-06-20 15:59:10 +00:00
|
|
|
_, err := tx.Exec(`INSERT INTO public.receipt_cids (tx_id, cid, contract, topic0s) VALUES ($1, $2, $3, $4)`,
|
|
|
|
txID, cidMeta.CID, cidMeta.ContractAddress, pq.Array(cidMeta.Topic0s))
|
2019-04-15 16:30:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-05 17:56:15 +00:00
|
|
|
func (repo *Repository) indexStateAndStorageCIDs(tx *sqlx.Tx, payload *ipfs.CIDPayload, headerID int64) error {
|
2019-05-17 04:08:53 +00:00
|
|
|
for accountKey, stateCID := range payload.StateNodeCIDs {
|
2019-04-15 16:30:23 +00:00
|
|
|
var stateID int64
|
2019-10-08 19:51:38 +00:00
|
|
|
queryErr := tx.QueryRowx(`INSERT INTO public.state_cids (header_id, state_key, cid, leaf) VALUES ($1, $2, $3, $4)
|
2019-05-17 15:23:39 +00:00
|
|
|
ON CONFLICT (header_id, state_key) DO UPDATE SET (cid, leaf) = ($3, $4)
|
2019-04-30 17:48:31 +00:00
|
|
|
RETURNING id`,
|
2019-05-17 04:08:53 +00:00
|
|
|
headerID, accountKey.Hex(), stateCID.CID, stateCID.Leaf).Scan(&stateID)
|
2019-10-08 19:51:38 +00:00
|
|
|
if queryErr != nil {
|
|
|
|
return queryErr
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
2019-05-17 04:08:53 +00:00
|
|
|
for _, storageCID := range payload.StorageNodeCIDs[accountKey] {
|
2019-10-08 19:51:38 +00:00
|
|
|
storageErr := repo.indexStorageCID(tx, storageCID, stateID)
|
|
|
|
if storageErr != nil {
|
|
|
|
return storageErr
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 02:09:27 +00:00
|
|
|
return nil
|
2019-04-15 16:30:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 17:56:15 +00:00
|
|
|
func (repo *Repository) indexStorageCID(tx *sqlx.Tx, storageCID ipfs.StorageNodeCID, stateID int64) error {
|
2019-06-07 02:09:27 +00:00
|
|
|
_, err := tx.Exec(`INSERT INTO public.storage_cids (state_id, storage_key, cid, leaf) VALUES ($1, $2, $3, $4)
|
2019-05-17 15:23:39 +00:00
|
|
|
ON CONFLICT (state_id, storage_key) DO UPDATE SET (cid, leaf) = ($3, $4)`,
|
2019-05-21 19:27:24 +00:00
|
|
|
stateID, storageCID.Key, storageCID.CID, storageCID.Leaf)
|
2019-04-15 16:30:23 +00:00
|
|
|
return err
|
2019-04-18 12:39:37 +00:00
|
|
|
}
|