forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 21
/
rune_updater.rs
461 lines (393 loc) Β· 12.5 KB
/
rune_updater.rs
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use {
super::*,
crate::runes::{varint, Edict, Runestone},
};
struct Claim {
id: RuneId,
limit: u128,
}
struct Etched {
balance: u128,
divisibility: u8,
id: RuneId,
mint: Option<MintEntry>,
rune: Rune,
spacers: u32,
symbol: Option<char>,
}
#[derive(Default)]
pub(crate) struct RuneUpdate {
pub(crate) burned: u128,
pub(crate) mints: u64,
pub(crate) supply: u128,
}
pub(super) struct RuneUpdater<'a, 'db, 'tx> {
pub(super) height: u32,
pub(super) id_to_entry: &'a mut Table<'db, 'tx, RuneIdValue, RuneEntryValue>,
pub(super) inscription_id_to_sequence_number: &'a Table<'db, 'tx, InscriptionIdValue, u32>,
pub(super) minimum: Rune,
pub(super) outpoint_to_balances: &'a mut Table<'db, 'tx, &'static OutPointValue, &'static [u8]>,
pub(super) outpoint_to_output: &'a mut Table<'db, 'tx, &'static OutPointValue, OutputValue>,
pub(super) rune_to_id: &'a mut Table<'db, 'tx, u128, RuneIdValue>,
pub(super) runes: u64,
pub(super) sequence_number_to_rune_id: &'a mut Table<'db, 'tx, u32, RuneIdValue>,
pub(super) statistic_to_count: &'a mut Table<'db, 'tx, u64, u64>,
pub(super) block_time: u32,
pub(super) transaction_id_to_rune: &'a mut Table<'db, 'tx, &'static TxidValue, u128>,
pub(super) updates: HashMap<RuneId, RuneUpdate>,
}
impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
pub(super) fn index_runes(
&mut self,
tx_index: usize,
tx: &Transaction,
txid: Txid,
) -> Result<()> {
let runestone = Runestone::from_transaction(tx);
let mut unallocated = self.unallocated(tx)?;
let cenotaph = runestone
.as_ref()
.map(|runestone| runestone.cenotaph)
.unwrap_or_default();
let default_output = runestone.as_ref().and_then(|runestone| {
runestone
.default_output
.and_then(|default| usize::try_from(default).ok())
});
let mut allocated: Vec<HashMap<RuneId, u128>> = vec![HashMap::new(); tx.output.len()];
if let Some(runestone) = runestone {
if let Some(claim) = runestone
.claim
.and_then(|id| self.claim(id).transpose())
.transpose()?
{
*unallocated.entry(claim.id).or_default() += claim.limit;
let update = self.updates.entry(claim.id).or_default();
update.mints += 1;
update.supply += claim.limit;
}
let mut etched = self.etched(tx_index, tx, &runestone)?;
if !cenotaph {
for Edict { id, amount, output } in runestone.edicts {
let Ok(output) = usize::try_from(output) else {
continue;
};
// edicts with output values greater than the number of outputs
// should never be produced by the edict parser
assert!(output <= tx.output.len());
let (balance, id) = if id == RuneId::default() {
// If this edict allocates new issuance runes, skip it
// if no issuance was present, or if the issuance was invalid.
// Additionally, replace ID 0 with the newly assigned ID, and
// get the unallocated balance of the issuance.
match etched.as_mut() {
Some(Etched { balance, id, .. }) => (balance, *id),
None => continue,
}
} else {
// Get the unallocated balance of the given ID
match unallocated.get_mut(&id) {
Some(balance) => (balance, id),
None => continue,
}
};
let mut allocate = |balance: &mut u128, amount: u128, output: usize| {
if amount > 0 {
*balance -= amount;
*allocated[output].entry(id).or_default() += amount;
}
};
if output == tx.output.len() {
// find non-OP_RETURN outputs
let destinations = tx
.output
.iter()
.enumerate()
.filter_map(|(output, tx_out)| {
(!tx_out.script_pubkey.is_op_return()).then_some(output)
})
.collect::<Vec<usize>>();
if amount == 0 {
// if amount is zero, divide balance between eligible outputs
let amount = *balance / destinations.len() as u128;
let remainder = usize::try_from(*balance % destinations.len() as u128).unwrap();
for (i, output) in destinations.iter().enumerate() {
allocate(
balance,
if i < remainder { amount + 1 } else { amount },
*output,
);
}
} else {
// if amount is non-zero, distribute amount to eligible outputs
for output in destinations {
allocate(balance, amount.min(*balance), output);
}
}
} else {
// Get the allocatable amount
let amount = if amount == 0 {
*balance
} else {
amount.min(*balance)
};
allocate(balance, amount, output);
}
}
}
if let Some(etched) = etched {
self.create_rune_entry(txid, cenotaph, etched)?;
}
}
let mut burned: HashMap<RuneId, u128> = HashMap::new();
if cenotaph {
for (id, balance) in unallocated {
*burned.entry(id).or_default() += balance;
}
} else {
// assign all un-allocated runes to the default output, or the first non
// OP_RETURN output if there is no default, or if the default output is
// too large
if let Some(vout) = default_output
.filter(|vout| *vout < allocated.len())
.or_else(|| {
tx.output
.iter()
.enumerate()
.find(|(_vout, tx_out)| !tx_out.script_pubkey.is_op_return())
.map(|(vout, _tx_out)| vout)
})
{
for (id, balance) in unallocated {
if balance > 0 {
*allocated[vout].entry(id).or_default() += balance;
}
}
} else {
for (id, balance) in unallocated {
if balance > 0 {
*burned.entry(id).or_default() += balance;
}
}
}
}
// update outpoint balances
let mut buffer: Vec<u8> = Vec::new();
for (vout, balances) in allocated.into_iter().enumerate() {
if balances.is_empty() {
continue;
}
// increment burned balances
if tx.output[vout].script_pubkey.is_op_return() {
for (id, balance) in &balances {
*burned.entry(*id).or_default() += balance;
}
continue;
}
buffer.clear();
let mut balances = balances.into_iter().collect::<Vec<(RuneId, u128)>>();
// Sort balances by id so tests can assert balances in a fixed order
balances.sort();
for (id, balance) in balances {
varint::encode_to_vec(id.into(), &mut buffer);
varint::encode_to_vec(balance, &mut buffer);
}
self.outpoint_to_balances.insert(
&OutPoint {
txid,
vout: vout.try_into().unwrap(),
}
.store(),
buffer.as_slice(),
)?;
}
for input in tx.input.iter() {
if input.previous_output.is_null() {
continue;
}
self
.outpoint_to_output
.remove(&input.previous_output.store())?
.unwrap();
}
for (vout, output) in tx.output.iter().enumerate() {
let outpoint = OutPoint {
txid,
vout: vout.try_into().unwrap(),
};
self.outpoint_to_output.insert(
&outpoint.store(),
OutputEntry {
height: self.height,
taproot: output.script_pubkey.is_v1_p2tr(),
}
.store(),
)?;
}
// increment entries with burned runes
for (id, amount) in burned {
self.updates.entry(id).or_default().burned += amount;
}
Ok(())
}
fn create_rune_entry(&mut self, txid: Txid, burn: bool, etched: Etched) -> Result {
let Etched {
balance,
divisibility,
id,
mint,
rune,
spacers,
symbol,
} = etched;
self.rune_to_id.insert(rune.0, id.store())?;
self.transaction_id_to_rune.insert(&txid.store(), rune.0)?;
let number = self.runes;
self.runes += 1;
let premine = u128::MAX - balance;
self
.statistic_to_count
.insert(&Statistic::Runes.into(), self.runes)?;
self.id_to_entry.insert(
id.store(),
RuneEntry {
burned: 0,
divisibility,
etching: txid,
mints: 0,
mint: mint.and_then(|mint| (!burn).then_some(mint)),
number,
premine,
rune,
spacers,
supply: premine,
symbol,
timestamp: self.block_time,
}
.store(),
)?;
let inscription_id = InscriptionId { txid, index: 0 };
if let Some(sequence_number) = self
.inscription_id_to_sequence_number
.get(&inscription_id.store())?
{
self
.sequence_number_to_rune_id
.insert(sequence_number.value(), id.store())?;
}
Ok(())
}
fn etched(
&mut self,
tx_index: usize,
tx: &Transaction,
runestone: &Runestone,
) -> Result<Option<Etched>> {
let Some(etching) = runestone.etching else {
return Ok(None);
};
let rune = if let Some(rune) = etching.rune {
if rune < self.minimum
|| rune.is_reserved()
|| self.rune_to_id.get(rune.0)?.is_some()
|| !self.tx_commits_to_rune(tx, rune)?
{
return Ok(None);
}
rune
} else {
let reserved_runes = self
.statistic_to_count
.get(&Statistic::ReservedRunes.into())?
.map(|entry| entry.value())
.unwrap_or_default();
self
.statistic_to_count
.insert(&Statistic::ReservedRunes.into(), reserved_runes + 1)?;
Rune::reserved(reserved_runes.into())
};
// Nota bene: Because it would require constructing a block
// with 2**16 + 1 transactions, there is no test that checks that
// an eching in a transaction with an out-of-bounds index is
// ignored.
let Ok(index) = u16::try_from(tx_index) else {
return Ok(None);
};
Ok(Some(Etched {
balance: u128::MAX,
divisibility: etching.divisibility,
id: RuneId {
block: self.height,
tx: index,
},
rune,
spacers: etching.spacers,
symbol: etching.symbol,
mint: etching.mint.map(|mint| MintEntry {
deadline: mint.deadline,
end: mint.term.map(|term| term + self.height),
limit: mint.limit.map(|limit| limit.min(runes::MAX_LIMIT)),
}),
}))
}
fn claim(&self, id: RuneId) -> Result<Option<Claim>> {
let Some(entry) = self.id_to_entry.get(&id.store())? else {
return Ok(None);
};
let rune_entry = RuneEntry::load(entry.value());
let Ok(limit) = rune_entry.mintable(Height(self.height), self.block_time) else {
return Ok(None);
};
Ok(Some(Claim { id, limit }))
}
fn tx_commits_to_rune(&self, tx: &Transaction, rune: Rune) -> Result<bool> {
let commitment = rune.commitment();
for input in &tx.input {
let Some(tapscript) = input.witness.tapscript() else {
continue;
};
for instruction in tapscript.instructions() {
let instruction = instruction?;
let Some(pushbytes) = instruction.push_bytes() else {
continue;
};
if pushbytes.as_bytes() != commitment {
continue;
}
let Some(output) = self
.outpoint_to_output
.get(&input.previous_output.store())?
else {
panic!("input not in UTXO set: {}", input.previous_output);
};
let output = OutputEntry::load(output.value());
if output.taproot && self.height >= output.height + RUNE_COMMIT_INTERVAL {
return Ok(true);
}
}
}
Ok(false)
}
fn unallocated(&mut self, tx: &Transaction) -> Result<HashMap<RuneId, u128>> {
// map of rune ID to un-allocated balance of that rune
let mut unallocated: HashMap<RuneId, u128> = HashMap::new();
// increment unallocated runes with the runes in tx inputs
for input in &tx.input {
if let Some(guard) = self
.outpoint_to_balances
.remove(&input.previous_output.store())?
{
let buffer = guard.value();
let mut i = 0;
while i < buffer.len() {
let (id, len) = varint::decode(&buffer[i..]);
i += len;
let (balance, len) = varint::decode(&buffer[i..]);
i += len;
*unallocated.entry(id.try_into().unwrap()).or_default() += balance;
}
}
}
Ok(unallocated)
}
}