Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

fix: make total-capacity(offer) and size(agreement) number type #367

Merged
merged 11 commits into from
Nov 9, 2020
37 changes: 31 additions & 6 deletions src/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ export function sequelizeFactory (): Sequelize {
}

/**
* consider that the field will be stored as string in the data base,
* so you not be able to use number comparision when querying
* BigNumberStringType for sequelize models
* BigNumber getter/setter functions for sequelize column declaration
* @param propName
* @constructor
*/
export function BigNumberStringType (propName: string): Partial<ModelAttributeColumnOptions> {
export function bigNumberGetterSetter (propName: string): Partial<ModelAttributeColumnOptions> {
return {
type: DataType.STRING(),
get (this: Model): BigNumber {
return new BigNumber(this.getDataValue(propName as any))
},
Expand All @@ -58,6 +54,35 @@ export function BigNumberStringType (propName: string): Partial<ModelAttributeCo
}
}

/**
* Consider that the field will be stored as a string in the database,
* so you won't be able to use number comparison when querying and also
* data are ordered lexicographically so most probably ordering won't work
* as you would expect.
* @param propName
* @constructor
*/
export function BigNumberStringType (propName: string): Partial<ModelAttributeColumnOptions> {
return {
type: DataType.STRING(),
...bigNumberGetterSetter(propName)
}
}

/**
* Consider that the field will be stored as BigInt in the database.
* Based on the database engine this might have limited precision.
* Using comparison and ordering is supported.
* @param propName
* @constructor
*/
export function BigNumberBigIntType (propName: string): Partial<ModelAttributeColumnOptions> {
return {
type: DataType.BIGINT(),
...bigNumberGetterSetter(propName)
}
}

/**
* consider that the field will be stored as string separated by '|' symbol in the database,
* so you not be able to use array comparision when querying
Expand Down
7 changes: 6 additions & 1 deletion src/services/storage/handlers/offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ const handlers: { [key: string]: Function } = {
const flag = firstMsg.substring(2, 4)

if (flag === '01') { // PeerId definition
offer.peerId = decodeByteArray([`0x${firstMsg.substring(4)}`, ...restMsg])
const newPeerId = decodeByteArray([`0x${firstMsg.substring(4)}`, ...restMsg])

if (offer.peerId === newPeerId) {
return
}

offer.peerId = newPeerId
await offer.save()

if (offerService.emit) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/storage/models/agreement.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Table, Column, Model, ForeignKey, BelongsTo, DataType } from 'sequelize
import BigNumber from 'bignumber.js'

import Offer from './offer.model'
import { BigNumberStringType } from '../../../sequelize'
import { BigNumberBigIntType, BigNumberStringType } from '../../../sequelize'
import { bnFloor } from '../../../utils'

@Table({
Expand All @@ -21,7 +21,7 @@ export default class Agreement extends Model {
consumer!: string

// In Megabytes
@Column({ ...BigNumberStringType('size') })
@Column({ ...BigNumberBigIntType('size') })
size!: BigNumber

@Column({ defaultValue: true })
Expand Down
6 changes: 3 additions & 3 deletions src/services/storage/models/offer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { Literal } from 'sequelize/types/lib/utils'
import BillingPlan from './billing-plan.model'
import { SupportedTokens } from '../../../definitions'
import Agreement from './agreement.model'
import { BigNumberStringType } from '../../../sequelize'
import { WEI } from '../utils'
import { BigNumberBigIntType } from '../../../sequelize'

@Scopes(() => ({
active: {
where: {
totalCapacity: { [Op.ne]: '0' }
totalCapacity: { [Op.gt]: 0 }
// peerId: { [Op.ne]: null }
},
include: [
Expand All @@ -28,7 +28,7 @@ export default class Offer extends Model {
@Column({ primaryKey: true, type: DataType.STRING(64) })
provider!: string

@Column({ ...BigNumberStringType('totalCapacity') })
@Column({ ...BigNumberBigIntType('totalCapacity') })
totalCapacity!: BigNumber

@Column
Expand Down