Skip to content

Commit

Permalink
F/btc delegations (#39)
Browse files Browse the repository at this point in the history
Use our own structs for handling `btc-staking` state:

- `BtcDelegation` (converted from
`btc_staking_api::ActiveBtcDelegation`).
- `BtcUndelegationInfo` (converted from
`btc_staking_api::BtcUndelegationInfo`).
- `CovenantAdaptorSignatures` (converted from
`btc_staking_api::CovenantAdaptorSignatures`).
- `SignatureInfo` (converted from `btc_staking_api::SignatureInfo`)

This PR also removes optional from `btc_undelegation`, for simplicity /
clarity.

These changes are **API-breaking** for the delegations queries.
  • Loading branch information
maurolacy authored Aug 13, 2024
1 parent e436ae1 commit fc7930d
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 292 deletions.
90 changes: 48 additions & 42 deletions contracts/babylon/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,45 +226,19 @@ pub(crate) mod ibc_packet {
active_del: btc_staking
.active_del
.iter()
.map(|d| ActiveBtcDelegation {
staker_addr: d.staker_addr.clone(),
btc_pk_hex: d.btc_pk_hex.clone(),
fp_btc_pk_list: d.fp_btc_pk_list.clone(),
start_height: d.start_height,
end_height: d.end_height,
total_sat: d.total_sat,
staking_tx: d.staking_tx.to_vec().into(),
slashing_tx: d.slashing_tx.to_vec().into(),
delegator_slashing_sig: d.delegator_slashing_sig.to_vec().into(),
covenant_sigs: d
.covenant_sigs
.iter()
.map(|s| CovenantAdaptorSignatures {
cov_pk: s.cov_pk.to_vec().into(),
adaptor_sigs: s
.adaptor_sigs
.iter()
.map(|a| a.to_vec().into())
.collect(),
})
.collect(),
staking_output_idx: d.staking_output_idx,
unbonding_time: d.unbonding_time,
undelegation_info: d.undelegation_info.as_ref().map(|ui| BtcUndelegationInfo {
unbonding_tx: ui.unbonding_tx.to_vec().into(),
delegator_unbonding_sig: ui.delegator_unbonding_sig.to_vec().into(),
covenant_unbonding_sig_list: ui
.covenant_unbonding_sig_list
.iter()
.map(|s| SignatureInfo {
pk: s.pk.to_vec().into(),
sig: s.sig.to_vec().into(),
})
.collect(),
slashing_tx: ui.slashing_tx.to_vec().into(),
delegator_slashing_sig: ui.delegator_slashing_sig.to_vec().into(),
covenant_slashing_sigs: ui
.covenant_slashing_sigs
.map(|d| {
Ok(ActiveBtcDelegation {
staker_addr: d.staker_addr.clone(),
btc_pk_hex: d.btc_pk_hex.clone(),
fp_btc_pk_list: d.fp_btc_pk_list.clone(),
start_height: d.start_height,
end_height: d.end_height,
total_sat: d.total_sat,
staking_tx: d.staking_tx.to_vec().into(),
slashing_tx: d.slashing_tx.to_vec().into(),
delegator_slashing_sig: d.delegator_slashing_sig.to_vec().into(),
covenant_sigs: d
.covenant_sigs
.iter()
.map(|s| CovenantAdaptorSignatures {
cov_pk: s.cov_pk.to_vec().into(),
Expand All @@ -275,10 +249,42 @@ pub(crate) mod ibc_packet {
.collect(),
})
.collect(),
}),
params_version: d.params_version,
staking_output_idx: d.staking_output_idx,
unbonding_time: d.unbonding_time,
undelegation_info: d
.undelegation_info
.as_ref()
.map(|ui| BtcUndelegationInfo {
unbonding_tx: ui.unbonding_tx.to_vec().into(),
delegator_unbonding_sig: ui.delegator_unbonding_sig.to_vec().into(),
covenant_unbonding_sig_list: ui
.covenant_unbonding_sig_list
.iter()
.map(|s| SignatureInfo {
pk: s.pk.to_vec().into(),
sig: s.sig.to_vec().into(),
})
.collect(),
slashing_tx: ui.slashing_tx.to_vec().into(),
delegator_slashing_sig: ui.delegator_slashing_sig.to_vec().into(),
covenant_slashing_sigs: ui
.covenant_slashing_sigs
.iter()
.map(|s| CovenantAdaptorSignatures {
cov_pk: s.cov_pk.to_vec().into(),
adaptor_sigs: s
.adaptor_sigs
.iter()
.map(|a| a.to_vec().into())
.collect(),
})
.collect(),
})
.ok_or(StdError::generic_err("undelegation info not set"))?,
params_version: d.params_version,
})
})
.collect(),
.collect::<StdResult<_>>()?,
slashed_del: vec![], // FIXME: Route this
unbonded_del: btc_staking
.unbonded_del
Expand Down
145 changes: 77 additions & 68 deletions contracts/btc-staking/schema/btc-staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@
"staking_tx",
"start_height",
"total_sat",
"unbonding_time"
"unbonding_time",
"undelegation_info"
],
"properties": {
"btc_pk_hex": {
Expand Down Expand Up @@ -328,12 +329,9 @@
},
"undelegation_info": {
"description": "undelegation_info is the undelegation info of this delegation.",
"anyOf": [
"allOf": [
{
"$ref": "#/definitions/BtcUndelegationInfo"
},
{
"type": "null"
}
]
}
Expand Down Expand Up @@ -1296,7 +1294,8 @@
"staking_tx",
"start_height",
"total_sat",
"unbonding_time"
"unbonding_time",
"undelegation_info"
],
"properties": {
"btc_pk_hex": {
Expand Down Expand Up @@ -1383,12 +1382,9 @@
},
"undelegation_info": {
"description": "undelegation_info is the undelegation info of this delegation.",
"anyOf": [
"allOf": [
{
"$ref": "#/definitions/BtcUndelegationInfo"
},
{
"type": "null"
}
]
}
Expand Down Expand Up @@ -1516,14 +1512,13 @@
"delegations": {
"type": "array",
"items": {
"$ref": "#/definitions/ActiveBtcDelegation"
"$ref": "#/definitions/BtcDelegation"
}
}
},
"additionalProperties": false,
"definitions": {
"ActiveBtcDelegation": {
"description": "ActiveBTCDelegation is a message sent when a BTC delegation newly receives covenant signatures and thus becomes active",
"BtcDelegation": {
"type": "object",
"required": [
"btc_pk_hex",
Expand All @@ -1538,7 +1533,8 @@
"staking_tx",
"start_height",
"total_sat",
"unbonding_time"
"unbonding_time",
"undelegation_info"
],
"properties": {
"btc_pk_hex": {
Expand All @@ -1554,11 +1550,12 @@
},
"delegator_slashing_sig": {
"description": "delegator_slashing_sig is the signature on the slashing tx by the delegator (i.e. SK corresponding to btc_pk) as string hex. It will be a part of the witness for the staking tx output.",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
"end_height": {
"description": "end_height is the end height of the BTC delegation it is the end BTC height of the time-lock - w",
Expand All @@ -1581,11 +1578,12 @@
},
"slashing_tx": {
"description": "slashing_tx is the slashing tx",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
"staker_addr": {
"description": "staker_addr is the address to receive rewards from BTC delegation",
Expand All @@ -1599,11 +1597,12 @@
},
"staking_tx": {
"description": "staking_tx is the staking tx",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
"start_height": {
"description": "start_height is the start BTC height of the BTC delegation. It is the start BTC height of the time-lock",
Expand All @@ -1625,24 +1624,16 @@
},
"undelegation_info": {
"description": "undelegation_info is the undelegation info of this delegation.",
"anyOf": [
"allOf": [
{
"$ref": "#/definitions/BtcUndelegationInfo"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
},
"Binary": {
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>. See also <https://github.com/CosmWasm/cosmwasm/blob/main/docs/MESSAGE_TYPES.md>.",
"type": "string"
},
"BtcUndelegationInfo": {
"description": "BTCUndelegationInfo provides all necessary info about the undeleagation",
"type": "object",
"required": [
"covenant_slashing_sigs",
Expand All @@ -1669,41 +1660,44 @@
},
"delegator_slashing_sig": {
"description": "delegator_slashing_sig is the signature on the slashing tx by the delegator (i.e. SK corresponding to btc_pk). It will be a part of the witness for the unbonding tx output.",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
"delegator_unbonding_sig": {
"description": "delegator_unbonding_sig is the signature on the unbonding tx by the delegator (i.e. SK corresponding to btc_pk). It effectively proves that the delegator wants to unbond and thus Babylon will consider this BTC delegation unbonded. Delegator's BTC on Bitcoin will be unbonded after time-lock.",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
"slashing_tx": {
"description": "slashing_tx is the unbonding slashing tx",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
"unbonding_tx": {
"description": "unbonding_tx is the transaction which will transfer the funds from staking output to unbonding output. Unbonding output will usually have lower timelock than staking output.",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
},
"additionalProperties": false
},
"CovenantAdaptorSignatures": {
"description": "CovenantAdaptorSignatures is a list adaptor signatures signed by the covenant with different finality provider's public keys as encryption keys",
"type": "object",
"required": [
"adaptor_sigs",
Expand All @@ -1714,33 +1708,48 @@
"description": "adaptor_sigs is a list of adaptor signatures, each encrypted by a restaked BTC finality provider's public key",
"type": "array",
"items": {
"$ref": "#/definitions/Binary"
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
},
"cov_pk": {
"description": "cov_pk is the public key of the covenant emulator, used as the public key of the adaptor signature",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
},
"additionalProperties": false
},
"SignatureInfo": {
"description": "SignatureInfo is a BIP-340 signature together with its signer's BIP-340 PK",
"type": "object",
"required": [
"pk",
"sig"
],
"properties": {
"pk": {
"$ref": "#/definitions/Binary"
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
"sig": {
"$ref": "#/definitions/Binary"
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
},
"additionalProperties": false
Expand Down
Loading

0 comments on commit fc7930d

Please sign in to comment.