Skip to content

Commit

Permalink
svm: fix universal-address issue
Browse files Browse the repository at this point in the history
Signed-off-by: bingyuyap <[email protected]>
  • Loading branch information
bingyuyap committed Nov 26, 2024
1 parent f6c90d5 commit 5088cd2
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 135 deletions.
32 changes: 8 additions & 24 deletions svm/idls/endpoint.json
Original file line number Diff line number Diff line change
Expand Up @@ -1904,9 +1904,7 @@
{
"name": "src_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -1959,9 +1957,7 @@
"name": "src_addr",
"docs": ["Source address (32 bytes)"],
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand All @@ -1978,9 +1974,7 @@
"name": "dst_addr",
"docs": ["Destination address (32 bytes)"],
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -2037,9 +2031,7 @@
{
"name": "src_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -2443,9 +2435,7 @@
"name": "src_addr",
"docs": ["The sending integrator as a 32-byte universal address"],
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand All @@ -2462,9 +2452,7 @@
"name": "dst_addr",
"docs": ["The destination address as a 32-byte universal address"],
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -2573,9 +2561,7 @@
{
"name": "src_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -2685,9 +2671,7 @@
{
"name": "dst_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down
21 changes: 0 additions & 21 deletions svm/programs/wormhole-guardian-adapter/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use anchor_lang::prelude::*;

// TODO: this should import from `universal-address` crate
// But doing so will create a mismatched type error
use crate::endpoint::types::UniversalAddress;

#[event]
pub struct AdminUpdated {
pub old_admin: Pubkey,
Expand All @@ -26,20 +22,3 @@ pub struct PeerAdded {
pub chain: u16,
pub peer_contract: [u8; 32],
}

#[event]
pub struct MessageSent {
pub src_addr: UniversalAddress,
pub dst_chain: u16,
pub dst_addr: UniversalAddress,
pub sequence: u64,
pub payload_hash: [u8; 32],
}

#[event]
pub struct MessageReceived {
pub vaa_hash: [u8; 32],
pub emitter_chain: u16,
pub emitter_address: UniversalAddress,
pub sequence: u64,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ declare_program!(endpoint);

use anchor_lang::prelude::*;

use endpoint::{
program::Endpoint,
types::{AttestMessageArgs, UniversalAddress},
};
use endpoint::{program::Endpoint, types::AttestMessageArgs};
use wormhole_anchor_sdk::wormhole::{self, PostedVaa};
use wormhole_io::Readable;

Expand Down Expand Up @@ -143,12 +140,10 @@ pub fn recv_message(ctx: Context<ReceiveMessage>, _args: ReceiveMessageArgs) ->
adapter_program_id: crate::id(),
adapter_pda_bump: ctx.bumps.adapter_pda,
src_chain,
src_addr: UniversalAddress {
bytes: message.src_addr.bytes,
},
src_addr: message.src_addr,
sequence: message.sequence,
dst_chain: message.dst_chain,
integrator_program_id: Pubkey::new_from_array(message.dst_addr.bytes),
integrator_program_id: Pubkey::new_from_array(message.dst_addr),
payload_hash: message.payload_hash,
},
)?;
Expand Down
26 changes: 8 additions & 18 deletions svm/programs/wormhole-guardian-adapter/src/wormhole/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ use anchor_lang::prelude::*;
use std::io;
use wormhole_io::Readable;

// TODO: this should import from `universal-address` crate
// But doing so will create a mismatched type error
use crate::endpoint::types::UniversalAddress;

#[derive(AnchorSerialize, Clone, Debug)]
pub struct GuardianMessage {
pub src_addr: UniversalAddress,
pub src_addr: [u8; 32],
pub sequence: u64,
pub dst_chain: u16,
pub dst_addr: UniversalAddress,
pub dst_addr: [u8; 32],
pub payload_hash: [u8; 32],
}

Expand All @@ -29,10 +25,10 @@ impl GuardianMessage {
pub fn to_vec(&self) -> Vec<u8> {
// Match EVM encoding: abi.encodePacked(srcAddr, sequence, dstChain, dstAddr, payloadHash)
let mut bytes = Vec::with_capacity(106); // 32 + 8 + 2 + 32 + 32
bytes.extend_from_slice(&self.src_addr.bytes);
bytes.extend_from_slice(&self.src_addr);
bytes.extend_from_slice(&self.sequence.to_be_bytes());
bytes.extend_from_slice(&self.dst_chain.to_be_bytes());
bytes.extend_from_slice(&self.dst_addr.bytes);
bytes.extend_from_slice(&self.dst_addr);
bytes.extend_from_slice(&self.payload_hash);
bytes
}
Expand All @@ -47,11 +43,8 @@ impl Readable for GuardianMessage {
R: io::Read,
{
// Read src_addr
let mut src_addr_bytes = [0u8; 32];
reader.read_exact(&mut src_addr_bytes)?;
let src_addr = UniversalAddress {
bytes: src_addr_bytes,
};
let mut src_addr: [u8; 32] = [0u8; 32];
reader.read_exact(&mut src_addr)?;

// Read and debug sequence
let mut sequence_bytes = [0u8; 8];
Expand All @@ -64,11 +57,8 @@ impl Readable for GuardianMessage {
let dst_chain = u16::from_be_bytes(chain_bytes);

// Read dst_addr
let mut dst_addr_bytes = [0u8; 32];
reader.read_exact(&mut dst_addr_bytes)?;
let dst_addr = UniversalAddress {
bytes: dst_addr_bytes,
};
let mut dst_addr = [0u8; 32];
reader.read_exact(&mut dst_addr)?;

// Read payload_hash
let mut payload_hash = [0u8; 32];
Expand Down
Binary file modified svm/tests/artifacts/endpoint.so
Binary file not shown.
Binary file modified svm/tests/artifacts/mock_integrator.so
Binary file not shown.
32 changes: 8 additions & 24 deletions svm/tests/idl/endpoint.json
Original file line number Diff line number Diff line change
Expand Up @@ -1904,9 +1904,7 @@
{
"name": "src_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -1959,9 +1957,7 @@
"name": "src_addr",
"docs": ["Source address (32 bytes)"],
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand All @@ -1978,9 +1974,7 @@
"name": "dst_addr",
"docs": ["Destination address (32 bytes)"],
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -2037,9 +2031,7 @@
{
"name": "src_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -2443,9 +2435,7 @@
"name": "src_addr",
"docs": ["The sending integrator as a 32-byte universal address"],
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand All @@ -2462,9 +2452,7 @@
"name": "dst_addr",
"docs": ["The destination address as a 32-byte universal address"],
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -2573,9 +2561,7 @@
{
"name": "src_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -2685,9 +2671,7 @@
{
"name": "dst_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down
8 changes: 2 additions & 6 deletions svm/tests/idl/mock_integrator.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@
{
"name": "src_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down Expand Up @@ -406,9 +404,7 @@
{
"name": "src_addr",
"type": {
"defined": {
"name": "UniversalAddress"
}
"array": ["u8", 32]
}
},
{
Expand Down
8 changes: 4 additions & 4 deletions svm/tests/svm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,10 @@ describe("wormhole-guardian-adapter", () => {
const expectedPayload = ethers.solidityPacked(
["bytes32", "uint64", "uint16", "bytes32", "bytes32"],
[
Buffer.from(outboxAccount.srcAddr.bytes),
Buffer.from(outboxAccount.srcAddr),
BigInt(outboxAccount.sequence.toString()),
Number(outboxAccount.dstChain),
Buffer.from(outboxAccount.dstAddr.bytes),
Buffer.from(outboxAccount.dstAddr),
Buffer.from(outboxAccount.payloadHash),
],
);
Expand Down Expand Up @@ -861,13 +861,13 @@ describe("wormhole-guardian-adapter", () => {
);
assert.equal(attestationInfo.srcChain, emitterChain);
assert.deepEqual(
attestationInfo.srcAddr.bytes,
attestationInfo.srcAddr,
Array.from(ethTransceiver.address.toUint8Array()),
);
assert.equal(attestationInfo.sequence.toNumber(), 0);
assert.equal(attestationInfo.dstChain, wormholeMessage.dstChain);
assert.deepEqual(
attestationInfo.dstAddr.bytes,
attestationInfo.dstAddr,
Array.from(wormholeMessage.dstAddr.toUint8Array()),
);
assert.deepEqual(
Expand Down
Loading

0 comments on commit 5088cd2

Please sign in to comment.