Skip to content

Commit c6c2464

Browse files
fix panic and add logs (#869)
* Fix error messages and prevent panic
1 parent 982d939 commit c6c2464

File tree

9 files changed

+71
-44
lines changed

9 files changed

+71
-44
lines changed

mixins/pasteld_handler.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mixins
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"strings"
78
"time"
@@ -309,11 +310,12 @@ func (pt *PastelHandler) ValidateBurnTxID(ctx context.Context, burnTxnID string,
309310

310311
confirmationChn := pt.WaitConfirmation(ctx, burnTxnID,
311312
burnTxnConfirmations, 15*time.Second, true, estimatedFee, burnTxnPercentage)
312-
log.WithContext(ctx).Debug("waiting for confirmation")
313+
313314
select {
314315
case retErr := <-confirmationChn:
315316
if retErr != nil {
316-
log.WithContext(ctx).WithError(retErr).Errorf("validate preburn transaction validation")
317+
log.WithContext(ctx).WithError(retErr).WithField("txn-id", burnTxnID).WithField("fee", estimatedFee).
318+
WithField("percentage", burnTxnPercentage).Error("validate preburn transaction validation")
317319
err = errors.Errorf("validate preburn transaction validation :%w", retErr)
318320
return err
319321
}
@@ -359,27 +361,30 @@ func (pt *PastelHandler) verifyTxn(ctx context.Context,
359361
}
360362

361363
log.WithContext(ctx).Debug("Verifying Burn Txn")
362-
isTxnAmountOk := false
363364
isTxnAddressOk := false
364365

365366
reqBurnAmount := totalAmt * percent / 100
366367
for _, vout := range txn.Vout {
367-
if inRange(vout.Value, reqBurnAmount, 2.0) {
368-
isTxnAmountOk = true
369-
for _, addr := range vout.ScriptPubKey.Addresses {
370-
if addr == pt.GetBurnAddress() {
371-
isTxnAddressOk = true
372-
}
368+
for _, addr := range vout.ScriptPubKey.Addresses {
369+
if addr == pt.GetBurnAddress() {
370+
isTxnAddressOk = true
373371
}
374372
}
375-
}
376373

377-
if !isTxnAmountOk {
378-
return fmt.Errorf("invalid txn amount: %v, required amount: %f", txn.Vout, reqBurnAmount)
374+
if isTxnAddressOk {
375+
if !inRange(vout.Value, reqBurnAmount, 2.0) {
376+
data, _ := json.Marshal(txn)
377+
return fmt.Errorf("invalid transaction amount: %v, required minimum amount: %f - raw transaction details: %s", vout.Value, reqBurnAmount, string(data))
378+
}
379+
380+
break
381+
}
379382
}
380383

381384
if !isTxnAddressOk {
382-
return fmt.Errorf("invalid txn address %s", pt.GetBurnAddress())
385+
data, _ := json.Marshal(txn)
386+
return fmt.Errorf("invalid txn address -- correct address: %s - rawTxnData: %s - total-amount: %f - req-burn-amount: %f",
387+
pt.GetBurnAddress(), string(data), totalAmt, reqBurnAmount)
383388
}
384389

385390
return nil
@@ -422,7 +427,7 @@ func (pt *PastelHandler) WaitConfirmation(ctx context.Context, txid string, minC
422427
if txResult.Confirmations >= minConfirmation {
423428
if verifyBurnAmt {
424429
if err := pt.verifyTxn(ctx, txResult, totalAmt, percent); err != nil {
425-
log.WithContext(ctx).WithError(err).Error("txn verification failed")
430+
log.WithContext(ctx).WithField("txid", txid).WithError(err).Error("txn verification failed")
426431
ch <- err
427432
return
428433
}

pastel/jsonrpc/jsonrpc.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919

2020
const (
2121
jsonrpcVersion = "2.0"
22-
timeout = 50 * time.Second
22+
timeout = 70 * time.Second
23+
httpTimeout = 60 * time.Second
2324
)
2425

2526
// RPCClient sends JSON-RPC requests over HTTP to the provided JSON-RPC backend.
@@ -310,8 +311,10 @@ func NewClient(endpoint string) RPCClient {
310311
// opts: RPCClientOpts provide custom configuration
311312
func NewClientWithOpts(endpoint string, opts *RPCClientOpts) RPCClient {
312313
rpcClient := &rpcClient{
313-
endpoint: endpoint,
314-
httpClient: &http.Client{},
314+
endpoint: endpoint,
315+
httpClient: &http.Client{
316+
Timeout: httpTimeout,
317+
},
315318
customHeaders: make(map[string]string),
316319
}
317320

supernode/services/common/reg_task_helper.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88
"time"
99

10+
json "github.com/json-iterator/go"
1011
"github.com/pastelnetwork/gonode/common/blocktracker"
1112
"github.com/pastelnetwork/gonode/common/errors"
1213
"github.com/pastelnetwork/gonode/common/log"
@@ -213,7 +214,7 @@ func (h *RegTaskHelper) WaitConfirmation(ctx context.Context, txid string, minCo
213214
if txResult.Confirmations >= minConfirmation {
214215
if verifyBurnAmt {
215216
if err := h.verifyTxn(ctx, txResult, totalAmt, percent); err != nil {
216-
log.WithContext(ctx).WithError(err).Error("txn verification failed")
217+
log.WithContext(ctx).WithField("txid", txid).WithError(err).Error("txn verification failed")
217218
ch <- err
218219
return
219220
}
@@ -252,28 +253,29 @@ func (h *RegTaskHelper) verifyTxn(ctx context.Context,
252253
return val >= lower
253254
}
254255

255-
log.WithContext(ctx).Debug("Verifying Burn Txn")
256-
isTxnAmountOk := false
257256
isTxnAddressOk := false
258-
259257
reqBurnAmount := totalAmt * percent / 100
260258
for _, vout := range txn.Vout {
261-
if inRange(vout.Value, reqBurnAmount, 2.0) {
262-
isTxnAmountOk = true
263-
for _, addr := range vout.ScriptPubKey.Addresses {
264-
if addr == h.PastelHandler.GetBurnAddress() {
265-
isTxnAddressOk = true
266-
}
259+
for _, addr := range vout.ScriptPubKey.Addresses {
260+
if addr == h.PastelHandler.GetBurnAddress() {
261+
isTxnAddressOk = true
267262
}
268263
}
269-
}
270264

271-
if !isTxnAmountOk {
272-
return fmt.Errorf("invalid txn amount: %v, required amount: %f", txn.Vout, reqBurnAmount)
265+
if isTxnAddressOk {
266+
if !inRange(vout.Value, reqBurnAmount, 2.0) {
267+
data, _ := json.Marshal(txn)
268+
return fmt.Errorf("invalid transaction amount: %v, required minimum amount : %f - raw transaction details: %s", vout.Value, reqBurnAmount, string(data))
269+
}
270+
271+
break
272+
}
273273
}
274274

275275
if !isTxnAddressOk {
276-
return fmt.Errorf("invalid txn address %s", h.PastelHandler.GetBurnAddress())
276+
data, _ := json.Marshal(txn)
277+
return fmt.Errorf("invalid txn address -- correct address: %s - rawTxnData: %s - total-amount: %f - req-burn-amount: %f",
278+
h.PastelHandler.GetBurnAddress(), string(data), totalAmt, reqBurnAmount)
277279
}
278280

279281
return nil

walletnode/api/services/cascade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ func (service *CascadeAPIHandler) UploadAsset(ctx context.Context, p *cascade.Up
6868
log.WithError(err).Error("error storing File")
6969
return nil, cascade.MakeInternalServerError(err)
7070
}
71-
log.Infof("file has been uploaded: %s", id)
71+
log.WithField("file-id", id).WithField("filename", *p.Filename).Info("file has been uploaded")
7272

7373
fee, err := service.register.CalculateFee(ctx, id)
7474
if err != nil {
7575
log.WithError(err).Error("error calculating fee")
7676
return nil, cascade.MakeInternalServerError(err)
7777
}
78-
log.Infof("estimated fee has been calculated: %f", fee)
78+
log.WithField("file-id", id).WithField("filename", *p.Filename).Infof("estimated fee has been calculated: %f", fee)
7979

8080
res = &cascade.Asset{
8181
FileID: id,

walletnode/api/services/nft.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ func (service *NftAPIHandler) UploadImage(ctx context.Context, p *nft.UploadImag
8989
return nil, nft.MakeInternalServerError(err)
9090
}
9191

92-
log.Infof("file has been uploaded: %s", id)
92+
log.WithField("file-id", id).WithField("filename", *p.Filename).Info("file has been uploaded")
9393

9494
fee, err := service.register.CalculateFee(ctx, id)
9595
if err != nil {
9696
log.WithError(err).Error("error calculating fee")
9797
return nil, nft.MakeInternalServerError(err)
9898
}
9999

100-
log.Infof("estimated fee has been calculated: %f", fee)
100+
log.WithField("file-id", id).WithField("filename", *p.Filename).Infof("estimated fee has been calculated: %f", fee)
101101

102102
res = &nft.ImageRes{
103103
ImageID: id,

walletnode/api/services/sense.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ func (service *SenseAPIHandler) UploadImage(ctx context.Context, p *sense.Upload
6464
log.WithError(err).Error("error storing file")
6565
return nil, sense.MakeInternalServerError(err)
6666
}
67-
log.Infof("file has been uploaded: %s", id)
67+
log.WithField("file-id", id).WithField("filename", *p.Filename).Info("file has been uploaded")
6868

6969
fee, err := service.register.CalculateFee(ctx, id)
7070
if err != nil {
7171
log.WithError(err).Error("error calculating fee")
7272
return nil, sense.MakeInternalServerError(err)
7373
}
74-
log.Infof("estimated fee has been calculated: %f", fee)
74+
log.WithField("file-id", id).WithField("filename", *p.Filename).Infof("estimated fee has been calculated: %f", fee)
7575

7676
res = &sense.Image{
7777
ImageID: id,

walletnode/node/grpc/register_cascade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (service *registerCascade) UploadAsset(ctx context.Context, asset *files.Fi
181181

182182
file, err := asset.Open()
183183
if err != nil {
184-
return errors.Errorf("open file %q: %w", file.Name(), err)
184+
return errors.Errorf("error opening file %q: %w", asset.Name(), err)
185185
}
186186
defer file.Close()
187187

walletnode/services/cascaderegister/task.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cascaderegister
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"time"
78

@@ -350,22 +351,31 @@ func (task *CascadeRegistrationTask) uploadImage(ctx context.Context) error {
350351
group, gctx := errgroup.WithContext(ctx)
351352

352353
for _, someNode := range task.MeshHandler.Nodes {
354+
if someNode == nil {
355+
return fmt.Errorf("node is nil - list of nodes: %s", task.MeshHandler.Nodes.String())
356+
}
357+
353358
cascadeNode, ok := someNode.SuperNodeAPIInterface.(*CascadeRegistrationNode)
354359
if !ok {
355360
//TODO: use assert here
356361
return errors.Errorf("node %s is not CascadeRegistrationNode", someNode.String())
357362
}
358363

359364
someNode := someNode
360-
if someNode == nil {
361-
log.WithContext(gctx).Debug("someNode is nil")
362-
}
363365

364366
image := task.Request.Image
367+
if image == nil {
368+
return errors.New("image is found to be nil")
369+
}
370+
371+
if cascadeNode == nil {
372+
return errors.New("cascadeNode is found to be nil")
373+
}
374+
365375
group.Go(func() error {
366376
err := cascadeNode.UploadAsset(gctx, image)
367377
if err != nil {
368-
log.WithContext(gctx).WithError(err).WithField("node", cascadeNode).Error("upload image with thumbnail failed")
378+
log.WithContext(gctx).WithError(err).WithField("node", someNode.String()).Error("upload image with thumbnail failed")
369379
return err
370380
}
371381
return nil

walletnode/services/nftregister/task.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,10 @@ func (task *NftRegistrationTask) uploadImageWithThumbnail(ctx context.Context, f
694694
task.ImageHandler.ClearHashes()
695695

696696
for _, someNode := range task.MeshHandler.Nodes {
697+
if someNode == nil {
698+
return fmt.Errorf("node is nil - list of nodes: %s", task.MeshHandler.Nodes.String())
699+
}
700+
697701
nftRegNode, ok := someNode.SuperNodeAPIInterface.(*NftRegistrationNode)
698702
if !ok {
699703
//TODO: use assert here
@@ -704,7 +708,7 @@ func (task *NftRegistrationTask) uploadImageWithThumbnail(ctx context.Context, f
704708
group.Go(func() error {
705709
hash1, hash2, hash3, err := nftRegNode.UploadImageWithThumbnail(gctx, file, thumbnail)
706710
if err != nil {
707-
log.WithContext(gctx).WithError(err).WithField("node", someNode).Error("upload image with thumbnail failed")
711+
log.WithContext(gctx).WithError(err).WithField("node", someNode.String()).Error("upload image with thumbnail failed")
708712
return err
709713
}
710714
task.ImageHandler.AddNewHashes(hash1, hash2, hash3, someNode.PastelID())
@@ -878,10 +882,14 @@ func (task *NftRegistrationTask) preburnRegistrationFeeGetTicketTxid(ctx context
878882
if err != nil {
879883
return fmt.Errorf("burn some coins: %w", err)
880884
}
885+
log.WithContext(ctx).WithField("burn_txid", burnTxid).Info("burn txn has been created")
881886
} else {
887+
log.WithContext(ctx).WithField("burn_txid", burnTxid).Info("burn txid has been provided in the request for NFT registration")
882888
burnTxid = *task.Request.BurnTxID
883889
}
884890

891+
task.StatusLog[common.FieldBurnTxnID] = burnTxid
892+
885893
log.WithContext(ctx).Info("validating burn transaction")
886894
task.UpdateStatus(common.StatusValidateBurnTxn)
887895
if err := task.service.pastelHandler.WaitTxidValid(ctx, burnTxid, 3,
@@ -893,7 +901,6 @@ func (task *NftRegistrationTask) preburnRegistrationFeeGetTicketTxid(ctx context
893901
task.UpdateStatus(common.StatusBurnTxnValidated)
894902
log.WithContext(ctx).Info("burn txn has been validated")
895903

896-
task.StatusLog[common.FieldBurnTxnID] = burnTxid
897904
group, gctx := errgroup.WithContext(ctx)
898905
for _, someNode := range task.MeshHandler.Nodes {
899906
nftRegNode, ok := someNode.SuperNodeAPIInterface.(*NftRegistrationNode)

0 commit comments

Comments
 (0)