Skip to content

Commit

Permalink
fix: node create / update
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanVerstraete committed Aug 29, 2022
1 parent 3f5b3c7 commit 3c2ef27
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 47 deletions.
2 changes: 1 addition & 1 deletion indexer/chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ name: tfchainindexer
description: Helm Chart for the tfchain hydra indexer
version: 2.4.0
apiVersion: v2
appVersion: "2.5.9"
appVersion: "2.5.10"
2 changes: 1 addition & 1 deletion processor-chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ apiVersion: v2
name: tfchain-processor
description: A chart for the tfchain graphql processor and query node
version: 1.0.4
appVersion: "2.5.9"
appVersion: "2.5.10"
1 change: 0 additions & 1 deletion src/mappings/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as v63 from '../types/v63'
export async function farmStored(ctx: EventHandlerContext) {
const farmStoredEvent = new TfgridModuleFarmStoredEvent(ctx)

console.log(ctx._chain.getEventHash('tfgridModule.FarmStored'))
let farmStoredEventParsed
if (farmStoredEvent.isV9) {
farmStoredEventParsed = farmStoredEvent.asV9
Expand Down
150 changes: 112 additions & 38 deletions src/mappings/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SmartContractModuleNodeMarkedAsDedicatedEvent, TfgridModuleNodeCertific

export async function nodeStored(ctx: EventHandlerContext) {
const node = new TfgridModuleNodeStoredEvent(ctx)

let nodeEvent
if (node.isV9) {
nodeEvent = node.asV9
Expand All @@ -17,9 +18,13 @@ export async function nodeStored(ctx: EventHandlerContext) {
nodeEvent = node.asV63
} else if (node.isV101) {
nodeEvent = node.asV101
} else if (node.isV105) {
nodeEvent = node.asV105
}

if (!nodeEvent) return
if (!nodeEvent) {
return
}

const newNode = new Node()
newNode.id = ctx.event.id
Expand All @@ -46,19 +51,30 @@ export async function nodeStored(ctx: EventHandlerContext) {

newNode.location = newLocation

const pubConfig = getNodePublicConfig(node)
const newPubConfig = new PublicConfig()
newPubConfig.id = ctx.event.id
newPubConfig.ipv4 = pubConfig?.ip4
newPubConfig.gw4 = pubConfig?.gw4
newPubConfig.ipv6 = pubConfig?.ip6
newPubConfig.gw6 = pubConfig?.gw6
newPubConfig.node = newNode

if (node.isV43) {
const nodeAsV43 = node.asV43
newNode.secure = nodeAsV43.secureBoot ? true : false
newNode.virtualized = nodeAsV43.virtualized ? true : false
newNode.serialNumber = nodeAsV43.serialNumber.toString()
}

if (node.isV101 || node.isV63) {
if (node.isV105 || node.isV101 || node.isV63) {
let nodeEvent
if (node.isV101) {
nodeEvent = node.asV101
} else if (node.isV63) {
nodeEvent = node.asV63
} else if (node.isV105) {
nodeEvent = node.asV105
} else {
return
}
Expand All @@ -80,20 +96,6 @@ export async function nodeStored(ctx: EventHandlerContext) {

await ctx.store.save<NodeResourcesTotal>(resourcesTotal)

if (nodeEvent.publicConfig) {
const pubConfig = new PublicConfig()
pubConfig.node = newNode
pubConfig.id = ctx.event.id
pubConfig.ipv4 = nodeEvent.publicConfig.ipv4.toString()
pubConfig.ipv6 = nodeEvent.publicConfig.ipv6.toString()
pubConfig.gw4 = nodeEvent.publicConfig.gw4.toString()
pubConfig.gw6 = nodeEvent.publicConfig.gw6.toString()
pubConfig.domain = nodeEvent.publicConfig.domain.toString() || ''

await ctx.store.save<PublicConfig>(pubConfig)
newNode.publicConfig = pubConfig
}

newNode.interfaces = []

const interfacesPromisses = nodeEvent.interfaces.map(async intf => {
Expand Down Expand Up @@ -125,6 +127,8 @@ export async function nodeUpdated(ctx: EventHandlerContext) {
nodeEvent = node.asV63
} else if (node.isV101) {
nodeEvent = node.asV101
} else if (node.isV105) {
nodeEvent = node.asV105
}

if (!nodeEvent) return
Expand Down Expand Up @@ -200,12 +204,14 @@ export async function nodeUpdated(ctx: EventHandlerContext) {
}
}

if (node.isV101 || node.isV63) {
if (node.isV105 || node.isV101 || node.isV63) {
let nodeEvent
if (node.isV101) {
nodeEvent = node.asV101
} else if (node.isV63) {
nodeEvent = node.asV63
}else if (node.isV105) {
nodeEvent = node.asV105
} else {
return
}
Expand Down Expand Up @@ -282,17 +288,7 @@ export async function nodeDeleted(ctx: EventHandlerContext) {
return ctx.store.remove(intf)
})
await Promise.all(promises)
// console.log("-------------DELETING NODE-------------")
// console.log(savedNode.interfaces)

// if (savedNode.interfaces) {
// console.log(savedNode.interfaces)
// const promises = savedNode.interfaces.map(async int => {
// const intf = await ctx.store.get(Interfaces, { where: { id: int.id }})
// return ctx.store.remove(intf)
// })
// await Promise.all(promises)
// }

await ctx.store.remove(savedNode)
}
}
Expand Down Expand Up @@ -368,16 +364,6 @@ export async function nodePublicConfigStored(ctx: EventHandlerContext) {
}
}

// export async function nodeMarkedAsDedicated(ctx: EventHandlerContext) {
// const [nodeID, dedicated] = new SmartContractModuleNodeMarkedAsDedicatedEvent(ctx).asV63

// const savedNode = await ctx.store.get(Node, { where: { nodeID: nodeID } })
// if (!savedNode) return

// savedNode.dedicated = dedicated
// await ctx.store.save<Node>(savedNode)
// }

export async function nodeCertificationSet(ctx: EventHandlerContext) {
const [nodeID, certification] = new TfgridModuleNodeCertificationSetEvent(ctx).asV63

Expand All @@ -397,4 +383,92 @@ export async function nodeCertificationSet(ctx: EventHandlerContext) {
savedNode.certification = certType

await ctx.store.save<Node>(savedNode)
}


interface NodePublicConfig {
ip4: string
gw4: string
ip6: string
gw6: string
domain: string
}

function getNodePublicConfig(node: TfgridModuleNodeStoredEvent): NodePublicConfig | null | undefined {
let nodeEvent
if (node.isV9) {
nodeEvent = node.asV9
if (nodeEvent.publicConfig) {
return {
ip4: nodeEvent.publicConfig?.ipv4.toString(),
gw4: nodeEvent.publicConfig?.gw4.toString(),
ip6: nodeEvent.publicConfig?.ipv6.toString(),
gw6: nodeEvent.publicConfig?.gw6.toString(),
domain: nodeEvent.publicConfig?.domain.toString()
}
}
} else if (node.isV28) {
nodeEvent = node.asV28
if (nodeEvent.publicConfig) {
return {
ip4: nodeEvent.publicConfig?.ipv4.toString(),
gw4: nodeEvent.publicConfig?.gw4.toString(),
ip6: nodeEvent.publicConfig?.ipv6.toString(),
gw6: nodeEvent.publicConfig?.gw6.toString(),
domain: nodeEvent.publicConfig?.domain.toString()
}
}
} else if (node.isV43) {
nodeEvent = node.asV43
if (nodeEvent.publicConfig) {
return {
ip4: nodeEvent.publicConfig?.ipv4.toString(),
gw4: nodeEvent.publicConfig?.gw4.toString(),
ip6: nodeEvent.publicConfig?.ipv6.toString(),
gw6: nodeEvent.publicConfig?.gw6.toString(),
domain: nodeEvent.publicConfig?.domain.toString()
}
}
} else if (node.isV63) {
nodeEvent = node.asV63
if (nodeEvent.publicConfig) {
return {
ip4: nodeEvent.publicConfig?.ipv4.toString(),
gw4: nodeEvent.publicConfig?.gw4.toString(),
ip6: nodeEvent.publicConfig?.ipv6.toString(),
gw6: nodeEvent.publicConfig?.gw6.toString(),
domain: nodeEvent.publicConfig?.domain.toString()
}
}
} else if (node.isV101) {
nodeEvent = node.asV101
if (nodeEvent.publicConfig) {
return {
ip4: nodeEvent.publicConfig?.ipv4.toString(),
gw4: nodeEvent.publicConfig?.gw4.toString(),
ip6: nodeEvent.publicConfig?.ipv6.toString(),
gw6: nodeEvent.publicConfig?.gw6.toString(),
domain: nodeEvent.publicConfig?.domain.toString()
}
}
} else if (node.isV105) {
nodeEvent = node.asV105
if (nodeEvent.publicConfig) {
let domain = ''
if (nodeEvent.publicConfig.domain) {
domain = nodeEvent.publicConfig.domain.toString()
}
return {
ip4: nodeEvent.publicConfig?.ip4.ip.toString(),
gw4: nodeEvent.publicConfig?.ip4.gw.toString(),
ip6: nodeEvent.publicConfig?.ip6?.ip.toString() || '',
gw6: nodeEvent.publicConfig?.ip6?.gw.toString() || '',
domain
}
}
} else {
return null
}

return null
}
30 changes: 24 additions & 6 deletions src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,14 +894,23 @@ export class TfgridModuleNodeStoredEvent {
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isV105(): boolean {
return this.ctx._chain.getEventHash('tfgridModule.NodeStored') === '050dcf105bd6f9b530372be57813e46ed54da9003777956afde8cea580623abf'
}

get asV105(): v105.Node {
assert(this.isV105)
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isLatest(): boolean {
deprecateLatest()
return this.isV101
return this.isV105
}

get asLatest(): v62.Node {
get asLatest(): v105.Node {
deprecateLatest()
return this.asV101
return this.asV105
}
}

Expand Down Expand Up @@ -955,14 +964,23 @@ export class TfgridModuleNodeUpdatedEvent {
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isV105(): boolean {
return this.ctx._chain.getEventHash('tfgridModule.NodeStored') === '050dcf105bd6f9b530372be57813e46ed54da9003777956afde8cea580623abf'
}

get asV105(): v105.Node {
assert(this.isV105)
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isLatest(): boolean {
deprecateLatest()
return this.isV101
return this.isV105
}

get asLatest(): v62.Node {
get asLatest(): v105.Node {
deprecateLatest()
return this.asV101
return this.asV105
}
}

Expand Down

0 comments on commit 3c2ef27

Please sign in to comment.