Skip to content

Commit

Permalink
Send metadata directly through chain (#5171)
Browse files Browse the repository at this point in the history
  • Loading branch information
michellebrier committed May 3, 2023
1 parent c159a76 commit 5ea630f
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 67 deletions.
18 changes: 12 additions & 6 deletions discovery-provider/src/tasks/index.py
Expand Up @@ -230,10 +230,14 @@ def fetch_cid_metadata(db, entity_manager_txs):
or event_type == EntityType.USER_REPLICA_SET
or action
in [
EntityType.REPOST,
EntityType.SAVE,
EntityType.FOLLOW,
EntityType.SUBSCRIPTION,
Action.REPOST,
Action.UNREPOST,
Action.SAVE,
Action.UNSAVE,
Action.FOLLOW,
Action.UNFOLLOW,
Action.SUBSCRIBE,
Action.UNSUBSCRIBE,
]
):
continue
Expand All @@ -256,7 +260,7 @@ def fetch_cid_metadata(db, entity_manager_txs):
# If metadata blob does not contain the required keys, skip
if "cid" not in data.keys() or "data" not in data.keys():
logger.info(
f"index_nethermind.py | required keys missing in metadata {cid}"
f"index.py | required keys missing in metadata {cid}"
)
continue
cid = data["cid"]
Expand Down Expand Up @@ -285,10 +289,12 @@ def fetch_cid_metadata(db, entity_manager_txs):
cid_metadata_from_chain[cid] = formatted_json
except Exception as e:
logger.info(
f"index_nethermind.py | error deserializing metadata {cid}: {e}"
f"index.py | error deserializing metadata {cid}: {e}"
)
continue

logger.info(f"index.py | falling back to fetching cid metadata from content nodes for metadata {cid}, event type {event_type}, action {action}, user {user_id}")

cids_txhash_set.add((cid, txhash))
cid_to_user_id[cid] = user_id
if event_type == EntityType.PLAYLIST:
Expand Down
14 changes: 10 additions & 4 deletions discovery-provider/src/tasks/index_nethermind.py
Expand Up @@ -254,10 +254,14 @@ def fetch_cid_metadata(db, entity_manager_txs):
or event_type == EntityType.USER_REPLICA_SET
or action
in [
EntityType.REPOST,
EntityType.SAVE,
EntityType.FOLLOW,
EntityType.SUBSCRIPTION,
Action.REPOST,
Action.UNREPOST,
Action.SAVE,
Action.UNSAVE,
Action.FOLLOW,
Action.UNFOLLOW,
Action.SUBSCRIBE,
Action.UNSUBSCRIBE,
]
):
continue
Expand Down Expand Up @@ -306,6 +310,8 @@ def fetch_cid_metadata(db, entity_manager_txs):
)
continue

logger.info(f"index_nethermind.py | falling back to fetching cid metadata from content nodes for metadata {cid}, event type {event_type}, action {action}, user {user_id}")

