2020-10-31 03:48:15 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-31 04:38:51 +00:00
|
|
|
"sync"
|
2020-10-31 03:48:15 +00:00
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
"github.com/Secured-Finance/dione/beacon"
|
|
|
|
|
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/Secured-Finance/dione/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Miner struct {
|
2020-11-14 00:03:47 +00:00
|
|
|
address peer.ID
|
|
|
|
ethAddress common.Address
|
2020-11-14 00:32:50 +00:00
|
|
|
api WalletAPI
|
2020-11-14 00:03:47 +00:00
|
|
|
mutex sync.Mutex
|
|
|
|
beacon beacon.BeaconNetworks
|
|
|
|
ethClient *ethclient.EthereumClient
|
|
|
|
minerStake types.BigInt
|
|
|
|
networkStake types.BigInt
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:32:50 +00:00
|
|
|
func NewMiner(
|
|
|
|
address peer.ID,
|
|
|
|
ethAddress common.Address,
|
|
|
|
api WalletAPI,
|
|
|
|
beacon beacon.BeaconNetworks,
|
|
|
|
ethClient *ethclient.EthereumClient,
|
|
|
|
) *Miner {
|
|
|
|
return &Miner{
|
|
|
|
address: address,
|
|
|
|
ethAddress: ethAddress,
|
|
|
|
api: api,
|
|
|
|
beacon: beacon,
|
|
|
|
ethClient: ethClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type WalletAPI interface {
|
2020-11-12 14:18:30 +00:00
|
|
|
WalletSign(context.Context, peer.ID, []byte) (*types.Signature, error)
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
func (m *Miner) UpdateCurrentStakeInfo() error {
|
|
|
|
mStake, err := m.ethClient.GetMinerStake(m.ethAddress)
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
m.minerStake = *mStake
|
|
|
|
m.networkStake = *nStake
|
2020-11-13 15:36:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:32:50 +00:00
|
|
|
func (m *Miner) MineTask(ctx context.Context, payload []byte) (*types.DioneTask, error) {
|
2020-11-14 00:03:47 +00:00
|
|
|
bvals, err := beacon.BeaconEntriesForTask(ctx, m.beacon)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get beacon entries: %w", err)
|
2020-11-13 14:04:10 +00:00
|
|
|
}
|
2020-11-14 00:03:47 +00:00
|
|
|
logrus.Debug("attempting to mine the task at epoch: ", bvals[1].Round)
|
|
|
|
|
|
|
|
rbase := bvals[1]
|
2020-11-13 14:04:10 +00:00
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
if err := m.UpdateCurrentStakeInfo(); err != nil {
|
2020-11-13 15:51:23 +00:00
|
|
|
return nil, xerrors.Errorf("failed to update miner stake: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
ticket, err := m.computeTicket(ctx, &rbase)
|
2020-10-31 03:48:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("scratching ticket failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
winner, err := IsRoundWinner(ctx, types.DrandRound(rbase.Round), m.address, rbase, m.minerStake, m.networkStake, m.api)
|
2020-10-31 03:48:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to check if we win next round: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if winner == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return &types.DioneTask{
|
|
|
|
Miner: m.address,
|
|
|
|
Ticket: ticket,
|
|
|
|
ElectionProof: winner,
|
2020-11-14 00:03:47 +00:00
|
|
|
BeaconEntries: bvals,
|
2020-11-14 00:32:50 +00:00
|
|
|
Payload: payload,
|
2020-10-31 03:48:15 +00:00
|
|
|
// TODO: signature
|
2020-11-14 00:03:47 +00:00
|
|
|
DrandRound: types.DrandRound(rbase.Round),
|
2020-10-31 03:48:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
func (m *Miner) computeTicket(ctx context.Context, brand *types.BeaconEntry) (*types.Ticket, error) {
|
2020-11-12 14:18:30 +00:00
|
|
|
buf, err := m.address.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to marshal address: %w", err)
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
round := types.DrandRound(brand.Round)
|
2020-10-31 03:48:15 +00:00
|
|
|
|
2020-11-12 14:18:30 +00:00
|
|
|
input, err := DrawRandomness(brand.Data, crypto.DomainSeparationTag_TicketProduction, round-types.TicketRandomnessLookback, buf)
|
2020-10-31 03:48:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
vrfOut, err := ComputeVRF(ctx, m.api.WalletSign, m.address, input)
|
2020-10-31 03:48:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.Ticket{
|
|
|
|
VRFProof: vrfOut,
|
|
|
|
}, nil
|
|
|
|
}
|