Skip to content

Commit eb7601e

Browse files
committed
chore: improve attestation request handling logging
This makes it easier to debug verifier connectivity/setup issues.
1 parent ebd1a09 commit eb7601e

File tree

9 files changed

+110
-75
lines changed

9 files changed

+110
-75
lines changed

client/attestation/attestation.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package attestation
22

33
import (
4-
"bytes"
54
"context"
65
"encoding/hex"
7-
"errors"
86
"fmt"
97
"math"
108
"math/big"
119
"sync"
1210

11+
"github.com/flare-foundation/fdc-client/client/utils"
1312
"github.com/flare-foundation/go-flare-common/pkg/contracts/fdchub"
1413
"github.com/flare-foundation/go-flare-common/pkg/database"
1514
"github.com/flare-foundation/go-flare-common/pkg/events"
1615
"github.com/flare-foundation/go-flare-common/pkg/logger"
1716
"github.com/flare-foundation/go-flare-common/pkg/priority"
17+
"github.com/pkg/errors"
1818

1919
bitvotes "github.com/flare-foundation/fdc-client/client/attestation/bitVotes"
2020
"github.com/flare-foundation/fdc-client/client/config"
@@ -175,18 +175,18 @@ func (a *Attestation) Handle(ctx context.Context) error {
175175
responseBytes, confirmed, err := ResolveAttestationRequest(ctx, a)
176176
if err != nil {
177177
a.Status = ProcessError
178-
return fmt.Errorf("handle, resolve request: %s", err)
178+
return errors.Wrap(err, "unable to resolve attestation request")
179179
}
180180
if !confirmed {
181181
a.Status = Unconfirmed
182-
logger.Debugf("unconfirmed request: ")
182+
logger.Debugf("attestation request %s for round %d successfully verified but not confirmed", a.Request.TypeAndSourceString(), a.RoundID)
183183
return nil
184184
}
185185

186186
a.Response = responseBytes
187187
err = a.validateResponse()
188188
if err != nil {
189-
return fmt.Errorf("handle, validate response: %s", err)
189+
return errors.Wrap(err, "unable to validate attestation response")
190190
}
191191

192192
return nil
@@ -209,7 +209,7 @@ func (a *Attestation) PrepareRequest(attestationTypesConfigs config.AttestationT
209209
attestationTypeConfig, ok := attestationTypesConfigs[attType]
210210
if !ok {
211211
a.Status = UnsupportedPair
212-
return fmt.Errorf("prepare request: no configs for: %s", string(bytes.Trim(attType[:], "\x00")))
212+
return fmt.Errorf("prepare request: no configs for: %s", utils.Bytes32ToString(attType))
213213
}
214214

215215
a.ResponseABI = &attestationTypeConfig.ResponseArguments
@@ -218,7 +218,7 @@ func (a *Attestation) PrepareRequest(attestationTypesConfigs config.AttestationT
218218
sourceConfig, ok := attestationTypeConfig.SourcesConfig[source]
219219
if !ok {
220220
a.Status = UnsupportedPair
221-
return fmt.Errorf("prepare request: no configs for: %s, %s", string(bytes.Trim(attType[:], "\x00")), string(bytes.Trim(source[:], "\x00")))
221+
return fmt.Errorf("prepare request: no configs for: %s, %s", utils.Bytes32ToString(attType), utils.Bytes32ToString(source))
222222
}
223223

224224
a.LUTLimit = sourceConfig.LUTLimit

client/attestation/verification.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ethereum/go-ethereum/accounts/abi"
1111
"github.com/ethereum/go-ethereum/common"
1212
"github.com/ethereum/go-ethereum/crypto"
13+
"github.com/flare-foundation/fdc-client/client/utils"
1314

1415
"github.com/flare-foundation/go-flare-common/pkg/logger"
1516
)
@@ -51,6 +52,23 @@ func (r Request) Source() ([32]byte, error) {
5152
return res, nil
5253
}
5354

55+
// TypeAndSourceString returns the attestation type and source name for debugging
56+
func (r Request) TypeAndSourceString() string {
57+
attType, err := r.AttestationType()
58+
attTypeString := "unknown_type"
59+
if err == nil {
60+
attTypeString = utils.Bytes32ToString(attType)
61+
}
62+
63+
source, err := r.Source()
64+
sourceString := "unknown_source"
65+
if err == nil {
66+
sourceString = utils.Bytes32ToString(source)
67+
}
68+
69+
return fmt.Sprintf("%s/%s", attTypeString, sourceString)
70+
}
71+
5472
// MIC returns Message Integrity code of the request (the third 32 bytes).
5573
func (r Request) MIC() (common.Hash, error) {
5674
if len(r) < 96 {

client/attestation/verification_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ func TestRequest(t *testing.T) {
149149
copy(expectedAttTypeAndSource[:], []byte(test.attType))
150150
copy(expectedAttTypeAndSource[32:], []byte(test.source))
151151

152+
// type and source string
153+
require.Equal(t, fmt.Sprintf("%s/%s", test.attType, test.source), req.TypeAndSourceString(), fmt.Sprintf("error type and source string in test %d", i))
154+
152155
// mic
153156
expectedMic := common.HexToHash(test.mic)
154157
mic, err := req.MIC()

client/attestation/verifiers.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"net/http"
1111
"strings"
1212
"time"
13+
14+
"github.com/pkg/errors"
1315
)
1416

1517
const timeout = 5 * time.Second // maximal duration for the verifier to resolve the query
@@ -40,19 +42,19 @@ func ResolveAttestationRequest(ctx context.Context, att *Attestation) ([]byte, b
4042

4143
encodedBody, err := json.Marshal(payload)
4244
if err != nil {
43-
return nil, false, err
45+
return nil, false, errors.Wrap(err, "failed to encode request body")
4446
}
4547

4648
request, err := http.NewRequestWithContext(ctx, "POST", att.Credentials.URL, bytes.NewBuffer(encodedBody))
4749
if err != nil {
48-
return nil, false, err
50+
return nil, false, errors.Wrap(err, "failed to create http request")
4951
}
5052
request.Header.Set("Content-Type", "application/json")
5153
request.Header.Set("X-API-KEY", att.Credentials.apiKey)
5254

5355
resp, err := client.Do(request)
5456
if err != nil {
55-
return nil, false, err
57+
return nil, false, errors.Wrap(err, "failed to send http request")
5658
}
5759
if resp.StatusCode != http.StatusOK {
5860
return nil, false, fmt.Errorf("request responded with code %d", resp.StatusCode)
@@ -69,15 +71,15 @@ func ResolveAttestationRequest(ctx context.Context, att *Attestation) ([]byte, b
6971

7072
err = decoder.Decode(&responseBody)
7173
if err != nil {
72-
return nil, false, err
74+
return nil, false, errors.Wrap(err, "failed to decode response body")
7375
}
7476
if responseBody.Status != ValidResponseStatus {
7577
return nil, false, nil
7678
}
7779

7880
responseBytes, err := hex.DecodeString(strings.TrimPrefix(responseBody.ABIEncodedResponse, "0x"))
7981
if err != nil {
80-
return nil, false, err
82+
return nil, false, errors.Wrap(err, "failed to decode ABI encoded response")
8183
}
8284

8385
return responseBytes, true, nil

client/manager/queues.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/flare-foundation/fdc-client/client/config"
88
"github.com/flare-foundation/go-flare-common/pkg/logger"
99
"github.com/flare-foundation/go-flare-common/pkg/priority"
10+
"github.com/pkg/errors"
1011
)
1112

1213
type attestationQueue = priority.PriorityQueue[*attestation.Attestation, attestation.Weight]
@@ -28,7 +29,13 @@ func buildQueues(queuesConfigs config.Queues) attestationQueues {
2829

2930
// handler handles dequeued attestation.
3031
func handler(ctx context.Context, at *attestation.Attestation) error {
31-
return at.Handle(ctx)
32+
err := at.Handle(ctx)
33+
if err != nil {
34+
wrapped := errors.Wrapf(err, "attestation request %s for round %d failed", at.Request.TypeAndSourceString(), at.RoundID)
35+
logger.Info(wrapped.Error())
36+
return wrapped
37+
}
38+
return nil
3239
}
3340

3441
// discard discards requests that do not need to be handled.

client/round/round.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (r *Round) MerkleTree() (merkle.Tree, error) {
155155
for i := range r.Attestations {
156156
if r.Attestations[i].Consensus {
157157
if r.Attestations[i].Status != attestation.Success {
158-
return merkle.Tree{}, errors.Errorf("attestation %d in consensus but not confirmed", i)
158+
return merkle.Tree{}, errors.Errorf("attestation %s, at index %d in consensus but not confirmed", r.Attestations[i].Request.TypeAndSourceString(), i)
159159
}
160160

161161
hashes = append(hashes, r.Attestations[i].Hash)

client/utils/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package utils
22

3+
import (
4+
"bytes"
5+
)
6+
37
// prepend places the element at the beginning of the slice and moves the potentially replaced element to the end.
48
func Prepend[T any](slice []T, element T) []T {
59
if len(slice) == 0 {
@@ -38,3 +42,7 @@ func Invert[K comparable, V comparable](m map[K]V) map[V]K {
3842

3943
return invertedMap
4044
}
45+
46+
func Bytes32ToString(b [32]byte) string {
47+
return string(bytes.Trim(b[:], "\x00"))
48+
}

go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@ require (
2525
github.com/bits-and-blooms/bitset v1.24.3 // indirect
2626
github.com/buger/jsonparser v1.1.1 // indirect
2727
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
28-
github.com/consensys/bavard v0.2.1 // indirect
2928
github.com/consensys/gnark-crypto v0.19.2 // indirect
3029
github.com/crate-crypto/go-eth-kzg v1.4.0 // indirect
3130
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
32-
github.com/crate-crypto/go-kzg-4844 v1.1.0 // indirect
3331
github.com/davecgh/go-spew v1.1.1 // indirect
3432
github.com/davidebianchi/gswagger v0.10.0 // indirect
3533
github.com/deckarep/golang-set/v2 v2.7.0 // indirect
3634
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
37-
github.com/ethereum/c-kzg-4844 v1.0.3 // indirect
3835
github.com/ethereum/c-kzg-4844/v2 v2.1.5 // indirect
3936
github.com/ethereum/go-verkle v0.2.2 // indirect
4037
github.com/fsnotify/fsnotify v1.8.0 // indirect
@@ -61,7 +58,6 @@ require (
6158
github.com/mailru/easyjson v0.9.0 // indirect
6259
github.com/mattn/go-sqlite3 v1.14.22 // indirect
6360
github.com/mitchellh/mapstructure v1.5.0 // indirect
64-
github.com/mmcloughlin/addchain v0.4.0 // indirect
6561
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
6662
github.com/perimeterx/marshmallow v1.1.5 // indirect
6763
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -85,9 +81,9 @@ require (
8581
golang.org/x/sys v0.37.0 // indirect
8682
golang.org/x/text v0.30.0 // indirect
8783
golang.org/x/time v0.9.0 // indirect
84+
golang.org/x/tools/godoc v0.1.0-deprecated // indirect
8885
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
8986
gopkg.in/yaml.v2 v2.4.0 // indirect
9087
gopkg.in/yaml.v3 v3.0.1 // indirect
9188
gorm.io/driver/mysql v1.5.7 // indirect
92-
rsc.io/tmplfunc v0.0.3 // indirect
9389
)

0 commit comments

Comments
 (0)