Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs module #186

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/fs/challenge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Address } from '../crypto';
import { hex2VarBytes, StringReader } from '../utils';
import {
dateToUnixTime,
decodeAddress,
decodeVarBytes,
decodeVarUint,
serializeAddress,
serializeVarUint,
unixTimeToDate
} from './utils';

export class Challenge {
static deserializeHex(hex: string): Challenge {
const sr: StringReader = new StringReader(hex);
const fileHash = decodeVarBytes(sr);
const fileOwner = decodeAddress(sr);
const nodeAddr = decodeAddress(sr);
const challengeHeight = decodeVarUint(sr);
const reward = decodeVarUint(sr);
const expiredTime = unixTimeToDate(decodeVarUint(sr));
const state = decodeVarUint(sr);

return new Challenge(
fileHash,
fileOwner,
nodeAddr,
challengeHeight,
reward,
expiredTime,
state
);
}
public constructor(
public readonly fileHash: string,
public readonly fileOwner: Address = new Address('0'.repeat(40)),
public readonly nodeAddr: Address = new Address('0'.repeat(40)),
public readonly challengeHeight: number = 0,
public readonly reward: number = 0,
public readonly expiredTime: Date = new Date(),
public readonly state: number = 0
) { }

public serializeHex(): string {
return (
hex2VarBytes(this.fileHash) +
serializeAddress(this.fileOwner) +
serializeAddress(this.nodeAddr) +
serializeVarUint(this.challengeHeight) +
serializeVarUint(this.reward) +
serializeVarUint(dateToUnixTime(this.expiredTime)) +
serializeVarUint(this.state)
);
}

public export() {
return {
fileHash: this.fileHash,
fileOwner: this.fileOwner.value,
nodeAddr: this.nodeAddr.value,
challengeHeight: this.challengeHeight,
reward: this.reward,
expiredTime: this.expiredTime,
state: this.state
};
}
}

export class ChallengeList {

static deserializeHex(hex: string): ChallengeList {
const sr: StringReader = new StringReader(hex);
const challenges: Challenge[] = [];
const count = decodeVarUint(sr);
for (let i = 0; i < count; i++) {
const challenge = Challenge.deserializeHex(decodeVarBytes(sr));
challenges.push(challenge);
}
return new ChallengeList(challenges);
}
public constructor(public challenges: Challenge[]) { }

public serializeHex(): string {
let str = '';
str += serializeVarUint(this.challenges.length);
for (const challenge of this.challenges) {
str += hex2VarBytes(challenge.serializeHex());
}
return str;
}

public export() {
return {
challenges: this.challenges.map((challenge) => challenge.export())
};
}
}
41 changes: 41 additions & 0 deletions src/fs/fileDel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { hex2VarBytes, StringReader } from '../utils';
import { decodeVarBytes, decodeVarUint, serializeVarUint } from './utils';

export class FileDel {
static deserializeHex(hex: string): FileDel {
const sr: StringReader = new StringReader(hex);
const fileHash = decodeVarBytes(sr);
return new FileDel(fileHash);
}
public constructor(
public readonly fileHash: string
) { }

public serializeHex(): string {
return hex2VarBytes(this.fileHash);
}
}

export class FileDelList {
static deserializeHex(hex: string): FileDelList {
const list: FileDel[] = [];
const sr: StringReader = new StringReader(hex);
const count = decodeVarUint(sr);
for (let i = 0; i < count; i++) {
const del = FileDel.deserializeHex(sr.readNextBytes());
list.push(del);
}
return new FileDelList(list);
}
public constructor(
public readonly filesDel: FileDel[] = []
) { }

public serializeHex(): string {
let str = serializeVarUint(this.filesDel.length);
for (const fileDel of this.filesDel) {
str += hex2VarBytes(fileDel.serializeHex());
}
return str;
}
}
52 changes: 52 additions & 0 deletions src/fs/fileHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { hex2VarBytes, hexstr2str, StringReader } from '../utils';
import { decodeVarBytes, decodeVarUint, serializeUint64 } from './utils';

export class FileHash {
static deserializeHex(hex: string): FileHash {
const sr: StringReader = new StringReader(hex);
const fileHash = decodeVarBytes(sr);
return new FileHash(fileHash);
}
public constructor(
public readonly fHash: string
) { }

public serializeHex(): string {
return hex2VarBytes(this.fHash);
}

public export() {
return {
fHash: this.fHash
};
}
}

export class FileHashList {
static deserializeHex(hex: string): FileHashList {
const sr: StringReader = new StringReader(hex);
const count = decodeVarUint(sr);
const hashes: FileHash[] = [];
for (let i = 0; i < count; i++) {
hashes.push(new FileHash(hexstr2str(decodeVarBytes(sr))));
}
return new FileHashList(hashes);
}
public constructor(
public readonly filesH: FileHash[]
) { }

public serializeHex(): string {
let str = serializeUint64(this.filesH.length);
for (const fileHash of this.filesH) {
str += hex2VarBytes(fileHash.fHash);
}
return str;
}

public export() {
return {
filesH: this.filesH.map((fileHash) => fileHash.export())
};
}
}
124 changes: 124 additions & 0 deletions src/fs/fileInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { Address } from '../crypto';
import { bool2VarByte, hex2VarBytes, num2VarInt, StringReader } from '../utils';
import { dateToUnixTime, decodeAddress, decodeBool, decodeVarBytes,
decodeVarUint, serializeAddress, serializeVarUint, unixTimeToDate } from './utils';

