From 78323c11e340964be97620011d3f9287b491b71c Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 20 Apr 2019 16:38:36 -0700 Subject: [PATCH 1/6] Fix getFreeToken blocking issue --- node/contract.go | 5 +++-- node/node.go | 7 ++++--- node/node_handler.go | 7 +++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/node/contract.go b/node/contract.go index 02a33393b..6aa414d30 100644 --- a/node/contract.go +++ b/node/contract.go @@ -4,6 +4,7 @@ import ( "crypto/ecdsa" "math/big" "strings" + "sync/atomic" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" @@ -170,8 +171,8 @@ func (node *Node) CallFaucetContract(address common.Address) common.Hash { } func (node *Node) callGetFreeToken(address common.Address) common.Hash { - nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(node.ContractDeployerKey.PublicKey)) - return node.callGetFreeTokenWithNonce(address, nonce) + nonce := atomic.AddUint64(&node.ContractDeployerCurrentNonce, 1) + return node.callGetFreeTokenWithNonce(address, nonce-1) } func (node *Node) callGetFreeTokenWithNonce(address common.Address, nonce uint64) common.Hash { diff --git a/node/node.go b/node/node.go index f2b20595d..5aafad723 100644 --- a/node/node.go +++ b/node/node.go @@ -144,9 +144,10 @@ type Node struct { Address common.Address // For test only - TestBankKeys []*ecdsa.PrivateKey - ContractDeployerKey *ecdsa.PrivateKey - ContractAddresses []common.Address + TestBankKeys []*ecdsa.PrivateKey + ContractDeployerKey *ecdsa.PrivateKey + ContractDeployerCurrentNonce uint64 // The nonce of the deployer contract at current block + ContractAddresses []common.Address // Shard group Message Receiver shardGroupReceiver p2p.GroupReceiver diff --git a/node/node_handler.go b/node/node_handler.go index a7de6831e..83a17d2f9 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -11,9 +11,12 @@ import ( "os" "os/exec" "strconv" + "sync/atomic" "syscall" "time" + "github.com/ethereum/go-ethereum/crypto" + "github.com/harmony-one/harmony/core" "github.com/ethereum/go-ethereum/common" @@ -315,6 +318,10 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block) { node.AddNewBlock(newBlock) + // Update contract deployer's nonce so default contract like faucet can issue transaction with current nonce + nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(node.ContractDeployerKey.PublicKey)) + atomic.StoreUint64(&node.ContractDeployerCurrentNonce, nonce) + if node.Consensus.ShardID == 0 { // ConfirmedBlockChannel which is listened by drand leader who will initiate DRG if its a epoch block (first block of a epoch) From a88eb643e66c46ccf9e3cb97ad5dfc34398d7180 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Sat, 20 Apr 2019 06:30:34 +0000 Subject: [PATCH 2/6] [crash] fix nil pointer error #749 Signed-off-by: Leo Chen --- p2p/host/hostv2/hostv2.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/p2p/host/hostv2/hostv2.go b/p2p/host/hostv2/hostv2.go index 193e2d2ff..14453ba31 100644 --- a/p2p/host/hostv2/hostv2.go +++ b/p2p/host/hostv2/hostv2.go @@ -87,6 +87,9 @@ func (r *GroupReceiverImpl) Close() error { func (r *GroupReceiverImpl) Receive(ctx context.Context) ( msg []byte, sender libp2p_peer.ID, err error, ) { + if r.sub == nil { + return nil, libp2p_peer.ID(""), fmt.Errorf("Receive: r.sub == nil") + } m, err := r.sub.Next(ctx) if err == nil { msg = m.Data From 11459a4ea0d3ff1d1d86bd2161cba7ce5b86343e Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Sat, 20 Apr 2019 18:40:27 -0700 Subject: [PATCH 3/6] Add regression test --- p2p/host/hostv2/hostv2_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/p2p/host/hostv2/hostv2_test.go b/p2p/host/hostv2/hostv2_test.go index 01018a14e..b98203dfa 100644 --- a/p2p/host/hostv2/hostv2_test.go +++ b/p2p/host/hostv2/hostv2_test.go @@ -128,4 +128,11 @@ func TestHostV2_GroupReceiver(t *testing.T) { t.Error("expected an error; got none") } }) + t.Run("Closed", func(t *testing.T) { + var emptyReceiver GroupReceiverImpl + _, _, err := emptyReceiver.Receive(context.Background()) + if err == nil { + t.Errorf("Receive() from nil/closed receiver did not return error") + } + }) } From ba6f4b24d098e4bed575804425eb8bcc9eb7569d Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Sun, 21 Apr 2019 00:03:11 -0700 Subject: [PATCH 4/6] Update p2p/host/hostv2/hostv2.go Co-Authored-By: LeoHChen --- p2p/host/hostv2/hostv2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/host/hostv2/hostv2.go b/p2p/host/hostv2/hostv2.go index 14453ba31..eb7fa66d2 100644 --- a/p2p/host/hostv2/hostv2.go +++ b/p2p/host/hostv2/hostv2.go @@ -88,7 +88,7 @@ func (r *GroupReceiverImpl) Receive(ctx context.Context) ( msg []byte, sender libp2p_peer.ID, err error, ) { if r.sub == nil { - return nil, libp2p_peer.ID(""), fmt.Errorf("Receive: r.sub == nil") + return nil, libp2p_peer.ID(""), fmt.Errorf("GroupReceiver has been closed") } m, err := r.sub.Next(ctx) if err == nil { From 94ee34072089c9b860d8bbb3ac6067ce4d6b109c Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Sat, 20 Apr 2019 06:37:47 +0000 Subject: [PATCH 5/6] [crash] fix index out of range #744 print out an error if index out of range is found. Signed-off-by: Leo Chen --- core/resharding.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/resharding.go b/core/resharding.go index 248542627..723aee79a 100644 --- a/core/resharding.go +++ b/core/resharding.go @@ -58,7 +58,11 @@ func (ss *ShardingState) assignNewNodes(newNodeList []types.NodeID) { if numActiveShards > 0 { id = i % numActiveShards } - ss.shardState[id].NodeList = append(ss.shardState[id].NodeList, nid) + if id < len(ss.shardState) { + ss.shardState[id].NodeList = append(ss.shardState[id].NodeList, nid) + } else { + utils.GetLogInstance().Error("assignNewNodes", "index out of range", len(ss.shardState), "id", id) + } } } From 0fe8229a66f2182f4a87779bf1fbee288cb4adcc Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Sat, 20 Apr 2019 06:30:02 +0000 Subject: [PATCH 6/6] [log] reduce number of logs Signed-off-by: Leo Chen --- node/node_handler.go | 8 -------- node/node_syncing.go | 1 - 2 files changed, 9 deletions(-) diff --git a/node/node_handler.go b/node/node_handler.go index 83a17d2f9..d211ac82b 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -126,10 +126,8 @@ func (node *Node) messageHandler(content []byte, sender string) { case proto.Consensus: msgPayload, _ := proto.GetConsensusMessagePayload(content) if consensusObj.IsLeader { - utils.GetLogInstance().Info("NET: Leader received consensus message") consensusObj.ProcessMessageLeader(msgPayload) } else { - utils.GetLogInstance().Info("NET: Validator received consensus message") consensusObj.ProcessMessageValidator(msgPayload) // TODO(minhdoan): add logic to check if the current blockchain is not sync with other consensus // we should switch to other state rather than DoingConsensus. @@ -138,10 +136,8 @@ func (node *Node) messageHandler(content []byte, sender string) { msgPayload, _ := proto.GetDRandMessagePayload(content) if node.DRand != nil { if node.DRand.IsLeader { - utils.GetLogInstance().Info("NET: DRand Leader received message") node.DRand.ProcessMessageLeader(msgPayload) } else { - utils.GetLogInstance().Info("NET: DRand Validator received message") node.DRand.ProcessMessageValidator(msgPayload) } } @@ -309,7 +305,6 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) bool { // 1. add the new block to blockchain // 2. [leader] send new block to the client func (node *Node) PostConsensusProcessing(newBlock *types.Block) { - utils.GetLogInstance().Info("PostConsensusProcessing") if node.Consensus.IsLeader { node.BroadcastNewBlock(newBlock) } else { @@ -413,7 +408,6 @@ func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int { // SendPongMessage is the a goroutine to periodcally send pong message to all peers func (node *Node) SendPongMessage() { - utils.GetLogInstance().Info("Starting Pong routing") tick := time.NewTicker(2 * time.Second) tick2 := time.NewTicker(120 * time.Second) @@ -583,8 +577,6 @@ func (node *Node) epochShardStateMessageHandler(msgPayload []byte) int { } func (node *Node) processEpochShardState(epochShardState *types.EpochShardState) { - utils.GetLogInstance().Error("[Processing new shard state]") - shardState := epochShardState.ShardState epoch := epochShardState.Epoch diff --git a/node/node_syncing.go b/node/node_syncing.go index 4abac8a36..a6aa70f85 100644 --- a/node/node_syncing.go +++ b/node/node_syncing.go @@ -185,7 +185,6 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest) (* response := &downloader_pb.DownloaderResponse{} switch request.Type { case downloader_pb.DownloaderRequest_HEADER: - utils.GetLogInstance().Debug("[SYNC] CalculateResponse DownloaderRequest_HEADER", "request.BlockHash", request.BlockHash) var startHeaderHash []byte if request.BlockHash == nil { tmp := node.blockchain.Genesis().Hash()