Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #156 from JackalLabs/fix-proof-unsupported-key-type
Browse files Browse the repository at this point in the history
prevent files from burning when rpc is down
  • Loading branch information
TheMarstonConnell authored Apr 25, 2024
2 parents b4273ee + 59cdbc2 commit 876b26e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 49 deletions.
13 changes: 3 additions & 10 deletions jprov/server/file_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,13 @@ func (f *FileServer) RecollectActiveDeals() error {
return err
}

count := 0

for _, q := range queryActiveDeals {
_, err := f.archivedb.GetFid(q.Cid)
if errors.Is(err, archive.ErrContractNotFound) {
err = f.archivedb.SetContract(q.Cid, q.Fid)
count++
if err != nil {
return err
}
err := f.archivedb.SetContract(q.Cid, q.Fid)
if err != nil && !errors.Is(err, archive.ErrContractAlreadyExists) {
return err
}
}

f.logger.Info(fmt.Sprintf("recollected deals: %d\n", count))
return nil
}

Expand Down
37 changes: 18 additions & 19 deletions jprov/server/proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,14 @@ func (f *FileServer) ContractState(cid string) string {
return f.QueryContractState(cid)
}

func (f *FileServer) Prove(cid string) error {
block, err := queryBlock(&f.cosmosCtx, string(cid))
if err != nil {
return err
}

dex, ok := sdk.NewIntFromString(block)
f.logger.Debug(fmt.Sprintf("BlockToProve: %s", block))
func (f *FileServer) Prove(deal storageTypes.ActiveDeals) error {
dex, ok := sdk.NewIntFromString(deal.Blocktoprove)
f.logger.Debug(fmt.Sprintf("BlockToProve: %s", deal.Blocktoprove))
if !ok {
return fmt.Errorf("failed to parse block number: %s", block)
return fmt.Errorf("failed to parse block number: %s", deal.Blocktoprove)
}

return f.postProof(string(cid), f.blockSize, dex.Int64())
return f.postProof(deal.Cid, f.blockSize, dex.Int64())
}

func (f *FileServer) handleContracts() error {
Expand All @@ -405,31 +400,34 @@ func (f *FileServer) handleContracts() error {
}

f.logger.Info(fmt.Sprintf("CID: %s FID: %s", cid, fid))
resp, respErr := f.QueryActiveDeal(cid)

switch state := f.QueryContractState(cid); state {
case verified:
switch state, err := types.ContractState(resp, respErr); state {
case types.Verified:
err := f.DeleteDowntime(cid)
if err != nil {
f.logger.Error("error when unmarking downtime cid: ", cid, ": ", err)
f.logger.Error(fmt.Sprintf("error when unmarking downtime cid: %s: %v", cid, err))
}
continue
case notFound:
case types.NotFound:
err := f.IncrementDowntime(cid)
if err != nil {
return err
}
case notVerified:
case types.NotVerified:
err := f.DeleteDowntime(cid)
if err != nil {
f.logger.Error("error when unmarking downtime cid: ", cid, ": ", err)
f.logger.Error(fmt.Sprintf("error when unmarking downtime cid: %s: %v", cid, err))
}

err = f.Prove(cid)
err = f.Prove(resp.ActiveDeals)
if err != nil {
f.logger.Error("failed to prove ", cid, ": ", err)
f.logger.Error(fmt.Sprintf("failed to prove: %s: %v", cid, err))
}
case types.Error:
f.logger.Error(fmt.Sprintf("query error: %v", err))
default:
f.logger.Error("unknown state to handle: ", state)
return fmt.Errorf("unkown state: %v %v", state, err)
}
}
return nil
Expand All @@ -452,6 +450,7 @@ func (f *FileServer) StartProofServer(interval uint16) {
for {
select {
case <-sigChan:
fmt.Println("shutting down proof server")
return
default:
start := time.Now()
Expand Down
6 changes: 5 additions & 1 deletion jprov/server/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
notFound = "not found"
)

// returns verified, notVerified or notFound
func (f *FileServer) QueryContractState(cid string) string {
req := storageTypes.QueryActiveDealRequest{Cid: cid}
resp, err := f.queryClient.ActiveDeals(f.cmd.Context(), &req)
Expand Down Expand Up @@ -136,3 +135,8 @@ func (f *FileServer) QueryMyActiveDeals() ([]storageTypes.ActiveDeals, error) {

return filterMyActiveDeals(activeDeals, f.provider.Address), nil
}

func (f *FileServer) QueryActiveDeal(cid string) (*storageTypes.QueryActiveDealResponse, error) {
req := storageTypes.QueryActiveDealRequest{Cid: cid}
return f.queryClient.ActiveDeals(f.cmd.Context(), &req)
}
19 changes: 0 additions & 19 deletions jprov/server/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server

import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
Expand All @@ -12,7 +11,6 @@ import (

"github.com/JackalLabs/jackal-provider/jprov/types"
"github.com/JackalLabs/jackal-provider/jprov/utils"
"github.com/cosmos/cosmos-sdk/client"
storageTypes "github.com/jackalLabs/canine-chain/v3/x/storage/types"
)

Expand Down Expand Up @@ -78,23 +76,6 @@ func testConnection(providers []storageTypes.Providers, ip string) bool {
return true
}

func queryBlock(clientCtx *client.Context, cid string) (string, error) {
queryClient := storageTypes.NewQueryClient(clientCtx)

argCid := cid

params := &storageTypes.QueryActiveDealRequest{
Cid: argCid,
}

res, err := queryClient.ActiveDeals(context.Background(), params)
if err != nil {
return "", err
}

return res.ActiveDeals.Blocktoprove, nil
}

func buildCid(address, sender, fid string) (string, error) {
h := sha256.New()

Expand Down
46 changes: 46 additions & 0 deletions jprov/types/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package types

import (
"strconv"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

storageTypes "github.com/jackalLabs/canine-chain/v3/x/storage/types"
)

type VerifiedStatus int

const (
Verified VerifiedStatus = iota
NotVerified
NotFound
Error
)

// Returns state of the active deal contract based on query response.
// Returns Error status with non-nil error if respErr is unknown code or parsing resp failed.
func ContractState(resp *storageTypes.QueryActiveDealResponse, respErr error) (VerifiedStatus, error) {
if respErr != nil {
stat, ok := status.FromError(respErr)
if !ok { // unknown grpc error
return Error, respErr
}

if codes.NotFound == stat.Code() {
return NotFound, nil
}

return Error, respErr
}

verified, err := strconv.ParseBool(resp.ActiveDeals.Proofverified)
if err != nil {
return Error, err
}

if verified {
return Verified, err
}
return NotVerified, err
}

0 comments on commit 876b26e

Please sign in to comment.