send staking transaction to beacon chain

pull/480/head
Minh Doan 6 years ago committed by Minh Doan
parent 0004108ec8
commit df1ce42ba1
  1. 64
      api/service/staking/service.go
  2. 6
      node/node.go

@ -3,11 +3,13 @@ package staking
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"math/big" "math/big"
"time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
pb "github.com/golang/protobuf/proto"
client "github.com/harmony-one/harmony/api/client/service" client "github.com/harmony-one/harmony/api/client/service"
proto "github.com/harmony-one/harmony/api/client/service/proto" proto "github.com/harmony-one/harmony/api/client/service/proto"
"github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/api/proto/message"
@ -16,25 +18,40 @@ import (
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
) )
type State byte
const (
NOT_STAKED_YET State = iota
STAKED
REJECTED
APPROVED
TRANSFORMED
)
// Service is the staking service. // Service is the staking service.
// Service requires private key here which is not a right design. // 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. // In stead in the right design, the end-user who runs mining needs to provide signed tx to this service.
type Service struct { type Service struct {
host p2p.Host
stopChan chan struct{} stopChan chan struct{}
stoppedChan chan struct{} stoppedChan chan struct{}
peerChan <-chan p2p.Peer peerChan <-chan p2p.Peer
messageChan <-chan *message.Message
accountKey *ecdsa.PrivateKey accountKey *ecdsa.PrivateKey
stakingAmount int64 stakingAmount int64
state State
} }
// New returns staking service. // New returns staking service.
func New(accountKey *ecdsa.PrivateKey, stakingAmount int64, peerChan <-chan p2p.Peer) *Service { func New(accountKey *ecdsa.PrivateKey, stakingAmount int64, peerChan <-chan p2p.Peer, messageChan <-chan *message.Message) *Service {
return &Service{ return &Service{
stopChan: make(chan struct{}), stopChan: make(chan struct{}),
stoppedChan: make(chan struct{}), stoppedChan: make(chan struct{}),
peerChan: peerChan, peerChan: peerChan,
accountKey: accountKey, accountKey: accountKey,
stakingAmount: stakingAmount, stakingAmount: stakingAmount,
messageChan: messageChan,
state: NOT_STAKED_YET,
} }
} }
@ -57,7 +74,7 @@ func (s *Service) Run() {
for { for {
select { select {
case peer := <-s.peerChan: case peer := <-s.peerChan:
utils.GetLogInstance().Info("Running role conversion") utils.GetLogInstance().Info("Running staking service")
// TODO: Write some logic here. // TODO: Write some logic here.
s.DoService(peer) s.DoService(peer)
case <-s.stopChan: case <-s.stopChan:
@ -71,8 +88,47 @@ func (s *Service) Run() {
func (s *Service) DoService(peer p2p.Peer) { func (s *Service) DoService(peer p2p.Peer) {
utils.GetLogInstance().Info("Staking with Peer") utils.GetLogInstance().Info("Staking with Peer")
// TODO(minhdoan): How to use the p2p or pubsub to send Staking Message to beacon chain. stakingMessage := s.createStakingMessage(peer)
// See below of how to create a staking message. 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")
}
}
func isStateResultMessage(msg *message.Message) bool {
return true
}
func (s *Service) stakeApproved(msg *message.Message) bool {
return true
} }
func (s *Service) getStakingInfo(beaconPeer p2p.Peer) *proto.StakingContractInfoResponse { func (s *Service) getStakingInfo(beaconPeer p2p.Peer) *proto.StakingContractInfoResponse {

@ -31,6 +31,7 @@ import (
"github.com/harmony-one/harmony/api/service/explorer" "github.com/harmony-one/harmony/api/service/explorer"
"github.com/harmony-one/harmony/api/service/networkinfo" "github.com/harmony-one/harmony/api/service/networkinfo"
randomness_service "github.com/harmony-one/harmony/api/service/randomness" randomness_service "github.com/harmony-one/harmony/api/service/randomness"
"github.com/harmony-one/harmony/api/service/staking"
"github.com/harmony-one/harmony/api/service/syncing" "github.com/harmony-one/harmony/api/service/syncing"
"github.com/harmony-one/harmony/api/service/syncing/downloader" "github.com/harmony-one/harmony/api/service/syncing/downloader"
@ -682,9 +683,8 @@ func (node *Node) setupForNewNode() {
nodeConfig, chanPeer := node.initNodeConfiguration() nodeConfig, chanPeer := node.initNodeConfiguration()
// Register staking service. // Register staking service.
// node.serviceManager.RegisterService(service_manager.Staking, staking.New(node.AccountKey, 0, stakingPeer)) node.serviceManager.RegisterService(service_manager.Staking, staking.New(node.AccountKey, 0, stakingPeer, nil))
// TODO: (leo) no need to start discovery service for new node until we received the sharding info // Register peer discovery service. "0" is the beacon shard ID
// Register peer discovery service.
node.serviceManager.RegisterService(service_manager.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer)) node.serviceManager.RegisterService(service_manager.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer))
// Register networkinfo service. "0" is the beacon shard ID // Register networkinfo service. "0" is the beacon shard ID
node.serviceManager.RegisterService(service_manager.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer)) node.serviceManager.RegisterService(service_manager.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer))

Loading…
Cancel
Save