forked from cerc-io/ipld-eth-server
going ahead and indexing the entire uncle blocks (one of the issues open on public); finish tests
This commit is contained in:
@@ -19,12 +19,10 @@ package repositories
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"math/big"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/libraries/shared/utilities"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
@@ -157,8 +155,8 @@ func (blockRepository BlockRepository) insertBlock(block core.Block) (int64, err
|
||||
}
|
||||
return 0, postgres.ErrDBInsertFailed(insertBlockErr)
|
||||
}
|
||||
if len(block.MappedUncleRewards) > 0 {
|
||||
insertUncleErr := blockRepository.createUncleRewards(tx, blockId, block.Hash, block.MappedUncleRewards)
|
||||
if len(block.Uncles) > 0 {
|
||||
insertUncleErr := blockRepository.createUncles(tx, blockId, block.Hash, block.Uncles)
|
||||
if insertUncleErr != nil {
|
||||
tx.Rollback()
|
||||
return 0, postgres.ErrDBInsertFailed(insertUncleErr)
|
||||
@@ -185,25 +183,23 @@ func (blockRepository BlockRepository) insertBlock(block core.Block) (int64, err
|
||||
return blockId, nil
|
||||
}
|
||||
|
||||
func (blockRepository BlockRepository) createUncleRewards(tx *sqlx.Tx, blockId int64, blockHash string, mappedUncleRewards map[string]map[string]*big.Int) error {
|
||||
for minerAddr, uncleRewards := range mappedUncleRewards {
|
||||
for uncleHash, reward := range uncleRewards {
|
||||
err := blockRepository.createUncleReward(tx, blockId, blockHash, minerAddr, uncleHash, reward.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
func (blockRepository BlockRepository) createUncles(tx *sqlx.Tx, blockId int64, blockHash string, uncles []core.Uncle) error {
|
||||
for _, uncle := range uncles {
|
||||
err := blockRepository.createUncle(tx, blockId, uncle)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (blockRepository BlockRepository) createUncleReward(tx *sqlx.Tx, blockId int64, blockHash, minerAddr, uncleHash, amount string) error {
|
||||
func (blockRepository BlockRepository) createUncle(tx *sqlx.Tx, blockId int64, uncle core.Uncle) error {
|
||||
_, err := tx.Exec(
|
||||
`INSERT INTO uncle_rewards
|
||||
(block_id, block_hash, miner_address, uncle_hash, uncle_reward)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`INSERT INTO uncles
|
||||
(hash, block_id, block_hash, reward, miner, raw, block_timestamp, eth_node_id, eth_node_fingerprint)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7::NUMERIC, $8, $9)
|
||||
RETURNING id`,
|
||||
blockId, blockHash, minerAddr, uncleHash, utilities.NullToZero(amount))
|
||||
uncle.Hash, blockId, uncle.BlockHash, utilities.NullToZero(uncle.Reward), uncle.Miner, uncle.Raw, uncle.Timestamp, blockRepository.database.NodeID, blockRepository.database.Node.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -158,6 +158,72 @@ var _ = Describe("Saving blocks", func() {
|
||||
Expect(len(savedBlock.Transactions)).To(Equal(2))
|
||||
})
|
||||
|
||||
It("saves one uncle associated to the block", func() {
|
||||
block := core.Block{
|
||||
Hash: fakes.FakeHash.String(),
|
||||
Number: 123,
|
||||
Transactions: []core.TransactionModel{fakes.FakeTransaction},
|
||||
Uncles: []core.Uncle{fakes.GetFakeUncle(common.BytesToHash([]byte{1, 2, 3}).String(), "100000")},
|
||||
UnclesReward: "156250000000000000",
|
||||
}
|
||||
|
||||
id, insertErr := blockRepository.CreateOrUpdateBlock(block)
|
||||
|
||||
Expect(insertErr).NotTo(HaveOccurred())
|
||||
savedBlock, getErr := blockRepository.GetBlock(123)
|
||||
Expect(getErr).NotTo(HaveOccurred())
|
||||
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
||||
Expect(savedBlock.UnclesReward).To(Equal(big.NewInt(0).Div(big.NewInt(5000000000000000000), big.NewInt(32)).String()))
|
||||
|
||||
var uncleModel core.Uncle
|
||||
err := db.Get(&uncleModel, `SELECT hash, block_hash, reward, miner, raw, block_timestamp FROM uncles
|
||||
WHERE block_id = $1 AND hash = $2`, id, common.BytesToHash([]byte{1, 2, 3}).Hex())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(uncleModel.Hash).To(Equal(common.BytesToHash([]byte{1, 2, 3}).Hex()))
|
||||
Expect(uncleModel.Reward).To(Equal("100000"))
|
||||
Expect(uncleModel.Miner).To(Equal(fakes.FakeAddress.Hex()))
|
||||
Expect(uncleModel.Timestamp).To(Equal("111111111"))
|
||||
})
|
||||
|
||||
It("saves one uncle associated to the block", func() {
|
||||
block := core.Block{
|
||||
Hash: fakes.FakeHash.String(),
|
||||
Number: 123,
|
||||
Transactions: []core.TransactionModel{fakes.FakeTransaction},
|
||||
Uncles: []core.Uncle{
|
||||
fakes.GetFakeUncle(common.BytesToHash([]byte{1, 2, 3}).String(), "100000"),
|
||||
fakes.GetFakeUncle(common.BytesToHash([]byte{3, 2, 1}).String(), "90000")},
|
||||
UnclesReward: "312500000000000000",
|
||||
}
|
||||
|
||||
id, insertErr := blockRepository.CreateOrUpdateBlock(block)
|
||||
|
||||
Expect(insertErr).NotTo(HaveOccurred())
|
||||
savedBlock, getErr := blockRepository.GetBlock(123)
|
||||
Expect(getErr).NotTo(HaveOccurred())
|
||||
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
||||
b := new(big.Int)
|
||||
b.SetString("10000000000000000000", 10)
|
||||
Expect(savedBlock.UnclesReward).To(Equal(big.NewInt(0).Div(b, big.NewInt(32)).String()))
|
||||
|
||||
var uncleModel core.Uncle
|
||||
err := db.Get(&uncleModel, `SELECT hash, block_hash, reward, miner, raw, block_timestamp FROM uncles
|
||||
WHERE block_id = $1 AND hash = $2`, id, common.BytesToHash([]byte{1, 2, 3}).Hex())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(uncleModel.Hash).To(Equal(common.BytesToHash([]byte{1, 2, 3}).Hex()))
|
||||
Expect(uncleModel.Reward).To(Equal("100000"))
|
||||
Expect(uncleModel.Miner).To(Equal(fakes.FakeAddress.Hex()))
|
||||
Expect(uncleModel.Timestamp).To(Equal("111111111"))
|
||||
|
||||
err = db.Get(&uncleModel, `SELECT hash, block_hash, reward, miner, raw, block_timestamp FROM uncles
|
||||
WHERE block_id = $1 AND hash = $2`, id, common.BytesToHash([]byte{3, 2, 1}).Hex())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(uncleModel.Hash).To(Equal(common.BytesToHash([]byte{3, 2, 1}).Hex()))
|
||||
Expect(uncleModel.Reward).To(Equal("90000"))
|
||||
Expect(uncleModel.Miner).To(Equal(fakes.FakeAddress.Hex()))
|
||||
Expect(uncleModel.Timestamp).To(Equal("111111111"))
|
||||
})
|
||||
|
||||
It(`replaces blocks and transactions associated to the block
|
||||
when a more new block is in conflict (same block number + nodeid)`, func() {
|
||||
blockOne := core.Block{
|
||||
|
||||
Reference in New Issue
Block a user