cids_txhash_set.add((cid, txhash))
cid_to_user_id[cid] = user_id
if event_type == EntityType.PLAYLIST:
Expand Down
20 changes: 13 additions & 7 deletions libs/src/api/Account.ts
Expand Up @@ -134,7 +134,8 @@ export class Account extends Base {
coverPhotoFile: Nullable<File> = null,
hasWallet = false,
host = (typeof window !== 'undefined' && window.location.origin) || null,
generateRecoveryLink = true
generateRecoveryLink = true,
writeMetadataThroughChain = false
) {
const phases = {
ADD_REPLICA_SET: 'ADD_REPLICA_SET',
Expand Down Expand Up @@ -169,14 +170,16 @@ export class Account extends Base {
// Add user to chain
phase = phases.ADD_USER
const { newMetadata, blockHash, blockNumber } =
await this.User.createEntityManagerUser({
metadata
})
await this.User.createEntityManagerUser(
{ metadata },
writeMetadataThroughChain
)
phase = phases.UPLOAD_PROFILE_IMAGES
await this.User.uploadProfileImages(
profilePictureFile!,
coverPhotoFile!,
newMetadata
newMetadata,
writeMetadataThroughChain
)
return { blockHash, blockNumber, userId: newMetadata.user_id }
} catch (e: any) {
Expand Down Expand Up @@ -378,13 +381,16 @@ export class Account extends Base {
* Updates a user's creator node endpoint. Sets the connected creator node in the libs instance
* and updates the user's metadata blob.
*/
async updateCreatorNodeEndpoint(url: string) {
async updateCreatorNodeEndpoint(
url: string,
writeMetadataThroughChain = false
) {
this.REQUIRES(Services.CREATOR_NODE)

const user = this.userStateManager.getCurrentUser() as User
await this.creatorNode.setEndpoint(url)
user.creator_node_endpoint = url
await this.User.updateCreator(user.user_id, user)
await this.User.updateCreator(user.user_id, user, writeMetadataThroughChain)
}

/**
Expand Down
9 changes: 7 additions & 2 deletions libs/src/api/File.ts
Expand Up @@ -266,12 +266,17 @@ export class File extends Base {
* Uploads an image to the connected Content Node.
* @param file
*/
async uploadImage(file: globalThis.File, square: boolean, timeoutMs = null) {
async uploadImage(
file: globalThis.File,
square: boolean,
timeoutMs = null,
writeMetadataThroughChain = false
) {
this.REQUIRES(Services.CREATOR_NODE)
this.FILE_IS_VALID(file)

// Assign a creator_node_endpoint to the user if necessary
await this.User.assignReplicaSetIfNecessary()
await this.User.assignReplicaSetIfNecessary(writeMetadataThroughChain)

const resp = await this.creatorNode.uploadImage(
file,
Expand Down
53 changes: 42 additions & 11 deletions libs/src/api/Track.ts
Expand Up @@ -28,6 +28,7 @@ type ChainInfo = {
metadataMultihash: string
metadataFileUUID: string
transcodedTrackUUID: string
metadata?: TrackMetadata
}

const { decodeHashId } = Utils
Expand Down Expand Up @@ -457,7 +458,8 @@ export class Track extends Base {
trackFile: File,
coverArtFile: File,
metadata: TrackMetadata,
onProgress: () => void
onProgress: () => void,
writeMetadataThroughChain = false
) {
this.REQUIRES(Services.CREATOR_NODE)
this.FILE_IS_VALID(trackFile)
Expand Down Expand Up @@ -522,12 +524,18 @@ export class Track extends Base {

// Write metadata to chain
const trackId = await this._generateTrackId()
const entityManagerMetadata = writeMetadataThroughChain
? JSON.stringify({
cid: metadataMultihash,
data: metadata
})
: metadataMultihash
const response = await this.contracts.EntityManagerClient!.manageEntity(
ownerId,
EntityManagerClient.EntityType.TRACK,
trackId,
EntityManagerClient.Action.CREATE,
metadataMultihash
entityManagerMetadata
)
const txReceipt = response.txReceipt

Expand Down Expand Up @@ -586,7 +594,8 @@ export class Track extends Base {
metadataMultihash,
metadataFileUUID,
transcodedTrackCID,
transcodedTrackUUID
transcodedTrackUUID,
metadata: respMetadata
} = await retry(
async () => {
return await this.creatorNode.uploadTrackContent(
Expand Down Expand Up @@ -614,7 +623,8 @@ export class Track extends Base {
metadataMultihash,
metadataFileUUID,
transcodedTrackCID,
transcodedTrackUUID
transcodedTrackUUID,
metadata: respMetadata
}
}

Expand All @@ -623,15 +633,18 @@ export class Track extends Base {
* Adds tracks to chain for this user
* Associates tracks with user on creatorNode
*/
async addTracksToChainAndCnode(trackMultihashAndUUIDList: ChainInfo[]) {
async addTracksToChainAndCnode(
trackMultihashAndUUIDList: ChainInfo[],
writeMetadataThroughChain = false
) {
this.REQUIRES(Services.CREATOR_NODE)
const ownerId = this.userStateManager.getCurrentUserId()
if (!ownerId) {
throw new Error('No users loaded for this wallet')
}

const addedToChain: Array<
Omit<ChainInfo, 'metadataMultihash'> & {
Omit<ChainInfo, 'metadataMultihash' | 'metadata'> & {
trackId: number
txReceipt: TransactionReceipt
}
Expand All @@ -640,8 +653,20 @@ export class Track extends Base {
await Promise.all(
trackMultihashAndUUIDList.map(async (trackInfo, i) => {
try {
const { metadataMultihash, metadataFileUUID, transcodedTrackUUID } =
trackInfo
const {
metadataMultihash,
metadataFileUUID,
transcodedTrackUUID,
metadata
} = trackInfo

let entityManagerMetadata = metadataMultihash
if (writeMetadataThroughChain && metadata) {
entityManagerMetadata = JSON.stringify({
cid: metadataMultihash,
data: metadata
})
}

// Write metadata to chain
const trackId = await this._generateTrackId()
Expand All @@ -651,7 +676,7 @@ export class Track extends Base {
EntityManagerClient.EntityType.TRACK,
trackId,
EntityManagerClient.Action.CREATE,
metadataMultihash
entityManagerMetadata
)
const txReceipt = response.txReceipt
addedToChain[i] = {
Expand Down Expand Up @@ -709,7 +734,10 @@ export class Track extends Base {
* such as track content, cover art are already on creator node.
* @param metadata json of the track metadata with all fields, missing fields will error
*/
async updateTrack(metadata: TrackMetadata) {
async updateTrack(
metadata: TrackMetadata,
writeMetadataThroughChain = false
) {
this.REQUIRES(Services.CREATOR_NODE)
this.IS_OBJECT(metadata)

Expand All @@ -726,12 +754,15 @@ export class Track extends Base {
await this.creatorNode.uploadTrackMetadata(metadata)
// Write the new metadata to chain
const trackId: number = metadata.track_id
const entityManagerMetadata = writeMetadataThroughChain
? JSON.stringify({ cid: metadataMultihash, data: metadata })
: metadataMultihash
const response = await this.contracts.EntityManagerClient!.manageEntity(
ownerId,
EntityManagerClient.EntityType.TRACK,
trackId,
EntityManagerClient.Action.UPDATE,
metadataMultihash
entityManagerMetadata
)
const txReceipt = response.txReceipt
// Re-associate the track id with the new metadata
Expand Down

0 comments on commit 5ea630f

Please sign in to comment.