add new staking message and add its logic

pull/480/head
Minh Doan 6 years ago committed by Minh Doan
parent 6eb8d7ea96
commit 089b40ccc4
  1. 8
      api/proto/common.go
  2. 37
      api/service/staking/service.go
  3. 8
      drand/drand.go

@ -32,6 +32,7 @@ const (
Client
Identity
DRand
Staking
// TODO: add more types
)
@ -97,3 +98,10 @@ func ConstructDRandMessage(payload []byte) []byte {
byteBuffer.Write(payload)
return byteBuffer.Bytes()
}
// ConstructStakingMessage creates a message with the payload and returns as byte array.
func ConstructStakingMessage(payload []byte) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(Staking)})
byteBuffer.Write(payload)
return byteBuffer.Bytes()
}

@ -9,7 +9,9 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
protobuf "github.com/golang/protobuf/proto"
proto "github.com/harmony-one/harmony/api/client/service/proto"
"github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
@ -61,6 +63,7 @@ func (s *Service) Run() {
go func() {
defer close(s.stoppedChan)
// Do service first time and after that doing it every 5 minutes.
// The reason we have to do it in every x minutes because of beacon chain syncing.
s.DoService()
for {
select {
@ -115,6 +118,26 @@ func (s *Service) getStakingInfo() *proto.StakingContractInfoResponse {
}
}
// Constructs the staking message
func constructStakingMessage(ts types.Transactions) []byte {
msg := &message.Message{
Type: message.MessageType_NEWNODE_BEACON_STAKING,
Request: &message.Message_Staking{
Staking: &message.StakingRequest{
Transaction: ts.GetRlp(0),
NodeId: "",
},
},
}
if data, err := protobuf.Marshal(msg); err == nil {
return data
} else {
utils.GetLogInstance().Error("Error when creating staking message")
return nil
}
}
func (s *Service) createStakingMessage() []byte {
stakingInfo := s.getStakingInfo()
toAddress := common.HexToAddress(stakingInfo.ContractAddress)
@ -128,18 +151,8 @@ func (s *Service) createStakingMessage() []byte {
nil)
if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, s.accountKey); err == nil {
_ = types.Transactions{signedTx}
// TODO(minhdoan): Use the new message protocol later.
// return &message.Message{
// Type: message.MessageType_NEWNODE_BEACON_STAKING,
// Request: &message.Message_Staking{
// Staking: &message.StakingRequest{
// Transaction: ts.GetRlp(0),
// NodeId: "",
// },
// },
// }
ts := types.Transactions{signedTx}
return constructStakingMessage(ts)
}
return nil
}

@ -7,15 +7,13 @@ import (
"strconv"
"sync"
"github.com/harmony-one/harmony/crypto/vrf"
"github.com/harmony-one/harmony/crypto/vrf/p256"
"github.com/harmony-one/harmony/core/types"
protobuf "github.com/golang/protobuf/proto"
"github.com/harmony-one/bls/ffi/go/bls"
drand_proto "github.com/harmony-one/harmony/api/drand"
"github.com/harmony-one/harmony/core/types"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/crypto/vrf"
"github.com/harmony-one/harmony/crypto/vrf/p256"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
)

Loading…
Cancel
Save