Skip to content

Commit

Permalink
feat: log when we encouter unknow event hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sameh-farouk committed May 13, 2024
1 parent 9e8000a commit f0a83b5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 180 deletions.
4 changes: 2 additions & 2 deletions src/mappings/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function contractCreated(
}

if (!contractEvent) {
ctx.log.error(`found contract with unknown version! make sure types are updated`);
ctx.log.error({eventName: item.name}, `found contract with unknown version! make sure types are updated`);
return
}

Expand Down Expand Up @@ -191,7 +191,7 @@ export async function contractUpdated(
}

if (!contractEvent) {
ctx.log.error(`found contract with unknown version! make sure types are updated`);
ctx.log.error({eventName: item.name}, `found contract with unknown version! make sure types are updated`);
return
}

Expand Down
183 changes: 9 additions & 174 deletions src/mappings/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export async function farmStored(
} else if (farmStoredEvent.isV63) {
farmStoredEventParsed = farmStoredEvent.asV63
} else if (farmStoredEvent.isV101) {
let eventValue = item.event.args as v63.Farm
eventValue.dedicatedFarm = false
farmStoredEventParsed = farmStoredEvent.asV101
}

if (!farmStoredEventParsed) return
if (!farmStoredEventParsed) {
ctx.log.error({eventName: item.name}, `found farm with unknown version! make sure types are updated`);
return
}

const newFarm = new Farm()

Expand Down Expand Up @@ -92,8 +93,6 @@ export async function farmUpdated(
}
}
} else if (farmUpdatedEvent.isV101) {
let eventValue = item.event.args as v63.Farm
eventValue.dedicatedFarm = false
farmUpdatedEventParsed = farmUpdatedEvent.asV101
switch (farmUpdatedEvent.asV101.certification.__kind) {
case "Gold": {
Expand All @@ -102,7 +101,10 @@ export async function farmUpdated(
}
}

if (!farmUpdatedEventParsed) return
if (!farmUpdatedEventParsed) {
ctx.log.error({eventName: item.name}, `found farm with unknown version! make sure types are updated`);
return
}

const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmUpdatedEventParsed.id } })
if (!savedFarm) return
Expand Down Expand Up @@ -201,6 +203,7 @@ export async function farmCertificationSet(
const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmID } })

if (!savedFarm) {
ctx.log.error({eventName: item.name}, `found FarmCertification with unknown version! make sure types are updated`);
return
}

Expand All @@ -217,171 +220,3 @@ export async function farmCertificationSet(
savedFarm.certification = certType
await ctx.store.save<Farm>(savedFarm)
}

