-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproto-decoder.js
More file actions
225 lines (186 loc) · 6.33 KB
/
proto-decoder.js
File metadata and controls
225 lines (186 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const protobuf = require('protobufjs');
const path = require('path');
let root = null;
let SubscribeUpdate = null;
let SubscribePreprocessedUpdate = null;
// Initialize protobuf types
async function initProtobuf() {
if (root) return; // Already initialized
try {
// Load the protobuf schema
root = await protobuf.load([
path.join(__dirname, 'proto', 'geyser.proto'),
path.join(__dirname, 'proto', 'solana-storage.proto')
]);
// Get the SubscribeUpdate message type
SubscribeUpdate = root.lookupType('geyser.SubscribeUpdate');
SubscribePreprocessedUpdate = root.lookupType('geyser.SubscribePreprocessedUpdate');
} catch (error) {
throw error;
}
}
// Decode protobuf bytes to JavaScript object
function decodeSubscribeUpdate(bytes) {
if (!SubscribeUpdate) {
throw new Error('Protobuf not initialized. Call initProtobuf() first.');
}
try {
// Decode the protobuf bytes
const message = SubscribeUpdate.decode(bytes);
// Convert to plain JavaScript object with proper handling
const obj = SubscribeUpdate.toObject(message, {
longs: String, // Convert uint64 to string (like Yellowstone)
enums: Number, // Keep enums as numbers
bytes: Buffer, // Keep bytes as Buffer (exactly like Yellowstone)
defaults: true, // Include default values
arrays: true, // Always include arrays
objects: true, // Always include objects
oneofs: false // Do not include oneof virtual fields
});
// Remove protobuf-oneof helper fields if present
delete obj.updateOneof;
delete obj.update;
// Post-process the object to match Yellowstone interface exactly
return processYellowstoneUpdate(obj);
} catch (error) {
throw error;
}
}
// Process the decoded object to match Yellowstone interface exactly
function processYellowstoneUpdate(obj) {
// Convert createdAt timestamp to Date object (matching Yellowstone)
if (obj.createdAt) {
const timestamp = obj.createdAt;
const millis = parseInt(timestamp.seconds) * 1000 + Math.floor(timestamp.nanos / 1_000_000);
obj.createdAt = new Date(millis);
}
// Initialize all update type fields to undefined (matching Yellowstone)
const updateFields = [
'account', 'slot', 'transaction', 'transactionStatus',
'block', 'blockMeta', 'entry', 'ping', 'pong'
];
updateFields.forEach(field => {
if (!(field in obj)) {
obj[field] = undefined;
}
});
// Process specific update types
if (obj.account) {
obj.account = processAccountUpdate(obj.account);
}
if (obj.slot) {
obj.slot = processSlotUpdate(obj.slot);
}
if (obj.transaction) {
obj.transaction = processTransactionUpdate(obj.transaction);
}
if (obj.transactionStatus) {
obj.transactionStatus = processTransactionStatusUpdate(obj.transactionStatus);
}
if (obj.block) {
obj.block = processBlockUpdate(obj.block);
}
if (obj.blockMeta) {
obj.blockMeta = processBlockMetaUpdate(obj.blockMeta);
}
if (obj.entry) {
obj.entry = processEntryUpdate(obj.entry);
}
// Ensure consistent field ordering like Yellowstone
const orderedObj = {
filters: obj.filters,
account: obj.account,
slot: obj.slot,
transaction: obj.transaction,
transactionStatus: obj.transactionStatus,
block: obj.block,
blockMeta: obj.blockMeta,
entry: obj.entry,
ping: obj.ping,
pong: obj.pong,
createdAt: obj.createdAt,
};
return orderedObj;
}
// Process account update to match Yellowstone format
function processAccountUpdate(account) {
// protobufjs with bytes: Buffer already returns Buffer objects
// No conversion needed - they already match Yellowstone format
return account;
}
// Process slot update to match Yellowstone format
function processSlotUpdate(slot) {
// Ensure parent is either string or undefined (not null)
if (slot.parent === null) {
slot.parent = undefined;
}
// Ensure deadError is either string or undefined (not null)
if (slot.deadError === null) {
slot.deadError = undefined;
}
return slot;
}
// Process transaction update to match Yellowstone format
function processTransactionUpdate(transaction) {
// protobufjs with bytes: Buffer already returns Buffer objects
// No conversion needed - they already match Yellowstone format
return transaction;
}
// Process transaction status update to match Yellowstone format
function processTransactionStatusUpdate(transactionStatus) {
// protobufjs with bytes: Buffer already returns Buffer objects
// Only need to handle null -> undefined conversion
if (transactionStatus.err === null) {
transactionStatus.err = undefined;
}
return transactionStatus;
}
// Process block update to match Yellowstone format
function processBlockUpdate(block) {
// protobufjs with bytes: Buffer already returns Buffer objects
// No conversion needed - they already match Yellowstone format
return block;
}
// Process block meta update to match Yellowstone format
function processBlockMetaUpdate(blockMeta) {
// BlockMeta is similar to Block but without transactions/accounts/entries arrays
return blockMeta;
}
// Process entry update to match Yellowstone format
function processEntryUpdate(entry) {
// protobufjs with bytes: Buffer already returns Buffer objects
// No conversion needed - they already match Yellowstone format
return entry;
}
// Decode preprocessed update protobuf bytes to JavaScript object
function decodeSubscribePreprocessedUpdate(bytes) {
if (!SubscribePreprocessedUpdate) {
throw new Error('Protobuf not initialized. Call initProtobuf() first.');
}
try {
// Decode the protobuf bytes
const message = SubscribePreprocessedUpdate.decode(bytes);
// Convert to plain JavaScript object
const obj = SubscribePreprocessedUpdate.toObject(message, {
longs: String,
enums: Number,
bytes: Buffer,
defaults: true,
arrays: true,
objects: true,
oneofs: false
});
// Remove protobuf-oneof helper fields if present
delete obj.updateOneof;
delete obj.update;
return obj;
} catch (error) {
throw new Error(`Failed to decode preprocessed update: ${error.message}`);
}
}
// Export functions
module.exports = {
initProtobuf,
decodeSubscribeUpdate,
decodeSubscribePreprocessedUpdate
};