Skip to content

Commit

Permalink
Update enforcer proto file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
CryptAxe committed Jan 31, 2025
1 parent 3825fb2 commit abac99a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 46 deletions.
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@
"productName": "Drivechain Launcher",
"files": [
"build/**/*",
"public/**/*"
"public/**/*",
{
"from": "src/data/proto",
"to": "src/data/proto"
},
{
"from": "node_modules/@grpc/proto-loader/node_modules/google-proto-files/google",
"to": "src/data/proto/google"
}
],
"directories": {
"buildResources": "public",
Expand Down
96 changes: 78 additions & 18 deletions public/modules/enforcerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,86 @@ const path = require('path');

class EnforcerClient {
constructor() {
// Load the proto file
const protoPath = path.join(__dirname, '../..', 'src/data/proto/cusf/mainchain/v1/validator.proto');
const packageDefinition = protoLoader.loadSync(protoPath, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs: [path.join(__dirname, '../..', 'src/data/proto')]
});
let packageDefinition;
let isDev, basePath, protoPath;

try {
// Load the proto file
isDev = process.env.NODE_ENV === 'development' || !process.resourcesPath;

// Try multiple possible locations for the proto file
const possiblePaths = [
// Development path
path.join(__dirname, '../..', 'src/data/proto/cusf/mainchain/v1/validator.proto'),
// Production path in app.asar
process.resourcesPath ? path.join(process.resourcesPath, 'app.asar', 'src/data/proto/cusf/mainchain/v1/validator.proto') : null,
// Fallback to local proto
path.join(__dirname, 'validator.proto')
].filter(Boolean);

let loadError;
for (const tryPath of possiblePaths) {
try {
console.log('Trying to load proto file from:', tryPath);
const includeDir = path.dirname(path.dirname(path.dirname(path.dirname(tryPath))));
console.log('Include directory:', includeDir);

packageDefinition = protoLoader.loadSync(tryPath, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs: [includeDir]
});

if (packageDefinition) {
protoPath = tryPath;
console.log('Successfully loaded proto from:', tryPath);
break;
}
} catch (e) {
loadError = e;
console.log('Failed to load from', tryPath, ':', e.message);
}
}

if (!packageDefinition) {
throw loadError || new Error('Failed to load proto from any location');
}
} catch (error) {
console.error('Failed to load proto file:', error);
console.error('Environment:', {
isDev,
triedPaths: possiblePaths,
protoPath,
resourcesPath: process.resourcesPath,
currentDir: __dirname
});
console.error('Proto dependencies:', {
grpcJs: require.resolve('@grpc/grpc-js'),
protoLoader: require.resolve('@grpc/proto-loader')
});
throw error;
}

// Load the ValidatorService package
const proto = grpc.loadPackageDefinition(packageDefinition);
const validatorService = proto.cusf.mainchain.v1.ValidatorService;
try {
// Load the ValidatorService package
const proto = grpc.loadPackageDefinition(packageDefinition);
if (!proto.cusf?.mainchain?.v1?.ValidatorService) {
throw new Error('ValidatorService not found in proto definition');
}
const validatorService = proto.cusf.mainchain.v1.ValidatorService;

// Create the client
this.client = new validatorService(
'localhost:50051', // Default gRPC port, adjust if needed
grpc.credentials.createInsecure()
);
// Create the client
this.client = new validatorService(
'localhost:50051', // Default gRPC port, adjust if needed
grpc.credentials.createInsecure()
);
} catch (error) {
console.error('Failed to create gRPC client:', error);
throw error;
}
}

async getBlockCount() {
Expand Down
27 changes: 0 additions & 27 deletions public/modules/validator.proto

This file was deleted.

0 comments on commit abac99a

Please sign in to comment.