export async function farmCreateOrUpdateOrDelete(ctx: Ctx): Promise<[Farm[], Farm[], Farm[], FarmWithIPs[]]> {
let newFarms: Farm[] = []
let updatedFarms: Farm[] = []
let deletedFarms: Farm[] = []
let publicIPs: FarmWithIPs[] = []

for (let block of ctx.blocks) {
for (let item of block.items) {
if (item.name === "TfgridModule.FarmStored") {
const farmStoredEvent = new TfgridModuleFarmStoredEvent(ctx, item.event)
let farmStoredEventParsed
if (farmStoredEvent.isV9) {
farmStoredEventParsed = farmStoredEvent.asV9
} else if (farmStoredEvent.isV50) {
farmStoredEventParsed = farmStoredEvent.asV50
} else if (farmStoredEvent.isV63) {
farmStoredEventParsed = farmStoredEvent.asV63
} else if (farmStoredEvent.isV101) {
farmStoredEventParsed = farmStoredEvent.asV101
}

if (!farmStoredEventParsed) continue

const newFarm = new Farm()

let eventID = item.event.id

newFarm.id = eventID
newFarm.gridVersion = farmStoredEventParsed.version
newFarm.farmID = farmStoredEventParsed.id
newFarm.name = validateString(ctx, farmStoredEventParsed.name.toString())
newFarm.twinID = farmStoredEventParsed.twinId
newFarm.pricingPolicyID = farmStoredEventParsed.pricingPolicyId
newFarm.dedicatedFarm = false
newFarm.certification = FarmCertification.NotCertified

newFarm.publicIPs = []

let ips: PublicIp[] = []
farmStoredEventParsed.publicIps.forEach(ip => {
// ctx.log.info("storing ips")
const newIP = new PublicIp()

newIP.id = eventID

newIP.ip = validateString(ctx, ip.ip.toString())
newIP.gateway = validateString(ctx, ip.gateway.toString())

newIP.contractId = ip.contractId
newIP.farm = newFarm

ips.push(newIP)
})

// ctx.log.info(`storing farm: ${newFarm.id}`)

newFarms.push(newFarm)
publicIPs.push(new FarmWithIPs(newFarm.farmID, ips))
}
if (item.name === "TfgridModule.FarmUpdated") {
const farmUpdatedEvent = new TfgridModuleFarmUpdatedEvent(ctx, item.event)

let farmUpdatedEventParsed: any
if (farmUpdatedEvent.isV9) {
farmUpdatedEventParsed = farmUpdatedEvent.asV9
} else if (farmUpdatedEvent.isV50) {
farmUpdatedEventParsed = farmUpdatedEvent.asV50
} else if (farmUpdatedEvent.isV63) {
farmUpdatedEventParsed = farmUpdatedEvent.asV63
} else if (farmUpdatedEvent.isV101) {
farmUpdatedEventParsed = farmUpdatedEvent.asV101
}

if (!farmUpdatedEventParsed) {
ctx.log.error('cannot parse farm updated event')
continue
}

const eventID = item.event.id

const foundInNewListIndex: number = newFarms.findIndex(t => t.farmID == farmUpdatedEventParsed.id)
if (foundInNewListIndex != -1) {
const savedFarm: Farm = newFarms[foundInNewListIndex]
savedFarm.gridVersion = farmUpdatedEventParsed.version
savedFarm.name = validateString(ctx, farmUpdatedEventParsed.name.toString())
savedFarm.twinID = farmUpdatedEventParsed.twinId
savedFarm.pricingPolicyID = farmUpdatedEventParsed.pricingPolicyId
// const pubIps = updatePublicIPs(ctx, farmUpdatedEventParsed.publicIps, eventID, savedFarm)
newFarms[foundInNewListIndex] = savedFarm

publicIPs = getPublicIPs(ctx, savedFarm, publicIPs, farmUpdatedEventParsed.publicIps, eventID)

continue
}

const foundInUpdatedListIndex: number = updatedFarms.findIndex(t => t.farmID == farmUpdatedEventParsed.id)
if (foundInUpdatedListIndex != -1) {
let savedFarm: Farm = updatedFarms[foundInUpdatedListIndex]
savedFarm.gridVersion = farmUpdatedEventParsed.version
savedFarm.name = validateString(ctx, farmUpdatedEventParsed.name.toString())
savedFarm.twinID = farmUpdatedEventParsed.twinId
savedFarm.pricingPolicyID = farmUpdatedEventParsed.pricingPolicyId
updatedFarms[foundInUpdatedListIndex] = savedFarm

publicIPs = getPublicIPs(ctx, savedFarm, publicIPs, farmUpdatedEventParsed.publicIps, eventID)

continue
}

const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmUpdatedEventParsed.id } })
if (!savedFarm) continue

savedFarm.gridVersion = farmUpdatedEventParsed.version
savedFarm.name = validateString(ctx, farmUpdatedEventParsed.name.toString())
savedFarm.twinID = farmUpdatedEventParsed.twinId
savedFarm.pricingPolicyID = farmUpdatedEventParsed.pricingPolicyId

// ctx.log.info(`updating farm: ${savedFarm.id}`)

publicIPs = getPublicIPs(ctx, savedFarm, publicIPs, farmUpdatedEventParsed.publicIps, eventID)

updatedFarms.push(savedFarm)
}
if (item.name === "TfgridModule.FarmDeleted") {
const farmID = new TfgridModuleFarmDeletedEvent(ctx, item.event).asV49
const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmID } })
if (savedFarm) {
deletedFarms.push(savedFarm)
}
}
}
}

return [newFarms, updatedFarms, deletedFarms, publicIPs]
}

