The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/api/service/staking/service.go

183 lines
5.0 KiB

6 years ago
package staking
import (
"crypto/ecdsa"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
pb "github.com/golang/protobuf/proto"
client "github.com/harmony-one/harmony/api/client/service"
proto "github.com/harmony-one/harmony/api/client/service/proto"
"github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/core/types"
6 years ago
"github.com/harmony-one/harmony/internal/utils"
6 years ago
"github.com/harmony-one/harmony/p2p"
)
// State is the state of staking service.
type State byte
// Constants for State
const (
NotStatedYet State = iota
Staked
Rejected
Approved
)
6 years ago
// Service is the staking service.
// Service requires private key here which is not a right design.
// In stead in the right design, the end-user who runs mining needs to provide signed tx to this service.
6 years ago
type Service struct {
host p2p.Host
stopChan chan struct{}
stoppedChan chan struct{}
peerChan <-chan p2p.Peer
messageChan <-chan *message.Message
accountKey *ecdsa.PrivateKey
stakingAmount int64
state State
6 years ago
}
// New returns staking service.
func New(host p2p.Host, accountKey *ecdsa.PrivateKey, stakingAmount int64, peerChan <-chan p2p.Peer, messageChan <-chan *message.Message) *Service {
return &Service{
host: host,
stopChan: make(chan struct{}),
stoppedChan: make(chan struct{}),
peerChan: peerChan,
accountKey: accountKey,
stakingAmount: stakingAmount,
messageChan: messageChan,
state: NotStatedYet,
}
}
6 years ago
// StartService starts staking service.
6 years ago
func (s *Service) StartService() {
log.Info("Start Staking Service")
s.Init()
s.Run()
}
6 years ago
// Init initializes staking service.
func (s *Service) Init() {
}
6 years ago
// Run runs staking.
func (s *Service) Run() {
// Wait until peer info of beacon chain is ready.
go func() {
defer close(s.stoppedChan)
for {
select {
case peer := <-s.peerChan:
utils.GetLogInstance().Info("Running staking service")
// TODO: Write some logic here.
s.DoService(peer)
case <-s.stopChan:
return
}
}
}()
6 years ago
}
6 years ago
// DoService does staking.
func (s *Service) DoService(peer p2p.Peer) {
utils.GetLogInstance().Info("Staking with Peer")
stakingMessage := s.createStakingMessage(peer)
s.state = Staked
if data, err := pb.Marshal(stakingMessage); err == nil {
// Send a staking transaction to beacon chain.
if err = s.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, data); err != nil {
utils.GetLogInstance().Error("Error when sending staking message")
return
}
tick := time.NewTicker(5 * time.Second)
for {
select {
// Retry sending the staking transaction if it does not get back any response.
case <-tick.C:
if err = s.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, data); err != nil {
utils.GetLogInstance().Error("Error when sending staking message")
return
}
case msg := <-s.messageChan:
if isStateResultMessage(msg) {
if s.stakeApproved(msg) {
s.state = Approved
// TODO(minhdoan): Should send a signal to another service.
} else {
s.state = Rejected
// TODO(minhdoan): what's next?
return
}
}
}
}
} else {
utils.GetLogInstance().Error("Error when creating staking message")
}
}
// TODO(minhdoan): Will implement this logic when introducing the result message from beacon chain.
func isStateResultMessage(msg *message.Message) bool {
return true
}
// TODO(minhdoan): Will implement this logic when introducing the result message from beacon chain.
func (s *Service) stakeApproved(msg *message.Message) bool {
return true
}
func (s *Service) getStakingInfo(beaconPeer p2p.Peer) *proto.StakingContractInfoResponse {
client := client.NewClient(beaconPeer.IP, beaconPeer.Port)
defer client.Close()
return client.GetStakingContractInfo(crypto.PubkeyToAddress(s.accountKey.PublicKey))
}
func (s *Service) createStakingMessage(beaconPeer p2p.Peer) *message.Message {
stakingInfo := s.getStakingInfo(beaconPeer)
toAddress := common.HexToAddress(stakingInfo.ContractAddress)
tx := types.NewTransaction(
stakingInfo.Nonce,
toAddress,
0, // beacon chain.
big.NewInt(s.stakingAmount),
params.CallValueTransferGas*2, // hard-code
big.NewInt(int64(params.Sha256BaseGas)), // pick some predefined gas price.
nil)
if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, s.accountKey); err == nil {
ts := types.Transactions{signedTx}
return &message.Message{
Type: message.MessageType_NEWNODE_BEACON_STAKING,
Request: &message.Message_Staking{
Staking: &message.StakingRequest{
Transaction: ts.GetRlp(0),
NodeId: "",
},
},
}
}
return nil
6 years ago
}
6 years ago
// StopService stops staking service.
6 years ago
func (s *Service) StopService() {
6 years ago
utils.GetLogInstance().Info("Stopping staking service.")
s.stopChan <- struct{}{}
<-s.stoppedChan
utils.GetLogInstance().Info("Role conversion stopped.")
6 years ago
}
// NotifyService notify service
func (s *Service) NotifyService(params map[string]interface{}) {
return
}