Add updateDeviceBlockHeight
This commit is contained in:
parent
3ce412d814
commit
77d9c1ad43
@ -1,7 +1,7 @@
|
|||||||
import { INotification } from '~/models/notification.model';
|
import { INotification } from '~/models/notification.model';
|
||||||
import { IProcess } from '~/models/process.model';
|
import { IProcess } from '~/models/process.model';
|
||||||
import { initWebsocket, sendMessage } from '../websockets';
|
import { initWebsocket, sendMessage } from '../websockets';
|
||||||
import { ApiReturn, Device, HandshakeMessage, Member, MerkleProofResult, OutPointProcessMap, Process, ProcessState, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client';
|
import { ApiReturn, Device, HandshakeMessage, Member, MerkleProofResult, NewTxMessage, OutPointProcessMap, Process, ProcessState, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client';
|
||||||
import ModalService from './modal.service';
|
import ModalService from './modal.service';
|
||||||
import Database from './database.service';
|
import Database from './database.service';
|
||||||
import { navigate } from '../router';
|
import { navigate } from '../router';
|
||||||
@ -10,9 +10,10 @@ import { BackUp } from '~/models/backup.model';
|
|||||||
|
|
||||||
export const U32_MAX = 4294967295;
|
export const U32_MAX = 4294967295;
|
||||||
|
|
||||||
const BASEURL = `https://demo.4nkweb.com`;
|
const BASEURL = `http://localhost`;
|
||||||
const BOOTSTRAPURL = [`${BASEURL}/ws/`];
|
const BOOTSTRAPURL = [`${BASEURL}:8090`];
|
||||||
const STORAGEURL = `${BASEURL}/storage`
|
const STORAGEURL = `${BASEURL}:8081`
|
||||||
|
const BLINDBITURL = `${BASEURL}:8000`
|
||||||
const DEFAULTAMOUNT = 1000n;
|
const DEFAULTAMOUNT = 1000n;
|
||||||
const EMPTY32BYTES = String('').padStart(64, '0');
|
const EMPTY32BYTES = String('').padStart(64, '0');
|
||||||
|
|
||||||
@ -934,6 +935,7 @@ export default class Services {
|
|||||||
async createNewDevice() {
|
async createNewDevice() {
|
||||||
let spAddress = '';
|
let spAddress = '';
|
||||||
try {
|
try {
|
||||||
|
// We set birthday later when we have the chain tip from relay
|
||||||
spAddress = await this.sdkClient.create_new_device(0, 'signet');
|
spAddress = await this.sdkClient.create_new_device(0, 'signet');
|
||||||
const device = this.dumpDeviceFromMemory();
|
const device = this.dumpDeviceFromMemory();
|
||||||
await this.saveDeviceInDatabase(device);
|
await this.saveDeviceInDatabase(device);
|
||||||
@ -952,6 +954,61 @@ export default class Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async updateDeviceBlockHeight(): Promise<void> {
|
||||||
|
if (this.currentBlockHeight === -1) {
|
||||||
|
throw new Error('Current block height not set');
|
||||||
|
}
|
||||||
|
|
||||||
|
let device: Device | null = null;
|
||||||
|
try {
|
||||||
|
device = await this.getDeviceFromDatabase();
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(`Failed to get device from database: ${e}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!device) {
|
||||||
|
throw new Error('Device not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const birthday = device.sp_wallet.birthday;
|
||||||
|
if (birthday === undefined || birthday === null) {
|
||||||
|
throw new Error('Birthday not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (birthday === 0) {
|
||||||
|
// This is a new device, so current chain tip is its birthday
|
||||||
|
device.sp_wallet.birthday = this.currentBlockHeight;
|
||||||
|
// We also set last_scan, impossible that we need to scan earlier than this
|
||||||
|
device.sp_wallet.last_scan = this.currentBlockHeight;
|
||||||
|
try {
|
||||||
|
// First set the updated device in memory
|
||||||
|
this.sdkClient.restore_device(device);
|
||||||
|
// Then save it to database
|
||||||
|
await this.saveDeviceInDatabase(device);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(`Failed to save updated device: ${e}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// For testing, set last_scan and birthday a few blocks ago
|
||||||
|
device.sp_wallet.last_scan = this.currentBlockHeight - 10;
|
||||||
|
device.sp_wallet.birthday = this.currentBlockHeight - 10;
|
||||||
|
try {
|
||||||
|
this.sdkClient.restore_device(device);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(`Failed to restore device: ${e}`);
|
||||||
|
}
|
||||||
|
await this.saveDeviceInDatabase(device);
|
||||||
|
// This is existing device, we need to catch up if last_scan is lagging behind chain_tip
|
||||||
|
if (device.sp_wallet.last_scan < this.currentBlockHeight) {
|
||||||
|
// We need to catch up
|
||||||
|
await this.sdkClient.scan_blocks(this.currentBlockHeight, BLINDBITURL);
|
||||||
|
} else {
|
||||||
|
// Up to date, just returns
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async removeProcess(processId: string): Promise<void> {
|
private async removeProcess(processId: string): Promise<void> {
|
||||||
const db = await Database.getInstance();
|
const db = await Database.getInstance();
|
||||||
const storeName = 'processes';
|
const storeName = 'processes';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user