Skip to content

Commit 87bd6cc

Browse files
committed
refactor(ledger): put error at end of return
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent 8312b54 commit 87bd6cc

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

ledger/verify_block.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
//nolint:staticcheck
31-
func VerifyBlock(block BlockHexCbor) (error, bool, string, uint64, uint64) {
31+
func VerifyBlock(block BlockHexCbor) (bool, string, uint64, uint64, error) {
3232
headerCborHex := block.HeaderCbor
3333
epochNonceHex := block.Eta0
3434
bodyHex := block.BlockBodyCbor
@@ -40,49 +40,49 @@ func VerifyBlock(block BlockHexCbor) (error, bool, string, uint64, uint64) {
4040
// check is KES valid
4141
headerCborByte, headerDecodeError := hex.DecodeString(headerCborHex)
4242
if headerDecodeError != nil {
43-
return fmt.Errorf(
43+
return false, "", 0, 0, fmt.Errorf(
4444
"VerifyBlock: headerCborByte decode error, %v",
4545
headerDecodeError.Error(),
46-
), false, "", 0, 0
46+
)
4747
}
4848
header, headerUnmarshalError := NewBabbageBlockHeaderFromCbor(
4949
headerCborByte,
5050
)
5151
if headerUnmarshalError != nil {
52-
return fmt.Errorf(
52+
return false, "", 0, 0, fmt.Errorf(
5353
"VerifyBlock: header unmarshal error, %v",
5454
headerUnmarshalError.Error(),
55-
), false, "", 0, 0
55+
)
5656
}
5757
if header == nil {
58-
return errors.New("VerifyBlock: header returned empty"), false, "", 0, 0
58+
return false, "", 0, 0, errors.New("VerifyBlock: header returned empty")
5959
}
6060
isKesValid, errKes := VerifyKes(header, slotPerKesPeriod)
6161
if errKes != nil {
62-
return fmt.Errorf(
62+
return false, "", 0, 0, fmt.Errorf(
6363
"VerifyBlock: KES invalid, %v",
6464
errKes.Error(),
65-
), false, "", 0, 0
65+
)
6666
}
6767

6868
// check is VRF valid
6969
// Ref: https://github.com/IntersectMBO/ouroboros-consensus/blob/de74882102236fdc4dd25aaa2552e8b3e208448c/ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Praos.hs#L541
7070
epochNonceByte, epochNonceDecodeError := hex.DecodeString(epochNonceHex)
7171
if epochNonceDecodeError != nil {
72-
return fmt.Errorf(
72+
return false, "", 0, 0, fmt.Errorf(
7373
"VerifyBlock: epochNonceByte decode error, %v",
7474
epochNonceDecodeError.Error(),
75-
), false, "", 0, 0
75+
)
7676
}
7777
vrfBytes := header.Body.VrfKey[:]
7878
vrfResult := header.Body.VrfResult
7979
seed := MkInputVrf(int64(header.Body.Slot), epochNonceByte) // #nosec G115
8080
output, errVrf := VrfVerifyAndHash(vrfBytes, vrfResult.Proof, seed)
8181
if errVrf != nil {
82-
return fmt.Errorf(
82+
return false, "", 0, 0, fmt.Errorf(
8383
"VerifyBlock: vrf invalid, %v",
8484
errVrf.Error(),
85-
), false, "", 0, 0
85+
)
8686
}
8787
isVrfValid := bytes.Equal(output, vrfResult.Output)
8888

@@ -91,16 +91,16 @@ func VerifyBlock(block BlockHexCbor) (error, bool, string, uint64, uint64) {
9191
blockBodyHashHex := hex.EncodeToString(blockBodyHash[:])
9292
isBodyValid, isBodyValidError := VerifyBlockBody(bodyHex, blockBodyHashHex)
9393
if isBodyValidError != nil {
94-
return fmt.Errorf(
94+
return false, "", 0, 0, fmt.Errorf(
9595
"VerifyBlock: VerifyBlockBody error, %v",
9696
isBodyValidError.Error(),
97-
), false, "", 0, 0
97+
)
9898
}
9999
isValid = isKesValid && isVrfValid && isBodyValid
100100
vrfHex = hex.EncodeToString(vrfBytes)
101101
blockNo := header.Body.BlockNumber
102102
slotNo := header.Body.Slot
103-
return nil, isValid, vrfHex, blockNo, slotNo
103+
return isValid, vrfHex, blockNo, slotNo, nil
104104
}
105105

106106
func ExtractBlockData(
@@ -143,7 +143,7 @@ func ExtractBlockData(
143143
// These are copied from types.go
144144

145145
type BlockHexCbor struct {
146-
_ struct{} `cbor:",toarray"`
146+
cbor.StructAsArray
147147
Flag int
148148
HeaderCbor string
149149
Eta0 string

ledger/verify_block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestVerifyBlockBody(t *testing.T) {
5050

5151
for _, tc := range testCases {
5252
t.Run(tc.name, func(t *testing.T) {
53-
err, isValid, _, _, _ := VerifyBlock(tc.blockHexCbor)
53+
isValid, _, _, _, err := VerifyBlock(tc.blockHexCbor)
5454
if tc.expectedValid != isValid {
5555
t.Errorf("unexpected error: %v", err)
5656
}

0 commit comments

Comments
 (0)