Skip to content

Commit 83d72b8

Browse files
authored
Merge pull request ethereum#22 from binance-chain/resolve-best-pratices
[R4R]: resolve best practice advice
2 parents 5cc893a + 8124e60 commit 83d72b8

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

consensus/parlia/parlia.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ func (p *Parlia) verifyCascadingFields(chain consensus.ChainReader, header *type
396396
}
397397

398398
// Verify that the gas limit is <= 2^63-1
399-
cap := uint64(0x7fffffffffffffff)
400-
if header.GasLimit > cap {
401-
return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, cap)
399+
capacity := uint64(0x7fffffffffffffff)
400+
if header.GasLimit > capacity {
401+
return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, capacity)
402402
}
403403
// Verify that the gasUsed is <= gasLimit
404404
if header.GasUsed > header.GasLimit {
@@ -809,7 +809,7 @@ func (p *Parlia) Seal(chain consensus.ChainReader, block *types.Block, results c
809809
}
810810

811811
// Sweet, the protocol permits us to sign the block, wait for our time
812-
delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple
812+
delay := time.Until(time.Unix(int64(header.Time), 0)) // nolint: gosimple
813813
if header.Difficulty.Cmp(diffNoTurn) == 0 {
814814
// It's not our turn explicitly to sign, delay it a bit
815815
wiggle := time.Duration(len(snap.Validators)/2+1) * wiggleTime
@@ -1072,8 +1072,8 @@ func (p *Parlia) applyTransaction(
10721072
return errors.New("supposed to get a actual transaction, but get none")
10731073
}
10741074
actualTx := (*receivedTxs)[0]
1075-
if bytes.Compare(p.signer.Hash(actualTx).Bytes(), expectedHash.Bytes()) != 0 {
1076-
return errors.New(fmt.Sprintf("expected tx hash %v, get %v", expectedHash.String(), actualTx.Hash().String()))
1075+
if !bytes.Equal(p.signer.Hash(actualTx).Bytes(), expectedHash.Bytes()) {
1076+
return fmt.Errorf("expected tx hash %v, get %v", expectedHash.String(), actualTx.Hash().String())
10771077
}
10781078
expectedTx = actualTx
10791079
// move to next

core/vm/contracts_lightclient.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
const (
12+
uint64TypeLength uint64 = 8
1213
precompileContractInputMetaDataLength uint64 = 32
1314
consensusStateLengthBytesLength uint64 = 32
1415

@@ -20,7 +21,7 @@ const (
2021
// consensus state length | consensus state | tendermint header |
2122
// 32 bytes | | |
2223
func decodeTendermintHeaderValidationInput(input []byte) (*lightclient.ConsensusState, *lightclient.Header, error) {
23-
csLen := binary.BigEndian.Uint64(input[consensusStateLengthBytesLength-8 : consensusStateLengthBytesLength])
24+
csLen := binary.BigEndian.Uint64(input[consensusStateLengthBytesLength-uint64TypeLength : consensusStateLengthBytesLength])
2425
if uint64(len(input)) <= consensusStateLengthBytesLength+csLen {
2526
return nil, nil, fmt.Errorf("expected payload size %d, actual size: %d", consensusStateLengthBytesLength+csLen, len(input))
2627
}
@@ -55,7 +56,7 @@ func (c *tmHeaderValidate) Run(input []byte) (result []byte, err error) {
5556
return nil, fmt.Errorf("invalid input")
5657
}
5758

58-
payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-8 : precompileContractInputMetaDataLength])
59+
payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-uint64TypeLength : precompileContractInputMetaDataLength])
5960
if uint64(len(input)) != payloadLength+precompileContractInputMetaDataLength {
6061
return nil, fmt.Errorf("invalid input: input size should be %d, actual the size is %d", payloadLength+precompileContractInputMetaDataLength, len(input))
6162
}
@@ -83,7 +84,7 @@ func (c *tmHeaderValidate) Run(input []byte) (result []byte, err error) {
8384
copy(lengthBytes[:1], []byte{0x01})
8485
}
8586
consensusStateBytesLength := uint64(len(consensusStateBytes))
86-
binary.BigEndian.PutUint64(lengthBytes[tmHeaderValidateResultMetaDataLength-8:], consensusStateBytesLength)
87+
binary.BigEndian.PutUint64(lengthBytes[tmHeaderValidateResultMetaDataLength-uint64TypeLength:], consensusStateBytesLength)
8788

8889
result = append(lengthBytes, consensusStateBytes...)
8990

@@ -113,7 +114,7 @@ func (c *iavlMerkleProofValidate) Run(input []byte) (result []byte, err error) {
113114
return nil, fmt.Errorf("invalid input: input should include %d bytes payload length and payload", precompileContractInputMetaDataLength)
114115
}
115116

116-
payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-8 : precompileContractInputMetaDataLength])
117+
payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-uint64TypeLength : precompileContractInputMetaDataLength])
117118
if uint64(len(input)) != payloadLength+precompileContractInputMetaDataLength {
118119
return nil, fmt.Errorf("invalid input: input size should be %d, actual the size is %d", payloadLength+precompileContractInputMetaDataLength, len(input))
119120
}
@@ -129,6 +130,6 @@ func (c *iavlMerkleProofValidate) Run(input []byte) (result []byte, err error) {
129130
}
130131

131132
result = make([]byte, merkleProofValidateResultLength)
132-
binary.BigEndian.PutUint64(result[merkleProofValidateResultLength-8:], 0x01)
133+
binary.BigEndian.PutUint64(result[merkleProofValidateResultLength-uint64TypeLength:], 0x01)
133134
return result, nil
134135
}

core/vm/contracts_lightclient_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestTmHeaderValidateAndMerkleProofValidate(t *testing.T) {
2424
"cc05d26c7b0be0c8b46418294171730e079f384fde2fa50bafc000000174876e80049b288e4ebbb3a281c2d546fc30253d5baf08993b6e5d295fb7" +
2525
"87a5b314a298e000000174876e80004224339688f012e649de48e241880092eaa8f6aa0f4f14bfcf9e0c76917c0b6000000174876e8004034b37ce" +
2626
"da8a0bf13b1abaeee7a8f9383542099a554d219b93d0ce69e3970e8000000174876e800")
27+
require.NoError(t, err)
2728

2829
cs, err := lightclient.DecodeConsensusState(consensusStateBytes)
2930
require.NoError(t, err)

core/vm/lightclient/types.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,11 @@ func (kvmp *KeyValueMerkleProof) Validate() bool {
226226

227227
if len(kvmp.Value) == 0 {
228228
err := prt.VerifyAbsence(kvmp.Proof, kvmp.AppHash, kp.String())
229-
if err != nil {
230-
return false
231-
}
232-
return true
229+
return err == nil
233230
}
234231

235232
err := prt.VerifyValue(kvmp.Proof, kvmp.AppHash, kp.String(), kvmp.Value)
236-
if err != nil {
237-
return false
238-
}
239-
return true
233+
return err == nil
240234
}
241235

242236
// input:

0 commit comments

Comments
 (0)