Skip to content

Commit

Permalink
added BatchManifestAllaccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Mar 28, 2024
1 parent 41e6e5f commit 6e75399
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 92 deletions.
29 changes: 13 additions & 16 deletions blockchain/bl_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (bl *FxBlockchain) ManifestStore(ctx context.Context, to peer.ID, r Manifes
}
}

func (bl *FxBlockchain) HandleManifestAvailableBatch(ctx context.Context, poolIDString string, account string, links []ipld.Link) ([]ipld.Link, error) {
func (bl *FxBlockchain) HandleManifestAvailableAllaccountsBatch(ctx context.Context, poolIDString string, links []ipld.Link) ([]ipld.Link, error) {
var availableLinks []ipld.Link
poolID, err := strconv.Atoi(poolIDString)
if err != nil {
Expand All @@ -100,41 +100,38 @@ func (bl *FxBlockchain) HandleManifestAvailableBatch(ctx context.Context, poolID
cids = append(cids, link.String())
}

reqBody := ReplicateRequest{
Cids: cids,
Account: account,
PoolID: poolID,
reqBody := AvailableAllaccountsBatchRequest{
Cids: cids,
PoolID: poolID,
}
log.Debugw("HandleManifestAvailableBatch", "reqBody", reqBody)
log.Debugw("HandleManifestAvailableAllaccountsBatch", "reqBody", reqBody)
req, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("failed to serialize request: %w", err)
}

response, statusCode, err := bl.callBlockchain(ctx, "POST", actionManifestAvailableBatch, req)
response, statusCode, err := bl.callBlockchain(ctx, "POST", actionManifestAvailableAllaccountsBatch, req)
if err != nil {
return nil, fmt.Errorf("blockchain call failed: %w", err)
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
}

var resp ReplicateResponse
var resp BatchManifestAllaccountsResponse
if err := json.Unmarshal(response, &resp); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
log.Debugw("HandleManifestAvailableBatch", "resp", resp)
log.Debugw("HandleManifestAvailableAllaccountsBatch", "resp", resp)

// Filter for available manifests.
for _, manifest := range resp.Manifests {
if manifest.ReplicationAvailable > 0 {
c, err := cid.Decode(manifest.Cid)
if err != nil {
// Log or handle the error based on your application's logging strategy.
continue // Skipping invalid CIDs.
}
availableLinks = append(availableLinks, cidlink.Link{Cid: c})
c, err := cid.Decode(manifest.Cid)
if err != nil {
// Log or handle the error based on your application's logging strategy.
continue // Skipping invalid CIDs.
}
availableLinks = append(availableLinks, cidlink.Link{Cid: c})
}

return availableLinks, nil
Expand Down
166 changes: 91 additions & 75 deletions blockchain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@ import (
)

const (
actionSeeded = "account-seeded"
actionAccountExists = "account-exists"
actionAccountCreate = "account-create"
actionAccountFund = "account-fund"
actionAccountBalance = "account-balance"
actionAssetsBalance = "asset-balance"
actionTransferToMumbai = "fula-mumbai-convert_tokens"
actionTransferToGoerli = "fula-goerli-convert_tokens"
actionPoolCreate = "fula-pool-create"
actionPoolJoin = "fula-pool-join"
actionPoolCancelJoin = "fula-pool-cancel_join"
actionPoolRequests = "fula-pool-poolrequests"
actionPoolList = "fula-pool"
actionPoolUserList = "fula-pool-users"
actionPoolVote = "fula-pool-vote"
actionPoolLeave = "fula-pool-leave"
actionManifestUpload = "fula-manifest-upload"
actionManifestStore = "fula-manifest-storage"
actionManifestBatchStore = "fula-manifest-batch_storage"
actionManifestBatchUpload = "fula-manifest-batch_upload"
actionManifestAvailable = "fula-manifest-available"
actionManifestRemove = "fula-manifest-remove"
actionManifestRemoveStorer = "fula-manifest-remove_storer"
actionManifestRemoveStored = "fula-manifest-remove_storing_manifest"
actionManifestAvailableBatch = "fula-manifest-available_batch"
actionSeeded = "account-seeded"
actionAccountExists = "account-exists"
actionAccountCreate = "account-create"
actionAccountFund = "account-fund"
actionAccountBalance = "account-balance"
actionAssetsBalance = "asset-balance"
actionTransferToMumbai = "fula-mumbai-convert_tokens"
actionTransferToGoerli = "fula-goerli-convert_tokens"
actionPoolCreate = "fula-pool-create"
actionPoolJoin = "fula-pool-join"
actionPoolCancelJoin = "fula-pool-cancel_join"
actionPoolRequests = "fula-pool-poolrequests"
actionPoolList = "fula-pool"
actionPoolUserList = "fula-pool-users"
actionPoolVote = "fula-pool-vote"
actionPoolLeave = "fula-pool-leave"
actionManifestUpload = "fula-manifest-upload"
actionManifestStore = "fula-manifest-storage"
actionManifestBatchStore = "fula-manifest-batch_storage"
actionManifestBatchUpload = "fula-manifest-batch_upload"
actionManifestAvailable = "fula-manifest-available"
actionManifestRemove = "fula-manifest-remove"
actionManifestRemoveStorer = "fula-manifest-remove_storer"
actionManifestRemoveStored = "fula-manifest-remove_storing_manifest"
actionManifestAvailableBatch = "fula-manifest-available_batch"
actionManifestAvailableAllaccountsBatch = "fula-manifest-available_allaccounts_batch"

//Hardware
actionBloxFreeSpace = "blox-free-space"
Expand All @@ -60,6 +61,11 @@ type ReplicateRequest struct {
PoolID int `json:"pool_id"`
}

type AvailableAllaccountsBatchRequest struct {
Cids []string `json:"cids"`
PoolID int `json:"pool_id"`
}

type ReplicateResponse struct {
Manifests []BatchManifest `json:"manifests"`
}
Expand All @@ -69,6 +75,14 @@ type BatchManifest struct {
ReplicationAvailable int `json:"replication_available"`
}

type BatchManifestAllaccountsResponse struct {
Manifests []BatchManifestAllaccounts `json:"manifests"`
}

type BatchManifestAllaccounts struct {
Cid string `json:"cid"`
}

type LinkWithLimit struct {
Link ipld.Link
Limit int
Expand Down Expand Up @@ -413,31 +427,32 @@ type Blockchain interface {
}

var requestTypes = map[string]reflect.Type{
actionSeeded: reflect.TypeOf(SeededRequest{}),
actionAccountExists: reflect.TypeOf(AccountExistsRequest{}),
actionAccountCreate: reflect.TypeOf(AccountCreateRequest{}),
actionAccountFund: reflect.TypeOf(AccountFundRequest{}),
actionPoolCreate: reflect.TypeOf(PoolCreateRequest{}),
actionPoolJoin: reflect.TypeOf(PoolJoinRequest{}),
actionPoolRequests: reflect.TypeOf(PoolRequestsRequest{}),
actionPoolCancelJoin: reflect.TypeOf(PoolCancelJoinRequest{}),
actionPoolList: reflect.TypeOf(PoolListRequest{}),
actionPoolUserList: reflect.TypeOf(PoolUserListRequest{}),
actionPoolVote: reflect.TypeOf(PoolVoteRequest{}),
actionPoolLeave: reflect.TypeOf(PoolLeaveRequest{}),
actionManifestUpload: reflect.TypeOf(ManifestUploadRequest{}),
actionManifestStore: reflect.TypeOf(ManifestStoreRequest{}),
actionManifestBatchStore: reflect.TypeOf(ManifestBatchStoreRequest{}),
actionManifestBatchUpload: reflect.TypeOf(ManifestBatchUploadRequest{}),
actionManifestAvailable: reflect.TypeOf(ManifestAvailableRequest{}),
actionManifestAvailableBatch: reflect.TypeOf(ReplicateRequest{}),
actionManifestRemove: reflect.TypeOf(ManifestRemoveRequest{}),
actionManifestRemoveStorer: reflect.TypeOf(ManifestRemoveStorerRequest{}),
actionManifestRemoveStored: reflect.TypeOf(ManifestRemoveStoredRequest{}),
actionAssetsBalance: reflect.TypeOf(AssetsBalanceRequest{}),
actionTransferToGoerli: reflect.TypeOf(TransferToFulaRequest{}),
actionTransferToMumbai: reflect.TypeOf(TransferToFulaRequest{}),
actionReplicateInPool: reflect.TypeOf(ReplicateRequest{}),
actionSeeded: reflect.TypeOf(SeededRequest{}),
actionAccountExists: reflect.TypeOf(AccountExistsRequest{}),
actionAccountCreate: reflect.TypeOf(AccountCreateRequest{}),
actionAccountFund: reflect.TypeOf(AccountFundRequest{}),
actionPoolCreate: reflect.TypeOf(PoolCreateRequest{}),
actionPoolJoin: reflect.TypeOf(PoolJoinRequest{}),
actionPoolRequests: reflect.TypeOf(PoolRequestsRequest{}),
actionPoolCancelJoin: reflect.TypeOf(PoolCancelJoinRequest{}),
actionPoolList: reflect.TypeOf(PoolListRequest{}),
actionPoolUserList: reflect.TypeOf(PoolUserListRequest{}),
actionPoolVote: reflect.TypeOf(PoolVoteRequest{}),
actionPoolLeave: reflect.TypeOf(PoolLeaveRequest{}),
actionManifestUpload: reflect.TypeOf(ManifestUploadRequest{}),
actionManifestStore: reflect.TypeOf(ManifestStoreRequest{}),
actionManifestBatchStore: reflect.TypeOf(ManifestBatchStoreRequest{}),
actionManifestBatchUpload: reflect.TypeOf(ManifestBatchUploadRequest{}),
actionManifestAvailable: reflect.TypeOf(ManifestAvailableRequest{}),
actionManifestAvailableBatch: reflect.TypeOf(ReplicateRequest{}),
actionManifestAvailableAllaccountsBatch: reflect.TypeOf(AvailableAllaccountsBatchRequest{}),
actionManifestRemove: reflect.TypeOf(ManifestRemoveRequest{}),
actionManifestRemoveStorer: reflect.TypeOf(ManifestRemoveStorerRequest{}),
actionManifestRemoveStored: reflect.TypeOf(ManifestRemoveStoredRequest{}),
actionAssetsBalance: reflect.TypeOf(AssetsBalanceRequest{}),
actionTransferToGoerli: reflect.TypeOf(TransferToFulaRequest{}),
actionTransferToMumbai: reflect.TypeOf(TransferToFulaRequest{}),
actionReplicateInPool: reflect.TypeOf(ReplicateRequest{}),

//Hardware
actionBloxFreeSpace: reflect.TypeOf(wifi.BloxFreeSpaceRequest{}),
Expand All @@ -455,31 +470,32 @@ var requestTypes = map[string]reflect.Type{
}

var responseTypes = map[string]reflect.Type{
actionSeeded: reflect.TypeOf(SeededResponse{}),
actionAccountExists: reflect.TypeOf(AccountExistsResponse{}),
actionAccountCreate: reflect.TypeOf(AccountCreateResponse{}),
actionAccountFund: reflect.TypeOf(AccountFundResponse{}),
actionPoolCreate: reflect.TypeOf(PoolCreateResponse{}),
actionPoolJoin: reflect.TypeOf(PoolJoinResponse{}),
actionPoolRequests: reflect.TypeOf(PoolRequestsResponse{}),
actionPoolCancelJoin: reflect.TypeOf(PoolCancelJoinResponse{}),
actionPoolList: reflect.TypeOf(PoolListResponse{}),
actionPoolUserList: reflect.TypeOf(PoolUserListResponse{}),
actionPoolVote: reflect.TypeOf(PoolVoteResponse{}),
actionPoolLeave: reflect.TypeOf(PoolLeaveResponse{}),
actionManifestUpload: reflect.TypeOf(ManifestUploadResponse{}),
actionManifestStore: reflect.TypeOf(ManifestStoreResponse{}),
actionManifestBatchStore: reflect.TypeOf(ManifestBatchStoreResponse{}),
actionManifestBatchUpload: reflect.TypeOf(ManifestBatchUploadResponse{}),
actionManifestAvailable: reflect.TypeOf(ManifestAvailableResponse{}),
actionManifestAvailableBatch: reflect.TypeOf(ReplicateResponse{}),
actionManifestRemove: reflect.TypeOf(ManifestRemoveResponse{}),
actionManifestRemoveStorer: reflect.TypeOf(ManifestRemoveStorerResponse{}),
actionManifestRemoveStored: reflect.TypeOf(ManifestRemoveStoredResponse{}),
actionAssetsBalance: reflect.TypeOf(AssetsBalanceResponse{}),
actionTransferToGoerli: reflect.TypeOf(TransferToFulaResponse{}),
actionTransferToMumbai: reflect.TypeOf(TransferToFulaResponse{}),
actionReplicateInPool: reflect.TypeOf(ReplicateResponse{}),
actionSeeded: reflect.TypeOf(SeededResponse{}),
actionAccountExists: reflect.TypeOf(AccountExistsResponse{}),
actionAccountCreate: reflect.TypeOf(AccountCreateResponse{}),
actionAccountFund: reflect.TypeOf(AccountFundResponse{}),
actionPoolCreate: reflect.TypeOf(PoolCreateResponse{}),
actionPoolJoin: reflect.TypeOf(PoolJoinResponse{}),
actionPoolRequests: reflect.TypeOf(PoolRequestsResponse{}),
actionPoolCancelJoin: reflect.TypeOf(PoolCancelJoinResponse{}),
actionPoolList: reflect.TypeOf(PoolListResponse{}),
actionPoolUserList: reflect.TypeOf(PoolUserListResponse{}),
actionPoolVote: reflect.TypeOf(PoolVoteResponse{}),
actionPoolLeave: reflect.TypeOf(PoolLeaveResponse{}),
actionManifestUpload: reflect.TypeOf(ManifestUploadResponse{}),
actionManifestStore: reflect.TypeOf(ManifestStoreResponse{}),
actionManifestBatchStore: reflect.TypeOf(ManifestBatchStoreResponse{}),
actionManifestBatchUpload: reflect.TypeOf(ManifestBatchUploadResponse{}),
actionManifestAvailable: reflect.TypeOf(ManifestAvailableResponse{}),
actionManifestAvailableBatch: reflect.TypeOf(ReplicateResponse{}),
actionManifestAvailableAllaccountsBatch: reflect.TypeOf(BatchManifestAllaccountsResponse{}),
actionManifestRemove: reflect.TypeOf(ManifestRemoveResponse{}),
actionManifestRemoveStorer: reflect.TypeOf(ManifestRemoveStorerResponse{}),
actionManifestRemoveStored: reflect.TypeOf(ManifestRemoveStoredResponse{}),
actionAssetsBalance: reflect.TypeOf(AssetsBalanceResponse{}),
actionTransferToGoerli: reflect.TypeOf(TransferToFulaResponse{}),
actionTransferToMumbai: reflect.TypeOf(TransferToFulaResponse{}),
actionReplicateInPool: reflect.TypeOf(ReplicateResponse{}),

//Hardware
actionBloxFreeSpace: reflect.TypeOf(wifi.BloxFreeSpaceResponse{}),
Expand Down
2 changes: 1 addition & 1 deletion blox/blox.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ func (p *Blox) Start(ctx context.Context) error {
// Call HandleManifestBatchStore method
if len(storedLinks) > 0 {
// Check if the manifests are available to be stored or not
availableLinks, err := p.bl.HandleManifestAvailableBatch(shortCtx, p.topicName, nodeAccount, storedLinks)
availableLinks, err := p.bl.HandleManifestAvailableAllaccountsBatch(shortCtx, p.topicName, storedLinks)
if err != nil {
log.Errorw("Error checking available manifests", "err", err)
continue // Or handle the error appropriately
Expand Down

0 comments on commit 6e75399

Please sign in to comment.