keep track of who is receiving uncle rewards

This commit is contained in:
Ian Norden
2019-04-02 10:10:17 -05:00
parent fea4ce0c7a
commit 9030cff2bd
8 changed files with 109 additions and 43 deletions
@@ -20,6 +20,7 @@ import (
"database/sql"
"errors"
log "github.com/sirupsen/logrus"
"math/big"
"github.com/jmoiron/sqlx"
@@ -139,6 +140,13 @@ 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 insertUncleErr != nil {
tx.Rollback()
return 0, postgres.ErrDBInsertFailed(insertUncleErr)
}
}
if len(block.Transactions) > 0 {
insertTxErr := blockRepository.createTransactions(tx, blockId, block.Transactions)
if insertTxErr != nil {
@@ -160,6 +168,28 @@ 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 miner, uncleRewards := range mappedUncleRewards {
for uncleHash, reward := range uncleRewards {
err := blockRepository.createUncleReward(tx, blockId, blockHash, miner, uncleHash, reward.String())
if err != nil {
return err
}
}
}
return nil
}
func (blockRepository BlockRepository) createUncleReward(tx *sqlx.Tx, blockId int64, blockHash, miner, uncleHash, amount string) error {
_, err := tx.Exec(
`INSERT INTO uncle_rewards
(block_id, block_hash, uncle_hash, uncle_reward, miner_address)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`,
blockId, blockHash, miner, uncleHash, amount)
return err
}
func (blockRepository BlockRepository) createTransactions(tx *sqlx.Tx, blockId int64, transactions []core.TransactionModel) error {
for _, transaction := range transactions {
err := blockRepository.createTransaction(tx, blockId, transaction)
@@ -84,8 +84,8 @@ var _ = Describe("Saving blocks", func() {
uncleHash := "x789"
blockSize := string("1000")
difficulty := int64(10)
blockReward := float64(5.132)
unclesReward := float64(3.580)
blockReward := "5132000000000000000"
unclesReward := "3580000000000000000"
block := core.Block{
Reward: blockReward,
Difficulty: difficulty,
@@ -0,0 +1 @@
package repositories
@@ -0,0 +1 @@
package repositories