Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous code quality improvements #7414

Merged
merged 13 commits into from
Oct 4, 2020
2 changes: 2 additions & 0 deletions beacon-chain/blockchain/receive_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []*ethpb.SignedB
}

if err := s.VerifyWeakSubjectivityRoot(s.ctx); err != nil {
// log.Fatalf will prevent defer from being called
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the real problem is that we shouldn't be using log.Fatalf. This isn't a graceful shutdown and can cause other issues like corrupt data writes: #7382

Copy link
Contributor Author

@rkapka rkapka Oct 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I absolutely agree. We can discuss this during the OKRs meeting since I am about to take a look at logging in general.

span.End()
// Exit run time if the node failed to verify weak subjectivity checkpoint.
log.Fatalf("Could not verify weak subjectivity checkpoint: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion beacon-chain/cache/feature_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ import (
func TestMain(m *testing.M) {
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableEth1DataVoteCache: true})
defer resetCfg()
os.Exit(m.Run())
code := m.Run()
// os.Exit will prevent defer from being called
resetCfg()
os.Exit(code)
}
2 changes: 0 additions & 2 deletions beacon-chain/cache/subnet_ids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ func TestSubnetIDsCache_RoundTrip(t *testing.T) {
}

func TestSubnetIDsCache_PersistentCommitteeRoundtrip(t *testing.T) {
pubkeySet := [][48]byte{}
c := newSubnetIDs()

for i := 0; i < 20; i++ {
pubkey := [48]byte{byte(i)}
pubkeySet = append(pubkeySet, pubkey)
c.AddPersistentCommittee(pubkey[:], []uint64{uint64(i)}, 0)
}

Expand Down
6 changes: 1 addition & 5 deletions beacon-chain/core/blocks/spectest/deposit_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package spectest

import (
"context"
"path"
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/params/spectest"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
Expand All @@ -26,9 +24,7 @@ func runDepositTest(t *testing.T, config string) {
require.NoError(t, deposit.UnmarshalSSZ(depositFile), "Failed to unmarshal")

body := &ethpb.BeaconBlockBody{Deposits: []*ethpb.Deposit{deposit}}
testutil.RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, state *state.BeaconState, b *ethpb.SignedBeaconBlock) (*state.BeaconState, error) {
return blocks.ProcessDeposits(ctx, state, b)
})
testutil.RunBlockOperationTest(t, folderPath, body, blocks.ProcessDeposits)
})
}
}
10 changes: 3 additions & 7 deletions beacon-chain/core/helpers/signing_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ func RetrieveBlockSignatureSet(blk *ethpb.BeaconBlock, pub []byte, signature []b
if err != nil {
return nil, errors.Wrap(err, "could not convert bytes to public key")
}
root, err := signingData(func() ([32]byte, error) {
// utilize custom block hashing function
return blk.HashTreeRoot()
}, domain)
// utilize custom block hashing function
root, err := signingData(blk.HashTreeRoot, domain)
if err != nil {
return nil, errors.Wrap(err, "could not compute signing root")
}
Expand All @@ -158,9 +156,7 @@ func VerifyBlockHeaderSigningRoot(blkHdr *ethpb.BeaconBlockHeader, pub []byte, s
if err != nil {
return errors.Wrap(err, "could not convert bytes to signature")
}
root, err := signingData(func() ([32]byte, error) {
return blkHdr.HashTreeRoot()
}, domain)
root, err := signingData(blkHdr.HashTreeRoot, domain)
if err != nil {
return errors.Wrap(err, "could not compute signing root")
}
Expand Down
15 changes: 9 additions & 6 deletions beacon-chain/forkchoice/protoarray/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ func TestComputeDelta_MoveOutOfTree(t *testing.T) {

indices[indexToHash(1)] = 0

votes = append(votes, Vote{indexToHash(1), params.BeaconConfig().ZeroHash, 0})
votes = append(votes, Vote{indexToHash(1), [32]byte{'A'}, 0})
votes = append(votes,
Vote{indexToHash(1), params.BeaconConfig().ZeroHash, 0},
Vote{indexToHash(1), [32]byte{'A'}, 0})

delta, _, err := computeDeltas(context.Background(), indices, votes, oldBalances, newBalances)
require.NoError(t, err)
Expand Down Expand Up @@ -201,8 +202,9 @@ func TestComputeDelta_ValidatorAppear(t *testing.T) {
indices[indexToHash(1)] = 0
indices[indexToHash(2)] = 1

votes = append(votes, Vote{indexToHash(1), indexToHash(2), 0})
votes = append(votes, Vote{indexToHash(1), indexToHash(2), 0})
votes = append(votes,
Vote{indexToHash(1), indexToHash(2), 0},
Vote{indexToHash(1), indexToHash(2), 0})

delta, _, err := computeDeltas(context.Background(), indices, votes, oldBalances, newBalances)
require.NoError(t, err)
Expand All @@ -225,8 +227,9 @@ func TestComputeDelta_ValidatorDisappears(t *testing.T) {
indices[indexToHash(1)] = 0
indices[indexToHash(2)] = 1

votes = append(votes, Vote{indexToHash(1), indexToHash(2), 0})
votes = append(votes, Vote{indexToHash(1), indexToHash(2), 0})
votes = append(votes,
Vote{indexToHash(1), indexToHash(2), 0},
Vote{indexToHash(1), indexToHash(2), 0})

delta, _, err := computeDeltas(context.Background(), indices, votes, oldBalances, newBalances)
require.NoError(t, err)
Expand Down
6 changes: 5 additions & 1 deletion beacon-chain/p2p/peers/peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func TestMain(m *testing.M) {
defer func() {
flags.Init(resetFlags)
}()
os.Exit(m.Run())
code := m.Run()
// os.Exit will prevent defer from being called
resetCfg()
flags.Init(resetFlags)
os.Exit(code)
}

// roundScore returns score rounded in accordance with the score manager's rounding factor.
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/p2p/peers/score_block_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sort"
"strconv"
"testing"

"github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -180,7 +181,7 @@ func TestPeerScorer_BlockProvider_WeightSorted(t *testing.T) {

var pids []peer.ID
for i := uint64(0); i < 10; i++ {
pid := peer.ID(i)
pid := peer.ID(strconv.FormatUint(i, 10))
scorer.IncrementProcessedBlocks(pid, i*batchSize)
pids = append(pids, pid)
}
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/p2p/peers/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ func TestBestFinalized_returnsMaxValue(t *testing.T) {
})

for i := 0; i <= maxPeers+100; i++ {
p.Add(new(enr.Record), peer.ID(i), nil, network.DirOutbound)
p.SetConnectionState(peer.ID(i), peers.PeerConnected)
p.SetChainState(peer.ID(i), &pb.Status{
p.Add(new(enr.Record), peer.ID(rune(i)), nil, network.DirOutbound)
p.SetConnectionState(peer.ID(rune(i)), peers.PeerConnected)
p.SetChainState(peer.ID(rune(i)), &pb.Status{
FinalizedEpoch: 10,
})
}
Expand All @@ -624,9 +624,9 @@ func TestStatus_BestNonFinalized(t *testing.T) {

peerSlots := []uint64{32, 32, 32, 32, 235, 233, 258, 268, 270}
for i, headSlot := range peerSlots {
p.Add(new(enr.Record), peer.ID(i), nil, network.DirOutbound)
p.SetConnectionState(peer.ID(i), peers.PeerConnected)
p.SetChainState(peer.ID(i), &pb.Status{
p.Add(new(enr.Record), peer.ID(rune(i)), nil, network.DirOutbound)
p.SetConnectionState(peer.ID(rune(i)), peers.PeerConnected)
p.SetChainState(peer.ID(rune(i)), &pb.Status{
HeadSlot: headSlot,
})
}
Expand Down
5 changes: 4 additions & 1 deletion beacon-chain/p2p/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ func (s *Service) pingPeers() {
func (s *Service) awaitStateInitialized() {
stateChannel := make(chan *feed.Event, 1)
stateSub := s.stateNotifier.StateFeed().Subscribe(stateChannel)
defer stateSub.Unsubscribe()
cleanup := stateSub.Unsubscribe
defer cleanup()
for {
select {
case event := <-stateChannel:
if event.Type == statefeed.Initialized {
data, ok := event.Data.(*statefeed.InitializedData)
if !ok {
// log.Fatalf will prevent defer from being called
cleanup()
log.Fatalf("Received wrong data over state initialized feed: %v", data)
}
s.genesisTime = data.StartTime
Expand Down
5 changes: 4 additions & 1 deletion beacon-chain/state/stateutil/stateutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ import (
func TestMain(m *testing.M) {
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableSSZCache: true})
defer resetCfg()
os.Exit(m.Run())
code := m.Run()
// os.Exit will prevent defer from being called
resetCfg()
os.Exit(code)
}
6 changes: 5 additions & 1 deletion beacon-chain/sync/initial-sync/initial_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ func TestMain(m *testing.M) {
defer func() {
flags.Init(resetFlags)
}()
os.Exit(m.Run())
code := m.Run()
// os.Exit will prevent defer from being called
resetCfg()
flags.Init(resetFlags)
os.Exit(code)
}

func initializeTestServices(t *testing.T, blocks []uint64, peers []*peerData) (*mock.ChainService, *p2pt.TestP2P, db.Database) {
Expand Down
7 changes: 2 additions & 5 deletions beacon-chain/sync/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,11 @@ func TestSortedObj_NoDuplicates(t *testing.T) {
slot := uint64(randFunc())
newBlk := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{Slot: slot}}
// append twice
blks = append(blks, newBlk)
blks = append(blks, newBlk)
blks = append(blks, newBlk, newBlk)

// append twice
root := bytesutil.ToBytes32(bytesutil.Bytes32(slot))
roots = append(roots, root)
roots = append(roots, root)

roots = append(roots, root, root)
}

r := &Service{}
Expand Down
5 changes: 4 additions & 1 deletion shared/aggregation/attestations/attestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func TestMain(m *testing.M) {
AttestationAggregationStrategy: string(MaxCoverAggregation),
})
defer resetCfg()
os.Exit(m.Run())
code := m.Run()
// os.Exit will prevent defer from being called
resetCfg()
os.Exit(code)
}

func TestAggregateAttestations_AggregatePair(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions shared/messagehandler/messagehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestSafelyHandleMessage(t *testing.T) {
hook := logTest.NewGlobal()

messagehandler.SafelyHandleMessage(nil, func(_ context.Context, _ proto.Message) error {
messagehandler.SafelyHandleMessage(context.Background(), func(_ context.Context, _ proto.Message) error {
panic("bad!")
return nil
}, &ethpb.BeaconBlock{})
Expand All @@ -26,7 +26,7 @@ func TestSafelyHandleMessage(t *testing.T) {
func TestSafelyHandleMessage_NoData(t *testing.T) {
hook := logTest.NewGlobal()

messagehandler.SafelyHandleMessage(nil, func(_ context.Context, _ proto.Message) error {
messagehandler.SafelyHandleMessage(context.Background(), func(_ context.Context, _ proto.Message) error {
panic("bad!")
return nil
}, nil)
Expand Down
2 changes: 0 additions & 2 deletions shared/slotutil/slotticker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ func TestGetSlotTickerWithOffset_OK(t *testing.T) {
t.Fatal("Expected normal ticker to tick first")
}
firstTicked = 1
break
}
}

}
10 changes: 8 additions & 2 deletions slasher/db/kv/proposer_slashings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,15 @@ func TestStore_SaveProposerSlashing(t *testing.T) {
proposerSlashings, err := db.ProposalSlashingsByStatus(ctx, tt.ss)
require.NoError(t, err, "Failed to get proposer slashings")

var diff string
if len(proposerSlashings) > 0 {
diff, _ = messagediff.PrettyDiff(proposerSlashings[0], tt.ps)
} else {
diff, _ = messagediff.PrettyDiff(nil, tt.ps)
}
t.Log(diff)

if proposerSlashings == nil || !reflect.DeepEqual(proposerSlashings[0], tt.ps) {
diff, _ := messagediff.PrettyDiff(proposerSlashings[0], tt.ps)
t.Log(diff)
t.Fatalf("Proposer slashing: %v should be part of proposer slashings response: %v", tt.ps, proposerSlashings)
}
}
Expand Down
1 change: 0 additions & 1 deletion tools/enr-calculator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ go_library(
visibility = ["//visibility:private"],
deps = [
"//shared/maxprocs:go_default_library",
"@com_github_btcsuite_btcd//btcec:go_default_library",
"@com_github_ethereum_go_ethereum//p2p/enode:go_default_library",
"@com_github_ethereum_go_ethereum//p2p/enr:go_default_library",
"@com_github_libp2p_go_libp2p_core//crypto:go_default_library",
Expand Down
5 changes: 2 additions & 3 deletions tools/enr-calculator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"net"

"github.com/btcsuite/btcd/btcec"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/libp2p/go-libp2p-core/crypto"
Expand All @@ -28,7 +27,7 @@ var (
func main() {
flag.Parse()

if len(*privateKey) == 0 {
if *privateKey == "" {
log.Fatal("No private key given")
}
dst, err := hex.DecodeString(*privateKey)
Expand All @@ -39,7 +38,7 @@ func main() {
if err != nil {
panic(err)
}
ecdsaPrivKey := (*ecdsa.PrivateKey)((*btcec.PrivateKey)(unmarshalledKey.(*crypto.Secp256k1PrivateKey)))
ecdsaPrivKey := (*ecdsa.PrivateKey)(unmarshalledKey.(*crypto.Secp256k1PrivateKey))

if net.ParseIP(*ipAddr).To4() == nil {
log.Fatalf("Invalid ipv4 address given: %v\n", err)
Expand Down
10 changes: 6 additions & 4 deletions tools/eth1exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ func MetricsHTTP(w http.ResponseWriter, r *http.Request) {
total.Add(total, bal)
allOut = append(allOut, fmt.Sprintf("%veth_balance{name=\"%v\",address=\"%v\"} %v", *prefix, v.Name, v.Address, v.Balance))
}
allOut = append(allOut, fmt.Sprintf("%veth_balance_total %0.18f", *prefix, total))
allOut = append(allOut, fmt.Sprintf("%veth_load_seconds %0.2f", *prefix, loadSeconds))
allOut = append(allOut, fmt.Sprintf("%veth_loaded_addresses %v", *prefix, totalLoaded))
allOut = append(allOut, fmt.Sprintf("%veth_total_addresses %v", *prefix, len(allWatching)))
allOut = append(allOut,
fmt.Sprintf("%veth_balance_total %0.18f", *prefix, total),
fmt.Sprintf("%veth_load_seconds %0.2f", *prefix, loadSeconds),
fmt.Sprintf("%veth_loaded_addresses %v", *prefix, totalLoaded),
fmt.Sprintf("%veth_total_addresses %v", *prefix, len(allWatching)))

if _, err := fmt.Fprintln(w, strings.Join(allOut, "\n")); err != nil {
logrus.WithError(err).Error("Failed to write metrics")
}
Expand Down
7 changes: 5 additions & 2 deletions tools/interop/convert-keys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ func main() {
if err != nil {
log.Fatalf("Failed to create file at %s: %v", os.Args[2], err)
}
defer func() {
cleanup := func() {
if err := outFile.Close(); err != nil {
panic(err)
}
}()
}
defer cleanup()
if err := keygen.SaveUnencryptedKeysToFile(outFile, out); err != nil {
// log.Fatalf will prevent defer from being called
cleanup()
log.Fatalf("Failed to save %v", err)
}
log.Printf("Wrote %s\n", os.Args[2])
Expand Down
5 changes: 4 additions & 1 deletion tools/relaynode/relaynode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ func main() {

ctx := context.Background()
log.Start(ctx, "main")
defer log.Finish(ctx)
cleanup := func() { log.Finish(ctx) }
defer cleanup()

srcMAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *port))
if err != nil {
// log.Fatalf will prevent defer from being called
cleanup()
log.Fatalf("Unable to construct multiaddr %v", err)
}

Expand Down
7 changes: 5 additions & 2 deletions tools/unencrypted-keys-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ func main() {
if err != nil {
log.Fatal(err)
}
defer func() {
cleanup := func() {
if err := file.Close(); err != nil {
log.Fatal(err)
}
}()
}
defer cleanup()

var ctnr *keygen.UnencryptedKeysContainer
if *random {
Expand All @@ -50,6 +51,8 @@ func main() {
ctnr = generateUnencryptedKeys(*startIndex)
}
if err := keygen.SaveUnencryptedKeysToFile(file, ctnr); err != nil {
// log.Fatal will prevent defer from being called
cleanup()
log.Fatal(err)
}
}
Expand Down
Loading