2019-08-27 19:22:09 +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/>.
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
package eth_test
|
2019-08-27 19:22:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2020-01-17 23:16:01 +00:00
|
|
|
|
2020-02-10 15:00:55 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/eth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/eth/mocks"
|
2020-02-09 21:28:30 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2019-08-27 19:22:09 +00:00
|
|
|
)
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
var _ = Describe("Indexer", func() {
|
2019-11-01 19:03:28 +00:00
|
|
|
var (
|
|
|
|
db *postgres.DB
|
|
|
|
err error
|
2020-02-09 21:28:30 +00:00
|
|
|
repo *eth.CIDIndexer
|
2019-11-01 19:03:28 +00:00
|
|
|
)
|
2019-08-27 19:22:09 +00:00
|
|
|
BeforeEach(func() {
|
2020-02-09 21:28:30 +00:00
|
|
|
db, err = shared.SetupDB()
|
2019-08-27 19:22:09 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2020-02-09 21:28:30 +00:00
|
|
|
repo = eth.NewCIDIndexer(db)
|
2019-08-27 19:22:09 +00:00
|
|
|
})
|
|
|
|
AfterEach(func() {
|
2020-01-17 23:16:01 +00:00
|
|
|
eth.TearDownDB(db)
|
2019-08-27 19:22:09 +00:00
|
|
|
})
|
2019-08-28 22:07:36 +00:00
|
|
|
|
2019-08-27 19:22:09 +00:00
|
|
|
Describe("Index", func() {
|
|
|
|
It("Indexes CIDs and related metadata into vulcanizedb", func() {
|
|
|
|
err = repo.Index(mocks.MockCIDPayload)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2020-02-20 22:13:19 +00:00
|
|
|
pgStr := `SELECT cid, td, reward FROM eth.header_cids
|
2020-01-26 19:55:26 +00:00
|
|
|
WHERE block_number = $1`
|
2019-08-27 19:22:09 +00:00
|
|
|
// check header was properly indexed
|
2020-01-16 23:21:30 +00:00
|
|
|
type res struct {
|
2020-02-20 22:13:19 +00:00
|
|
|
CID string
|
|
|
|
TD string
|
|
|
|
Reward string
|
2020-01-16 23:21:30 +00:00
|
|
|
}
|
|
|
|
headers := new(res)
|
|
|
|
err = db.QueryRowx(pgStr, 1).StructScan(headers)
|
2019-08-27 19:22:09 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(headers.CID).To(Equal(mocks.HeaderCID.String()))
|
|
|
|
Expect(headers.TD).To(Equal(mocks.MockBlock.Difficulty().String()))
|
|
|
|
Expect(headers.Reward).To(Equal("5000000000000000000"))
|
2019-08-27 19:22:09 +00:00
|
|
|
// check trxs were properly indexed
|
|
|
|
trxs := make([]string, 0)
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr = `SELECT transaction_cids.cid FROM eth.transaction_cids INNER JOIN eth.header_cids ON (transaction_cids.header_id = header_cids.id)
|
2019-08-27 19:22:09 +00:00
|
|
|
WHERE header_cids.block_number = $1`
|
|
|
|
err = db.Select(&trxs, pgStr, 1)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(trxs)).To(Equal(2))
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(shared.ListContainsString(trxs, mocks.Trx1CID.String())).To(BeTrue())
|
|
|
|
Expect(shared.ListContainsString(trxs, mocks.Trx2CID.String())).To(BeTrue())
|
2019-08-27 19:22:09 +00:00
|
|
|
// check receipts were properly indexed
|
|
|
|
rcts := make([]string, 0)
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr = `SELECT receipt_cids.cid FROM eth.receipt_cids, eth.transaction_cids, eth.header_cids
|
2019-08-27 19:22:09 +00:00
|
|
|
WHERE receipt_cids.tx_id = transaction_cids.id
|
|
|
|
AND transaction_cids.header_id = header_cids.id
|
|
|
|
AND header_cids.block_number = $1`
|
|
|
|
err = db.Select(&rcts, pgStr, 1)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(rcts)).To(Equal(2))
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(shared.ListContainsString(rcts, mocks.Rct1CID.String())).To(BeTrue())
|
|
|
|
Expect(shared.ListContainsString(rcts, mocks.Rct2CID.String())).To(BeTrue())
|
2019-08-27 19:22:09 +00:00
|
|
|
// check that state nodes were properly indexed
|
2020-01-17 23:16:01 +00:00
|
|
|
stateNodes := make([]eth.StateNodeModel, 0)
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr = `SELECT state_cids.cid, state_cids.state_key, state_cids.leaf FROM eth.state_cids INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
|
2019-08-27 19:22:09 +00:00
|
|
|
WHERE header_cids.block_number = $1`
|
|
|
|
err = db.Select(&stateNodes, pgStr, 1)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(stateNodes)).To(Equal(2))
|
2019-08-28 18:41:49 +00:00
|
|
|
for _, stateNode := range stateNodes {
|
2020-02-20 22:13:19 +00:00
|
|
|
if stateNode.CID == mocks.State1CID.String() {
|
2019-08-28 18:41:49 +00:00
|
|
|
Expect(stateNode.Leaf).To(Equal(true))
|
2020-01-17 23:16:01 +00:00
|
|
|
Expect(stateNode.StateKey).To(Equal(mocks.ContractLeafKey.Hex()))
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
if stateNode.CID == mocks.State2CID.String() {
|
2019-08-28 18:41:49 +00:00
|
|
|
Expect(stateNode.Leaf).To(Equal(true))
|
2020-01-17 23:16:01 +00:00
|
|
|
Expect(stateNode.StateKey).To(Equal(mocks.AnotherContractLeafKey.Hex()))
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-27 19:22:09 +00:00
|
|
|
// check that storage nodes were properly indexed
|
2020-01-17 23:16:01 +00:00
|
|
|
storageNodes := make([]eth.StorageNodeWithStateKeyModel, 0)
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr = `SELECT storage_cids.cid, state_cids.state_key, storage_cids.storage_key, storage_cids.leaf FROM eth.storage_cids, eth.state_cids, eth.header_cids
|
2019-08-27 19:22:09 +00:00
|
|
|
WHERE storage_cids.state_id = state_cids.id
|
|
|
|
AND state_cids.header_id = header_cids.id
|
|
|
|
AND header_cids.block_number = $1`
|
|
|
|
err = db.Select(&storageNodes, pgStr, 1)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(storageNodes)).To(Equal(1))
|
2020-01-17 23:16:01 +00:00
|
|
|
Expect(storageNodes[0]).To(Equal(eth.StorageNodeWithStateKeyModel{
|
2020-02-20 22:13:19 +00:00
|
|
|
CID: mocks.StorageCID.String(),
|
2020-01-17 23:16:01 +00:00
|
|
|
Leaf: true,
|
|
|
|
StorageKey: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
|
|
StateKey: mocks.ContractLeafKey.Hex(),
|
2019-08-27 19:22:09 +00:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|