Skip to content

Commit

Permalink
feat: Do not build TreeMetadata into anchor merkle trees
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody committed May 29, 2024
1 parent 9820eb6 commit 6e9486b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 211 deletions.
133 changes: 0 additions & 133 deletions src/merkle/__tests__/bloom-metadata.test.ts

This file was deleted.

56 changes: 0 additions & 56 deletions src/merkle/bloom-metadata.ts

This file was deleted.

24 changes: 16 additions & 8 deletions src/merkle/merkle-car-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ import type { DiagnosticsLogger } from '@ceramicnetwork/common'
import { CARFactory, CAR } from 'cartonne'
import * as DAG_JOSE from 'dag-jose'
import {
IpfsLeafCompare,
MerkleTreeFactory,
IpfsMerge,
type MerkleTree,
type Node,
type PathDirection,
type CIDHolder,
type TreeMetadata,
type TreeMetadata, CompareFunction,
} from '@ceramicnetwork/anchor-utils'
import { BloomMetadata } from './bloom-metadata.js'
import type { Candidate } from '../services/candidate.js'
import type { CID } from 'multiformats/cid'
import {ICandidate} from "@ceramicnetwork/anchor-utils/lib/merkle/candidate.type.js";

const carFactory = new CARFactory()
carFactory.codecs.add(DAG_JOSE)

/**
* Simple Candidate sorter that sorts based on the CIDs of the leaf nodes. We actually don't need
* to sort the leaf nodes in the merkle tree at all anymore, but all the tree building code is set
* up to expect a sort function, so we just do this for simplicity for now.
*/
class CIDSort implements CompareFunction<ICandidate> {
compare(left: Node<ICandidate>, right: Node<ICandidate>): number {
return left.data.cid.toString().localeCompare(right.data.cid.toString());
}
}

export class CARIpfsService {
readonly car: CAR
constructor(car?: CAR) {
Expand Down Expand Up @@ -65,12 +75,10 @@ export class MerkleCAR implements IMerkleTree<CIDHolder, Candidate, TreeMetadata
}

export class MerkleCarFactory {
private readonly ipfsCompare: IpfsLeafCompare
private readonly bloomMetadata: BloomMetadata
private readonly ipfsCompare: CIDSort

constructor(private readonly logger: DiagnosticsLogger, private readonly depthLimit: number) {
this.ipfsCompare = new IpfsLeafCompare(this.logger)
this.bloomMetadata = new BloomMetadata()
this.ipfsCompare = new CIDSort()
}

async build(candidates: Candidate[]): Promise<MerkleCAR> {
Expand All @@ -79,7 +87,7 @@ export class MerkleCarFactory {
const factory = new MerkleTreeFactory<CIDHolder, Candidate, TreeMetadata>(
carMerge,
this.ipfsCompare,
this.bloomMetadata,
undefined,
this.depthLimit
)
const merkleTree = await factory.build(candidates)
Expand Down
16 changes: 3 additions & 13 deletions src/services/anchor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import type { Knex } from 'knex'
import type { IIpfsService } from './ipfs-service.type.js'
import type { IAnchorRepository } from '../repositories/anchor-repository.type.js'
import { REPEATED_READ_SERIALIZATION_ERROR } from '../repositories/repository-types.js'
import type { IMetadataService } from './metadata-service.js'
import { pathString, type CIDHolder, type TreeMetadata } from '@ceramicnetwork/anchor-utils'
import {pathString, type CIDHolder, type TreeMetadata, ICandidateMetadata} from '@ceramicnetwork/anchor-utils'
import { Candidate } from './candidate.js'
import { MerkleCarFactory, type IMerkleTree, type MerkleCAR } from '../merkle/merkle-car-factory.js'
import { IQueueConsumerService } from './queue/queue-service.type.js'
Expand Down Expand Up @@ -157,7 +156,6 @@ export class AnchorService {
private readonly anchorRepository: IAnchorRepository,
private readonly connection: Knex,
private readonly eventProducerService: EventProducerService,
private readonly metadataService: IMetadataService,
private readonly anchorBatchQueueService: IQueueConsumerService<AnchorBatchQMessage>,
private readonly merkleCarService: IMerkleCarService,
private readonly witnessService: IWitnessService
Expand Down Expand Up @@ -647,19 +645,11 @@ export class AnchorService {
*/
async _buildCandidates(requests: Request[]): Promise<Array<Candidate>> {
const candidates = []
const metadataByStreamId = await this.metadataService
.batchRetrieve(requests.map((r) => StreamID.fromString(r.streamId)))
.then((metadata) => {
return Object.fromEntries(metadata.map((m) => [m.streamId.toString(), m]))
})

for (const request of requests) {
const streamId = StreamID.fromString(request.streamId)
const metadata = metadataByStreamId[request.streamId]
if (metadata) {
const candidate = new Candidate(streamId, request, metadata.metadata)
candidates.push(candidate)
}
const candidate = new Candidate(streamId, request, null as unknown as ICandidateMetadata)
candidates.push(candidate)
}
// Make sure we process candidate streams in order of their earliest request.
candidates.sort(Candidate.sortByTimestamp)
Expand Down
1 change: 0 additions & 1 deletion src/services/candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class Candidate implements ICandidate {
if (!request.cid) throw new Error(`No CID present for request`)
this.cid = CID.parse(request.cid)
this.metadata = metadata
this.model = this.metadata.model
}

/**
Expand Down

0 comments on commit 6e9486b

Please sign in to comment.