Skip to content

Commit

Permalink
Apply new naming conventions (#43)
Browse files Browse the repository at this point in the history
* The relayer address in the XRPL and coreum should be called
`RelayerXRPLAddress`, `RelayerCoreumAddress`
* The bridge address should be called `BridgeXRPLAddress`
* Use `save` action name for evidence and signatures
* Rename `TicketNumber` -> `TicketSequence`, `SequenceNumber` ->
`AccountSequence`, `UsedTicketsThreshold` ->
`UsedTicketSequenceThreshold`
* Use `Origin` word instead of `Native` for tokens
  • Loading branch information
dzmitryhil authored Nov 8, 2023
1 parent 3ebaef0 commit 1d2b542
Show file tree
Hide file tree
Showing 35 changed files with 531 additions and 520 deletions.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fmt-go:
mockgen-go:
which mockgen || go install github.com/golang/mock/mockgen@v1.6.0
cd $(RELAYER_DIR) && go generate ./...
make fmt-go

.PHONY: lint-go
lint-go:
Expand Down Expand Up @@ -90,3 +91,16 @@ restart-dev-env:
.PHONY: rebuild-dev-env
rebuild-dev-env:
crust build/crust images/cored

.PHONY: smoke
smoke:
make lint-contract
make build-dev-contract
make test-contract
make mockgen-go
make fmt-go
make test-relayer
make restart-dev-env
make test-integration
# last since checks the committed files
make lint-go
54 changes: 27 additions & 27 deletions contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub fn instantiate(
}

// We need to allow at least 2 tickets and less than 250 (XRPL limit) to be used
if msg.used_tickets_threshold <= 1 || msg.used_tickets_threshold > MAX_TICKETS {
return Err(ContractError::InvalidUsedTicketsThreshold {});
if msg.used_ticket_sequence_threshold <= 1 || msg.used_ticket_sequence_threshold > MAX_TICKETS {
return Err(ContractError::InvalidUsedTicketSequenceThreshold {});
}

// We initialize these values here so that we can immediately start working with them
Expand All @@ -96,7 +96,7 @@ pub fn instantiate(
let config = Config {
relayers: msg.relayers,
evidence_threshold: msg.evidence_threshold,
used_tickets_threshold: msg.used_tickets_threshold,
used_ticket_sequence_threshold: msg.used_ticket_sequence_threshold,
trust_set_limit_amount: msg.trust_set_limit_amount,
};

Expand All @@ -115,17 +115,17 @@ pub fn instantiate(
uri_hash: None,
}));

let xrp_in_coreum = format!("{}-{}", XRP_SUBUNIT, env.contract.address).to_lowercase();
let xrp_coreum_denom = format!("{}-{}", XRP_SUBUNIT, env.contract.address).to_lowercase();

// We save the link between the denom in the Coreum chain and the denom in XRPL, so that when we receive
// We save the link between the denom in the coreum chain and the denom in XRPL, so that when we receive
// a token we can inform the relayers of what is being sent back.
let token = XRPLToken {
issuer: XRP_ISSUER.to_string(),
currency: XRP_CURRENCY.to_string(),
coreum_denom: xrp_in_coreum,
coreum_denom: xrp_coreum_denom,
sending_precision: XRP_DEFAULT_SENDING_PRECISION,
max_holding_amount: Uint128::new(XRP_DEFAULT_MAX_HOLDING_AMOUNT),
// The XRP active token will be active from the start because it doesn't need approval to be received by the multisig
// The XRP token is enabled from the start because it doesn't need approval to be received on the XRPL side.
state: TokenState::Enabled,
};

Expand Down Expand Up @@ -172,18 +172,18 @@ pub fn execute(
save_evidence(deps.into_empty(), info.sender, evidence)
}
ExecuteMsg::RecoverTickets {
sequence_number,
account_sequence,
number_of_tickets,
} => recover_tickets(
deps.into_empty(),
info.sender,
sequence_number,
account_sequence,
number_of_tickets,
),
ExecuteMsg::RegisterSignature {
ExecuteMsg::SaveSignature {
operation_id,
signature,
} => register_signature(deps.into_empty(), info.sender, operation_id, signature),
} => save_signature(deps.into_empty(), info.sender, operation_id, signature),
}
}

