Procházet zdrojové kódy

resolve best practice advice

HaoyangLiu před 5 roky
rodič
revize
8124e60e9d

+ 6 - 6
consensus/parlia/parlia.go

@@ -396,9 +396,9 @@ func (p *Parlia) verifyCascadingFields(chain consensus.ChainReader, header *type
 	}
 
 	// Verify that the gas limit is <= 2^63-1
-	cap := uint64(0x7fffffffffffffff)
-	if header.GasLimit > cap {
-		return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, cap)
+	capacity := uint64(0x7fffffffffffffff)
+	if header.GasLimit > capacity {
+		return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, capacity)
 	}
 	// Verify that the gasUsed is <= gasLimit
 	if header.GasUsed > header.GasLimit {
@@ -809,7 +809,7 @@ func (p *Parlia) Seal(chain consensus.ChainReader, block *types.Block, results c
 	}
 
 	// Sweet, the protocol permits us to sign the block, wait for our time
-	delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple
+	delay := time.Until(time.Unix(int64(header.Time), 0)) // nolint: gosimple
 	if header.Difficulty.Cmp(diffNoTurn) == 0 {
 		// It's not our turn explicitly to sign, delay it a bit
 		wiggle := time.Duration(len(snap.Validators)/2+1) * wiggleTime
@@ -1072,8 +1072,8 @@ func (p *Parlia) applyTransaction(
 			return errors.New("supposed to get a actual transaction, but get none")
 		}
 		actualTx := (*receivedTxs)[0]
-		if bytes.Compare(p.signer.Hash(actualTx).Bytes(), expectedHash.Bytes()) != 0 {
-			return errors.New(fmt.Sprintf("expected tx hash %v, get %v", expectedHash.String(), actualTx.Hash().String()))
+		if !bytes.Equal(p.signer.Hash(actualTx).Bytes(), expectedHash.Bytes()) {
+			return fmt.Errorf("expected tx hash %v, get %v", expectedHash.String(), actualTx.Hash().String())
 		}
 		expectedTx = actualTx
 		// move to next

+ 6 - 5
core/vm/contracts_lightclient.go

@@ -9,6 +9,7 @@ import (
 )
 
 const (
+	uint64TypeLength                      uint64 = 8
 	precompileContractInputMetaDataLength uint64 = 32
 	consensusStateLengthBytesLength       uint64 = 32
 
@@ -20,7 +21,7 @@ const (
 // consensus state length | consensus state | tendermint header |
 // 32 bytes               |                 |                   |
 func decodeTendermintHeaderValidationInput(input []byte) (*lightclient.ConsensusState, *lightclient.Header, error) {
-	csLen := binary.BigEndian.Uint64(input[consensusStateLengthBytesLength-8 : consensusStateLengthBytesLength])
+	csLen := binary.BigEndian.Uint64(input[consensusStateLengthBytesLength-uint64TypeLength : consensusStateLengthBytesLength])
 	if uint64(len(input)) <= consensusStateLengthBytesLength+csLen {
 		return nil, nil, fmt.Errorf("expected payload size %d, actual size: %d", consensusStateLengthBytesLength+csLen, len(input))
 	}
@@ -55,7 +56,7 @@ func (c *tmHeaderValidate) Run(input []byte) (result []byte, err error) {
 		return nil, fmt.Errorf("invalid input")
 	}
 
-	payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-8 : precompileContractInputMetaDataLength])
+	payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-uint64TypeLength : precompileContractInputMetaDataLength])
 	if uint64(len(input)) != payloadLength+precompileContractInputMetaDataLength {
 		return nil, fmt.Errorf("invalid input: input size should be %d, actual the size is %d", payloadLength+precompileContractInputMetaDataLength, len(input))
 	}
@@ -83,7 +84,7 @@ func (c *tmHeaderValidate) Run(input []byte) (result []byte, err error) {
 		copy(lengthBytes[:1], []byte{0x01})
 	}
 	consensusStateBytesLength := uint64(len(consensusStateBytes))
-	binary.BigEndian.PutUint64(lengthBytes[tmHeaderValidateResultMetaDataLength-8:], consensusStateBytesLength)
+	binary.BigEndian.PutUint64(lengthBytes[tmHeaderValidateResultMetaDataLength-uint64TypeLength:], consensusStateBytesLength)
 
 	result = append(lengthBytes, consensusStateBytes...)
 
@@ -113,7 +114,7 @@ func (c *iavlMerkleProofValidate) Run(input []byte) (result []byte, err error) {
 		return nil, fmt.Errorf("invalid input: input should include %d bytes payload length and payload", precompileContractInputMetaDataLength)
 	}
 
-	payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-8 : precompileContractInputMetaDataLength])
+	payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-uint64TypeLength : precompileContractInputMetaDataLength])
 	if uint64(len(input)) != payloadLength+precompileContractInputMetaDataLength {
 		return nil, fmt.Errorf("invalid input: input size should be %d, actual the size is %d", payloadLength+precompileContractInputMetaDataLength, len(input))
 	}
@@ -129,6 +130,6 @@ func (c *iavlMerkleProofValidate) Run(input []byte) (result []byte, err error) {
 	}
 
 	result = make([]byte, merkleProofValidateResultLength)
-	binary.BigEndian.PutUint64(result[merkleProofValidateResultLength-8:], 0x01)
+	binary.BigEndian.PutUint64(result[merkleProofValidateResultLength-uint64TypeLength:], 0x01)
 	return result, nil
 }

+ 1 - 0
core/vm/contracts_lightclient_test.go

@@ -24,6 +24,7 @@ func TestTmHeaderValidateAndMerkleProofValidate(t *testing.T) {
 		"cc05d26c7b0be0c8b46418294171730e079f384fde2fa50bafc000000174876e80049b288e4ebbb3a281c2d546fc30253d5baf08993b6e5d295fb7" +
 		"87a5b314a298e000000174876e80004224339688f012e649de48e241880092eaa8f6aa0f4f14bfcf9e0c76917c0b6000000174876e8004034b37ce" +
 		"da8a0bf13b1abaeee7a8f9383542099a554d219b93d0ce69e3970e8000000174876e800")
+	require.NoError(t, err)
 
 	cs, err := lightclient.DecodeConsensusState(consensusStateBytes)
 	require.NoError(t, err)

+ 2 - 8
core/vm/lightclient/types.go

@@ -226,17 +226,11 @@ func (kvmp *KeyValueMerkleProof) Validate() bool {
 
 	if len(kvmp.Value) == 0 {
 		err := prt.VerifyAbsence(kvmp.Proof, kvmp.AppHash, kp.String())
-		if err != nil {
-			return false
-		}
-		return true
+		return err == nil
 	}
 
 	err := prt.VerifyValue(kvmp.Proof, kvmp.AppHash, kp.String(), kvmp.Value)
-	if err != nil {
-		return false
-	}
-	return true
+	return err == nil
 }
 
 // input: