Skip to content

Commit

Permalink
Allow specifying destination for unallocated runes (ordinals#2899)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Dec 25, 2023
1 parent 8b38b32 commit a20bd68
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
.map(|runestone| runestone.burn)
.unwrap_or_default();

let default_output = runestone.as_ref().and_then(|runestone| {
runestone
.default_output
.and_then(|default| usize::try_from(default).ok())
});

// A vector of allocated transaction output rune balances
let mut allocated: Vec<HashMap<u128, u128>> = vec![HashMap::new(); tx.output.len()];

Expand Down Expand Up @@ -327,12 +333,18 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
*burned.entry(id).or_default() += balance;
}
} else {
// Assign all un-allocated runes to the first non OP_RETURN output
if let Some((vout, _)) = tx
.output
.iter()
.enumerate()
.find(|(_, tx_out)| !tx_out.script_pubkey.is_op_return())
// 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 {
Expand Down
265 changes: 265 additions & 0 deletions src/runes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ mod tests {
rune: Some(Rune(RUNE)),
..Default::default()
}),
default_output: None,
burn: true,
}
.encipher(),
Expand Down Expand Up @@ -944,6 +945,7 @@ mod tests {
term: Some(1),
spacers: 1,
}),
default_output: None,
burn: true,
}
.encipher(),
Expand Down Expand Up @@ -997,6 +999,7 @@ mod tests {
}],
etching: Some(Etching::default()),
burn: true,
default_output: None,
}
.encipher(),
),
Expand Down Expand Up @@ -1267,6 +1270,268 @@ mod tests {
);
}

#[test]
fn unallocated_runes_are_assigned_to_default_output() {
let context = Context::builder().arg("--index-runes").build();

context.mine_blocks(1);

let txid0 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, Witness::new())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id: 0,
amount: u128::max_value(),
output: 0,
}],
etching: Some(Etching {
rune: Some(Rune(RUNE)),
..Default::default()
}),
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

let id = RuneId {
height: 2,
index: 1,
};

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
supply: u128::max_value(),
timestamp: 2,
..Default::default()
},
)],
[(
OutPoint {
txid: txid0,
vout: 0,
},
vec![(id, u128::max_value())],
)],
);

let txid1 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(2, 1, 0, Witness::new())],
outputs: 2,
op_return: Some(
Runestone {
default_output: Some(1),
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
supply: u128::max_value(),
timestamp: 2,
..Default::default()
},
)],
[(
OutPoint {
txid: txid1,
vout: 1,
},
vec![(id, u128::max_value())],
)],
);
}

#[test]
fn unallocated_runes_are_assigned_to_first_non_op_return_output_if_default_is_too_large() {
let context = Context::builder().arg("--index-runes").build();

context.mine_blocks(1);

let txid0 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, Witness::new())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id: 0,
amount: u128::max_value(),
output: 0,
}],
etching: Some(Etching {
rune: Some(Rune(RUNE)),
..Default::default()
}),
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

let id = RuneId {
height: 2,
index: 1,
};

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
supply: u128::max_value(),
timestamp: 2,
..Default::default()
},
)],
[(
OutPoint {
txid: txid0,
vout: 0,
},
vec![(id, u128::max_value())],
)],
);

let txid1 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(2, 1, 0, Witness::new())],
outputs: 2,
op_return: Some(
Runestone {
default_output: Some(3),
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
supply: u128::max_value(),
timestamp: 2,
..Default::default()
},
)],
[(
OutPoint {
txid: txid1,
vout: 0,
},
vec![(id, u128::max_value())],
)],
);
}

#[test]
fn unallocated_runes_are_burned_if_default_output_is_op_return() {
let context = Context::builder().arg("--index-runes").build();

context.mine_blocks(1);

let txid0 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, Witness::new())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id: 0,
amount: u128::max_value(),
output: 0,
}],
etching: Some(Etching {
rune: Some(Rune(RUNE)),
..Default::default()
}),
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

let id = RuneId {
height: 2,
index: 1,
};

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
supply: u128::max_value(),
timestamp: 2,
..Default::default()
},
)],
[(
OutPoint {
txid: txid0,
vout: 0,
},
vec![(id, u128::max_value())],
)],
);

context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(2, 1, 0, Witness::new())],
outputs: 2,
op_return: Some(
Runestone {
default_output: Some(2),
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
supply: u128::max_value(),
burned: u128::max_value(),
timestamp: 2,
..Default::default()
},
)],
[],
);
}

#[test]
fn unallocated_runes_in_transactions_with_no_runestone_are_assigned_to_first_non_op_return_output(
) {
Expand Down
Loading

0 comments on commit a20bd68

Please sign in to comment.