Skip to content

Commit

Permalink
add version
Browse files Browse the repository at this point in the history
  • Loading branch information
crypticmeta committed Sep 19, 2024
1 parent 8fca3e1 commit 3254232
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 131 deletions.
100 changes: 51 additions & 49 deletions indexer/src/insertSkippedBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,54 +288,56 @@ const processInscription = async (inscription_id: string, bulkOps: any) => {
};

export const handlePreSaveLogic = async (bulkDocs: Array<Partial<any>>) => {

console.log({bulkDocs: bulkDocs.length})
const transformedBulkOps: any[] = [];


// Pre-compute the maximum existing version for each unique SHA
// const uniqueShas = [
// ...new Set(insertOps.map((doc:any) => doc.updateOne.update.$set.sha)),
// ];
const uniqueShas = [
...new Set(bulkDocs.map((doc:any) => doc.updateOne.update.$set.sha)),
];


// const latestDocumentsWithSameShas = await Inscription.aggregate([
// {
// $match: {
// sha: { $in: uniqueShas },
// },
// },
// {
// $sort: { sha: 1, version: -1 }, // Sort by sha and then by version descending
// },
// {
// $group: {
// _id: "$sha", // Group by sha
// doc: { $first: "$$ROOT" }, // Get the first document in each group (i.e., the one with the highest version)
// },
// },
// {
// $replaceRoot: { newRoot: "$doc" }, // Replace the root with the document itself
// },
// ]);

// console.log(`total unique shas... ${uniqueShas.length}`)
// console.log('total docs found in db with same sha...', latestDocumentsWithSameShas.length)

// for (const sha of uniqueShas) {
// if (sha) {
// const latestDocumentWithSameShaMatch = latestDocumentsWithSameShas.filter(a=>a.sha === sha);

// if(latestDocumentWithSameShaMatch.length>1){
// throw Error("Multiple docs with same sha received");
// }
// const latestDocumentWithSameSha = latestDocumentWithSameShaMatch[0]
// //@ts-ignore
// shaMap[sha] = latestDocumentWithSameSha
// ? latestDocumentWithSameSha.version
// : 0;
// }
// }
const latestDocumentsWithSameShas = await Inscription.aggregate([
{
$match: {
sha: { $in: uniqueShas },
},
},
{
$sort: { sha: 1, version: -1 }, // Sort by sha and then by version descending
},
{
$group: {
_id: "$sha", // Group by sha
doc: { $first: "$$ROOT" }, // Get the first document in each group (i.e., the one with the highest version)
},
},
{
$replaceRoot: { newRoot: "$doc" }, // Replace the root with the document itself
},
]);

console.log(`total unique shas... ${uniqueShas.length}`)
console.log('total docs found in db with same sha...', latestDocumentsWithSameShas.length)


let shaMap: Map<string, number> = new Map();

for (const sha of uniqueShas) {
if (sha) {
const latestDocumentWithSameShaMatch = latestDocumentsWithSameShas.filter(a=>a.sha === sha);

if(latestDocumentWithSameShaMatch.length>1){
throw Error("Multiple docs with same sha received");
}
const latestDocumentWithSameSha = latestDocumentWithSameShaMatch[0]
//@ts-ignore
shaMap[sha] = latestDocumentWithSameSha
? latestDocumentWithSameSha.version
: 0;
}
}


for (let i = 0; i < bulkDocs.length; i++) {
Expand Down Expand Up @@ -372,14 +374,14 @@ export const handlePreSaveLogic = async (bulkDocs: Array<Partial<any>>) => {
// }

// Updated SHA version logic
// if (doc.sha && !doc.token) {
// if (shaMap[doc.sha] != null) {
// shaMap[doc.sha]++;
// } else {
// shaMap[doc.sha] = 1;
// }
// doc.version = shaMap[doc.sha];
// }
if (doc.sha && !doc.token) {
if (shaMap.has(doc.sha)) {
shaMap.set(doc.sha, shaMap.get(doc.sha)! + 1); // Use get and set for Map
} else {
shaMap.set(doc.sha, 1);
}
doc.version = shaMap.get(doc.sha); // Access the version from the Map
}

if(doc.sha && doc.token){
doc.sha = null;
Expand Down
83 changes: 1 addition & 82 deletions indexer/src/models/inscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,7 @@ export const inscriptionSchema = new mongoose.Schema(
type: Date,
},
delegate: { type: String },
// children: { type: Array }, //remove
// next: { type: String }, //remove
// previous: { type: String }, //remove
// parent: { type: String }, // should be an array

// genesis_address: { type: String }, // remove
// genesis_fee: { type: Number }, // remove
// genesis_height: { type: Number, index: true },
// genesis_transaction: { type: String }, // remove
// flagged: { type: Boolean }, // keep only true values
// banned: { type: Boolean }, // keep only true value
// reason: { type: String }, // keep only true values
// updated_by: { type: String }, // remove
// block: { type: Number }, // remove
// content_length: { type: Number }, // remove
genesis_height: { type: Number, index: true },
content_type: { type: String },
// collection detail
official_collection: {
Expand All @@ -70,50 +56,6 @@ export const inscriptionSchema = new mongoose.Schema(
},
collection_item_name: { type: String, set: (v: string) => v.trim() },
collection_item_number: { type: Number },
attributes: { type: [attributeSchema] }, // remove empty values
// sat details

// sat_timestamp: {
// // remove
// type: Date,
// },
// cycle: { type: Number }, // remove
// decimal: { type: String }, // remove
// degree: { type: String }, // remove
// epoch: { type: Number }, // remove
// percentile: { type: String }, // remove
// period: { type: Number }, // remove
// rarity: { type: String }, // remove
// sat: { type: Number },
// sat_name: { type: String },
// sat_offset: { type: Number }, // remove
// lists: [{ type: Schema.Types.ObjectId, ref: "Collection" }], // remove
// tags: {
// type: Array,
// required: false,
// validate: {
// validator: function (tags: any[]) {
// const pattern = /^[^A-Z]+$/;
// return tags.every((tag) => pattern.test(tag));
// },

// message: () =>
// `Tags should only contain lowercase letters and hyphens.`,
// },
// },
// error: {
// type: Boolean,
// default: false,
// validate: {
// validator: function (this: any, value: boolean) {
// if (value) console.log(this.inscription_id, "error here");
// return !value || (value === true && !!this.error_tag);
// },
// message: 'If "error" is set to true, "error_tag" must be provided.',
// },
// },
// error_retry: { type: Number, default: 0 },
// error_tag: { type: String, default: null },
offset: { type: Number },
output_value: { type: Number },
address: {
Expand Down Expand Up @@ -166,35 +108,12 @@ export const inscriptionSchema = new mongoose.Schema(
unsigned_psbt: { type: String },
in_mempool: { type: Boolean, default: false },
txid: { type: String },
// sat_block_time: { type: Date }, // remove
// sattributes: [{ type: String }], // remove
// last_checked: { type: Date }, //remove
version: { type: Number },
token: { type: Boolean },
// domain_name: { type: String, set: (v: string) => v.trim() }, // remove
// domain_valid: { type: Boolean }, // remove

// new fields (metadata + metaprotocols)
// charms: { type: Number },
// charms_array: { type: [String] },
// transfer_valid: { type: Boolean }, // remove
metaprotocol: { type: String },
// parsed_metaprotocol: {
// type: [String],
// set: function (value: string) {
// // Check if the value is a string and not empty
// if (typeof value === "string" && value.trim().length > 0) {
// // Split the string by a delimiter (e.g., comma), trim and convert each part to lowercase
// return value.split(":").map((item) => item.trim().toLowerCase());
// } else {
// return [];
// }
// },
// },
metadata: {
type: Schema.Types.Mixed,
},
// valid: { type: Boolean },
},
{
timestamps: { createdAt: "created_at", updatedAt: "updated_at" },
Expand Down

0 comments on commit 3254232

Please sign in to comment.