Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Hash computation over older item #454

Merged
merged 2 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Item/Item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Bridge } from '../ethereum/api/Bridge'
import { collectionAPI } from '../ethereum/api/collection'
import { peerAPI } from '../ethereum/api/peer'
import { thirdPartyAPI } from '../ethereum/api/thirdParty'
import { isStandardItemPublished } from '../ItemAndCollection/utils'
import { Ownable } from '../Ownable'
import { buildModelDates } from '../utils/dates'
import {
Expand Down Expand Up @@ -360,7 +361,6 @@ export class ItemService {
dbCollection: CollectionAttributes | undefined,
eth_address: string
): Promise<FullItem> {
let contentHash = null
const canUpsert = await new Ownable(Item).canUpsert(item.id, eth_address)
if (!canUpsert) {
throw new UnauthorizedToUpsertError(item.id, eth_address)
Expand Down Expand Up @@ -420,9 +420,6 @@ export class ItemService {
ItemAction.RARITY_UPDATE
)
}

// Compute the content hash of the item to later store it in the DB
contentHash = await calculateItemContentHash(dbItem, dbCollection!)
} else if (this.collectionService.isLockActive(dbCollection.lock)) {
throw new CollectionForItemLockedError(item.id, ItemAction.UPSERT)
}
Expand All @@ -433,7 +430,13 @@ export class ItemService {
eth_address,
})

attributes.local_content_hash = contentHash
attributes.blockchain_item_id = dbItem ? dbItem.blockchain_item_id : null

// Compute the content hash of the item to later store it in the DB
attributes.local_content_hash =
dbCollection && isStandardItemPublished(attributes, dbCollection)
? await calculateItemContentHash(attributes, dbCollection)
: null

const upsertedItem: ItemAttributes = await Item.upsert(attributes)
return Bridge.toFullItem(upsertedItem, dbCollection)
Expand Down
3 changes: 2 additions & 1 deletion src/Item/hashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
keccak256Hash,
} from '@dcl/hashing'
import { CollectionAttributes } from '../Collection'
import { isStandardItemPublished } from '../ItemAndCollection/utils'
import { getDecentralandItemURN, isTPCollection } from '../utils/urn'
import {
StandardWearableEntityMetadata,
Expand All @@ -18,7 +19,7 @@ export function buildStandardWearableEntityMetadata(
item: ItemAttributes,
collection: CollectionAttributes
): StandardWearableEntityMetadata {
if (!collection.contract_address || !item.blockchain_item_id!) {
if (!isStandardItemPublished(item, collection)) {
throw new Error(
"The item's collection must be published to build its metadata"
)
Expand Down
46 changes: 46 additions & 0 deletions src/ItemAndCollection/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { dbCollectionMock } from '../../spec/mocks/collections'
import { dbItemMock } from '../../spec/mocks/items'
import { CollectionAttributes } from '../Collection/Collection.types'
import { ItemAttributes } from '../Item/Item.types'
import { isStandardItemPublished } from './utils'

describe('when checking if an item is published', () => {
let item: ItemAttributes
let collection: CollectionAttributes

beforeEach(() => {
collection = { ...dbCollectionMock }
item = { ...dbItemMock, collection_id: dbCollectionMock.id }
})

describe("and the item doesn't have a blockchain item id", () => {
beforeEach(() => {
item.blockchain_item_id = null
})

it('should return false', () => {
expect(isStandardItemPublished(item, collection)).toBe(false)
})
})

describe("and the item's collection doesn't have a contract address", () => {
beforeEach(() => {
collection.contract_address = null
})

it('should return false', () => {
expect(isStandardItemPublished(item, collection)).toBe(false)
})
})

describe('and the item has a blockchain item id and the collection has a contract address', () => {
beforeEach(() => {
item.blockchain_item_id = '20'
collection.contract_address = '0x123'
})

it('should return true', () => {
expect(isStandardItemPublished(item, collection)).toBe(true)
})
})
})
9 changes: 9 additions & 0 deletions src/ItemAndCollection/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CollectionAttributes } from '../Collection/Collection.types'
import { ItemAttributes } from '../Item/Item.types'

export function isStandardItemPublished(
item: ItemAttributes,
collection: CollectionAttributes
): boolean {
return !!item.blockchain_item_id && !!collection.contract_address
}