Expand Down Expand Up @@ -320,8 +320,8 @@ fn register_xrpl_token(
deps.storage,
ticket,
&Operation {
ticket_number: Some(ticket),
sequence_number: None,
ticket_sequence: Some(ticket),
account_sequence: None,
signatures: vec![],
operation_type: OperationType::TrustSet {
issuer: issuer.clone(),
Expand Down Expand Up @@ -405,13 +405,13 @@ fn save_evidence(deps: DepsMut, sender: Addr, evidence: Evidence) -> CoreumResul
}
Evidence::XRPLTransactionResult {
tx_hash,
sequence_number,
ticket_number,
account_sequence,
ticket_sequence,
transaction_result,
operation_result,
} => {
let operation_id =
check_operation_exists(deps.storage, sequence_number, ticket_number)?;
check_operation_exists(deps.storage, account_sequence, ticket_sequence)?;

// custom state validation of the transaction result
match operation_result.clone() {
Expand Down Expand Up @@ -449,7 +449,7 @@ fn save_evidence(deps: DepsMut, sender: Addr, evidence: Evidence) -> CoreumResul
}
PENDING_OPERATIONS.remove(deps.storage, operation_id);

if transaction_result.ne(&TransactionResult::Invalid) && ticket_number.is_some() {
if transaction_result.ne(&TransactionResult::Invalid) && ticket_sequence.is_some() {
register_used_ticket(deps.storage)?
};
}
Expand All @@ -473,7 +473,7 @@ fn save_evidence(deps: DepsMut, sender: Addr, evidence: Evidence) -> CoreumResul
fn recover_tickets(
deps: DepsMut,
sender: Addr,
sequence_number: u64,
account_sequence: u64,
number_of_tickets: Option<u32>,
) -> CoreumResult<ContractError> {
assert_owner(deps.storage, &sender)?;
Expand All @@ -497,19 +497,19 @@ fn recover_tickets(
let number_to_allocate = number_of_tickets.unwrap_or(used_tickets);

let config = CONFIG.load(deps.storage)?;
// We check that number_to_allocate > config.used_tickets_threshold in order to cover the
// We check that number_to_allocate > config.used_ticket_sequence_threshold in order to cover the
// reallocation with just one XRPL transaction, otherwise the relocation might cause the
// additional reallocation.
if number_to_allocate <= config.used_tickets_threshold || number_to_allocate > MAX_TICKETS {
return Err(ContractError::InvalidTicketNumberToAllocate {});
if number_to_allocate <= config.used_ticket_sequence_threshold || number_to_allocate > MAX_TICKETS {
return Err(ContractError::InvalidTicketSequenceToAllocate {});
}

check_and_save_pending_operation(
deps.storage,
sequence_number,
account_sequence,
&Operation {
ticket_number: None,
sequence_number: Some(sequence_number),
ticket_sequence: None,
account_sequence: Some(account_sequence),
signatures: vec![],
operation_type: OperationType::AllocateTickets {
number: number_to_allocate,
Expand All @@ -519,10 +519,10 @@ fn recover_tickets(

Ok(Response::new()
.add_attribute("action", ContractActions::RecoverTickets.as_str())
.add_attribute("sequence_number", sequence_number.to_string()))
.add_attribute("account_sequence", account_sequence.to_string()))
}

fn register_signature(
fn save_signature(
deps: DepsMut,
sender: Addr,
operation_id: u64,
Expand All @@ -533,7 +533,7 @@ fn register_signature(
add_signature(deps, operation_id, sender.clone(), signature.clone())?;

Ok(Response::new()
.add_attribute("action", ContractActions::RegisterSignature.as_str())
.add_attribute("action", ContractActions::SaveSignature.as_str())
.add_attribute("operation_id", operation_id.to_string())
.add_attribute("relayer_address", sender.to_string())
.add_attribute("signature", signature.as_str()))
Expand Down
8 changes: 4 additions & 4 deletions contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub enum ContractError {
#[error("InvalidAmount: Amount must be more than 0")]
InvalidAmount {},

#[error("InvalidUsedTicketsThreshold: Used tickets threshold must be more than 1 and less or equal than {}", MAX_TICKETS)]
InvalidUsedTicketsThreshold {},
#[error("InvalidUsedTicketSequenceThreshold: Used ticket sequences threshold must be more than 1 and less or equal than {}", MAX_TICKETS)]
InvalidUsedTicketSequenceThreshold {},

#[error("NoAvailableTickets: There are no available tickets")]
NoAvailableTickets {},
Expand Down Expand Up @@ -112,8 +112,8 @@ pub enum ContractError {
#[error("SignatureAlreadyProvided: There is already a signature provided for this relayer and this operation")]
SignatureAlreadyProvided {},

#[error("InvalidTicketNumberToAllocate: The number of tickets to recover must be greater than used ticket threshold and less than or equal to max allowed")]
InvalidTicketNumberToAllocate {},
#[error("InvalidTicketSequenceToAllocate: The number of tickets to recover must be greater than used ticket threshold and less than or equal to max allowed")]
InvalidTicketSequenceToAllocate {},

#[error("InvalidXRPLIssuer: The issuer must be a valid XRPL address")]
InvalidXRPLIssuer {},
Expand Down
26 changes: 13 additions & 13 deletions contract/src/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub enum Evidence {
#[serde(rename = "xrpl_transaction_result")]
XRPLTransactionResult {
tx_hash: Option<String>,
sequence_number: Option<u64>,
ticket_number: Option<u64>,
account_sequence: Option<u64>,
ticket_sequence: Option<u64>,
transaction_result: TransactionResult,
operation_result: OperationResult,
},
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Evidence {
Evidence::XRPLToCoreumTransfer { tx_hash, .. } => tx_hash.clone(),
Evidence::XRPLTransactionResult { tx_hash, .. } => tx_hash.clone().unwrap(),
}
.to_lowercase()
.to_lowercase()
}
pub fn is_operation_valid(&self) -> bool {
match self {
Expand All @@ -98,13 +98,13 @@ impl Evidence {
}
Evidence::XRPLTransactionResult {
tx_hash,
sequence_number,
ticket_number,
account_sequence,
ticket_sequence,
transaction_result,
operation_result,
} => {
if (sequence_number.is_none() && ticket_number.is_none())
|| (sequence_number.is_some() && ticket_number.is_some())
if (account_sequence.is_none() && ticket_sequence.is_none())
|| (account_sequence.is_some() && ticket_sequence.is_some())
{
return Err(ContractError::InvalidTransactionResultEvidence {});
}
Expand Down Expand Up @@ -147,7 +147,7 @@ impl Evidence {

#[cw_serde]
pub struct Evidences {
pub relayers: Vec<Addr>,
pub relayer_coreum_addresses: Vec<Addr>,
}

pub fn hash_bytes(bytes: Vec<u8>) -> String {
Expand All @@ -171,27 +171,27 @@ pub fn handle_evidence(
let mut evidences: Evidences;
match TX_EVIDENCES.may_load(storage, evidence.get_hash())? {
Some(stored_evidences) => {
if stored_evidences.relayers.contains(&sender) {
if stored_evidences.relayer_coreum_addresses.contains(&sender) {
return Err(ContractError::EvidenceAlreadyProvided {});
}
evidences = stored_evidences;
evidences.relayers.push(sender)
evidences.relayer_coreum_addresses.push(sender)
}
None => {
evidences = Evidences {
relayers: vec![sender],
relayer_coreum_addresses: vec![sender],
};
}
}

let config = CONFIG.load(storage)?;
if evidences.relayers.len() >= config.evidence_threshold.try_into().unwrap() {
if evidences.relayer_coreum_addresses.len() >= config.evidence_threshold.try_into().unwrap() {
// We only registered the transaction as processed if its execution didn't fail
if operation_valid {
PROCESSED_TXS.save(storage, evidence.get_tx_hash(), &Empty {})?;
}
// If there is just one relayer there is nothing to delete
if evidences.relayers.len() != 1 {
if evidences.relayer_coreum_addresses.len() != 1 {
TX_EVIDENCES.remove(storage, evidence.get_hash());
}
return Ok(true);
Expand Down
6 changes: 3 additions & 3 deletions contract/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct InstantiateMsg {
// How many relayers need to provide evidence for a message
pub evidence_threshold: u32,
// Amount of tickets that we can use before triggering a ticket allocation action
pub used_tickets_threshold: u32,
pub used_ticket_sequence_threshold: u32,
// Trust set limit amount that will be used when registering XRPL tokens
pub trust_set_limit_amount: Uint128,
}
Expand All @@ -34,10 +34,10 @@ pub enum ExecuteMsg {
max_holding_amount: Uint128,
},
RecoverTickets {
sequence_number: u64,
account_sequence: u64,
number_of_tickets: Option<u32>,
},
RegisterSignature {
SaveSignature {
operation_id: u64,
signature: String,
},
Expand Down
10 changes: 5 additions & 5 deletions contract/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{

#[cw_serde]
pub struct Operation {
pub ticket_number: Option<u64>,
pub sequence_number: Option<u64>,
pub ticket_sequence: Option<u64>,
pub account_sequence: Option<u64>,
pub signatures: Vec<Signature>,
pub operation_type: OperationType,
}
Expand All @@ -31,11 +31,11 @@ pub enum OperationType {

pub fn check_operation_exists(
storage: &mut dyn Storage,
sequence_number: Option<u64>,
ticket_number: Option<u64>,
account_sequence: Option<u64>,
ticket_sequence: Option<u64>,
) -> Result<u64, ContractError> {
// Get the sequence or ticket number (priority for sequence number)
let operation_id = sequence_number.unwrap_or(ticket_number.unwrap_or_default());
let operation_id = account_sequence.unwrap_or(ticket_sequence.unwrap_or_default());

if !PENDING_OPERATIONS.has(storage, operation_id) {
return Err(ContractError::PendingOperationNotFound {});
Expand Down
8 changes: 4 additions & 4 deletions contract/src/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{error::ContractError, state::PENDING_OPERATIONS};

#[cw_serde]
pub struct Signature {
pub relayer: Addr,
pub relayer_coreum_address: Addr,
pub signature: String,
}

Expand All @@ -25,16 +25,16 @@ pub fn add_signature(
// If this relayer already provided a signature he can't overwrite it
if signatures.clone().into_iter().any(
|Signature {
relayer,
relayer_coreum_address,
signature: _,
}| relayer == sender,
}| relayer_coreum_address == sender,
) {
return Err(ContractError::SignatureAlreadyProvided {});
}

// Add signature and store it
signatures.push(Signature {
relayer: sender,
relayer_coreum_address: sender,
signature,
});

Expand Down
Loading

0 comments on commit 1d2b542

Please sign in to comment.