Recover from panics which can get sent from goleveldb due to bad arguments. Closes #1059

This commit is contained in:
Pedro Pombeiro 2018-06-26 11:13:12 +02:00 committed by Pedro Pombeiro
parent 1bc63df5f2
commit ab7b9b914c
2 changed files with 23 additions and 5 deletions

View File

@ -156,8 +156,19 @@ func (s *WMailServer) Close() {
}
}
func recoverLevelDBPanics(calleMethodName string) {
// Recover from possible goleveldb panics
if r := recover(); r != nil {
if errString, ok := r.(string); ok {
log.Error(fmt.Sprintf("recovered from panic in %s: %s", calleMethodName, errString))
}
}
}
// Archive a whisper envelope.
func (s *WMailServer) Archive(env *whisper.Envelope) {
defer recoverLevelDBPanics("Archive")
key := NewDbKey(env.Expiry-env.TTL, env.Hash())
rawEnvelope, err := rlp.EncodeToBytes(env)
if err != nil {
@ -188,6 +199,8 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)
return
}
defer recoverLevelDBPanics("DeliverMail")
if ok, lower, upper, bloom := s.validateRequest(peer.ID(), request); ok {
s.processRequest(peer, lower, upper, bloom)
if err := s.sendHistoricMessageResponse(peer, request); err != nil {

View File

@ -111,7 +111,7 @@ func (s *WhisperMailboxSuite) TestRequestMessageFromMailboxAsync() {
senderWhisperService.SubscribeEnvelopeEvents(events)
// Request messages (including the previous one, expired) from mailbox.
result := s.requestHistoricMessages(senderWhisperService, rpcClient, mailboxPeerStr, MailServerKeyID, topic.String())
result := s.requestHistoricMessagesFromLast12Hours(senderWhisperService, rpcClient, mailboxPeerStr, MailServerKeyID, topic.String())
requestID := common.BytesToHash(result)
// And we receive message, it comes from mailbox.
@ -282,8 +282,8 @@ func (s *WhisperMailboxSuite) TestRequestMessagesInGroupChat() {
s.Require().Empty(messages)
// Request each one messages from mailbox using enode.
s.requestHistoricMessages(bobWhisperService, bobRPCClient, mailboxEnode, bobMailServerKeyID, groupChatTopic.String())
s.requestHistoricMessages(charlieWhisperService, charlieRPCClient, mailboxEnode, charlieMailServerKeyID, groupChatTopic.String())
s.requestHistoricMessagesFromLast12Hours(bobWhisperService, bobRPCClient, mailboxEnode, bobMailServerKeyID, groupChatTopic.String())
s.requestHistoricMessagesFromLast12Hours(charlieWhisperService, charlieRPCClient, mailboxEnode, charlieMailServerKeyID, groupChatTopic.String())
// Bob receive p2p message from group chat filter.
messages = s.getMessagesByMessageFilterIDWithTracer(bobRPCClient, bobGroupChatMessageFilterID, bobTracer, groupChatMessageHash)
@ -515,11 +515,16 @@ func (s *WhisperMailboxSuite) addSymKey(rpcCli *rpc.Client, symkey string) strin
return symkeyID
}
// requestHistoricMessages asks a mailnode to resend messages.
func (s *WhisperMailboxSuite) requestHistoricMessages(w *whisper.Whisper, rpcCli *rpc.Client, mailboxEnode, mailServerKeyID, topic string) []byte {
// requestHistoricMessagesFromLast12Hours asks a mailnode to resend messages from last 12 hours.
func (s *WhisperMailboxSuite) requestHistoricMessagesFromLast12Hours(w *whisper.Whisper, rpcCli *rpc.Client, mailboxEnode, mailServerKeyID, topic string) []byte {
currentTime := w.GetCurrentTime()
from := currentTime.Add(-12 * time.Hour)
to := currentTime
return s.requestHistoricMessages(w, rpcCli, mailboxEnode, mailServerKeyID, topic, from, to)
}
// requestHistoricMessages asks a mailnode to resend messages.
func (s *WhisperMailboxSuite) requestHistoricMessages(w *whisper.Whisper, rpcCli *rpc.Client, mailboxEnode, mailServerKeyID, topic string, from, to time.Time) []byte {
resp := rpcCli.CallRaw(`{
"jsonrpc": "2.0",
"id": 2,