Skip to content

Commit

Permalink
Merge pull request #22 from BigWhaleLabs/update-obss-storage
Browse files Browse the repository at this point in the history
Update obss storage
  • Loading branch information
MixailE authored Mar 17, 2023
2 parents 67ebb53 + 2e4218d commit 39a1f2b
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 84 deletions.
47 changes: 25 additions & 22 deletions contracts/OBSSStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ contract OBSSStorage is
mapping(address => Counters.Counter) public lastProfilePostIds;
mapping(address => CID) public subscriptions;
// Reactions
mapping(bytes32 => mapping(uint256 => Reaction)) public reactions;
mapping(bytes32 => Counters.Counter) public lastReactionIds;
mapping(bytes32 => mapping(address => uint256)) public reactionsUserToId;
mapping(uint256 => mapping(uint256 => Reaction)) public reactions;
mapping(uint256 => Counters.Counter) public lastReactionIds;
mapping(uint256 => mapping(address => uint256)) public reactionsUserToId;
bool public isDataMigrationLocked;

// IPFS cid represented in a more efficient way
Expand All @@ -58,6 +58,7 @@ contract OBSSStorage is
}
// 1 = upvote, 2 = downvote
struct Reaction {
uint256 postId;
uint8 reactionType;
uint256 value;
address reactionOwner;
Expand All @@ -84,7 +85,6 @@ contract OBSSStorage is
}
struct LegacyReaction {
Reaction reaction;
Post post;
}