function getPublicIPs(ctx: Ctx, farm: Farm, savedFarmIps: FarmWithIPs[], newIps: PublicIp[], eventID: any): FarmWithIPs[] {
let toModify = savedFarmIps.filter(f => f.farmID === farm.farmID)
if (toModify.length === 0) {
return []
}

// For every IP in the updated event:
// Check if it's already in the savedFarmIps to be saved, if so, update the value
// If it's not there, add it
newIps.forEach((ip: PublicIp) => {
let foundIdx = toModify[0].publicIPs.findIndex(pubip => pubip.ip === ip.ip.toString())
// console.log(`found index: ${foundIdx}`)
if (foundIdx !== -1) {
toModify[0].publicIPs[foundIdx].contractId = ip.contractId
toModify[0].publicIPs[foundIdx].ip = validateString(ctx, ip.ip.toString())
toModify[0].publicIPs[foundIdx].gateway = validateString(ctx, ip.gateway.toString())
} else {
const newIP = new PublicIp()
newIP.id = eventID
newIP.ip = ip.ip.toString()
newIP.gateway = validateString(ctx, ip.gateway.toString())
newIP.contractId = ip.contractId
newIP.farm = farm
// ctx.log.info(`saving new ip: ${newIP.ip}`)
// ctx.log.warn(`farm ips: ${toModify[0].publicIPs}`)
toModify[0].publicIPs.push(newIP)
}
})

return savedFarmIps
}
8 changes: 6 additions & 2 deletions src/mappings/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function nodeStored(
}

if (!nodeEvent) {
ctx.log.error({eventName: item.name}, `found Node with unknown version! make sure types are updated`);
return
}

Expand Down Expand Up @@ -176,8 +177,10 @@ export async function nodeUpdated(
nodeEvent = node.asV118
}

if (!nodeEvent) return

if (!nodeEvent) {
ctx.log.error({eventName: item.name}, `found Node with unknown version! make sure types are updated`);
return
}
const savedNode = await ctx.store.get(Node, { where: { nodeID: nodeEvent.id }, relations: { location: true, interfaces: true } })

if (!savedNode) return
Expand Down Expand Up @@ -692,6 +695,7 @@ function getNodePublicConfig(ctx: Ctx, node: TfgridModuleNodeStoredEvent): NodeP
}
}
} else {
ctx.log.error(`found Node with unknown version during parsing NodePublicConfig! make sure types are updated`);
return null
}

Expand Down
8 changes: 6 additions & 2 deletions src/mappings/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export async function pricingPolicyStored(
pricingPolicyEventParsed = pricingPolicyEvent.asV101
}

if (!pricingPolicyEventParsed) return

if (!pricingPolicyEventParsed) {
ctx.log.error({eventName: item.name}, `found pricingPolicy with unknown version! make sure types are updated`);
return
}
let pricingPolicy = new PricingPolicy()
pricingPolicy.id = item.event.id

Expand Down Expand Up @@ -86,6 +88,7 @@ export async function farmingPolicyStored(
const farmingPolicyEvent = new TfgridModuleFarmingPolicyStoredEvent(ctx, item.event)

if (!farmingPolicyEvent.isV63) {
ctx.log.error({eventName: item.name}, `found pricingPolicy with unknown version! make sure types are updated`);
return
}

Expand Down Expand Up @@ -140,6 +143,7 @@ export async function farmingPolicyUpdated(
const farmingPolicyEvent = new TfgridModuleFarmingPolicyUpdatedEvent(ctx, item.event)

if (!farmingPolicyEvent.isV63) {
ctx.log.error({eventName: item.name}, `found pricingPolicy with unknown version! make sure types are updated`);
return
}

Expand Down
1 change: 1 addition & 0 deletions src/mappings/serviceContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export async function serviceContractCanceled(
serviceContractCanceledEvent = SmartContractModuleServiceContractCanceled.asV148
}
if (!serviceContractCanceledEvent) {
ctx.log.error({eventName: item.name}, `found serviceContract with unknown version! make sure types are updated`);
return
}
const savedServiceContract = await ctx.store.get(ServiceContract, { where: { serviceContractID: serviceContractCanceledEvent.serviceContractId } })
Expand Down

0 comments on commit f0a83b5

Please sign in to comment.