2021-07-19 20:19:06 +00:00
|
|
|
package blockchain
|
2020-10-31 03:48:15 +00:00
|
|
|
|
|
|
|
import (
|
2021-06-08 21:30:23 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2021-07-26 21:18:16 +00:00
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
|
|
|
|
"github.com/asaskevich/EventBus"
|
2020-10-31 03:48:15 +00:00
|
|
|
|
2021-07-19 20:19:06 +00:00
|
|
|
"github.com/Secured-Finance/dione/beacon"
|
|
|
|
"github.com/Secured-Finance/dione/types"
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
"github.com/Secured-Finance/dione/blockchain/pool"
|
2020-11-27 16:16:08 +00:00
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2020-11-27 16:16:08 +00:00
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/blockchain/types"
|
2020-11-18 18:33:03 +00:00
|
|
|
|
2020-11-12 14:18:30 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
|
2020-11-13 15:36:01 +00:00
|
|
|
"github.com/Secured-Finance/dione/ethclient"
|
2020-10-31 03:48:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-07-11 23:23:00 +00:00
|
|
|
var (
|
|
|
|
ErrNoTxForBlock = fmt.Errorf("no transactions for including into block")
|
|
|
|
)
|
|
|
|
|
2020-10-31 03:48:15 +00:00
|
|
|
type Miner struct {
|
2021-07-26 21:18:16 +00:00
|
|
|
bus EventBus.Bus
|
|
|
|
address peer.ID
|
|
|
|
ethClient *ethclient.EthereumClient
|
|
|
|
minerStake *big.Int
|
|
|
|
networkStake *big.Int
|
|
|
|
privateKey crypto.PrivKey
|
|
|
|
mempool *pool.Mempool
|
|
|
|
latestBlockHeader *types2.BlockHeader
|
|
|
|
blockchain *BlockChain
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:32:50 +00:00
|
|
|
func NewMiner(
|
2021-07-26 21:18:16 +00:00
|
|
|
h host.Host,
|
2020-11-14 00:32:50 +00:00
|
|
|
ethClient *ethclient.EthereumClient,
|
2021-06-08 21:30:23 +00:00
|
|
|
privateKey crypto.PrivKey,
|
|
|
|
mempool *pool.Mempool,
|
2021-07-26 21:18:16 +00:00
|
|
|
bus EventBus.Bus,
|
2020-11-14 00:32:50 +00:00
|
|
|
) *Miner {
|
2021-07-26 21:18:16 +00:00
|
|
|
m := &Miner{
|
|
|
|
address: h.ID(),
|
2020-11-23 21:59:27 +00:00
|
|
|
ethClient: ethClient,
|
2020-11-27 16:16:08 +00:00
|
|
|
privateKey: privateKey,
|
2021-06-08 21:30:23 +00:00
|
|
|
mempool: mempool,
|
2021-07-26 21:18:16 +00:00
|
|
|
bus: bus,
|
2020-11-14 00:32:50 +00:00
|
|
|
}
|
2021-07-26 21:18:16 +00:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) SetBlockchainInstance(b *BlockChain) {
|
|
|
|
m.blockchain = b
|
|
|
|
|
|
|
|
m.bus.SubscribeAsync("blockchain:latestBlockHeightUpdated", func(block *types2.Block) {
|
|
|
|
m.latestBlockHeader = block.Header
|
|
|
|
}, true)
|
|
|
|
|
|
|
|
height, _ := m.blockchain.GetLatestBlockHeight()
|
|
|
|
header, err := m.blockchain.FetchBlockHeaderByHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("err", err.Error()).Fatal("Failed to initialize miner subsystem")
|
|
|
|
}
|
|
|
|
m.latestBlockHeader = header
|
|
|
|
|
|
|
|
logrus.Info("Mining subsystem has been initialized!")
|
2020-11-14 00:32:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
func (m *Miner) UpdateCurrentStakeInfo() error {
|
2021-07-26 21:18:16 +00:00
|
|
|
mStake, err := m.ethClient.GetMinerStake(*m.ethClient.GetEthAddress())
|
2020-11-13 15:36:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn("Can't get miner stake", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
nStake, err := m.ethClient.GetTotalStake()
|
2020-11-13 15:36:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn("Can't get miner stake", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
m.minerStake = mStake
|
|
|
|
m.networkStake = nStake
|
2020-11-13 15:36:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func (m *Miner) GetStakeInfo(miner common.Address) (*big.Int, *big.Int, error) {
|
2020-11-27 16:16:08 +00:00
|
|
|
mStake, err := m.ethClient.GetMinerStake(miner)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn("Can't get miner stake", err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nStake, err := m.ethClient.GetTotalStake()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn("Can't get miner stake", err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mStake, nStake, nil
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
func (m *Miner) MineBlock(randomness []byte, randomnessRound uint64) (*types2.Block, error) {
|
|
|
|
if m.latestBlockHeader == nil {
|
|
|
|
return nil, fmt.Errorf("latest block header is null")
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.WithField("height", m.latestBlockHeader.Height+1).Debug("Trying to mine new block...")
|
2020-11-13 14:04:10 +00:00
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
if err := m.UpdateCurrentStakeInfo(); err != nil {
|
2021-06-08 21:30:23 +00:00
|
|
|
return nil, fmt.Errorf("failed to update miner stake: %w", err)
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 20:19:06 +00:00
|
|
|
winner, err := isRoundWinner(
|
2021-07-26 21:18:16 +00:00
|
|
|
m.latestBlockHeader.Height+1,
|
2020-11-27 16:16:08 +00:00
|
|
|
m.address,
|
2021-06-08 21:30:23 +00:00
|
|
|
randomness,
|
2021-07-11 23:23:00 +00:00
|
|
|
randomnessRound,
|
2020-11-27 16:16:08 +00:00
|
|
|
m.minerStake,
|
|
|
|
m.networkStake,
|
2021-06-08 21:30:23 +00:00
|
|
|
m.privateKey,
|
2020-11-27 16:16:08 +00:00
|
|
|
)
|
2020-10-31 03:48:15 +00:00
|
|
|
if err != nil {
|
2021-06-08 21:30:23 +00:00
|
|
|
return nil, fmt.Errorf("failed to check if we winned in next round: %w", err)
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if winner == nil {
|
2021-07-26 21:18:16 +00:00
|
|
|
logrus.WithField("height", m.latestBlockHeader.Height+1).Debug("Block is not mined because we are not leader in consensus round")
|
2020-10-31 03:48:15 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-11-15 05:59:46 +00:00
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
logrus.WithField("height", m.latestBlockHeader.Height+1).Infof("We have been elected in the current consensus round")
|
|
|
|
|
2021-06-14 21:45:35 +00:00
|
|
|
txs := m.mempool.GetTransactionsForNewBlock()
|
2021-06-08 21:30:23 +00:00
|
|
|
if txs == nil {
|
2021-07-11 23:23:00 +00:00
|
|
|
return nil, ErrNoTxForBlock // skip new consensus round because there is no transaction for processing
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
newBlock, err := types2.CreateBlock(m.latestBlockHeader, txs, *m.ethClient.GetEthAddress(), m.privateKey, winner)
|
2020-10-31 03:48:15 +00:00
|
|
|
if err != nil {
|
2021-06-08 21:30:23 +00:00
|
|
|
return nil, fmt.Errorf("failed to create new block: %w", err)
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
return newBlock, nil
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
2020-11-27 16:16:08 +00:00
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func (m *Miner) IsMinerEligibleToProposeBlock(ethAddress common.Address) error {
|
2020-11-27 16:16:08 +00:00
|
|
|
mStake, err := m.ethClient.GetMinerStake(ethAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
if mStake.Cmp(big.NewInt(ethclient.MinMinerStake)) == -1 {
|
|
|
|
return errors.New("miner doesn't have enough staked tokens")
|
2020-11-27 16:16:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-19 20:19:06 +00:00
|
|
|
|
|
|
|
func isRoundWinner(round uint64,
|
|
|
|
worker peer.ID, randomness []byte, randomnessRound uint64, minerStake, networkStake *big.Int, privKey crypto.PrivKey) (*types.ElectionProof, error) {
|
|
|
|
|
|
|
|
buf, err := worker.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
electionRand, err := beacon.DrawRandomness(randomness, beacon.RandomnessTypeElectionProofProduction, round, buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to draw randomness: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vrfout, err := beacon.ComputeVRF(privKey, electionRand)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to compute VRF: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep := &types.ElectionProof{VRFProof: vrfout, RandomnessRound: randomnessRound}
|
|
|
|
j := ep.ComputeWinCount(minerStake, networkStake)
|
|
|
|
ep.WinCount = j
|
|
|
|
if j < 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep, nil
|
|
|
|
}
|