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-27 16:16:08 +00:00
|
|
|
big2 "github.com/filecoin-project/go-state-types/big"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/sigs"
|
|
|
|
|
2020-11-23 21:59:27 +00:00
|
|
|
"github.com/Secured-Finance/dione/rpc"
|
2020-11-18 18:33:03 +00:00
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
"github.com/Secured-Finance/dione/beacon"
|
2021-03-05 19:29:09 +00:00
|
|
|
"github.com/Secured-Finance/dione/contracts/dioneOracle"
|
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-23 21:59:27 +00:00
|
|
|
address peer.ID
|
|
|
|
ethAddress common.Address
|
|
|
|
mutex sync.Mutex
|
|
|
|
beacon beacon.BeaconNetworks
|
|
|
|
ethClient *ethclient.EthereumClient
|
|
|
|
minerStake types.BigInt
|
|
|
|
networkStake types.BigInt
|
2020-11-27 16:16:08 +00:00
|
|
|
privateKey []byte
|
2020-10-31 03:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:32:50 +00:00
|
|
|
func NewMiner(
|
|
|
|
address peer.ID,
|
|
|
|
ethAddress common.Address,
|
|
|
|
beacon beacon.BeaconNetworks,
|
|
|
|
ethClient *ethclient.EthereumClient,
|
2020-11-27 16:16:08 +00:00
|
|
|
privateKey []byte,
|
2020-11-14 00:32:50 +00:00
|
|
|
) *Miner {
|
|
|
|
return &Miner{
|
2020-11-23 21:59:27 +00:00
|
|
|
address: address,
|
|
|
|
ethAddress: ethAddress,
|
|
|
|
beacon: beacon,
|
|
|
|
ethClient: ethClient,
|
2020-11-27 16:16:08 +00:00
|
|
|
privateKey: privateKey,
|
2020-11-14 00:32:50 +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-27 16:16:08 +00:00
|
|
|
func (m *Miner) GetStakeInfo(miner common.Address) (*types.BigInt, *types.BigInt, error) {
|
|
|
|
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-03-05 19:29:09 +00:00
|
|
|
func (m *Miner) MineTask(ctx context.Context, event *dioneOracle.DioneOracleNewOracleRequest) (*types.DioneTask, error) {
|
2020-11-27 16:16:08 +00:00
|
|
|
beaconValues, err := beacon.BeaconEntriesForTask(ctx, m.beacon)
|
2020-11-14 00:03:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get beacon entries: %w", err)
|
2020-11-13 14:04:10 +00:00
|
|
|
}
|
2020-11-27 16:16:08 +00:00
|
|
|
logrus.Debug("attempting to mine the task at epoch: ", beaconValues[1].Round)
|
2020-11-14 00:03:47 +00:00
|
|
|
|
2020-11-27 16:16:08 +00:00
|
|
|
randomBase := beaconValues[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-27 16:16:08 +00:00
|
|
|
ticket, err := m.computeTicket(&randomBase)
|
2020-10-31 03:48:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("scratching ticket failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-27 16:16:08 +00:00
|
|
|
winner, err := IsRoundWinner(
|
|
|
|
types.DrandRound(randomBase.Round),
|
|
|
|
m.address,
|
|
|
|
randomBase,
|
|
|
|
m.minerStake,
|
|
|
|
m.networkStake,
|
|
|
|
func(id peer.ID, bytes []byte) (*types.Signature, error) {
|
|
|
|
return sigs.Sign(types.SigTypeEd25519, m.privateKey, bytes)
|
|
|
|
},
|
|
|
|
)
|
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
|
|
|
|
}
|
2020-11-15 05:59:46 +00:00
|
|
|
|
2020-11-23 21:59:27 +00:00
|
|
|
rpcMethod := rpc.GetRPCMethod(event.OriginChain, event.RequestType)
|
|
|
|
if rpcMethod == nil {
|
|
|
|
return nil, xerrors.Errorf("invalid rpc method name/type")
|
2020-11-15 05:59:46 +00:00
|
|
|
}
|
2020-11-23 21:59:27 +00:00
|
|
|
res, err := rpcMethod(event.RequestParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("couldn't do rpc request: %w", err)
|
2020-11-15 05:59:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 03:48:15 +00:00
|
|
|
return &types.DioneTask{
|
2021-04-15 21:01:27 +00:00
|
|
|
OriginChain: event.OriginChain,
|
|
|
|
RequestType: event.RequestType,
|
|
|
|
RequestParams: event.RequestParams,
|
|
|
|
RequestID: event.ReqID.String(),
|
|
|
|
ConsensusID: event.ReqID.String(),
|
|
|
|
CallbackAddress: event.CallbackAddress.Bytes(),
|
|
|
|
CallbackMethodID: event.CallbackMethodID[:],
|
|
|
|
Miner: m.address,
|
|
|
|
MinerEth: m.ethAddress.Hex(),
|
|
|
|
Ticket: ticket,
|
|
|
|
ElectionProof: winner,
|
|
|
|
BeaconEntries: beaconValues,
|
|
|
|
Payload: res,
|
|
|
|
DrandRound: types.DrandRound(randomBase.Round),
|
2020-10-31 03:48:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-27 16:16:08 +00:00
|
|
|
func (m *Miner) computeTicket(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-27 16:16:08 +00:00
|
|
|
vrfOut, err := ComputeVRF(func(id peer.ID, bytes []byte) (*types.Signature, error) {
|
|
|
|
return sigs.Sign(types.SigTypeEd25519, m.privateKey, bytes)
|
|
|
|
}, m.address, input)
|
2020-10-31 03:48:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.Ticket{
|
|
|
|
VRFProof: vrfOut,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-11-27 16:16:08 +00:00
|
|
|
|
|
|
|
func (m *Miner) IsMinerEligibleToProposeTask(ethAddress common.Address) error {
|
|
|
|
mStake, err := m.ethClient.GetMinerStake(ethAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ok := mStake.GreaterThanEqual(big2.NewInt(ethclient.MinMinerStake))
|
|
|
|
if !ok {
|
|
|
|
return xerrors.Errorf("miner doesn't have enough staked tokens")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|