diff --git a/LHDealEngine.go b/LHDealEngine.go deleted file mode 100644 index 52625c0..0000000 --- a/LHDealEngine.go +++ /dev/null @@ -1,228 +0,0 @@ -package main - -import ( - "encoding/hex" - "encoding/json" - "fmt" - "io" - "log" - "net/http" - "strconv" - "time" - - "github.com/filecoin-project/go-data-segment/merkletree" - "github.com/ipfs/go-cid" -) - -const dealEngineURL = "https://calibration.lighthouse.storage" - -// Define a struct for the response -type ProofData struct { - CID string `json:"cid"` - PieceCID string `json:"piece_cid"` - FileProofs []struct { - InclusionProof struct { - ProofIndex struct { - Index string `json:"index"` - Path []string `json:"path"` - } `json:"proofIndex"` - ProofSubtree struct { - Index string `json:"index"` - Path []string `json:"path"` - } `json:"proofSubtree"` - } `json:"inclusionProof"` - IndexRecord struct { - ProofIndex string `json:"proofIndex"` - ProofSubtree int `json:"proofSubtree"` - Size int `json:"size"` - Checksum string `json:"checksum"` - } `json:"indexRecord"` - VerifierData struct { - CommPc string `json:"commPc"` - SizePc string `json:"sizePc"` - } `json:"verifierData"` - } `json:"file_proofs"` - LastUpdate int64 `json:"last_update"` -} - -type FilecoinDeal struct { - DealUUID string `json:"dealUUID"` - AggregateIn string `json:"aggregateIn"` - StorageProvider string `json:"storageProvider"` - StartEpoch int `json:"startEpoch"` - EndEpoch int `json:"endEpoch"` - ProviderCollateral string `json:"providerCollateral"` - PublishCID string `json:"publishCID"` - ChainDealID int `json:"chainDealID"` - DealStatus string `json:"dealStatus"` - PrevDealID int `json:"prevDealID"` - LastUpdate int64 `json:"lastUpdate"` -} - -type ResponseData struct { - Proof ProofData `json:"proof"` - FilecoinDeals []FilecoinDeal `json:"filecoin_deals"` -} - -func sendToLighthouseDE(cid string, authToken string) error { - log.Printf("Sending request to lighthouse Deal Engine to add CID: %s", cid) - // Construct the URL - url := fmt.Sprintf("https://calibration.lighthouse.storage/api/v1/deal/add_cid?cid=%s", cid) - - // Create a new HTTP request - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return fmt.Errorf("failed to create request: %s", err) - } - - // Set headers - req.Header.Set("Authorization", "Bearer "+authToken) - req.Header.Set("Content-Type", "application/json") - - // Send the request using the HTTP client - client := &http.Client{ - Timeout: 30 * time.Second, // Set a timeout of 30 seconds - } - resp, err := client.Do(req) - if err != nil { - return fmt.Errorf("failed to send request: %w", err) - } - defer resp.Body.Close() - - // Check the response status - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("unexpected response status: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode)) - } - - log.Println("response status: ", resp.StatusCode) - // Read the response body - body, err := io.ReadAll(resp.Body) - if err != nil { - return fmt.Errorf("failed to read response body: %w", err) - } - - // Determine response type - contentType := resp.Header.Get("Content-Type") - if contentType == "application/json" { - // Parse JSON response - var responseJSON map[string]interface{} - if err := json.Unmarshal(body, &responseJSON); err != nil { - return fmt.Errorf("failed to parse JSON: %w", err) - } - log.Println("POST Request JSON Response:", responseJSON) - } else { - // Handle non-JSON response - log.Println("POST Request Non-JSON Response:", string(body)) - } - - return nil -} - -// Function to check deal status -func getDealStatus(cid string, authToken string) (*ResponseData, error) { - log.Printf("Checking deal status and PoDSI for CID: %s", cid) - - url := fmt.Sprintf("https://calibration.lighthouse.storage/api/v1/deal/deal_status?cid=%s", cid) - - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, fmt.Errorf("failed to create request: %w", err) - } - - req.Header.Set("Authorization", "Bearer "+authToken) - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - return nil, fmt.Errorf("failed to send request: %w", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("unexpected response status: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode)) - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, fmt.Errorf("failed to read response body: %w", err) - } - - contentType := resp.Header.Get("Content-Type") - if contentType == "application/json" { - var response ResponseData - err := json.Unmarshal([]byte(body), &response) - if err != nil { - log.Fatalf("Failed to parse JSON: %v", err) - } - - var result map[string]json.RawMessage - err = json.Unmarshal(body, &result) - if err != nil { - fmt.Println("Error:", err) - return nil, nil - } - - return &response, nil - } - return nil, nil -} - -func ExtractProofDetail(proof ProofData) (cid.Cid, merkletree.ProofData, error) { - // Extract piece_cid and proofSubtree - commP, err := cid.Decode(proof.PieceCID) - if err != nil { - log.Fatalln("failed to parse cid %w", err) - } - - var proofSubtree merkletree.ProofData - if len(proof.FileProofs) > 0 { - var proofSubtreeRaw = proof.FileProofs[0].InclusionProof.ProofSubtree - indexNum, err := strconv.ParseUint(proofSubtreeRaw.Index, 10, 64) - if err != nil { - fmt.Println("Error:", err) - } - proofSubtree.Index = indexNum - - path := make([]merkletree.Node, len(proofSubtreeRaw.Path)) - for i, hash := range proofSubtreeRaw.Path { - if len(hash) != 32*2 { // Check if the hash has the correct length (64 characters for 32 bytes) - return cid.Cid{}, merkletree.ProofData{}, fmt.Errorf("invalid hash length at index %d", i) - } - nodeBytes, err := hex.DecodeString(hash) - if err != nil { - return cid.Cid{}, merkletree.ProofData{}, fmt.Errorf("failed to decode hash at index %d: %w", i, err) - } - copy(path[i][:], nodeBytes) // Copy the bytes into the Node array - } - - proofSubtree.Path = path - } - - return commP, proofSubtree, nil -} - -// func main() { -// cid := "bafkreidl6jh2cdnv6lvlhccsn2viliafq63lvoti7k7zyenaprtegwkiie" -// authToken := "u8t8gf6ds06re" - -// // Call the GET request function -// responseJSON, err := getDealStatus(cid, authToken) -// if err != nil { -// log.Fatalf("Error in GET request: %v", err) -// } - -// // Log or process the JSON response -// log.Printf("Response JSON: %s", responseJSON.Proof.FileProofs[0].InclusionProof.ProofSubtree) - -// pieceCID, proofSubtree, err := ExtractProofDetail(responseJSON.Proof) -// if err != nil { -// fmt.Println("Error:", err) -// return -// } - -// // Print the extracted values -// fmt.Println("Piece CID:", pieceCID) -// fmt.Println("Proof Subtree:", proofSubtree) - -// log.Println("Process completed successfully.") -// } diff --git a/xchain.go b/xchain.go index 22757df..625a80e 100644 --- a/xchain.go +++ b/xchain.go @@ -259,106 +259,6 @@ func main() { } log.Printf("Tx %s included: %d", tx.Hash().Hex(), receipt.Status) - return nil - }, - }, - { - Name: "dealStatus", - Usage: "Check deal status for a specific CID", - ArgsUsage: " ", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "config", - Usage: "Path to the configuration file", - Value: "./config/config.json", - }, - &cli.StringFlag{ - Name: "chain", - Usage: "Name of the source blockchain (e.g., ethereum, polygon)", - Required: true, - }, - }, - Action: func(cctx *cli.Context) error { - cfg, err := config.LoadConfig(cctx.String("config")) - if err != nil { - log.Fatal(err) - } - - cidString := cctx.Args().First() - if cidString == "" { - log.Printf("Please provide a CID to check deal status") - return nil - } - - offerId := cctx.Args().Get(1) - if offerId == "" { - log.Printf("Please provide offerID to check deal status") - return nil - } - - // Get chain name - chainName := cctx.String("chain") - srcCfg, err := config.GetSourceConfig(cfg, chainName) - if err != nil { - log.Fatalf("Invalid chain name '%s': %v", chainName, err) - } - - //Request deal status from Lighthouse Deal Engine - log.Println("Start checking deal status and process aggregation for CID", cidString) - proofResponse, err := getDealStatus(cidString, cfg.LighthouseAuth) - if err != nil { - log.Fatalf("Error in GET request: %v", err) - } - commP, proofSubtree, err := ExtractProofDetail(proofResponse.Proof) - if err != nil { - fmt.Println("Error:", err) - return nil - } - log.Println("offer ID is ", offerId) - log.Println("commP is ", commP) - log.Println("proofSubtree is ", proofSubtree) - - // Dial network - client, err := ethclient.Dial(srcCfg.Api) - if err != nil { - log.Fatalf("failed to connect to Ethereum client for source chain %s at %s: %v", chainName, srcCfg.Api, err) - } - - // Load onramp contract handle - contractAddress := common.HexToAddress(srcCfg.OnRampAddress) - parsedABI, err := LoadAbi(cfg.OnRampABIPath) - if err != nil { - log.Fatal(err) - } - - onramp := bind.NewBoundContract(contractAddress, *parsedABI, client, client, client) - if err != nil { - log.Fatal(err) - } - - // Get auth - auth, err := loadPrivateKey(cfg) - if err != nil { - log.Fatal(err) - } - - inclProofs := make([]merkletree.ProofData, 1) - ids := make([]uint64, 1) - ids[0] = 42 - inclProofs[0] = proofSubtree - - tx, err := onramp.Transact(auth, "commitAggregate", commP.Bytes(), ids, inclProofs, common.HexToAddress(cfg.PayoutAddr)) - if err != nil { - return err - } - - log.Printf("Waiting for transaction: %s\n", tx.Hash().Hex()) - receipt, err := bind.WaitMined(cctx.Context, client, tx) - if err != nil { - log.Fatalf("failed to wait for tx: %v", err) - } - log.Printf("Tx %s committing aggregate commp %s included: %d", tx.Hash().Hex(), commP.String(), receipt.Status) - return nil }, }, @@ -613,16 +513,11 @@ func (a *aggregator) run(ctx context.Context) error { return err }) - // Start Lighthouse Deal Engine listener + // Start aggregatation event handling g.Go(func() error { - return a.sendingToLighthouseDe(ctx) + return a.runAggregate(ctx) }) - // Start aggregatation event handling - // g.Go(func() error { - // return a.runAggregate(ctx) - // }) - // Start handling data transfer requests g.Go(func() error { http.HandleFunc("/", a.transferHandler) @@ -668,43 +563,6 @@ const ( dealDuration = 518400 // 6 months (on mainnet) ) -// No aggregation required, just sending cid to Lighthouse Deal Engine. -func (a *aggregator) sendingToLighthouseDe(ctx context.Context) error { - log.Println("Sending files to Lighthouse Deal Engine.") - log.Println("Events count = ", len(a.ch)) - - for { - select { - case <-ctx.Done(): - log.Printf("ctx done shutting down Lighthouse Event Listener") - return nil - case latestEvent := <-a.ch: - log.Println("Processing Offer ", latestEvent.OfferID) - log.Println("Offer CID ", latestEvent.Offer.Cid) - - // Check if the offer is too big to fit in a valid aggregate on its own - sendToLighthouseDE(latestEvent.Offer.Cid, a.LighthouseAuth) - } - } -} - -// Request Deal Status from Lighthouse Engine and send PoDSI to onramp contract -func processDealStatus(ctx context.Context, cfg *config.Config, cid string) error { - //Request deal status from Lighthouse Deal Engine - proofResponse, err := getDealStatus(cid, cfg.LighthouseAuth) - if err != nil { - log.Fatalf("Error in GET request: %v", err) - } - // Print or log the response body - log.Printf("PoDSI Proof: %v", proofResponse.Proof) - log.Printf("Filecoin Deal: %v", proofResponse.FilecoinDeals) - - // Log the end of the process - log.Println("Process completed successfully.") - - return nil -} - func (a *aggregator) runAggregate(ctx context.Context) error { // pieces being aggregated, flushed upon commitment // Invariant: the pieces in the pending queue can always make a valid aggregate w.r.t a.targetDealSize