From 28cd1ed618f71ef67d9067190a4a8c87e79b86dc Mon Sep 17 00:00:00 2001 From: DinhLN Date: Mon, 22 Apr 2019 14:24:49 +0700 Subject: [PATCH] Fixed change panic to log error for encrypt/decrypt secret and opening for randomize. --- consensus/posv/posv_test.go | 2 +- contracts/utils.go | 12 ++++++++---- eth/downloader/downloader.go | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/consensus/posv/posv_test.go b/consensus/posv/posv_test.go index 1c0a5baeb1b8..2f41f1395cc6 100644 --- a/consensus/posv/posv_test.go +++ b/consensus/posv/posv_test.go @@ -27,7 +27,7 @@ func TestGetM1M2FromCheckpointHeader(t *testing.T) { Epoch: uint64(epoch), }, } - testMoveM2 := []uint64{0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2} + testMoveM2 := []uint64{0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2} //try from block 3410001 to 3410018 for i := uint64(3464001); i <= 3464018; i++ { currentNumber := int64(i) diff --git a/contracts/utils.go b/contracts/utils.go index 9a720de984c1..fdbb11a5da90 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -503,7 +503,8 @@ func Encrypt(key []byte, text string) string { block, err := aes.NewCipher(key) if err != nil { - panic(err) + log.Error("Fail to encrypt", "err", err) + return "" } // The IV needs to be unique, but not secure. Therefore it's common to @@ -511,7 +512,8 @@ func Encrypt(key []byte, text string) string { ciphertext := make([]byte, aes.BlockSize+len(plaintext)) iv := ciphertext[:aes.BlockSize] if _, err := io.ReadFull(cryptoRand.Reader, iv); err != nil { - panic(err) + log.Error("Fail to encrypt iv", "err", err) + return "" } stream := cipher.NewCFBEncrypter(block, iv) @@ -527,13 +529,15 @@ func Decrypt(key []byte, cryptoText string) string { block, err := aes.NewCipher(key) if err != nil { - panic(err) + log.Error("Fail to decrypt", "err", err) + return "" } // The IV needs to be unique, but not secure. Therefore it's common to // include it at the beginning of the ciphertext. if len(ciphertext) < aes.BlockSize { - panic("ciphertext too short") + log.Error("ciphertext too short") + return "" } iv := ciphertext[:aes.BlockSize] ciphertext = ciphertext[aes.BlockSize:] diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 4901b04446be..bc26c525d964 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -422,7 +422,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I }(time.Now()) // Look up the sync boundaries: the common ancestor and the target block - latest, err := d.fetchHeight(p,hash) + latest, err := d.fetchHeight(p, hash) if err != nil { return err } @@ -541,7 +541,7 @@ func (d *Downloader) Terminate() { // fetchHeight retrieves the head header of the remote peer to aid in estimating // the total time a pending synchronisation would take. -func (d *Downloader) fetchHeight(p *peerConnection,hash common.Hash) (*types.Header, error) { +func (d *Downloader) fetchHeight(p *peerConnection, hash common.Hash) (*types.Header, error) { // Request the advertised remote head block and wait for the response go p.peer.RequestHeadersByHash(hash, 1, 0, false)