export class FileInfo {
static deserializeHex(hex: string): FileInfo {
const sr: StringReader = new StringReader(hex);
const fileHash = decodeVarBytes(sr);
const fileOwner = decodeAddress(sr);
const fileDesc = decodeVarBytes(sr);
const fileBlockCount = decodeVarUint(sr);
const realFileSize = decodeVarUint(sr);
const copyNumber = decodeVarUint(sr);
const payAmount = decodeVarUint(sr);
const restAmount = decodeVarUint(sr);
const firstPdp = decodeBool(sr);
const timeStart = unixTimeToDate(decodeVarUint(sr));
const timeExpired = unixTimeToDate(decodeVarUint(sr));
const beginHeight = decodeVarUint(sr);
const expiredHeight = decodeVarUint(sr);
const pdpParam = decodeVarBytes(sr);
const validFlag = decodeBool(sr);
const currFeeRate = decodeVarUint(sr)
const storageType = decodeVarUint(sr);
return new FileInfo(
fileHash, fileOwner, fileDesc, fileBlockCount, realFileSize, copyNumber, payAmount, restAmount,
firstPdp, timeStart, timeExpired, beginHeight, expiredHeight, pdpParam, validFlag, currFeeRate, storageType
);
}
public constructor(
public readonly fileHash: string,
public readonly fileOwner: Address,
public readonly fileDesc: string,
public readonly fileBlockCount: number,
public readonly realFileSize: number,
public readonly copyNumber: number,
public readonly payAmount: number,
public readonly restAmount: number,
public readonly firstPdp: boolean,
public readonly timeStart: Date,
public readonly timeExpired: Date,
public readonly beginHeight: number,
public readonly expiredHeight: number,
public readonly pdpParam: string,
public readonly validFlag: boolean,
public readonly currFeeRate: number,
public readonly storageType: number
) { }

public serializeHex(): string {
return hex2VarBytes(this.fileHash)
+ serializeAddress(this.fileOwner)
+ hex2VarBytes(this.fileDesc)
+ serializeVarUint(this.fileBlockCount)
+ serializeVarUint(this.realFileSize)
+ serializeVarUint(this.copyNumber)
+ serializeVarUint(this.payAmount)
+ serializeVarUint(this.restAmount)
+ bool2VarByte(this.firstPdp)
+ serializeVarUint(dateToUnixTime(this.timeStart))
+ serializeVarUint(dateToUnixTime(this.timeExpired))
+ serializeVarUint(this.beginHeight)
+ serializeVarUint(this.expiredHeight)
+ hex2VarBytes(this.pdpParam)
+ bool2VarByte(this.validFlag)
+ serializeVarUint(this.currFeeRate)
+ serializeVarUint(this.storageType);
}

public export() {
return {
fileHash: this.fileHash,
fileOwner: this.fileOwner.value,
fileDesc: this.fileDesc,
fileBlockCount: this.fileBlockCount,
realFileSize: this.realFileSize,
copyNumber: this.copyNumber,
payAmount: this.payAmount,
restAmount: this.restAmount,
firstPdp: this.firstPdp,
timeStart: this.timeStart,
timeExpired: this.timeExpired,
beginHeight: this.beginHeight,
expiredHeight: this.expiredHeight,
pdpParam: this.pdpParam,
validFlag: this.validFlag,
currFeeRate: this.currFeeRate,
storageType: this.storageType
};
}
}

export class FileInfoList {
static deserializeHex(hex: string): FileInfoList {
const list: FileInfo[] = [];
const sr: StringReader = new StringReader(hex);
const count = decodeVarUint(sr);
for (let i = 0; i < count; i++) {
const item = FileInfo.deserializeHex(sr.readNextBytes());
list.push(item);
}
return new FileInfoList(list);
}
public constructor(
public readonly filesI: FileInfo[] = []
) { }

public serializeHex(): string {
let str = serializeVarUint(this.filesI.length);
for (const fileInfo of this.filesI) {
const fileInfoHex = fileInfo.serializeHex();
const hexLen = num2VarInt(fileInfoHex.length / 2);
str += hexLen + fileInfoHex;
}
return str;
}

public export() {
return {
filesI: this.filesI.map((fileInfo) => fileInfo.export())
};
}
}
29 changes: 29 additions & 0 deletions src/fs/filePdpData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Address } from '../crypto';
import { hex2VarBytes, StringReader } from '../utils';
import { decodeAddress, decodeVarBytes, decodeVarUint, serializeUint64 } from './utils';

export class FilePdpData {
static deserializeHex(hex: string): FilePdpData {
const sr: StringReader = new StringReader(hex);
const nodeAddr = decodeAddress(sr);
const fileHash = decodeVarBytes(sr);
const proveData = decodeVarBytes(sr);
const challengeHeight = decodeVarUint(sr);
return new FilePdpData(nodeAddr, fileHash, proveData, challengeHeight);
}
public constructor(
public readonly nodeAddr: Address,
public readonly fileHash: string,
public readonly proveData: string,
public readonly challengeHeight: number
) { }

public serializeHex(): string {
let str = '';
str += this.nodeAddr.serialize()
+ hex2VarBytes(this.fileHash)
+ hex2VarBytes(this.proveData)
+ serializeUint64(this.challengeHeight);
return str;
}
}
Loading