Skip to content

Commit

Permalink
Edict with zero amount allocates all remaining runes (ordinals#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Oct 15, 2023
1 parent fd7cc20 commit 659ce6c
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
};

// Get the allocatable amount
let amount = amount.min(*balance);
let amount = if amount == 0 {
*balance
} else {
amount.min(*balance)
};

// If the amount to be allocated is greater than zero,
// deduct it from the remaining balance, and increment
Expand Down Expand Up @@ -158,7 +162,9 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
.find(|(_, tx_out)| !tx_out.script_pubkey.is_op_return())
{
for (id, balance) in unallocated {
*allocated[vout].entry(id).or_default() += balance;
if balance > 0 {
*allocated[vout].entry(id).or_default() += balance;
}
}
}

Expand Down
163 changes: 163 additions & 0 deletions src/runes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2553,4 +2553,167 @@ mod tests {
[(OutPoint { txid, vout: 0 }, vec![(id, u128::max_value())])]
);
}

#[test]
fn allocate_all_remaining_runes_in_etching() {
let context = Context::builder()
.arg("--index-runes-pre-alpha-i-agree-to-get-rekt")
.build();

context.mine_blocks(1);

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

context.mine_blocks(1);

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

assert_eq!(
context.index.runes().unwrap().unwrap(),
[(
id,
RuneEntry {
burned: 0,
divisibility: 0,
etching: txid,
rune: Rune(RUNE),
supply: u128::max_value(),
symbol: None,
}
)]
);

assert_eq!(
context.index.get_rune_balances(),
[(OutPoint { txid, vout: 0 }, vec![(id, u128::max_value())])]
);
}

#[test]
fn allocate_all_remaining_runes_in_inputs() {
let context = Context::builder()
.arg("--index-runes-pre-alpha-i-agree-to-get-rekt")
.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 {
divisibility: 0,
rune: Rune(RUNE),
symbol: None,
}),
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

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

assert_eq!(
context.index.runes().unwrap().unwrap(),
[(
id,
RuneEntry {
burned: 0,
divisibility: 0,
etching: txid0,
rune: Rune(RUNE),
supply: u128::max_value(),
symbol: None,
}
)]
);

assert_eq!(
context.index.get_rune_balances(),
[(
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 {
edicts: vec![Edict {
id: id.into(),
amount: 0,
output: 1,
}],
etching: None,
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

assert_eq!(
context.index.runes().unwrap().unwrap(),
[(
id,
RuneEntry {
burned: 0,
divisibility: 0,
etching: txid0,
rune: Rune(RUNE),
supply: u128::max_value(),
symbol: None,
}
)]
);

assert_eq!(
context.index.get_rune_balances(),
[(
OutPoint {
txid: txid1,
vout: 1,
},
vec![(id, u128::max_value())]
)]
);
}
}
18 changes: 18 additions & 0 deletions src/runes/runestone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,24 @@ mod tests {
21,
);

case(
vec![Edict {
amount: 0,
id: RuneId {
height: 0,
index: 0,
}
.into(),
output: 0,
}],
Some(Etching {
divisibility: MAX_DIVISIBILITY,
rune: Rune(u128::max_value()),
symbol: None,
}),
25,
);

case(
vec![Edict {
amount: u128::max_value(),
Expand Down

0 comments on commit 659ce6c

Please sign in to comment.