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-05-02 16:17:02 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2019-04-02 18:30:15 +00:00
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
2019-07-15 22:08:40 +00:00
|
|
|
"strconv"
|
2019-03-22 03:43:06 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-05-02 16:17:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-03-22 03:43:06 +00:00
|
|
|
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2018-05-02 16:17:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BlockConverter struct {
|
|
|
|
transactionConverter TransactionConverter
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockConverter(transactionConverter TransactionConverter) BlockConverter {
|
|
|
|
return BlockConverter{transactionConverter: transactionConverter}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc BlockConverter) ToCoreBlock(gethBlock *types.Block) (core.Block, error) {
|
2019-03-27 04:05:30 +00:00
|
|
|
transactions, err := bc.transactionConverter.ConvertBlockTransactionsToCore(gethBlock)
|
2018-05-02 16:17:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return core.Block{}, err
|
|
|
|
}
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-05-02 16:17:02 +00:00
|
|
|
coreBlock := core.Block{
|
|
|
|
Difficulty: gethBlock.Difficulty().Int64(),
|
|
|
|
ExtraData: hexutil.Encode(gethBlock.Extra()),
|
|
|
|
GasLimit: gethBlock.GasLimit(),
|
|
|
|
GasUsed: gethBlock.GasUsed(),
|
|
|
|
Hash: gethBlock.Hash().Hex(),
|
|
|
|
Miner: strings.ToLower(gethBlock.Coinbase().Hex()),
|
|
|
|
Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]),
|
|
|
|
Number: gethBlock.Number().Int64(),
|
|
|
|
ParentHash: gethBlock.ParentHash().Hex(),
|
|
|
|
Size: gethBlock.Size().String(),
|
2019-07-15 22:08:40 +00:00
|
|
|
Time: gethBlock.Time(),
|
2018-05-02 16:17:02 +00:00
|
|
|
Transactions: transactions,
|
|
|
|
UncleHash: gethBlock.UncleHash().Hex(),
|
|
|
|
}
|
2019-03-21 19:52:24 +00:00
|
|
|
coreBlock.Reward = CalcBlockReward(coreBlock, gethBlock.Uncles()).String()
|
2019-04-02 18:30:15 +00:00
|
|
|
totalUncleReward, uncles := bc.ToCoreUncle(coreBlock, gethBlock.Uncles())
|
|
|
|
|
|
|
|
coreBlock.UnclesReward = totalUncleReward.String()
|
|
|
|
coreBlock.Uncles = uncles
|
2018-05-02 16:17:02 +00:00
|
|
|
return coreBlock, nil
|
|
|
|
}
|
2019-04-02 18:30:15 +00:00
|
|
|
|
|
|
|
// Rewards for the miners of uncles is calculated as (U_n + 8 - B_n) * R / 8
|
|
|
|
// Where U_n is the uncle block number, B_n is the parent block number and R is the static block reward at B_n
|
|
|
|
// https://github.com/ethereum/go-ethereum/issues/1591
|
|
|
|
// https://ethereum.stackexchange.com/questions/27172/different-uncles-reward
|
|
|
|
// https://github.com/ethereum/homestead-guide/issues/399
|
|
|
|
// Returns the total uncle reward and the individual processed uncles
|
|
|
|
func (bc BlockConverter) ToCoreUncle(block core.Block, uncles []*types.Header) (*big.Int, []core.Uncle) {
|
|
|
|
totalUncleRewards := new(big.Int)
|
|
|
|
coreUncles := make([]core.Uncle, 0, len(uncles))
|
|
|
|
for _, uncle := range uncles {
|
2019-04-04 20:21:39 +00:00
|
|
|
thisUncleReward := calcUncleMinerReward(block.Number, uncle.Number.Int64())
|
2019-04-02 18:30:15 +00:00
|
|
|
raw, _ := json.Marshal(uncle)
|
|
|
|
coreUncle := core.Uncle{
|
|
|
|
Miner: uncle.Coinbase.Hex(),
|
|
|
|
Hash: uncle.Hash().Hex(),
|
|
|
|
Raw: raw,
|
|
|
|
Reward: thisUncleReward.String(),
|
2019-07-15 22:08:40 +00:00
|
|
|
Timestamp: strconv.FormatUint(uncle.Time, 10),
|
2019-04-02 18:30:15 +00:00
|
|
|
}
|
|
|
|
coreUncles = append(coreUncles, coreUncle)
|
|
|
|
totalUncleRewards.Add(totalUncleRewards, thisUncleReward)
|
|
|
|
}
|
|
|
|
return totalUncleRewards, coreUncles
|
|
|
|
}
|