/* Events */
Expand Down Expand Up @@ -285,27 +285,28 @@ contract OBSSStorage is
if (post.author == address(0)) {
revert("Post not found");
}
uint256 oldReactionId = reactionsUserToId[post.metadata.digest][
uint256 oldReactionId = reactionsUserToId[reactionRequest.postId][
_msgSender()
];
if (
reactions[post.metadata.digest][oldReactionId].reactionType ==
reactions[reactionRequest.postId][oldReactionId].reactionType ==
reactionRequest.reactionType
) revert("Reaction already added");
if (oldReactionId > 0) {
delete reactions[post.metadata.digest][oldReactionId];
delete reactionsUserToId[post.metadata.digest][_msgSender()];
delete reactions[reactionRequest.postId][oldReactionId];
delete reactionsUserToId[reactionRequest.postId][_msgSender()];
emit ReactionRemoved(_msgSender(), reactionRequest.postId, oldReactionId);
}
Reaction memory reaction = Reaction(
reactionRequest.postId,
reactionRequest.reactionType,
msg.value,
_msgSender()
);
lastReactionIds[post.metadata.digest].increment();
uint256 reactionId = lastReactionIds[post.metadata.digest].current();
reactions[post.metadata.digest][reactionId] = reaction;
reactionsUserToId[post.metadata.digest][_msgSender()] = reactionId;
lastReactionIds[reactionRequest.postId].increment();
uint256 reactionId = lastReactionIds[reactionRequest.postId].current();
reactions[reactionRequest.postId][reactionId] = reaction;
reactionsUserToId[reactionRequest.postId][_msgSender()] = reactionId;
if (msg.value > 0) {
payable(post.author).transfer(msg.value);
}
Expand Down Expand Up @@ -351,12 +352,13 @@ contract OBSSStorage is
}
if (
_msgSender() !=
reactions[post.metadata.digest][reactionRequest.reactionId].reactionOwner
reactions[reactionRequest.postId][reactionRequest.reactionId]
.reactionOwner
) {
revert("You are not the reaction owner");
}
delete reactions[post.metadata.digest][reactionRequest.reactionId];
delete reactionsUserToId[post.metadata.digest][_msgSender()];
delete reactions[reactionRequest.postId][reactionRequest.reactionId];
delete reactionsUserToId[reactionRequest.postId][_msgSender()];
emit ReactionRemoved(
_msgSender(),
reactionRequest.postId,
Expand Down Expand Up @@ -431,23 +433,24 @@ contract OBSSStorage is
for (uint8 i = 0; i < length; ) {
LegacyReaction memory legacyReaction = legacyReactions[i];
Reaction memory reaction = Reaction(
legacyReaction.reaction.postId,
legacyReaction.reaction.reactionType,
legacyReaction.reaction.value,
legacyReaction.reaction.reactionOwner
);
lastReactionIds[legacyReaction.post.metadata.digest].increment();
uint256 reactionId = lastReactionIds[legacyReaction.post.metadata.digest]
lastReactionIds[legacyReaction.reaction.postId].increment();
uint256 reactionId = lastReactionIds[legacyReaction.reaction.postId]
.current();
reactions[legacyReaction.post.metadata.digest][reactionId] = reaction;
reactionsUserToId[legacyReaction.post.metadata.digest][
reactions[legacyReaction.reaction.postId][reactionId] = reaction;
reactionsUserToId[legacyReaction.reaction.postId][
legacyReaction.reaction.reactionOwner
] = reactionId;
if (msg.value > 0) {
payable(legacyReaction.reaction.reactionOwner).transfer(msg.value);
}
emit ReactionAdded(
legacyReaction.reaction.reactionOwner,
0,
legacyReaction.reaction.postId,
legacyReaction.reaction.reactionType,
reactionId,
0
Expand Down Expand Up @@ -516,12 +519,12 @@ contract OBSSStorage is
if (post.author == address(0)) {
revert("Post not found");
}
uint256 reactionsLength = lastReactionIds[post.metadata.digest].current();
uint256 reactionsLength = lastReactionIds[postId].current();
uint256 negativeReactions = 0;
uint256 positiveReactions = 0;

for (uint256 i = 1; i < reactionsLength + 1; ) {
Reaction memory currentReaction = reactions[post.metadata.digest][i];
Reaction memory currentReaction = reactions[postId][i];
if (currentReaction.reactionType == 1) {
positiveReactions += 1;
} else if (currentReaction.reactionType == 2) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@big-whale-labs/obss-storage-contract",
"version": "0.2.3-0",
"version": "0.2.4-0",
"description": "Storage contract for OBSS",
"repository": {
"type": "git",
Expand Down Expand Up @@ -78,6 +78,7 @@
"dependencies": {
"@big-whale-labs/constants": "^0.1.39",
"@big-whale-labs/ketl-allow-map-contract": "^0.0.3",
"@big-whale-labs/obss-storage-contract": "^0.1.4",
"@openzeppelin/contracts-upgradeable": "^4.8.2",
"@openzeppelin/hardhat-upgrades": "^1.22.1",
"@zk-kit/incremental-merkle-tree": "^1.0.0",
Expand Down
166 changes: 116 additions & 50 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BigNumber, utils } from 'ethers'
import { GSN_MUMBAI_FORWARDER_CONTRACT_ADDRESS } from '@big-whale-labs/constants'
import { JsonRpcProvider } from '@ethersproject/providers'
import { OBSSStorage, OBSSStorage__factory } from 'typechain'
import { Signer, utils } from 'ethers'
import { OBSSStorage__factory as LegacyOBSSStorage__factory } from '@big-whale-labs/obss-storage-contract'
import { OBSSStorage } from 'typechain'
import { Provider } from '@ethersproject/providers'
import { ethers, run, upgrades } from 'hardhat'
import { version } from '../package.json'
import prompt from 'prompt'
Expand Down Expand Up @@ -59,6 +60,7 @@ async function main() {
})

const provider = ethers.provider

const { chainId } = await provider.getNetwork()
const chains = {
1: 'mainnet',
Expand Down Expand Up @@ -109,16 +111,13 @@ async function main() {
console.log('Admin address:', adminAddress)

console.log('Migrating data...')
const { legacyPosts, legacyReactions } = await downloadData(
factory,
provider,
deployer
)

const { legacyPosts, legacyReactions } = await downloadData(provider)
const legacyPostsBatches = prepareAllBatches(legacyPosts)
const legacyReactionsBatches = prepareAllBatches(legacyReactions)

for (let i = 0; i < legacyPostsBatches.length; i++) {
console.log(`Loading data batch ${i}`)
console.log(`Loading data batch ${i} / ${legacyPostsBatches.length}`)
const tx = await deployedContract.migrateLegacyData(
legacyPostsBatches[i] as OBSSStorage.LegacyPostStruct[],
[] as OBSSStorage.LegacyReactionStruct[]
Expand All @@ -130,7 +129,7 @@ async function main() {
)
}
for (let i = 0; i < legacyReactionsBatches.length; i++) {
console.log(`Loading data batch ${i}`)
console.log(`Loading data batch ${i} / ${legacyReactionsBatches.length}`)
const tx = await deployedContract.migrateLegacyData(
[] as OBSSStorage.LegacyPostStruct[],
legacyReactionsBatches[i] as OBSSStorage.LegacyReactionStruct[]
Expand Down Expand Up @@ -180,21 +179,26 @@ async function main() {

const legacyContractAddress = '0x9e7A15E77e5E4f536b8215aaF778e786005D0f8d'

async function downloadData(
factory: OBSSStorage__factory,
provider: JsonRpcProvider,
signer: Signer
) {
const legacyContract = factory
.attach(legacyContractAddress)
.connect(provider)
.connect(signer)
async function downloadData(provider: Provider) {
const legacyContract = LegacyOBSSStorage__factory.connect(
legacyContractAddress,
provider
)

console.log(legacyContract.address)
const totalFeeds = await legacyContract.lastFeedId()
console.log(`Total feeds count: ${totalFeeds.toNumber()}`)
const legacyPosts: OBSSStorage.LegacyPostStruct[] = []
const legacyReactions: OBSSStorage.LegacyReactionStruct[] = []
const userToReaction = new Map<
string,
{
postId: BigNumber
reactionType: number
value: BigNumber
reactionOwner: string
}
>()
for (let i = 0; i < totalFeeds.toNumber(); i++) {
const postsInFeed = await legacyContract.lastFeedPostIds(i)
if (postsInFeed.toNumber() === 0) continue
Expand All @@ -204,52 +208,114 @@ async function downloadData(
0,
postsInFeed.toNumber()
)
console.log(`Feed ID: ${i}`)
feedPosts.forEach(async (post: OBSSStorage.PostStructOutput, j) => {

const [first] = feedPosts

if (first) {
const maxId = Math.max(
...legacyPosts.map(({ post }) => Number(post.commentsFeedId)),
0
)
for (
let prevFeed = maxId;
prevFeed < first.commentsFeedId.toNumber();
prevFeed += 1
) {
const metadata = await legacyContract.feeds(prevFeed)
legacyPosts.push({
post: {
author: '0x0000000000000000000000000000000000000000',
metadata,
commentsFeedId: BigNumber.from(prevFeed),
timestamp: first.timestamp.toNumber(),
},
feedId: 0,
})
}
}

feedPosts.forEach(async (post: OBSSStorage.PostStructOutput) => {
legacyPosts.push({
post: {
...post,
author: post.author,
metadata: {
digest: post.metadata.digest,
hashFunction: post.metadata.hashFunction,
size: post.metadata.size,
},
commentsFeedId: post.commentsFeedId,
timestamp: post.timestamp.toNumber(),
commentsFeedId: 0,
},
feedId: j,
feedId: i,
})
// Collect reactions
try {
const reactionId = await legacyContract.lastReactionIds(
const lastReactionId = await legacyContract.lastReactionIds(
post.metadata.digest
)
const reaction = await legacyContract.reactions(
post.metadata.digest,
reactionId
)
if (Number(reaction.reactionOwner) !== 0) {
legacyReactions.push({
reaction: {
value: 0,
reactionOwner: reaction.reactionOwner,
reactionType: reaction.reactionType,
},
post: {
metadata: {
digest: post.metadata.digest,
hashFunction: post.metadata.hashFunction,
size: post.metadata.size,
},
author: '0x0000000000000000000000000000000000000000',
commentsFeedId: 0,
timestamp: 0,
},
})
for (
let reactionId = 0;
reactionId < lastReactionId.toNumber();
reactionId++
) {
const reaction = await legacyContract.reactions(
post.metadata.digest,
reactionId
)

if (
userToReaction.has(
`${post.commentsFeedId}-${reaction.reactionOwner}`
) ||
reaction.reactionOwner ===
'0x0000000000000000000000000000000000000000'
)
continue

const currentReactionId = await legacyContract.reactionsUserToId(
post.metadata.digest,
reaction.reactionOwner
)

const currentReaction = currentReactionId.eq(reactionId)
? reaction
: await legacyContract.reactions(
post.metadata.digest,
currentReactionId.toNumber()
)

const { reactionType, value, reactionOwner } = currentReaction

userToReaction.set(
`${post.commentsFeedId}-${reaction.reactionOwner}`,
{
postId: post.commentsFeedId,
reactionType,
value,
reactionOwner,
}
)
}
} catch (e) {
console.error(e)
} catch (error) {
console.log(error)
}
})
}

const reactions = Array.from(userToReaction.values()).map((reaction) => ({
reaction,
}))

legacyReactions.push(...reactions)

const sorted = legacyPosts.sort(
(a, b) =>
(a.post.commentsFeedId as BigNumber).toNumber() -
(b.post.commentsFeedId as BigNumber).toNumber()
)

return {
legacyPosts,
legacyPosts: sorted,
legacyReactions,
}
}
Expand Down
Loading

0 comments on commit 39a1f2b

Please sign in to comment.