Skip to content

Commit d9e8636

Browse files
committed
multi: makes switches exhaustive or simplify
1 parent 8f40c64 commit d9e8636

File tree

12 files changed

+98
-112
lines changed

12 files changed

+98
-112
lines changed

asset/mock.go

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ func SignOutputRaw(priv *btcec.PrivateKey, tx *wire.MsgTx,
270270
signDesc.Output.Value, signDesc.Output.PkScript,
271271
leaf, signDesc.HashType, privKey,
272272
)
273+
default:
274+
// A witness V0 sign method should never appear here.
273275
}
274276
if err != nil {
275277
return nil, err

cmd/tapcli/addrs.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ var newAddrCommand = cli.Command{
6464
}
6565

6666
func newAddr(ctx *cli.Context) error {
67-
switch {
68-
case ctx.String(assetIDName) == "":
67+
if ctx.String(assetIDName) == "" {
6968
return cli.ShowSubcommandHelp(ctx)
7069
}
7170

cmd/tapcli/proofs.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ var verifyProofCommand = cli.Command{
5959
}
6060

6161
func verifyProof(ctx *cli.Context) error {
62-
switch {
63-
case ctx.String(proofPathName) == "":
62+
if ctx.String(proofPathName) == "" {
6463
return cli.ShowSubcommandHelp(ctx)
6564
}
6665

@@ -126,8 +125,7 @@ func decodeProof(ctx *cli.Context) error {
126125
client, cleanUp := getClient(ctx)
127126
defer cleanUp()
128127

129-
switch {
130-
case !ctx.IsSet(proofPathName):
128+
if !ctx.IsSet(proofPathName) {
131129
_ = cli.ShowCommandHelp(ctx, "decode")
132130
return nil
133131
}
@@ -176,8 +174,7 @@ var verifyOwnershipCommand = cli.Command{
176174
}
177175

178176
func verifyOwnershipProof(ctx *cli.Context) error {
179-
switch {
180-
case ctx.String(proofPathName) == "":
177+
if ctx.String(proofPathName) == "" {
181178
return cli.ShowSubcommandHelp(ctx)
182179
}
183180

cmd/tapcli/universe.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,7 @@ var universeProofInsertInsert = cli.Command{
516516
}
517517

518518
func universeProofInsert(ctx *cli.Context) error {
519-
switch {
520-
case ctx.String(proofPathName) == "":
519+
if ctx.String(proofPathName) == "" {
521520
return cli.ShowSubcommandHelp(ctx)
522521
}
523522

@@ -617,8 +616,7 @@ var universeSyncCommand = cli.Command{
617616
}
618617

619618
func universeSync(ctx *cli.Context) error {
620-
switch {
621-
case ctx.String(universeHostName) == "":
619+
if ctx.String(universeHostName) == "" {
622620
return cli.ShowSubcommandHelp(ctx)
623621
}
624622

@@ -721,8 +719,7 @@ var universeFederationAddCommand = cli.Command{
721719
}
722720

723721
func universeFederationAdd(ctx *cli.Context) error {
724-
switch {
725-
case ctx.String(universeHostName) == "":
722+
if ctx.String(universeHostName) == "" {
726723
return cli.ShowSubcommandHelp(ctx)
727724
}
728725

@@ -771,9 +768,9 @@ var universeFederationDelCommand = cli.Command{
771768
}
772769

773770
func universeFederationDel(ctx *cli.Context) error {
774-
switch {
775-
case ctx.String(universeHostName) == "" &&
776-
ctx.Int(universeServerID) == 0:
771+
if ctx.String(universeHostName) == "" &&
772+
ctx.Int(universeServerID) == 0 {
773+
777774
return cli.ShowSubcommandHelp(ctx)
778775
}
779776

commitment/commitment_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,16 @@ func TestMintAndDeriveTapCommitment(t *testing.T) {
450450

451451
var tapCommitment *TapCommitment
452452

453-
if includesAsset && includesAssetGroup {
453+
switch {
454+
case includesAsset && includesAssetGroup:
454455
tapCommitment, err = proof.DeriveByAssetInclusion(
455456
asset,
456457
)
457-
} else if includesAssetGroup {
458+
case !includesAsset && includesAssetGroup:
458459
tapCommitment, err = proof.DeriveByAssetExclusion(
459460
asset.AssetCommitmentKey(),
460461
)
461-
} else {
462+
case !includesAsset && !includesAssetGroup:
462463
tapCommitment, err = proof.DeriveByAssetCommitmentExclusion(
463464
asset.TapCommitmentKey(),
464465
)

itest/send_test.go

+30-82
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,9 @@ func testBasicSendUnidirectional(t *harnessTest) {
5454

5555
broadcastState := tapfreighter.SendStateBroadcast.String()
5656
targetEventSelector := func(event *taprpc.SendAssetEvent) bool {
57-
switch eventTyped := event.Event.(type) {
58-
case *taprpc.SendAssetEvent_ExecuteSendStateEvent:
59-
ev := eventTyped.ExecuteSendStateEvent
60-
61-
// Log send state execution.
62-
timestamp := time.UnixMicro(ev.Timestamp)
63-
t.Logf("Executing send state (%v): %v",
64-
timestamp.Format(time.RFC3339Nano),
65-
ev.SendState)
66-
67-
return ev.SendState == broadcastState
68-
}
69-
70-
return false
57+
return AssertSendEventExecuteSendState(
58+
t, event, broadcastState,
59+
)
7160
}
7261

7362
timeout := 2 * defaultProofTransferReceiverAckTimeout
@@ -168,20 +157,9 @@ func testRestartReceiverCheckBalance(t *harnessTest) {
168157

169158
broadcastState := tapfreighter.SendStateBroadcast.String()
170159
targetEventSelector := func(event *taprpc.SendAssetEvent) bool {
171-
switch eventTyped := event.Event.(type) {
172-
case *taprpc.SendAssetEvent_ExecuteSendStateEvent:
173-
ev := eventTyped.ExecuteSendStateEvent
174-
175-
// Log send state execution.
176-
timestamp := time.UnixMicro(ev.Timestamp)
177-
t.Logf("Executing send state (%v): %v",
178-
timestamp.Format(time.RFC3339Nano),
179-
ev.SendState)
180-
181-
return ev.SendState == broadcastState
182-
}
183-
184-
return false
160+
return AssertSendEventExecuteSendState(
161+
t, event, broadcastState,
162+
)
185163
}
186164

187165
timeout := 2 * defaultProofTransferReceiverAckTimeout
@@ -620,18 +598,9 @@ func testReattemptFailedSendHashmailCourier(t *harnessTest) {
620598
// Define a target event selector to match the backoff wait
621599
// event. This function selects for a specific event type.
622600
targetEventSelector := func(event *taprpc.SendAssetEvent) bool {
623-
switch eventTyped := event.Event.(type) {
624-
case *taprpc.SendAssetEvent_ProofTransferBackoffWaitEvent:
625-
ev := eventTyped.ProofTransferBackoffWaitEvent
626-
if ev.TransferType != transferTypeSend {
627-
return false
628-
}
629-
630-
t.Logf("Found event ntfs: %v", ev)
631-
return true
632-
}
633-
634-
return false
601+
return AssertSendEventProofTransferBackoffWaitTypeSend(
602+
t, event,
603+
)
635604
}
636605

637606
// Expected number of events is one less than the number of
@@ -727,18 +696,9 @@ func testReattemptFailedSendUniCourier(t *harnessTest) {
727696
// Define a target event selector to match the backoff wait
728697
// event. This function selects for a specific event type.
729698
targetEventSelector := func(event *taprpc.SendAssetEvent) bool {
730-
switch eventTyped := event.Event.(type) {
731-
case *taprpc.SendAssetEvent_ProofTransferBackoffWaitEvent:
732-
ev := eventTyped.ProofTransferBackoffWaitEvent
733-
if ev.TransferType != transferTypeSend {
734-
return false
735-
}
736-
737-
t.Logf("Found event ntfs: %v", ev)
738-
return true
739-
}
740-
741-
return false
699+
return AssertSendEventProofTransferBackoffWaitTypeSend(
700+
t, event,
701+
)
742702
}
743703

744704
// Expected number of events is one less than the number of
@@ -889,22 +849,21 @@ func testReattemptFailedReceiveUniCourier(t *harnessTest) {
889849
// Define a target event selector to match the backoff wait event. This
890850
// function selects for a specific event type.
891851
targetEventSelector := func(event *taprpc.ReceiveAssetEvent) bool {
892-
switch eventTyped := event.Event.(type) {
893-
case *taprpc.ReceiveAssetEvent_ProofTransferBackoffWaitEvent:
894-
ev := eventTyped.ProofTransferBackoffWaitEvent
895-
896-
// We are attempting to identify receive transfer types.
897-
// Skip the event if it is not a receiving transfer
898-
// type.
899-
if ev.TransferType != taprpc.ProofTransferType_PROOF_TRANSFER_TYPE_RECEIVE {
900-
return false
901-
}
852+
ev := event.GetProofTransferBackoffWaitEvent()
853+
if ev == nil {
854+
return false
855+
}
902856

903-
t.Logf("Found event ntfs: %v", ev)
904-
return true
857+
// We are attempting to identify receive transfer types.
858+
// Skip the event if it is not a receiving transfer
859+
// type.
860+
typeRecv := taprpc.ProofTransferType_PROOF_TRANSFER_TYPE_RECEIVE
861+
if ev.TransferType != typeRecv {
862+
return false
905863
}
906864

907-
return false
865+
t.Logf("Found event ntfs: %v", ev)
866+
return true
908867
}
909868

910869
// Expected minimum number of events to receive.
@@ -996,23 +955,12 @@ func testOfflineReceiverEventuallyReceives(t *harnessTest) {
996955
// Define a target event selector to match the backoff wait
997956
// event. This function selects for a specific event type.
998957
targetEventSelector := func(event *taprpc.SendAssetEvent) bool {
999-
switch eventTyped := event.Event.(type) {
1000-
case *taprpc.SendAssetEvent_ProofTransferBackoffWaitEvent:
1001-
ev := eventTyped.ProofTransferBackoffWaitEvent
1002-
1003-
// We're listening for events on the sender
1004-
// node. We therefore expect to receive
1005-
// deliver transfer type backoff wait events
1006-
// for sending transfers.
1007-
if ev.TransferType != transferTypeSend {
1008-
return false
1009-
}
1010-
1011-
t.Logf("Found event ntfs: %v", ev)
1012-
return true
1013-
}
1014-
1015-
return false
958+
// We're listening for events on the sender node. We
959+
// therefore expect to receive deliver transfer type
960+
// backoff wait events for sending transfers.
961+
return AssertSendEventProofTransferBackoffWaitTypeSend(
962+
t, event,
963+
)
1016964
}
1017965

1018966
// Lower bound number of proof delivery attempts.

itest/utils.go

+41
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,47 @@ func ParseGenInfo(t *testing.T, genInfo *taprpc.GenesisInfo) *asset.Genesis {
6060
return &parsedGenesis
6161
}
6262

63+
// AssertSendEventExecuteSendState asserts that the send asset event is an
64+
// ExecuteSendState event, and logs the event timestamp if so.
65+
func AssertSendEventExecuteSendState(t *harnessTest,
66+
event *taprpc.SendAssetEvent, broadcastState string) bool {
67+
68+
ev := event.GetExecuteSendStateEvent()
69+
if ev == nil {
70+
return false
71+
}
72+
73+
// Log send state execution.
74+
timestamp := time.UnixMicro(ev.Timestamp)
75+
t.Logf("Executing send state (%v): %v",
76+
timestamp.Format(time.RFC3339Nano),
77+
ev.SendState)
78+
79+
return ev.SendState == broadcastState
80+
}
81+
82+
// AssertSendEventProofTransferBackoffWait asserts that the send asset event is
83+
// a ProofTransferBackoffWait event, with the transfer type set as send.
84+
func AssertSendEventProofTransferBackoffWaitTypeSend(t *harnessTest,
85+
event *taprpc.SendAssetEvent) bool {
86+
87+
ev := event.GetProofTransferBackoffWaitEvent()
88+
if ev == nil {
89+
return false
90+
}
91+
92+
// We're listening for events on the sender node. We therefore expect to
93+
// receive deliver transfer type backoff wait events for sending
94+
// transfers.
95+
typeSend := taprpc.ProofTransferType_PROOF_TRANSFER_TYPE_SEND
96+
if ev.TransferType != typeSend {
97+
return false
98+
}
99+
100+
t.Logf("Found event ntfs: %v", ev)
101+
return true
102+
}
103+
63104
// MineBlocks mine 'num' of blocks and check that blocks are present in
64105
// node blockchain. numTxs should be set to the number of transactions
65106
// (excluding the coinbase) we expect to be included in the first mined block.

mssmt/tree_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func genTestStores(t *testing.T) map[string]makeTestTreeStoreFunc {
6868
}
6969

7070
func printStoreStats(t *testing.T, store mssmt.TreeStore) {
71-
switch s := store.(type) {
72-
case *mssmt.DefaultStore:
71+
s, ok := store.(*mssmt.DefaultStore)
72+
if ok {
7373
t.Logf("%s: %s", t.Name(), s.Stats())
7474
}
7575
}

proof/tx.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ func NewTxMerkleProof(txs []*wire.MsgTx, txIdx int) (*TxMerkleProof, error) {
6262
// If we are the left child, a right sibling may not
6363
// exist.
6464
hash := hashes[currentIdx+1]
65-
if hash != nil {
65+
switch {
66+
case hash != nil:
6667
sibling = *hash
67-
} else if hashes[currentIdx] == nil {
68-
return nil, errors.New("invalid merkle tree")
69-
} else {
68+
case hashes[currentIdx] != nil:
7069
sibling = *hashes[currentIdx]
70+
default:
71+
return nil, errors.New("invalid merkle tree")
7172
}
7273
} else {
7374
// If we are the right child, there'll always be a left

rfq/manager.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ func (m *Manager) handleIncomingMessage(incomingMsg rfqmsg.IncomingMsg) error {
296296
// messages that will be sent to a peer.
297297
func (m *Manager) handleOutgoingMessage(outgoingMsg rfqmsg.OutgoingMsg) error {
298298
// Perform type specific handling of the outgoing message.
299-
switch msg := outgoingMsg.(type) {
300-
case *rfqmsg.Accept:
299+
msg, ok := outgoingMsg.(*rfqmsg.Accept)
300+
if ok {
301301
// Before sending an accept message to a peer, inform the HTLC
302302
// order handler that we've accepted the quote request.
303303
m.orderHandler.RegisterChannelRemit(*msg)

rpcserver.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,7 @@ func (r *rpcServer) checkBalanceOverflow(ctx context.Context,
720720
func (r *rpcServer) ListAssets(ctx context.Context,
721721
req *taprpc.ListAssetRequest) (*taprpc.ListAssetResponse, error) {
722722

723-
switch {
724-
case req.IncludeSpent && req.IncludeLeased:
723+
if req.IncludeSpent && req.IncludeLeased {
725724
return nil, fmt.Errorf("cannot specify both include_spent " +
726725
"and include_leased")
727726
}

tapdb/migrations.go

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (m *migrationLogger) Printf(format string, v ...interface{}) {
5959
m.log.Errorf(format, v...)
6060
case btclog.LevelCritical:
6161
m.log.Criticalf(format, v...)
62+
case btclog.LevelOff:
6263
}
6364
}
6465

0 commit comments

Comments
 (0)