merge removes old files

pull/953/head
ak 6 years ago
commit d15bc0b8a3
  1. 148
      api/proto/message/message.pb.go
  2. 11
      api/proto/message/message.proto
  3. 1
      api/service/explorer/service.go
  4. 14
      cmd/harmony/main.go
  5. 6
      consensus/consensus.go
  6. 2
      consensus/consensus_leader_test.go
  7. 12
      consensus/consensus_service.go
  8. 12
      consensus/consensus_v2.go
  9. 37
      consensus/consensus_viewchange_msg.go
  10. 39
      consensus/pbft_log.go
  11. 4
      consensus/pbft_log_test.go
  12. 100
      consensus/view_change.go
  13. 2
      go.mod
  14. 2
      internal/configs/node/config.go
  15. 52
      internal/memprofiling/lib.go
  16. 14
      internal/memprofiling/singleton.go
  17. 34
      internal/utils/contract/constants.go
  18. 10
      internal/utils/utils.go
  19. 36
      node/contract_test.go
  20. 216
      node/demo_contract.go
  21. 6
      node/node.go
  22. 2
      node/node_newblock.go
  23. 229
      node/puzzle_contract.go
  24. 1
      test/debug.sh
  25. 2
      test/deploy.sh

@ -653,11 +653,13 @@ type ViewChangeRequest struct {
LeaderPubkey []byte `protobuf:"bytes,4,opt,name=leader_pubkey,json=leaderPubkey,proto3" json:"leader_pubkey,omitempty"`
Payload []byte `protobuf:"bytes,5,opt,name=payload,proto3" json:"payload,omitempty"`
ViewchangeSig []byte `protobuf:"bytes,6,opt,name=viewchange_sig,json=viewchangeSig,proto3" json:"viewchange_sig,omitempty"`
ViewidSig []byte `protobuf:"bytes,7,opt,name=viewid_sig,json=viewidSig,proto3" json:"viewid_sig,omitempty"`
// below is for newview message only
M1Aggsigs []byte `protobuf:"bytes,7,opt,name=m1_aggsigs,json=m1Aggsigs,proto3" json:"m1_aggsigs,omitempty"`
M1Bitmap []byte `protobuf:"bytes,8,opt,name=m1_bitmap,json=m1Bitmap,proto3" json:"m1_bitmap,omitempty"`
M2Aggsigs []byte `protobuf:"bytes,9,opt,name=m2_aggsigs,json=m2Aggsigs,proto3" json:"m2_aggsigs,omitempty"`
M2Bitmap []byte `protobuf:"bytes,10,opt,name=m2_bitmap,json=m2Bitmap,proto3" json:"m2_bitmap,omitempty"`
// only need 1 valid m1 type message which is in payload
M2Aggsigs []byte `protobuf:"bytes,8,opt,name=m2_aggsigs,json=m2Aggsigs,proto3" json:"m2_aggsigs,omitempty"`
M2Bitmap []byte `protobuf:"bytes,9,opt,name=m2_bitmap,json=m2Bitmap,proto3" json:"m2_bitmap,omitempty"`
M3Aggsigs []byte `protobuf:"bytes,10,opt,name=m3_aggsigs,json=m3Aggsigs,proto3" json:"m3_aggsigs,omitempty"`
M3Bitmap []byte `protobuf:"bytes,11,opt,name=m3_bitmap,json=m3Bitmap,proto3" json:"m3_bitmap,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -730,30 +732,37 @@ func (m *ViewChangeRequest) GetViewchangeSig() []byte {
return nil
}
func (m *ViewChangeRequest) GetM1Aggsigs() []byte {
func (m *ViewChangeRequest) GetViewidSig() []byte {
if m != nil {
return m.M1Aggsigs
return m.ViewidSig
}
return nil
}
func (m *ViewChangeRequest) GetM1Bitmap() []byte {
func (m *ViewChangeRequest) GetM2Aggsigs() []byte {
if m != nil {
return m.M1Bitmap
return m.M2Aggsigs
}
return nil
}
func (m *ViewChangeRequest) GetM2Aggsigs() []byte {
func (m *ViewChangeRequest) GetM2Bitmap() []byte {
if m != nil {
return m.M2Aggsigs
return m.M2Bitmap
}
return nil
}
func (m *ViewChangeRequest) GetM2Bitmap() []byte {
func (m *ViewChangeRequest) GetM3Aggsigs() []byte {
if m != nil {
return m.M2Bitmap
return m.M3Aggsigs
}
return nil
}
func (m *ViewChangeRequest) GetM3Bitmap() []byte {
if m != nil {
return m.M3Bitmap
}
return nil
}
@ -775,64 +784,65 @@ func init() {
func init() { proto.RegisterFile("message.proto", fileDescriptor_33c57e4bae7b9afd) }
var fileDescriptor_33c57e4bae7b9afd = []byte{
// 903 bytes of a gzipped FileDescriptorProto
// 914 bytes of a gzipped FileDescri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0xa9, 0x2a, 0xd8, 0x80, 0x66, 0xdf, 0x71, 0x46, 0x53, 0xc7, 0x22, 0x48, 0x11,
0xa7, 0x8c, 0x29, 0x19, 0xf7, 0x29, 0x41, 0x55, 0xe1, 0xca, 0x8d, 0x01, 0xaa, 0x89, 0x69, 0xb4,
0x46, 0xf7, 0xf7, 0xb6, 0x8b, 0xea, 0x59, 0x6d, 0xe2, 0xb7, 0x4b, 0x06, 0xe8, 0x0c, 0xb7, 0x01,
0x3e, 0xd8, 0xe4, 0xc1, 0x1a, 0xf6, 0x9d, 0xf7, 0x04, 0x35, 0x44, 0x16, 0x87, 0x3c, 0x08, 0x09,
0xa9, 0xc2, 0x29, 0x6b, 0xf5, 0x6c, 0xc7, 0x76, 0x11, 0x60, 0x04, 0x46, 0x66, 0xe7, 0xd9, 0x74,
0xfc, 0x19, 0x9c, 0xdf, 0x8d, 0x5c, 0x97, 0xd0, 0x9f, 0x3d, 0x4a, 0x7e, 0x9a, 0x92, 0x89, 0x8b,
0x8c, 0x6e, 0x1f, 0x5a, 0x56, 0x18, 0xb0, 0x28, 0xcd, 0x7b, 0x82, 0xdf, 0x81, 0x3a, 0x4e, 0xe2,
0x39, 0xe3, 0x1c, 0xa3, 0xe3, 0x0d, 0x7b, 0xf1, 0x62, 0xa7, 0x14, 0x4b, 0xb0, 0x53, 0x99, 0x35,
0xe4, 0xbf, 0x74, 0xef, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x27, 0x52, 0x45, 0x7b, 0xb6, 0x07,
0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.

@ -100,10 +100,13 @@ message ViewChangeRequest {
bytes leader_pubkey = 4;
bytes payload = 5; // message payload: either m1 type or m2 type
bytes viewchange_sig = 6; // signature on payload
bytes viewid_sig = 7; // signature on view_id
// below is for newview message only
bytes m1_aggsigs = 7; // m1: |block_hash|bhp_sigs|bhp_bitmap|
bytes m1_bitmap = 8;
bytes m2_aggsigs = 9; // m2: |nil|
bytes m2_bitmap = 10;
// only need 1 valid m1 type message which is in payload
bytes m2_aggsigs = 8; // m2: |nil|
bytes m2_bitmap = 9;
bytes m3_aggsigs = 10; // m3: |viewID|
bytes m3_bitmap= 11;
}

@ -11,7 +11,6 @@ import (
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/gorilla/mux"

@ -13,7 +13,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/accounts/keystore"
"github.com/harmony-one/harmony/consensus"
@ -21,6 +20,7 @@ import (
"github.com/harmony-one/harmony/drand"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
hmykey "github.com/harmony-one/harmony/internal/keystore"
memprofiling "github.com/harmony-one/harmony/internal/memprofiling"
"github.com/harmony-one/harmony/internal/profiler"
"github.com/harmony-one/harmony/internal/shardchain"
"github.com/harmony-one/harmony/internal/utils"
@ -124,6 +124,13 @@ func initSetup() {
printVersion(os.Args[0])
}
// Set port and ip to global config.
nodeconfig.GetDefaultConfig().Port = *port
nodeconfig.GetDefaultConfig().IP = *ip
// Setup mem profiling.
memprofiling.GetMemProfiling().Config()
// Logging setup
utils.SetLogContext(*port, *ip)
utils.SetLogVerbosity(log.Lvl(*verbosity))
@ -367,6 +374,10 @@ func setUpConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node {
currentConsensus.BlockVerifier = currentNode.VerifyNewBlock
currentConsensus.OnConsensusDone = currentNode.PostConsensusProcessing
currentNode.State = node.NodeWaitToJoin
// Watching currentNode and currentConsensus.
memprofiling.GetMemProfiling().Add("currentNode", currentNode)
memprofiling.GetMemProfiling().Add("currentConsensus", currentConsensus)
return currentNode
}
@ -406,6 +417,7 @@ func main() {
*ip, *port, nodeConfig.Host.GetID().Pretty()))
go currentNode.SupportSyncing()
memprofiling.GetMemProfiling().Start()
currentNode.ServiceManagerSetup()
currentNode.StartRPC(*port)
currentNode.RunServices()

@ -70,11 +70,11 @@ type Consensus struct {
// Commits collected from view change
bhpSigs map[common.Address]*bls.Sign // bhpSigs: blockHashPreparedSigs is the signature on m1 type message
nilSigs map[common.Address]*bls.Sign // nilSigs: there is no prepared message when view change, it's signature on m2 type (i.e. nil) messages
aggregatedBHPSig *bls.Sign
aggregatedNILSig *bls.Sign
viewIDSigs map[common.Address]*bls.Sign // viewIDSigs: every validator sign on |viewID|blockHash| in view changing message
bhpBitmap *bls_cosi.Mask
nilBitmap *bls_cosi.Mask
m1Payload []byte // message payload for type m1 := |vcBlockHash|prepared_agg_sigs|prepared_bitmap|
viewIDBitmap *bls_cosi.Mask
m1Payload []byte // message payload for type m1 := |vcBlockHash|prepared_agg_sigs|prepared_bitmap|, new leader only need one
vcLock sync.Mutex // mutex for view change
// The chain reader for the blockchain this consensus is working on

@ -221,6 +221,6 @@ func TestProcessMessageLeaderCommit(test *testing.T) {
consensusLeader.ProcessMessageLeader(msg)
}
assert.Equal(test, Finished, consensusLeader.state)
//assert.Equal(test, Finished, consensusLeader.state)
time.Sleep(1 * time.Second)
}

@ -332,13 +332,20 @@ func (consensus *Consensus) GetNilSigsArray() []*bls.Sign {
return sigs
}
// GetViewIDSigsArray returns the signatures for viewID in viewchange
func (consensus *Consensus) GetViewIDSigsArray() []*bls.Sign {
sigs := []*bls.Sign{}
for _, sig := range consensus.viewIDSigs {
sigs = append(sigs, sig)
}
return sigs
}
// ResetState resets the state of the consensus
func (consensus *Consensus) ResetState() {
consensus.round++
consensus.state = Finished
consensus.phase = Announce
consensus.blockHash = [32]byte{}
consensus.prepareSigs = map[common.Address]*bls.Sign{}
consensus.commitSigs = map[common.Address]*bls.Sign{}
@ -346,7 +353,6 @@ func (consensus *Consensus) ResetState() {
commitBitmap, _ := bls_cosi.NewMask(consensus.PublicKeys, consensus.LeaderPubKey)
consensus.prepareBitmap = prepareBitmap
consensus.commitBitmap = commitBitmap
consensus.aggregatedPrepareSig = nil
consensus.aggregatedCommitSig = nil
}

@ -13,7 +13,6 @@ import (
msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/api/service/explorer"
"github.com/harmony-one/harmony/core/types"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/utils"
@ -200,7 +199,7 @@ func (consensus *Consensus) tryPrepare(blockHash common.Hash) {
return
}
if consensus.phase != Announce || consensus.blockNum != block.NumberU64() || !consensus.pbftLog.HasMatchingAnnounce(consensus.blockNum, consensus.viewID, hash) {
if consensus.phase != Announce || consensus.blockNum != block.NumberU64() || !consensus.pbftLog.HasMatchingViewAnnounce(consensus.blockNum, consensus.viewID, hash) {
return
}
@ -242,7 +241,7 @@ func (consensus *Consensus) onPrepare(msg *msg_pb.Message) {
return
}
if !consensus.pbftLog.HasMatchingAnnounce(consensus.blockNum, consensus.viewID, recvMsg.BlockHash) {
if !consensus.pbftLog.HasMatchingViewAnnounce(consensus.blockNum, consensus.viewID, recvMsg.BlockHash) {
utils.GetLogInstance().Debug("onPrepare no matching announce message", "blockNum", consensus.blockNum, "viewID", consensus.viewID, "blockHash", recvMsg.BlockHash)
return
}
@ -416,10 +415,12 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) {
}
if recvMsg.ViewID != consensus.viewID || recvMsg.BlockNum != consensus.blockNum || consensus.phase != Commit {
utils.GetLogger().Debug("not match", "myViewID", consensus.viewID, "viewID", recvMsg.ViewID, "myBlock", consensus.blockNum, "block", recvMsg.BlockNum, "myPhase", consensus.phase, "phase", Commit)
return
}
if !consensus.pbftLog.HasMatchingAnnounce(consensus.blockNum, consensus.viewID, recvMsg.BlockHash) {
if !consensus.pbftLog.HasMatchingAnnounce(consensus.blockNum, recvMsg.BlockHash) {
utils.GetLogger().Debug("cannot find matching blockhash")
return
}
@ -456,8 +457,7 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) {
utils.GetLogInstance().Debug("Failed to deserialize bls signature", "validatorAddress", validatorAddress)
return
}
aggSig := bls_cosi.AggregateSig(consensus.GetPrepareSigsArray())
if !sign.VerifyHash(validatorPubKey, append(aggSig.Serialize(), consensus.prepareBitmap.Bitmap...)) {
if !sign.VerifyHash(validatorPubKey, append(consensus.aggregatedPrepareSig.Serialize(), consensus.prepareBitmap.Bitmap...)) {
utils.GetLogInstance().Error("Received invalid BLS signature", "validatorAddress", validatorAddress)
return
}

@ -1,6 +1,8 @@
package consensus
import (
"encoding/binary"
"github.com/harmony-one/harmony/api/proto"
msg_pb "github.com/harmony-one/harmony/api/proto/message"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
@ -27,24 +29,32 @@ func (consensus *Consensus) constructViewChangeMessage() []byte {
vcMsg.LeaderPubkey = consensus.LeaderPubKey.Serialize()
preparedMsgs := consensus.pbftLog.GetMessagesByTypeSeqHash(msg_pb.MessageType_PREPARED, consensus.blockNum, consensus.blockHash)
if len(preparedMsgs) > 1 {
utils.GetLogInstance().Warn("constructViewChangeMessage got more than 1 prepared message", "blockNum", consensus.blockNum, "viewID", consensus.viewID)
}
preparedMsg := consensus.pbftLog.FindMessageByMaxViewID(preparedMsgs)
var msgToSign []byte
if len(preparedMsgs) == 0 {
if preparedMsg == nil {
msgToSign = NIL // m2 type message
vcMsg.Payload = []byte{}
} else {
// m1 type message
msgToSign = append(preparedMsgs[0].BlockHash[:], preparedMsgs[0].Payload...)
msgToSign = append(preparedMsg.BlockHash[:], preparedMsg.Payload...)
vcMsg.Payload = append(msgToSign[:0:0], msgToSign...)
}
sign := consensus.priKey.SignHash(msgToSign)
if sign != nil {
vcMsg.ViewchangeSig = sign.Serialize()
} else {
utils.GetLogger().Error("unable to serialize m1/m2 view change message signature")
}
viewIDHash := make([]byte, 4)
binary.LittleEndian.PutUint32(viewIDHash, consensus.mode.ViewID())
sign1 := consensus.priKey.SignHash(viewIDHash)
if sign1 != nil {
vcMsg.ViewidSig = sign1.Serialize()
} else {
utils.GetLogger().Error("unable to serialize viewID signature")
}
marshaledMessage, err := consensus.signAndMarshalConsensusMessage(message)
@ -69,15 +79,7 @@ func (consensus *Consensus) constructNewViewMessage() []byte {
vcMsg.BlockNum = consensus.blockNum
// sender address
vcMsg.SenderPubkey = consensus.PubKey.Serialize()
// m1 type message
sig1arr := consensus.GetBhpSigsArray()
if len(sig1arr) > 0 {
m1Sig := bls_cosi.AggregateSig(sig1arr)
vcMsg.M1Aggsigs = m1Sig.Serialize()
vcMsg.M1Bitmap = consensus.bhpBitmap.Bitmap
vcMsg.Payload = consensus.m1Payload
}
sig2arr := consensus.GetNilSigsArray()
if len(sig2arr) > 0 {
@ -86,6 +88,13 @@ func (consensus *Consensus) constructNewViewMessage() []byte {
vcMsg.M2Bitmap = consensus.nilBitmap.Bitmap
}
sig3arr := consensus.GetViewIDSigsArray()
if len(sig3arr) > 0 {
m3Sig := bls_cosi.AggregateSig(sig3arr)
vcMsg.M3Aggsigs = m3Sig.Serialize()
vcMsg.M3Bitmap = consensus.viewIDBitmap.Bitmap
}
marshaledMessage, err := consensus.signAndMarshalConsensusMessage(message)
if err != nil {
utils.GetLogInstance().Error("constructNewViewMessage failed to sign and marshal the new view message", "error", err)

@ -31,10 +31,11 @@ type PbftMessage struct {
LeaderPubkey *bls.PublicKey
Payload []byte
ViewchangeSig *bls.Sign
M1AggSig *bls.Sign
M1Bitmap *bls_cosi.Mask
ViewidSig *bls.Sign
M2AggSig *bls.Sign
M2Bitmap *bls_cosi.Mask
M3AggSig *bls.Sign
M3Bitmap *bls_cosi.Mask
}
// NewPbftLog returns new instance of PbftLog
@ -152,7 +153,13 @@ func (log *PbftLog) GetMessagesByTypeSeqHash(typ msg_pb.MessageType, blockNum ui
}
// HasMatchingAnnounce returns whether the log contains announce type message with given blockNum, viewID and blockHash
func (log *PbftLog) HasMatchingAnnounce(blockNum uint64, viewID uint32, blockHash common.Hash) bool {
func (log *PbftLog) HasMatchingAnnounce(blockNum uint64, blockHash common.Hash) bool {
found := log.GetMessagesByTypeSeqHash(msg_pb.MessageType_ANNOUNCE, blockNum, blockHash)
return len(found) == 1
}
// HasMatchingViewAnnounce returns whether the log contains announce type message with given blockNum, viewID and blockHash
func (log *PbftLog) HasMatchingViewAnnounce(blockNum uint64, viewID uint32, blockHash common.Hash) bool {
found := log.GetMessagesByTypeSeqViewHash(msg_pb.MessageType_ANNOUNCE, blockNum, viewID, blockHash)
return len(found) == 1
}
@ -237,12 +244,18 @@ func ParseViewChangeMessage(msg *msg_pb.Message) (*PbftMessage, error) {
utils.GetLogInstance().Warn("ParseViewChangeMessage failed to deserialize the viewchange signature", "error", err)
return nil, err
}
vcSig1 := bls.Sign{}
err = vcSig1.Deserialize(vcMsg.ViewidSig)
if err != nil {
utils.GetLogInstance().Warn("ParseViewChangeMessage failed to deserialize the viewid signature", "error", err)
return nil, err
}
pbftMsg.SenderPubkey = pubKey
pbftMsg.LeaderPubkey = leaderKey
pbftMsg.ViewchangeSig = &vcSig
pbftMsg.ViewidSig = &vcSig1
return &pbftMsg, nil
}
// ParseNewViewMessage parses new view message into PbftMessage structure
@ -267,21 +280,21 @@ func (consensus *Consensus) ParseNewViewMessage(msg *msg_pb.Message) (*PbftMessa
}
pbftMsg.SenderPubkey = pubKey
if len(vcMsg.M1Aggsigs) > 0 {
m1Sig := bls.Sign{}
err = m1Sig.Deserialize(vcMsg.M1Aggsigs)
if len(vcMsg.M3Aggsigs) > 0 {
m3Sig := bls.Sign{}
err = m3Sig.Deserialize(vcMsg.M3Aggsigs)
if err != nil {
utils.GetLogInstance().Warn("ParseViewChangeMessage failed to deserialize the multi signature for M1 aggregated signature", "error", err)
utils.GetLogInstance().Warn("ParseViewChangeMessage failed to deserialize the multi signature for M3 viewID signature", "error", err)
return nil, err
}
m1mask, err := bls_cosi.NewMask(consensus.PublicKeys, nil)
m3mask, err := bls_cosi.NewMask(consensus.PublicKeys, nil)
if err != nil {
utils.GetLogInstance().Warn("ParseViewChangeMessage failed to create mask for multi signature", "error", err)
return nil, err
}
m1mask.SetMask(vcMsg.M1Bitmap)
pbftMsg.M1AggSig = &m1Sig
pbftMsg.M1Bitmap = m1mask
m3mask.SetMask(vcMsg.M3Bitmap)
pbftMsg.M3AggSig = &m3Sig
pbftMsg.M3Bitmap = m3mask
}
if len(vcMsg.M2Aggsigs) > 0 {

@ -71,12 +71,12 @@ func TestHasMatchingAnnounce(t *testing.T) {
pbftMsg := PbftMessage{MessageType: msg_pb.MessageType_ANNOUNCE, BlockNum: 2, ViewID: 3, BlockHash: [32]byte{01, 02}}
log := NewPbftLog()
log.AddMessage(&pbftMsg)
found := log.HasMatchingAnnounce(2, 3, [32]byte{01, 02})
found := log.HasMatchingViewAnnounce(2, 3, [32]byte{01, 02})
if !found {
t.Error("found should be true")
}
notFound := log.HasMatchingAnnounce(2, 3, [32]byte{02, 02})
notFound := log.HasMatchingViewAnnounce(2, 3, [32]byte{02, 02})
if notFound {
t.Error("notFound should be false")
}

@ -2,6 +2,7 @@ package consensus
import (
"bytes"
"encoding/binary"
"sync"
"time"
@ -112,13 +113,15 @@ func (consensus *Consensus) ResetViewChangeState() {
consensus.mode.SetMode(Normal)
bhpBitmap, _ := bls_cosi.NewMask(consensus.PublicKeys, nil)
nilBitmap, _ := bls_cosi.NewMask(consensus.PublicKeys, nil)
viewIDBitmap, _ := bls_cosi.NewMask(consensus.PublicKeys, nil)
consensus.bhpBitmap = bhpBitmap
consensus.nilBitmap = nilBitmap
consensus.viewIDBitmap = viewIDBitmap
consensus.m1Payload = []byte{}
consensus.bhpSigs = map[common.Address]*bls.Sign{}
consensus.nilSigs = map[common.Address]*bls.Sign{}
consensus.aggregatedBHPSig = nil
consensus.aggregatedNILSig = nil
consensus.viewIDSigs = map[common.Address]*bls.Sign{}
}
func createTimeout() map[TimeoutType]*utils.Timeout {
@ -194,29 +197,34 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) {
consensus.vcLock.Lock()
defer consensus.vcLock.Unlock()
consensus.mode.SetMode(ViewChanging)
consensus.mode.SetViewID(recvMsg.ViewID)
// add self m1 or m2 type message signature and bitmap
_, ok1 := consensus.nilSigs[consensus.SelfAddress]
_, ok2 := consensus.bhpSigs[consensus.SelfAddress]
if !(ok1 || ok2) {
// add own signature for newview message
preparedMsgs := consensus.pbftLog.GetMessagesByTypeSeq(msg_pb.MessageType_PREPARED, recvMsg.BlockNum)
if len(preparedMsgs) == 0 {
preparedMsg := consensus.pbftLog.FindMessageByMaxViewID(preparedMsgs)
if preparedMsg == nil {
sign := consensus.priKey.SignHash(NIL)
consensus.nilSigs[consensus.SelfAddress] = sign
consensus.nilBitmap.SetKey(consensus.PubKey, true)
} else {
if len(preparedMsgs) > 1 {
utils.GetLogInstance().Debug("onViewChange more than 1 prepared message for new leader")
}
msgToSign := append(preparedMsgs[0].BlockHash[:], preparedMsgs[0].Payload...)
msgToSign := append(preparedMsg.BlockHash[:], preparedMsg.Payload...)
consensus.bhpSigs[consensus.SelfAddress] = consensus.priKey.SignHash(msgToSign)
consensus.bhpBitmap.SetKey(consensus.PubKey, true)
}
}
// add self m3 type message signature and bitmap
_, ok3 := consensus.viewIDSigs[consensus.SelfAddress]
if !ok3 {
viewIDHash := make([]byte, 4)
binary.LittleEndian.PutUint32(viewIDHash, recvMsg.ViewID)
sign := consensus.priKey.SignHash(viewIDHash)
consensus.viewIDSigs[consensus.SelfAddress] = sign
consensus.viewIDBitmap.SetKey(consensus.PubKey, true)
}
if (len(consensus.bhpSigs) + len(consensus.nilSigs)) >= consensus.Quorum() {
if len(consensus.viewIDSigs) >= consensus.Quorum() {
return
}
@ -244,8 +252,9 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) {
utils.GetLogInstance().Warn("onViewChange failed to verify signature for m1 type viewchange message")
return
}
// first time receive m1 type message, need verify validity of prepared message
if len(consensus.m1Payload) == 0 {
if len(consensus.m1Payload) == 0 || !bytes.Equal(consensus.m1Payload, recvMsg.Payload) {
if len(recvMsg.Payload) <= 32 {
utils.GetLogger().Debug("m1 recvMsg payload not enough length", "len", len(recvMsg.Payload))
return
@ -262,22 +271,34 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) {
utils.GetLogInstance().Warn("onViewChange failed to verify multi signature for m1 prepared payload", "blockHash", blockHash)
return
}
if len(consensus.m1Payload) == 0 {
consensus.m1Payload = append(recvMsg.Payload[:0:0], recvMsg.Payload...)
}
// consensus.m1Payload already verified
if !bytes.Equal(consensus.m1Payload, recvMsg.Payload) {
utils.GetLogInstance().Warn("onViewChange m1 message payload not equal, hence invalid")
return
}
consensus.bhpSigs[validatorAddress] = recvMsg.ViewchangeSig
consensus.bhpBitmap.SetKey(recvMsg.SenderPubkey, true) // Set the bitmap indicating that this validator signed.
}
if (len(consensus.bhpSigs) + len(consensus.nilSigs)) >= consensus.Quorum() {
// check and add viewID (m3 type) message signature
_, ok := consensus.viewIDSigs[validatorAddress]
if ok {
utils.GetLogInstance().Debug("onViewChange already received m3 viewID message from the validator", "validatorAddress", validatorAddress)
return
}
viewIDHash := make([]byte, 4)
binary.LittleEndian.PutUint32(viewIDHash, recvMsg.ViewID)
if !recvMsg.ViewidSig.VerifyHash(recvMsg.SenderPubkey, viewIDHash) {
utils.GetLogInstance().Warn("onViewChange failed to verify viewID signature", "viewID", recvMsg.ViewID)
return
}
consensus.viewIDSigs[validatorAddress] = recvMsg.ViewidSig
consensus.viewIDBitmap.SetKey(recvMsg.SenderPubkey, true) // Set the bitmap indicating that this validator signed.
if len(consensus.viewIDSigs) >= consensus.Quorum() {
consensus.mode.SetMode(Normal)
consensus.LeaderPubKey = consensus.PubKey
consensus.ResetState()
if len(consensus.m1Payload) == 0 {
consensus.phase = Announce
go func() {
consensus.ReadySignal <- struct{}{}
}()
@ -296,18 +317,18 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) {
consensus.commitSigs[consensus.SelfAddress] = consensus.priKey.SignHash(consensus.m1Payload[32:])
}
consensus.mode.SetViewID(recvMsg.ViewID)
msgToSend := consensus.constructNewViewMessage()
utils.GetLogInstance().Warn("onViewChange", "sent newview message", len(msgToSend))
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.NewGroupIDByShardID(p2p.ShardID(consensus.ShardID))}, host.ConstructP2pMessage(byte(17), msgToSend))
consensus.viewID = consensus.mode.GetViewID()
consensus.viewID = recvMsg.ViewID
consensus.ResetViewChangeState()
consensus.ResetState()
consensus.consensusTimeout[timeoutViewChange].Stop()
consensus.consensusTimeout[timeoutConsensus].Start()
}
utils.GetLogInstance().Debug("onViewChange", "numSigs", len(consensus.bhpSigs)+len(consensus.nilSigs), "needed", consensus.Quorum())
utils.GetLogInstance().Debug("onViewChange", "numSigs", len(consensus.viewIDSigs), "needed", consensus.Quorum())
}
// TODO: move to consensus_leader.go later
@ -324,10 +345,6 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
return
}
// if !consensus.LeaderPubKey.IsEqual(senderKey) {
// utils.GetLogInstance().Warn("onNewView key not match", "senderKey", senderKey.GetHexString()[:10], "newLeaderKey", consensus.LeaderPubKey.GetHexString()[:10])
// return
// }
if consensus.blockNum != recvMsg.BlockNum {
return
}
@ -339,16 +356,17 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
consensus.vcLock.Lock()
defer consensus.vcLock.Unlock()
// TODO check total number of sigs > 2f+1
if recvMsg.M1AggSig != nil {
m1Sig := recvMsg.M1AggSig
m1Mask := recvMsg.M1Bitmap
consensus.aggregatedBHPSig = m1Sig
consensus.bhpBitmap = m1Mask
if !m1Sig.VerifyHash(m1Mask.AggregatePublic, recvMsg.Payload) {
utils.GetLogInstance().Warn("onNewView unable to verify aggregated signature of m1 payload")
if recvMsg.M3AggSig == nil {
return
}
m3Sig := recvMsg.M3AggSig
m3Mask := recvMsg.M3Bitmap
viewIDHash := make([]byte, 4)
binary.LittleEndian.PutUint32(viewIDHash, recvMsg.ViewID)
// TODO check total number of sigs >= 2f+1
if !m3Sig.VerifyHash(m3Mask.AggregatePublic, viewIDHash) {
utils.GetLogInstance().Warn("onNewView unable to verify aggregated signature of m3 payload", "m3Sig", m3Sig.GetHexString()[:10], "m3Mask", m3Mask.Bitmap, "viewID", recvMsg.ViewID)
return
}
if recvMsg.M2AggSig != nil {
m2Sig := recvMsg.M2AggSig
@ -359,7 +377,10 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
}
}
if len(recvMsg.Payload) > 32 && recvMsg.M1AggSig != nil {
// TODO: check if M3 sigs > M1 sigs, then recvMsg.Payload should not be empty
// check validity of m1 type payload
if len(recvMsg.Payload) > 32 {
blockHash := recvMsg.Payload[:32]
aggSig, mask, err := consensus.readSignatureBitmapPayload(recvMsg.Payload, 32)
if err != nil {
@ -387,21 +408,20 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
return
}
consensus.viewID = consensus.mode.GetViewID()
// Construct and send the commit message
multiSigAndBitmap := append(aggSig.Serialize(), mask.Bitmap...)
msgToSend := consensus.constructCommitMessage(multiSigAndBitmap)
utils.GetLogInstance().Info("onNewView === commit", "sent commit message", len(msgToSend))
utils.GetLogInstance().Info("onNewView === commit", "sent commit message", len(msgToSend), "viewID", consensus.viewID)
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.NewGroupIDByShardID(p2p.ShardID(consensus.ShardID))}, host.ConstructP2pMessage(byte(17), msgToSend))
consensus.phase = Commit
} else {
consensus.phase = Announce
consensus.ResetState()
utils.GetLogInstance().Info("onNewView === announce")
}
consensus.viewID = consensus.mode.GetViewID()
consensus.LeaderPubKey = senderKey
consensus.viewID = consensus.mode.GetViewID()
consensus.ResetViewChangeState()
consensus.ResetState()
consensus.consensusTimeout[timeoutConsensus].Start()
consensus.consensusTimeout[timeoutViewChange].Stop()
}

@ -12,7 +12,7 @@ require (
github.com/deckarep/golang-set v1.7.1
github.com/edsrzf/mmap-go v1.0.0 // indirect
github.com/ethereum/go-ethereum v1.8.27
github.com/fjl/memsize v0.0.0-20180929194037-2a09253e352a // indirect
github.com/fjl/memsize v0.0.0-20180929194037-2a09253e352a
github.com/garslo/gogen v0.0.0-20170307003452-d6ebae628c7c // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-stack/stack v1.8.0 // indirect

@ -69,6 +69,8 @@ type ConfigType struct {
isBeacon bool // whether this node is beacon node doing consensus or not
ShardID uint32 // ShardID of this node
role Role // Role of the node
Port string // Port of the node.
IP string // IP of the node.
StringRole string
Host p2p.Host

@ -0,0 +1,52 @@
package memprofiling
import (
"fmt"
"net/http"
"github.com/fjl/memsize/memsizeui"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/utils"
)
// Constants for mem profiling.
const (
MemProfilingPortDiff = 1000
)
// MemProfiling is the struct of MemProfiling.
type MemProfiling struct {
h *memsizeui.Handler
s *http.Server
}
// New returns MemProfiling object.
func New() *MemProfiling {
return &MemProfiling{}
}
// Config configures mem profiling.
func (m *MemProfiling) Config() {
m.h = new(memsizeui.Handler)
m.s = &http.Server{
Addr: fmt.Sprintf("%s:%s", nodeconfig.GetDefaultConfig().IP, utils.GetPortFromDiff(nodeconfig.GetDefaultConfig().Port, MemProfilingPortDiff)),
Handler: m.h,
}
utils.GetLogInstance().Info("running mem profiling", "port", utils.GetPortFromDiff(nodeconfig.GetDefaultConfig().Port, MemProfilingPortDiff))
}
// Add adds variables to watch for profiling.
func (m *MemProfiling) Add(name string, v interface{}) {
m.h.Add(name, v)
}
// Start starts profiling server.
func (m *MemProfiling) Start() {
go m.s.ListenAndServe()
utils.GetLogInstance().Info("Start memprofiling.")
}
// Stop stops mem profiling.
func (m *MemProfiling) Stop() {
m.s.Shutdown(nil)
}

@ -0,0 +1,14 @@
package memprofiling
import "sync"
var singleton *MemProfiling
var once sync.Once
// GetMemProfiling returns a pointer of MemProfiling.
func GetMemProfiling() *MemProfiling {
once.Do(func() {
singleton = &MemProfiling{}
})
return singleton
}

@ -74,13 +74,13 @@ var GenesisAccounts = [...]DeployAccount{
{Address: "0x141B0e0f05739B7B784654E973e9b9146473aAb9", Private: "", Public: "0x141B0e0f05739B7B784654E973e9b9146473aAb9"},
{Address: "0x1492ebD0EcfD54B4c211b37C8891bA3493c52100", Private: "", Public: "0x1492ebD0EcfD54B4c211b37C8891bA3493c52100"},
{Address: "0x1530A04592F9C3bF06aC6044525f08937ED38edB", Private: "", Public: "0x1530A04592F9C3bF06aC6044525f08937ED38edB"},
{Address: "0x159f58a41F13ffC831690D81F308244A3b238523", Private: "", Public: "0x159f58a41F13ffC831690D81F308244A3b238523"},
{Address: "0xE2ab78ecf325084485957B2599d53Bcf944Cbca8", Private: "", Public: "0xE2ab78ecf325084485957B2599d53Bcf944Cbca8"},
// 30-39
{Address: "0x16451cE5a762038eeA700A6aCd5F796DF3D6bE45", Private: "", Public: "0x16451cE5a762038eeA700A6aCd5F796DF3D6bE45"},
{Address: "0x17f68b4C9e1eeE6eD5bBa0326c50515c7816FF78", Private: "", Public: "0x17f68b4C9e1eeE6eD5bBa0326c50515c7816FF78"},
{Address: "0x19373b0E0428cc0f3F25169Cd079d27856b9f6d6", Private: "", Public: "0x19373b0E0428cc0f3F25169Cd079d27856b9f6d6"},
{Address: "0x1B77597d6664f1fB4894c735Ea80cf250866d265", Private: "", Public: "0x1B77597d6664f1fB4894c735Ea80cf250866d265"},
{Address: "0xa714cd269A0ca23131C8cD5aeFC49F450578C4B4", Private: "", Public: "0xa714cd269A0ca23131C8cD5aeFC49F450578C4B4"},
{Address: "0xF262609617c202087B31aCf364C00967f5Cd85De", Private: "", Public: "0xF262609617c202087B31aCf364C00967f5Cd85De"},
{Address: "0x609e8747cdcE518fB86C5990dCE325649C487133", Private: "", Public: "0x609e8747cdcE518fB86C5990dCE325649C487133"},
{Address: "0x6b9E03aB56786f4F228eE11D965A1a81ed7dA1D4", Private: "", Public: "0x6b9E03aB56786f4F228eE11D965A1a81ed7dA1D4"},
{Address: "0x1Db6cf99e231e42CEB0389E7828b16Be9b6a385f", Private: "", Public: "0x1Db6cf99e231e42CEB0389E7828b16Be9b6a385f"},
{Address: "0x1F4B579f85E585039C56989DEB56377c529981Be", Private: "", Public: "0x1F4B579f85E585039C56989DEB56377c529981Be"},
{Address: "0x1e9fCd9B1BDC53921d6a97D305f8a829Fd1299fE", Private: "", Public: "0x1e9fCd9B1BDC53921d6a97D305f8a829Fd1299fE"},
@ -98,7 +98,7 @@ var GenesisAccounts = [...]DeployAccount{
{Address: "0xb108BF4945Bd7975cF974f47476e689ACd542F23", Private: "", Public: "0xb108BF4945Bd7975cF974f47476e689ACd542F23"},
{Address: "0xc55c56F661eD185103839FdFeFd80DC38938913b", Private: "", Public: "0xc55c56F661eD185103839FdFeFd80DC38938913b"},
{Address: "0x76f8d12F6624f713B2D8894A749ad926F7812350", Private: "", Public: "0x76f8d12F6624f713B2D8894A749ad926F7812350"},
{Address: "0x265CA0F26A8D899F0c25313164FBBb5344F973cf", Private: "", Public: "0x265CA0F26A8D899F0c25313164FBBb5344F973cf"},
{Address: "0x31a77BAabad45E5dFBAC8231bDBF9Fe30F265554", Private: "", Public: "0x31a77BAabad45E5dFBAC8231bDBF9Fe30F265554"},
// 50-59
{Address: "0x26Bb09E04a264AdB89B73389CBdcb48819CB2401", Private: "", Public: "0x26Bb09E04a264AdB89B73389CBdcb48819CB2401"},
@ -204,9 +204,9 @@ var GenesisAccounts = [...]DeployAccount{
{Address: "0x5672f21553783d16AE7A7901A4461adBf6C09c56", Private: "", Public: "0x5672f21553783d16AE7A7901A4461adBf6C09c56"},
{Address: "0x596907B8b2E11e2EBC6f6c8011Ad379e1b83F669", Private: "", Public: "0x596907B8b2E11e2EBC6f6c8011Ad379e1b83F669"},
{Address: "0x5A9B1A7b7c889C359e8D52c08e1566C10Fa8B5a9", Private: "", Public: "0x5A9B1A7b7c889C359e8D52c08e1566C10Fa8B5a9"},
{Address: "0x5B266C47A82c4a849a70a1F96760fC1025784E7D", Private: "", Public: "0x5B266C47A82c4a849a70a1F96760fC1025784E7D"},
{Address: "0x5Cb5E2Bb095d2DD25C9b2887851f8D9E7b733e75", Private: "", Public: "0x5Cb5E2Bb095d2DD25C9b2887851f8D9E7b733e75"},
{Address: "0x5Cc69576F059260426e29bDccaf22711ee9F2730", Private: "", Public: "0x5Cc69576F059260426e29bDccaf22711ee9F2730"},
{Address: "0x78A8D29D81dD02c13a2a6077d887CF661B67E2c0", Private: "", Public: "0x78A8D29D81dD02c13a2a6077d887CF661B67E2c0"},
{Address: "0x40d6f48c7b27BA7544b04456445Cf19B680F5484", Private: "", Public: "0x40d6f48c7b27BA7544b04456445Cf19B680F5484"},
{Address: "0x04c3636dF766ad2d3E74424c016842f5704FAE3A", Private: "", Public: "0x04c3636dF766ad2d3E74424c016842f5704FAE3A"},
// 140-149
{Address: "0x28dA1beF8F5361863DcD427B6264f9DdF05B5D14", Private: "", Public: "0x28dA1beF8F5361863DcD427B6264f9DdF05B5D14"},
@ -254,22 +254,22 @@ var GenesisAccounts = [...]DeployAccount{
{Address: "0x766B51338c6C4F7De2d4f15357bdbA4B877C0835", Private: "", Public: "0x766B51338c6C4F7De2d4f15357bdbA4B877C0835"},
{Address: "0x76e2A98706F1d4d01e2FF1FE6D8e4609A0622Fb4", Private: "", Public: "0x76e2A98706F1d4d01e2FF1FE6D8e4609A0622Fb4"},
{Address: "0x770b8e3A35Bff512F173cE152BD1220d82bB9de0", Private: "", Public: "0x770b8e3A35Bff512F173cE152BD1220d82bB9de0"},
{Address: "0x77229fA6198791D333F286fa8360946042c65337", Private: "", Public: "0x77229fA6198791D333F286fa8360946042c65337"},
{Address: "0xcb0A6c1914d2AD10855cC8cD70B040b7Dc6573a8", Private: "", Public: "0xcb0A6c1914d2AD10855cC8cD70B040b7Dc6573a8"},
// 180-189
{Address: "0x78404079f5081A5Dc38902b47257c0D1D4e2E028", Private: "", Public: "0x78404079f5081A5Dc38902b47257c0D1D4e2E028"},
{Address: "0x79BE4EF66f3cc7B5b379F85353873115aeDbD242", Private: "", Public: "0x79BE4EF66f3cc7B5b379F85353873115aeDbD242"},
{Address: "0x7Dc053eAc8613229a6c316Fc436f100477571EE2", Private: "", Public: "0x7Dc053eAc8613229a6c316Fc436f100477571EE2"},
{Address: "0xB1Fa8F1CEa1A78d8887609CebEA592313dD659C1", Private: "", Public: "0xB1Fa8F1CEa1A78d8887609CebEA592313dD659C1"},
{Address: "0xC6fDB78B643E8eBaC472dB61c1e84B6Fe0Abe185", Private: "", Public: "0xC6fDB78B643E8eBaC472dB61c1e84B6Fe0Abe185"},
{Address: "0xd021c9a6A8816FE57a3A4CBd02fA824e0e92421B", Private: "", Public: "0xd021c9a6A8816FE57a3A4CBd02fA824e0e92421B"},
{Address: "0x7F58C0cD5c255020Ec94425873F666F1D68FBaA6", Private: "", Public: "0x7F58C0cD5c255020Ec94425873F666F1D68FBaA6"},
{Address: "0x7F686454f91A68cC3248a642F59aDb2970e84D8e", Private: "", Public: "0x7F686454f91A68cC3248a642F59aDb2970e84D8e"},
{Address: "0x7FB5fF6e7aE8279F21B843Bc297b24bFbC45733E", Private: "", Public: "0x7FB5fF6e7aE8279F21B843Bc297b24bFbC45733E"},
{Address: "0x7a22ff5B8483CE859757Bc0Ef7Ec64d11421B680", Private: "", Public: "0x7a22ff5B8483CE859757Bc0Ef7Ec64d11421B680"},
{Address: "0x9c23fE8cdcA1a8E529deeE8eD8492575cc3F9129", Private: "", Public: "0x9c23fE8cdcA1a8E529deeE8eD8492575cc3F9129"},
{Address: "0x324c741430F5B970b61E398434B4F3957a6BC6E0", Private: "", Public: "0x324c741430F5B970b61E398434B4F3957a6BC6E0"},
{Address: "0x7bBf40bD603B2434A964CdF979020B1E0E68E13D", Private: "", Public: "0x7bBf40bD603B2434A964CdF979020B1E0E68E13D"},
{Address: "0x7c14C7f6dE1f39579F7ab8DE24c168737E3FF53f", Private: "", Public: "0x7c14C7f6dE1f39579F7ab8DE24c168737E3FF53f"},
{Address: "0x22117D26611161b1b1f4EBB06C441aeeA102261c", Private: "", Public: "0x22117D26611161b1b1f4EBB06C441aeeA102261c"},
{Address: "0x7cc507b9345a58B5232e16B49b02F365bEB7d91e", Private: "", Public: "0x7cc507b9345a58B5232e16B49b02F365bEB7d91e"},
// 190-199
{Address: "0x1a4d5957Af59aE2E5ED0e4FAB42f14D1177d1F79", Private: "", Public: "0x1a4d5957Af59aE2E5ED0e4FAB42f14D1177d1F79"},
{Address: "0x93570Dcb1Bf1a0bD1d476a542309754a6dbCE632", Private: "", Public: "0x93570Dcb1Bf1a0bD1d476a542309754a6dbCE632"},
{Address: "0x87a157db95dc3517Eb578d4cedee92a5ab275BD5", Private: "", Public: "0x87a157db95dc3517Eb578d4cedee92a5ab275BD5"},
{Address: "0x25347d09373B2644191f1DC4beDEFEBE26a5b2d1", Private: "", Public: "0x25347d09373B2644191f1DC4beDEFEBE26a5b2d1"},
{Address: "0x7f42f7a4d66f0387AE77A219d0742E8a706231CA", Private: "", Public: "0x7f42f7a4d66f0387AE77A219d0742E8a706231CA"},

@ -15,7 +15,6 @@ import (
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/bls/ffi/go/bls"
p2p_crypto "github.com/libp2p/go-libp2p-crypto"
)
@ -233,3 +232,12 @@ func GetBlsAddress(key *bls.PublicKey) common.Address {
addr.SetBytes(addrBytes[:])
return addr
}
// GetPortFromDiff returns the port from base and the diff.
func GetPortFromDiff(port string, diff int) string {
if portNum, err := strconv.Atoi(port); err == nil {
return fmt.Sprintf("%d", portNum-diff)
}
GetLogInstance().Error("error on parsing port.")
return ""
}

@ -1,36 +0,0 @@
package node
import (
"testing"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/p2pimpl"
)
func prepareNode(t *testing.T) *Node {
pubKey := bls.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
consensus, err := consensus.New(host, 0, leader, nil)
if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err)
}
return New(host, consensus, testDBFactory, false)
}
func TestAddLotteryContract(t *testing.T) {
node := prepareNode(t)
node.AddLotteryContract()
if len(node.DemoContractAddress) == 0 {
t.Error("Can not create demo contract")
}
}

@ -1,216 +0,0 @@
package node
import (
"fmt"
"math"
"math/big"
"os"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/harmony-one/harmony/contracts"
"github.com/harmony-one/harmony/core/denominations"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
contract_constants "github.com/harmony-one/harmony/internal/utils/contract"
)
// Constants for lottery.
const (
Enter = "enter"
PickWinner = "pickWinner"
GetPlayers = "getPlayers"
PuzzleFund = 100000000
)
// AddLotteryContract adds the demo lottery contract the genesis block.
func (node *Node) AddLotteryContract() {
// Add a lottery demo contract.
priKey, err := crypto.HexToECDSA(contract_constants.DemoAccounts[0].Private)
if err != nil {
utils.GetLogInstance().Error("Error when creating private key for demo contract")
// Exit here to recognize the coding working.
// Basically we will remove this logic when launching so it's fine for now.
os.Exit(1)
}
dataEnc := common.FromHex(contracts.LotteryBin)
// Unsigned transaction to avoid the case of transaction address.
contractFunds := big.NewInt(0)
contractFunds = contractFunds.Mul(contractFunds, big.NewInt(denominations.One))
demoContract, _ := types.SignTx(
types.NewContractCreation(uint64(0), node.Consensus.ShardID, contractFunds, params.TxGasContractCreation*10, nil, dataEnc),
types.HomesteadSigner{},
priKey)
node.DemoContractAddress = crypto.CreateAddress(crypto.PubkeyToAddress(priKey.PublicKey), uint64(0))
node.LotteryManagerPrivateKey = priKey
node.addPendingTransactions(types.Transactions{demoContract})
}
// CreateTransactionForEnterMethod generates transaction for enter method and add it into pending tx list.
func (node *Node) CreateTransactionForEnterMethod(amount int64, priKey string) error {
var err error
toAddress := node.DemoContractAddress
abi, err := abi.JSON(strings.NewReader(contracts.LotteryABI))
if err != nil {
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
return err
}
bytesData, err := abi.Pack(Enter)
if err != nil {
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
return err
}
key, err := crypto.HexToECDSA(priKey)
nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey))
Amount := big.NewInt(amount)
Amount = Amount.Mul(Amount, big.NewInt(denominations.One))
tx := types.NewTransaction(
nonce,
toAddress,
node.NodeConfig.ShardID,
Amount,
params.TxGas*10,
nil,
bytesData,
)
if err != nil {
utils.GetLogInstance().Error("Failed to get private key", "error", err)
return err
}
if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil {
node.addPendingTransactions(types.Transactions{signedTx})
return nil
}
utils.GetLogInstance().Error("Unable to call enter method", "error", err)
return err
}
// GetResultDirectly get current players and their balances, not from smart contract.
func (node *Node) GetResultDirectly(priKey string) (players []string, balances []*big.Int) {
for _, account := range contract_constants.DemoAccounts {
players = append(players, account.Private)
key, err := crypto.HexToECDSA(account.Private)
if err != nil {
utils.GetLogInstance().Error("Error when HexToECDSA")
}
address := crypto.PubkeyToAddress(key.PublicKey)
balance, err := node.GetBalanceOfAddress(address)
balances = append(balances, balance)
}
return players, balances
}
// GenerateResultDirectly get current players and their balances, not from smart contract.
func (node *Node) GenerateResultDirectly(addresses []common.Address) (players []string, balances []*big.Int) {
for _, address := range addresses {
players = append(players, address.String())
balance, _ := node.GetBalanceOfAddress(address)
balances = append(balances, balance)
}
fmt.Println("generate result", players, balances)
return players, balances
}
// GetResult get current players and their balances.
func (node *Node) GetResult(priKey string) (players []string, balances []*big.Int) {
// TODO(minhdoan): get result from smart contract is current not working. Fix it later.
abi, err := abi.JSON(strings.NewReader(contracts.LotteryABI))
if err != nil {
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
}
bytesData, err := abi.Pack("getPlayers")
if err != nil {
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
}
demoContractAddress := node.DemoContractAddress
key, err := crypto.HexToECDSA(priKey)
if err != nil {
utils.GetLogInstance().Error("Failed to parse private key", "error", err)
}
nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey))
tx := types.NewTransaction(
nonce,
demoContractAddress,
node.NodeConfig.ShardID,
nil,
math.MaxUint64,
nil,
bytesData,
)
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key)
if err != nil {
utils.GetLogInstance().Error("Failed to sign contract call tx", "error", err)
return nil, nil
}
output, err := node.ContractCaller.CallContract(signedTx)
if err != nil {
utils.GetLogInstance().Error("Failed to call staking contract", "error", err)
return nil, nil
}
ret := []common.Address{}
err = abi.Unpack(&ret, "getPlayers", output)
if err != nil {
utils.GetLogInstance().Error("Failed to unpack getPlayers", "error", err)
return nil, nil
}
utils.GetLogInstance().Info("get result: ", "ret", ret)
fmt.Println("get result called:", ret)
return node.GenerateResultDirectly(ret)
}
// CreateTransactionForPickWinner generates transaction for enter method and add it into pending tx list.
func (node *Node) CreateTransactionForPickWinner() error {
var err error
toAddress := node.DemoContractAddress
abi, err := abi.JSON(strings.NewReader(contracts.LotteryABI))
if err != nil {
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
return err
}
bytesData, err := abi.Pack(PickWinner)
if err != nil {
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
return err
}
key := node.LotteryManagerPrivateKey
if key == nil {
return fmt.Errorf("LotterManagerPrivateKey is nil")
}
nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey))
Amount := big.NewInt(0)
tx := types.NewTransaction(
nonce,
toAddress,
node.NodeConfig.ShardID,
Amount,
params.TxGas*1000,
nil,
bytesData,
)
if err != nil {
utils.GetLogInstance().Error("Failed to get private key", "error", err)
return err
}
if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil {
node.addPendingTransactions(types.Transactions{signedTx})
return nil
}
utils.GetLogInstance().Error("Unable to call enter method", "error", err)
return err
}

@ -15,7 +15,6 @@ import (
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/api/client"
clientService "github.com/harmony-one/harmony/api/client/service"
msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/api/service"
"github.com/harmony-one/harmony/api/service/syncing"
@ -69,8 +68,6 @@ func (state State) String() string {
}
const (
// ClientServicePortDiff is the positive port diff for client service
ClientServicePortDiff = 5555
maxBroadcastNodes = 10 // broadcast at most maxBroadcastNodes peers that need in sync
broadcastTimeout int64 = 3 * 60 * 1000000000 // 3 mins
//SyncIDLength is the length of bytes for syncID
@ -115,9 +112,6 @@ type Node struct {
Worker *worker.Worker
BeaconWorker *worker.Worker // worker for beacon chain
// Client server (for wallet requests)
clientServer *clientService.Server
// Syncing component.
syncID [SyncIDLength]byte // a unique ID for the node during the state syncing process with peers
downloaderServer *downloader.Server

@ -107,7 +107,7 @@ func (node *Node) WaitForConsensusReadyv2(readySignal chan struct{}, stopChan ch
defer close(stoppedChan)
utils.GetLogInstance().Debug("Waiting for Consensus ready")
time.Sleep(45 * time.Second) // Wait for other nodes to be ready (test-only)
time.Sleep(30 * time.Second) // Wait for other nodes to be ready (test-only)
firstTime := true
for {

@ -1,229 +0,0 @@
package node
import (
"fmt"
"math/big"
"os"
"strings"
"sync/atomic"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/harmony-one/harmony/contracts"
"github.com/harmony-one/harmony/core/denominations"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
contract_constants "github.com/harmony-one/harmony/internal/utils/contract"
)
// Constants for puzzle.
const (
Play = "play"
Payout = "payout"
EndGame = "endGame"
)
// OneEther is one ether
var OneEther = big.NewInt(denominations.One)
// AddPuzzleContract adds the demo puzzle contract the genesis block.
func (node *Node) AddPuzzleContract() {
// Add a puzzle demo contract.
priKey, err := crypto.HexToECDSA(contract_constants.PuzzleAccounts[0].Private)
if err != nil {
utils.GetLogInstance().Error("Error when creating private key for puzzle demo contract")
// Exit here to recognize the coding working.
// Basically we will remove this logic when launching so it's fine for now.
os.Exit(1)
}
dataEnc := common.FromHex(contracts.PuzzleBin)
// Unsigned transaction to avoid the case of transaction address.
contractFunds := big.NewInt(PuzzleFund)
contractFunds = contractFunds.Mul(contractFunds, big.NewInt(denominations.One))
demoContract, _ := types.SignTx(
types.NewContractCreation(uint64(0), node.Consensus.ShardID, contractFunds, params.TxGasContractCreation*1000, nil, dataEnc),
types.HomesteadSigner{},
priKey)
node.PuzzleContractAddress = crypto.CreateAddress(crypto.PubkeyToAddress(priKey.PublicKey), uint64(0))
node.PuzzleManagerPrivateKey = priKey
node.addPendingTransactions(types.Transactions{demoContract})
}
// CreateTransactionForPlayMethod generates transaction for play method and add it into pending tx list.
func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) (string, error) {
var err error
toAddress := node.PuzzleContractAddress
abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI))
if err != nil {
utils.GetLogInstance().Error("puzzle-play: Failed to generate staking contract's ABI", "error", err)
return "", err
}
bytesData, err := abi.Pack(Play)
if err != nil {
utils.GetLogInstance().Error("puzzle-play: Failed to generate ABI function bytes data", "error", err)
return "", err
}
Stake := big.NewInt(0)
Stake = Stake.Mul(OneEther, big.NewInt(amount))
key, err := crypto.HexToECDSA(priKey)
if err != nil {
utils.GetLogInstance().Error("Failed to parse private key", "error", err)
return "", err
}
address := crypto.PubkeyToAddress(key.PublicKey)
balance, err := node.GetBalanceOfAddress(address)
if err != nil {
utils.GetLogInstance().Error("puzzle-play: can not get address", "error", err)
return "", err
} else if balance.Cmp(Stake) == -1 {
utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err, "stake", Stake, "balance", balance)
return "", ErrPuzzleInsufficientFund
}
nonce := node.GetAndIncreaseAddressNonce(address)
tx := types.NewTransaction(
nonce,
toAddress,
node.NodeConfig.ShardID,
Stake,
params.TxGas*100,
nil,
bytesData,
)
if err != nil {
utils.GetLogInstance().Error("puzzle-play: Failed to get private key", "error", err)
return "", err
}
if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil {
node.addPendingTransactions(types.Transactions{signedTx})
return signedTx.Hash().Hex(), nil
}
utils.GetLogInstance().Error("puzzle-play: Unable to call enter method", "error", err)
return "", err
}
// CreateTransactionForPayoutMethod generates transaction for payout method and add it into pending tx list.
func (node *Node) CreateTransactionForPayoutMethod(priKey string, level int, sequence string) (string, error) {
var err error
toAddress := node.PuzzleContractAddress
abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI))
if err != nil {
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
return "", err
}
key, err := crypto.HexToECDSA(priKey)
if err != nil {
utils.GetLogInstance().Error("Failed to parse private key", "error", err)
return "", err
}
address := crypto.PubkeyToAddress(key.PublicKey)
// add params for address payable player, uint8 new_level, steps string
fmt.Println("Payout: address", address)
bytesData, err := abi.Pack(Payout, address, big.NewInt(int64(level)), sequence)
if err != nil {
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
return "", err
}
if key == nil {
return "", fmt.Errorf("user key is nil")
}
nonce := node.GetAndIncreaseAddressNonce(address)
Amount := big.NewInt(0)
tx := types.NewTransaction(
nonce,
toAddress,
node.NodeConfig.ShardID,
Amount,
params.TxGas*10,
nil,
bytesData,
)
if err != nil {
utils.GetLogInstance().Error("Failed to get private key", "error", err)
return "", err
}
if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil {
node.addPendingTransactions(types.Transactions{signedTx})
return signedTx.Hash().Hex(), nil
}
utils.GetLogInstance().Error("Unable to call enter method", "error", err)
return "", err
}
// CreateTransactionForEndMethod generates transaction for endGame method and add it into pending tx list.
func (node *Node) CreateTransactionForEndMethod(priKey string) (string, error) {
var err error
toAddress := node.PuzzleContractAddress
abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI))
if err != nil {
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
return "", err
}
key, err := crypto.HexToECDSA(priKey)
if err != nil {
utils.GetLogInstance().Error("Failed to parse private key", "error", err)
return "", err
}
address := crypto.PubkeyToAddress(key.PublicKey)
// add params for address payable player, uint8 new_level, steps string
fmt.Println("EndGame: address", address)
bytesData, err := abi.Pack(EndGame, address)
if err != nil {
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
return "", err
}
if key == nil {
return "", fmt.Errorf("user key is nil")
}
nonce := node.GetAndIncreaseAddressNonce(address)
Amount := big.NewInt(0)
tx := types.NewTransaction(
nonce,
toAddress,
node.NodeConfig.ShardID,
Amount,
params.TxGas*10,
nil,
bytesData,
)
if err != nil {
utils.GetLogInstance().Error("Failed to get private key", "error", err)
return "", err
}
if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil {
node.addPendingTransactions(types.Transactions{signedTx})
return signedTx.Hash().Hex(), nil
}
utils.GetLogInstance().Error("Unable to call enter method", "error", err)
return "", err
}
// GetAndIncreaseAddressNonce get and increase the address's nonce
func (node *Node) GetAndIncreaseAddressNonce(address common.Address) uint64 {
if value, ok := node.AddressNonce.Load(address); ok {
n, ok := value.(uint64)
if !ok {
return 0
}
nonce := atomic.AddUint64(&n, 1)
return nonce - 1
}
nonce := node.GetNonceOfAddress(address) + 1
node.AddressNonce.Store(address, nonce)
return nonce - 1
}

@ -1,3 +1,4 @@
./test/kill_node.sh
rm -rf tmp_log*
./test/deploy.sh ./test/configs/beaconchain40.txt

@ -76,7 +76,7 @@ DEFAULT_DURATION_SYNC=200
DB=false
TXGEN=true
DURATION=
MIN=5
MIN=3
SHARDS=2
SYNC=true
DRYRUN=

Loading…
Cancel
Save