Skip to content

Commit

Permalink
[PSL-1193] get cascade-multi-volume contract
Browse files Browse the repository at this point in the history
  • Loading branch information
mateeullahmalik committed Jun 27, 2024
1 parent d31d465 commit 2e6c749
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 31 deletions.
8 changes: 0 additions & 8 deletions pastel/cascade_multi_volume.go

This file was deleted.

72 changes: 49 additions & 23 deletions pastel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"github.com/pastelnetwork/gonode/common/errors"
"github.com/pastelnetwork/gonode/common/log"
"github.com/pastelnetwork/gonode/pastel/jsonrpc"
"github.com/pastelnetwork/gonode/common/utils"
"github.com/pastelnetwork/gonode/pastel/jsonrpc"
)

const (
Expand All @@ -25,8 +25,13 @@ const (
TicketTypeInactive RegTicketsFilter = "inactive"
// TicketTypeAll is all filter for tickets
TicketTypeAll RegTicketsFilter = "all"

// CascadeMultiVolumeMetadata is the only contract type currently available
CascadeMultiVolumeMetadata ContractType = "cascade_multi_volume_metadata"
)

type ContractType string

// RegTicketsFilter is filter for retrieving action & nft registration tickets
type RegTicketsFilter string

Expand Down Expand Up @@ -290,6 +295,16 @@ func (client *client) RegTicket(ctx context.Context, regTxid string) (RegTicket,
return ticket, nil
}

func (client *client) GetContractTicket(ctx context.Context, txid string) (Contract, error) {
ticket := Contract{}

if err := client.callFor(ctx, &ticket, "tickets", "get", txid); err != nil {
return ticket, errors.Errorf("failed to get contract ticket %s: %w", txid, err)
}

return ticket, nil
}

// ActionRegTicket implements pastel.Client.RegTicket
func (client *client) ActionRegTicket(ctx context.Context, regTxid string) (ActionRegTicket, error) {
ticket := ActionRegTicket{}
Expand Down Expand Up @@ -533,30 +548,41 @@ func (client *client) RegisterActionTicket(ctx context.Context, request Register
return txID.TxID, nil
}

func (client *client) RegisterCascadeMultiVolumeTicket(ctx context.Context, ticket CascadeMultiVolumeTicket) (string, error){
ticketJSON, err := json.Marshal(ticket)
if err != nil {
return "", errors.Errorf("failed to call register action ticket: %w", err)
}
ticketBlob := base64.StdEncoding.EncodeToString(ticketJSON)
func (client *client) RegisterCascadeMultiVolumeTicket(ctx context.Context, ticket CascadeMultiVolumeTicket) (string, error) {
return client.registerContract(ctx, ticket, CascadeMultiVolumeMetadata)
}

func (client *client) registerContract(ctx context.Context, data interface{}, contractType ContractType) (string, error) {
ticketJSON, err := json.Marshal(data)
if err != nil {
return "", errors.Errorf("failed to call register action ticket: %w", err)
}
ticketBlob := base64.StdEncoding.EncodeToString(ticketJSON)

hash, _ := utils.Sha3256hash(ticketJSON)
// Assuming some additional data or parameters are needed, similar to the RegisterNFTRequest example
params := []interface{}{
"register",
"contract",
ticketBlob,
ticket.ID,
hash,
}

resp := make(map[string]interface{})
if err := client.callFor(ctx, &resp, "tickets", params...); err != nil {
return "", errors.Errorf("failed to call register contract: %w", err)
}
log.WithContext(ctx).WithField("resp", resp).WithField("txid", ticket.ID).Info("RegisterCascadeMultiVolumeTicket Response")

return ticket.ID, nil
// Assuming some additional data or parameters are needed, similar to the RegisterNFTRequest example
params := []interface{}{
"register",
"contract",
ticketBlob,
contractType,
hash,
}

type ContractResponse struct {
TxID string `json:"txid"`
Key string `json:"key"`
}
resp := ContractResponse{}

if err := client.callFor(ctx, &resp, "tickets", params...); err != nil {
return "", errors.Errorf("failed to call register contract: %w", err)
}
if resp.TxID == "" {
return "", errors.Errorf("failed to call register contract (no txid rcvd.): %w", err)
}

return resp.TxID, nil
}

func (client *client) ActivateActionTicket(ctx context.Context, request ActivateActionRequest) (string, error) {
Expand Down
65 changes: 65 additions & 0 deletions pastel/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package pastel

import (
"encoding/base64"
"encoding/json"
"fmt"

"github.com/pastelnetwork/gonode/common/errors"
)

// ContractTicket defines the contract ticket
type ContractTicket struct {
ContractTicketData string `json:"contract_ticket"`
Key string `json:"key"`
SecondaryKey string `json:"secondary_key"`
SubType string `json:"sub_type"`
Timestamp int64 `json:"timestamp"`
Type string `json:"type"`
Version int `json:"version"`
}

// TxInfo defines the transaction information
type TxInfo struct {
CompressedSize int `json:"compressed_size"`
CompressionRatio string `json:"compression_ratio"`
IsCompressed bool `json:"is_compressed"`
MultisigOutputsCount int `json:"multisig_outputs_count"`
MultisigTxTotalFee int `json:"multisig_tx_total_fee"`
UncompressedSize int `json:"uncompressed_size"`
}

// Contract defines the contract
type Contract struct {
Height int `json:"height"`
Ticket ContractTicket `json:"ticket"`
TxInfo TxInfo `json:"tx_info"`
TxID string `json:"txid"`
}

// -------------------------------------- Contract Types -------------------------------------------//

// CascadeMultiVolumeTicket defines the cascade multi volume ticket contract type
type CascadeMultiVolumeTicket struct {
NameOfOriginalFile string `json:"name_of_original_file"`
SizeOfOriginalFileMB int `json:"size_of_original_file_mb"`
SHA3256HashOfOriginalFile string `json:"sha3_256_hash_of_original_file"`
Volumes map[int]string `json:"volumes"` // key (int): index of the volume, value (string): txid of the volume
}

func (c *Contract) GetCascadeMultiVolumeMetadataTicket() (t CascadeMultiVolumeTicket, err error) {
if c.Ticket.SubType != string(CascadeMultiVolumeMetadata) {
return t, errors.New("contract is not of type cascade_multi_volume_metadata")
}

data, err := base64.StdEncoding.DecodeString(c.Ticket.ContractTicketData)
if err != nil {
return t, fmt.Errorf("unable to b64 decode contract: %w", err)
}

if err := json.Unmarshal(data, &t); err != nil {
return t, fmt.Errorf("unable to decode contract: %w", err)
}

return t, nil
}
5 changes: 5 additions & 0 deletions pastel/pastel.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,10 @@ type Client interface {
NFTStorageFee(ctx context.Context, sizeInMB int) (*NFTStorageFeeEstimate, error)

// RegisterCascadeMultiVolumeTicket registers a cascade multi-volume ticket
// Command `tickets register contract <<ticket>>, <<sub-type>>, <<hash of the ticket data>>`
RegisterCascadeMultiVolumeTicket(ctx context.Context, ticket CascadeMultiVolumeTicket) (string, error)

// GetContractTicket returns contract ticket.
// Command `tickets get <txid>`.
GetContractTicket(ctx context.Context, txid string) (Contract, error)
}

0 comments on commit 2e6c749

Please sign in to comment.