Resolve warnings in the NFT bridge

Change-Id: I6d5e5a7d66e8e35418eaf9346c53b3439314c9c5
This commit is contained in:
Hendrik Hofstadt 2021-09-20 12:22:13 +02:00
parent 6cb244ba83
commit 371c9b3dcc
8 changed files with 39 additions and 104 deletions

View File

@ -2,10 +2,6 @@ use crate::types::*;
use bridge::{ use bridge::{
accounts::BridgeData, accounts::BridgeData,
api::ForeignAddress, api::ForeignAddress,
vaa::{
DeserializePayload,
PayloadMessage,
},
}; };
use primitive_types::U256; use primitive_types::U256;
use solana_program::pubkey::Pubkey; use solana_program::pubkey::Pubkey;
@ -18,28 +14,28 @@ pub type AuthoritySigner<'b> = Derive<Info<'b>, "authority_signer">;
pub type CustodySigner<'b> = Derive<Info<'b>, "custody_signer">; pub type CustodySigner<'b> = Derive<Info<'b>, "custody_signer">;
pub type MintSigner<'b> = Derive<Info<'b>, "mint_signer">; pub type MintSigner<'b> = Derive<Info<'b>, "mint_signer">;
pub type CoreBridge<'a, const State: AccountState> = Data<'a, BridgeData, { State }>; pub type CoreBridge<'a, const STATE: AccountState> = Data<'a, BridgeData, { STATE }>;
pub type EmitterAccount<'b> = Derive<Info<'b>, "emitter">; pub type EmitterAccount<'b> = Derive<Info<'b>, "emitter">;
pub type ConfigAccount<'b, const State: AccountState> = pub type ConfigAccount<'b, const STATE: AccountState> =
Derive<Data<'b, Config, { State }>, "config">; Derive<Data<'b, Config, { STATE }>, "config">;
pub type CustodyAccount<'b, const State: AccountState> = Data<'b, SplAccount, { State }>; pub type CustodyAccount<'b, const STATE: AccountState> = Data<'b, SplAccount, { STATE }>;
pub struct CustodyAccountDerivationData { pub struct CustodyAccountDerivationData {
pub mint: Pubkey, pub mint: Pubkey,
} }
impl<'b, const State: AccountState> Seeded<&CustodyAccountDerivationData> impl<'b, const STATE: AccountState> Seeded<&CustodyAccountDerivationData>
for CustodyAccount<'b, { State }> for CustodyAccount<'b, { STATE }>
{ {
fn seeds(accs: &CustodyAccountDerivationData) -> Vec<Vec<u8>> { fn seeds(accs: &CustodyAccountDerivationData) -> Vec<Vec<u8>> {
vec![accs.mint.to_bytes().to_vec()] vec![accs.mint.to_bytes().to_vec()]
} }
} }
pub type WrappedMint<'b, const State: AccountState> = Data<'b, SplMint, { State }>; pub type WrappedMint<'b, const STATE: AccountState> = Data<'b, SplMint, { STATE }>;
pub struct WrappedDerivationData { pub struct WrappedDerivationData {
pub token_chain: ChainID, pub token_chain: ChainID,
@ -47,7 +43,7 @@ pub struct WrappedDerivationData {
pub token_id: U256, pub token_id: U256,
} }
impl<'b, const State: AccountState> Seeded<&WrappedDerivationData> for WrappedMint<'b, { State }> { impl<'b, const STATE: AccountState> Seeded<&WrappedDerivationData> for WrappedMint<'b, { STATE }> {
fn seeds(data: &WrappedDerivationData) -> Vec<Vec<u8>> { fn seeds(data: &WrappedDerivationData) -> Vec<Vec<u8>> {
let mut token_id = vec![0u8; 32]; let mut token_id = vec![0u8; 32];
data.token_id.to_big_endian(&mut token_id); data.token_id.to_big_endian(&mut token_id);
@ -60,14 +56,14 @@ impl<'b, const State: AccountState> Seeded<&WrappedDerivationData> for WrappedMi
} }
} }
pub type WrappedTokenMeta<'b, const State: AccountState> = Data<'b, WrappedMeta, { State }>; pub type WrappedTokenMeta<'b, const STATE: AccountState> = Data<'b, WrappedMeta, { STATE }>;
pub struct WrappedMetaDerivationData { pub struct WrappedMetaDerivationData {
pub mint_key: Pubkey, pub mint_key: Pubkey,
} }
impl<'b, const State: AccountState> Seeded<&WrappedMetaDerivationData> impl<'b, const STATE: AccountState> Seeded<&WrappedMetaDerivationData>
for WrappedTokenMeta<'b, { State }> for WrappedTokenMeta<'b, { STATE }>
{ {
fn seeds(data: &WrappedMetaDerivationData) -> Vec<Vec<u8>> { fn seeds(data: &WrappedMetaDerivationData) -> Vec<Vec<u8>> {
vec![ vec![
@ -78,7 +74,7 @@ impl<'b, const State: AccountState> Seeded<&WrappedMetaDerivationData>
} }
/// Registered chain endpoint /// Registered chain endpoint
pub type Endpoint<'b, const State: AccountState> = Data<'b, EndpointRegistration, { State }>; pub type Endpoint<'b, const STATE: AccountState> = Data<'b, EndpointRegistration, { STATE }>;
pub struct EndpointDerivationData { pub struct EndpointDerivationData {
pub emitter_chain: u16, pub emitter_chain: u16,
@ -86,7 +82,7 @@ pub struct EndpointDerivationData {
} }
/// Seeded implementation based on an incoming VAA /// Seeded implementation based on an incoming VAA
impl<'b, const State: AccountState> Seeded<&EndpointDerivationData> for Endpoint<'b, { State }> { impl<'b, const STATE: AccountState> Seeded<&EndpointDerivationData> for Endpoint<'b, { STATE }> {
fn seeds(data: &EndpointDerivationData) -> Vec<Vec<u8>> { fn seeds(data: &EndpointDerivationData) -> Vec<Vec<u8>> {
vec![ vec![
data.emitter_chain.to_be_bytes().to_vec(), data.emitter_chain.to_be_bytes().to_vec(),

View File

@ -28,8 +28,6 @@ use solana_program::{
invoke, invoke,
invoke_signed, invoke_signed,
}, },
program_error::ProgramError,
pubkey::Pubkey,
}; };
use solitaire::{ use solitaire::{
processors::seeded::{ processors::seeded::{
@ -39,14 +37,6 @@ use solitaire::{
CreationLamports::Exempt, CreationLamports::Exempt,
*, *,
}; };
use spl_token::state::{
Account,
Mint,
};
use std::ops::{
Deref,
DerefMut,
};
#[derive(FromAccounts)] #[derive(FromAccounts)]
pub struct CompleteNative<'b> { pub struct CompleteNative<'b> {
@ -81,7 +71,8 @@ impl<'a> From<&CompleteNative<'a>> for CustodyAccountDerivationData {
} }
} }
impl<'b> InstructionContext<'b> for CompleteNative<'b> {} impl<'b> InstructionContext<'b> for CompleteNative<'b> {
}
#[derive(BorshDeserialize, BorshSerialize, Default)] #[derive(BorshDeserialize, BorshSerialize, Default)]
pub struct CompleteNativeData {} pub struct CompleteNativeData {}
@ -89,7 +80,7 @@ pub struct CompleteNativeData {}
pub fn complete_native( pub fn complete_native(
ctx: &ExecutionContext, ctx: &ExecutionContext,
accs: &mut CompleteNative, accs: &mut CompleteNative,
data: CompleteNativeData, _data: CompleteNativeData,
) -> Result<()> { ) -> Result<()> {
// Verify the chain registration // Verify the chain registration
let derivation_data: EndpointDerivationData = (&*accs).into(); let derivation_data: EndpointDerivationData = (&*accs).into();
@ -205,7 +196,8 @@ impl<'a> From<&CompleteWrapped<'a>> for WrappedMetaDerivationData {
} }
} }
impl<'b> InstructionContext<'b> for CompleteWrapped<'b> {} impl<'b> InstructionContext<'b> for CompleteWrapped<'b> {
}
#[derive(BorshDeserialize, BorshSerialize, Default)] #[derive(BorshDeserialize, BorshSerialize, Default)]
pub struct CompleteWrappedData {} pub struct CompleteWrappedData {}
@ -213,7 +205,7 @@ pub struct CompleteWrappedData {}
pub fn complete_wrapped( pub fn complete_wrapped(
ctx: &ExecutionContext, ctx: &ExecutionContext,
accs: &mut CompleteWrapped, accs: &mut CompleteWrapped,
data: CompleteWrappedData, _data: CompleteWrappedData,
) -> Result<()> { ) -> Result<()> {
use bstr::ByteSlice; use bstr::ByteSlice;
@ -239,7 +231,7 @@ pub fn complete_wrapped(
if !accs.meta.is_initialized() { if !accs.meta.is_initialized() {
// Create mint account // Create mint account
accs.mint accs.mint
.create(&((&*accs).into()), ctx, accs.payer.key, Exempt); .create(&((&*accs).into()), ctx, accs.payer.key, Exempt)?;
// Initialize mint // Initialize mint
let init_ix = spl_token::instruction::initialize_mint( let init_ix = spl_token::instruction::initialize_mint(
@ -253,7 +245,7 @@ pub fn complete_wrapped(
// Create meta account // Create meta account
accs.meta accs.meta
.create(&((&*accs).into()), ctx, accs.payer.key, Exempt); .create(&((&*accs).into()), ctx, accs.payer.key, Exempt)?;
// Initialize spl meta // Initialize spl meta
accs.spl_metadata.verify_derivation( accs.spl_metadata.verify_derivation(

View File

@ -8,7 +8,6 @@ use crate::{
GovernancePayloadUpgrade, GovernancePayloadUpgrade,
PayloadGovernanceRegisterChain, PayloadGovernanceRegisterChain,
}, },
types::*,
TokenBridgeError::{ TokenBridgeError::{
InvalidChain, InvalidChain,
InvalidGovernanceKey, InvalidGovernanceKey,
@ -18,14 +17,12 @@ use bridge::{
vaa::{ vaa::{
ClaimableVAA, ClaimableVAA,
DeserializePayload, DeserializePayload,
PayloadMessage,
}, },
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
}; };
use solana_program::{ use solana_program::{
account_info::AccountInfo, account_info::AccountInfo,
program::invoke_signed, program::invoke_signed,
program_error::ProgramError,
pubkey::Pubkey, pubkey::Pubkey,
sysvar::{ sysvar::{
clock::Clock, clock::Clock,
@ -37,10 +34,6 @@ use solitaire::{
CreationLamports::Exempt, CreationLamports::Exempt,
*, *,
}; };
use std::ops::{
Deref,
DerefMut,
};
// Confirm that a ClaimableVAA came from the correct chain, signed by the right emitter. // Confirm that a ClaimableVAA came from the correct chain, signed by the right emitter.
fn verify_governance<'a, T>(vaa: &ClaimableVAA<'a, T>) -> Result<()> fn verify_governance<'a, T>(vaa: &ClaimableVAA<'a, T>) -> Result<()>
@ -151,7 +144,7 @@ pub struct RegisterChainData {}
pub fn register_chain( pub fn register_chain(
ctx: &ExecutionContext, ctx: &ExecutionContext,
accs: &mut RegisterChain, accs: &mut RegisterChain,
data: RegisterChainData, _data: RegisterChainData,
) -> Result<()> { ) -> Result<()> {
let derivation_data: EndpointDerivationData = (&*accs).into(); let derivation_data: EndpointDerivationData = (&*accs).into();
accs.endpoint accs.endpoint
@ -168,7 +161,7 @@ pub fn register_chain(
// Create endpoint // Create endpoint
accs.endpoint accs.endpoint
.create(&((&*accs).into()), ctx, accs.payer.key, Exempt); .create(&((&*accs).into()), ctx, accs.payer.key, Exempt)?;
accs.endpoint.chain = accs.vaa.chain; accs.endpoint.chain = accs.vaa.chain;
accs.endpoint.contract = accs.vaa.endpoint_address; accs.endpoint.contract = accs.vaa.endpoint_address;

View File

@ -1,21 +1,12 @@
use crate::{ use crate::accounts::ConfigAccount;
accounts::ConfigAccount,
types::*,
};
use solana_program::{ use solana_program::{
account_info::AccountInfo, account_info::AccountInfo,
msg,
program_error::ProgramError,
pubkey::Pubkey, pubkey::Pubkey,
}; };
use solitaire::{ use solitaire::{
CreationLamports::Exempt, CreationLamports::Exempt,
*, *,
}; };
use std::ops::{
Deref,
DerefMut,
};
#[derive(FromAccounts)] #[derive(FromAccounts)]
pub struct Initialize<'b> { pub struct Initialize<'b> {

View File

@ -10,7 +10,6 @@ use crate::{
MintSigner, MintSigner,
SplTokenMeta, SplTokenMeta,
SplTokenMetaDerivationData, SplTokenMetaDerivationData,
WrappedDerivationData,
WrappedMetaDerivationData, WrappedMetaDerivationData,
WrappedMint, WrappedMint,
WrappedTokenMeta, WrappedTokenMeta,
@ -25,11 +24,7 @@ use crate::{
}, },
}; };
use bridge::{ use bridge::{
accounts::Bridge, api::PostMessageData,
api::{
PostMessage,
PostMessageData,
},
types::ConsistencyLevel, types::ConsistencyLevel,
vaa::SerializePayload, vaa::SerializePayload,
}; };
@ -44,9 +39,7 @@ use solana_program::{
invoke, invoke,
invoke_signed, invoke_signed,
}, },
program_error::ProgramError,
program_option::COption, program_option::COption,
pubkey::Pubkey,
sysvar::clock::Clock, sysvar::clock::Clock,
}; };
use solitaire::{ use solitaire::{
@ -57,18 +50,7 @@ use solitaire::{
CreationLamports::Exempt, CreationLamports::Exempt,
*, *,
}; };
use spl_token::{
error::TokenError::OwnerMismatch,
state::{
Account,
Mint,
},
};
use spl_token_metadata::state::Metadata; use spl_token_metadata::state::Metadata;
use std::ops::{
Deref,
DerefMut,
};
#[derive(FromAccounts)] #[derive(FromAccounts)]
pub struct TransferNative<'b> { pub struct TransferNative<'b> {

View File

@ -1,6 +1,6 @@
#![feature(const_generics)] #![feature(const_generics)]
#![allow(warnings)] #![allow(incomplete_features)]
#![deny(unused_must_use)]
// #![cfg(all(target_arch = "bpf", not(feature = "no-entrypoint")))] // #![cfg(all(target_arch = "bpf", not(feature = "no-entrypoint")))]
#[cfg(feature = "no-entrypoint")] #[cfg(feature = "no-entrypoint")]
@ -44,7 +44,6 @@ pub use api::{
}; };
use solitaire::*; use solitaire::*;
use std::error::Error;
pub enum TokenBridgeError { pub enum TokenBridgeError {
AlreadyExecuted, AlreadyExecuted,

View File

@ -1,13 +1,6 @@
use crate::{ use crate::types::{
types::{ Address,
Address, ChainID,
ChainID,
},
TokenBridgeError,
};
use borsh::{
BorshDeserialize,
BorshSerialize,
}; };
use bridge::{ use bridge::{
vaa::{ vaa::{
@ -24,23 +17,14 @@ use byteorder::{
}; };
use primitive_types::U256; use primitive_types::U256;
use solana_program::{ use solana_program::{
native_token::Sol, program_error::ProgramError::InvalidAccountData,
program_error::{
ProgramError,
ProgramError::InvalidAccountData,
},
pubkey::Pubkey, pubkey::Pubkey,
}; };
use solitaire::SolitaireError; use solitaire::SolitaireError;
use std::{ use std::io::{
error::Error, Cursor,
io::{ Read,
Cursor, Write,
Read,
Write,
},
str::Utf8Error,
string::FromUtf8Error,
}; };
pub const MODULE: &str = "NFTBridge"; pub const MODULE: &str = "NFTBridge";
@ -140,13 +124,13 @@ impl SerializePayload for PayloadTransfer {
for i in 0..self.symbol.len() { for i in 0..self.symbol.len() {
symbol[i] = self.symbol.as_bytes()[i]; symbol[i] = self.symbol.as_bytes()[i];
} }
writer.write(&symbol); writer.write(&symbol)?;
let mut name: [u8; 32] = [0; 32]; let mut name: [u8; 32] = [0; 32];
for i in 0..self.name.len() { for i in 0..self.name.len() {
name[i] = self.name.as_bytes()[i]; name[i] = self.name.as_bytes()[i];
} }
writer.write(&name); writer.write(&name)?;
let mut id_data: [u8; 32] = [0; 32]; let mut id_data: [u8; 32] = [0; 32];
self.token_id.to_big_endian(&mut id_data); self.token_id.to_big_endian(&mut id_data);
@ -206,7 +190,7 @@ where
Self: SerializeGovernancePayload, Self: SerializeGovernancePayload,
{ {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), SolitaireError> { fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), SolitaireError> {
self.write_governance_header(writer); self.write_governance_header(writer)?;
// Payload ID // Payload ID
writer.write_u16::<BigEndian>(self.chain)?; writer.write_u16::<BigEndian>(self.chain)?;
writer.write(&self.endpoint_address[..])?; writer.write(&self.endpoint_address[..])?;
@ -223,7 +207,7 @@ pub struct GovernancePayloadUpgrade {
impl SerializePayload for GovernancePayloadUpgrade { impl SerializePayload for GovernancePayloadUpgrade {
fn serialize<W: Write>(&self, v: &mut W) -> std::result::Result<(), SolitaireError> { fn serialize<W: Write>(&self, v: &mut W) -> std::result::Result<(), SolitaireError> {
self.write_governance_header(v); self.write_governance_header(v)?;
v.write(&self.new_contract.to_bytes())?; v.write(&self.new_contract.to_bytes())?;
Ok(()) Ok(())
} }

View File

@ -2,7 +2,6 @@ use borsh::{
BorshDeserialize, BorshDeserialize,
BorshSerialize, BorshSerialize,
}; };
use primitive_types::U256;
use serde::{ use serde::{
Deserialize, Deserialize,
Serialize, Serialize,
@ -19,7 +18,6 @@ use spl_token::state::{
Account, Account,
Mint, Mint,
}; };
use spl_token_metadata::state::Metadata;
pub type Address = [u8; 32]; pub type Address = [u8; 32];
pub type ChainID = u16; pub type ChainID = u16;