Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_owner method for Aurora DAO #690

Merged
merged 48 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
9f52be1
add set_owner method
hskang9 Feb 15, 2023
f55ffce
fix format
hskang9 Feb 15, 2023
c380457
refactor set_owner method with borsh args parameters and adding new t…
hskang9 Feb 15, 2023
b1aab21
remove warnings
hskang9 Feb 15, 2023
e16801a
add traits for new TransactionKind parameter
hskang9 Feb 15, 2023
f34555e
add a match fill arm for new TransactionKind parameter
hskang9 Feb 15, 2023
da35891
add match arms for BorshTransaction and TransactionKind
hskang9 Feb 15, 2023
f009f7a
replace `as` coversions to `from` conversions
hskang9 Feb 16, 2023
29e4b3b
explicitly declare types on number constants
hskang9 Feb 16, 2023
636206f
add custom type conversion From trait for handling data loss on `as` …
hskang9 Feb 16, 2023
96cec00
replace `as` to `try_into`
hskang9 Feb 16, 2023
b1778da
fix typo
hskang9 Feb 16, 2023
06c996e
lint fix
hskang9 Feb 16, 2023
fe267fe
lint fix
hskang9 Feb 16, 2023
f5e07c1
set variant order in BorshTransactionKind and return json encoded boo…
hskang9 Feb 16, 2023
06f4f5a
use TryFrom in type conversions
hskang9 Feb 16, 2023
895ddf9
Merge remote-tracking branch 'origin/feat/add-set-owner' into feat/ad…
hskang9 Feb 16, 2023
a25bd05
format code
hskang9 Feb 16, 2023
7be858c
remove as keyword
hskang9 Feb 16, 2023
95a284a
Panic on same previous owner input
hskang9 Feb 16, 2023
91fc238
Update engine-standalone-storage/src/relayer_db/types.rs
hskang9 Feb 17, 2023
a7b7cd4
Update engine-standalone-storage/src/relayer_db/types.rs
hskang9 Feb 17, 2023
8ff5c40
add tests for setting owner in standalone environment
hskang9 Feb 17, 2023
b58eb27
format codes
hskang9 Feb 17, 2023
9fc5f3d
Merge remote-tracking branch 'origin/feat/add-set-owner' into feat/ad…
hskang9 Feb 17, 2023
0067f9d
update Error type on TransactionDuration
hskang9 Feb 17, 2023
8abdbe9
Optimize set_owner tests
hskang9 Feb 17, 2023
173fb43
Refactor set_owner standalone tests
hskang9 Feb 17, 2023
7d5f49b
fix lint
hskang9 Feb 17, 2023
1b47231
fix input on test_set_owner_fail_on_same_owner
hskang9 Feb 17, 2023
5fe6039
add error type on standalone tests
hskang9 Feb 17, 2023
6642a73
lint fix
hskang9 Feb 17, 2023
0d6e240
remove unused dependancy
hskang9 Feb 17, 2023
dc2944b
remove wrong comments
hskang9 Feb 20, 2023
281279b
add set_owner method to standalone env
hskang9 Feb 20, 2023
ba810bf
format codes
hskang9 Feb 20, 2023
4e3c988
remove unneeded output in set_owner
hskang9 Feb 20, 2023
d8713d4
refactor json args parse in set_owner
hskang9 Feb 20, 2023
2f91deb
modify tests
hskang9 Feb 20, 2023
9a595a3
remove outdated comment
hskang9 Feb 20, 2023
3719912
refactor standalone test with json input
hskang9 Feb 20, 2023
ff1f2ad
format codes
hskang9 Feb 20, 2023
57cfedb
refactor standalone input method handler with json input
hskang9 Feb 20, 2023
56c0180
use borsh for now
hskang9 Feb 21, 2023
277bad6
Merge branch 'develop' into feat/add-set-owner
hskang9 Feb 23, 2023
62afd52
merge all converted types into one struct type
hskang9 Feb 23, 2023
f44834a
remove unused comment
hskang9 Feb 23, 2023
08fc7b9
remove unnecessary output in set_owner
hskang9 Feb 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion engine-standalone-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ pub enum StoragePrefix {
EngineAccountId = 0x07,
}

impl From<StoragePrefix> for u8 {
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
fn from(prefix: StoragePrefix) -> Self {
match prefix {
StoragePrefix::BlockHash => 0x00,
StoragePrefix::BlockHeight => 0x01,
StoragePrefix::TransactionData => 0x02,
StoragePrefix::TransactionHash => 0x03,
StoragePrefix::Diff => 0x04,
StoragePrefix::Engine => 0x05,
StoragePrefix::BlockMetadata => 0x06,
StoragePrefix::EngineAccountId => 0x07,
}
}
}

const ACCOUNT_ID_KEY: &[u8] = b"engine_account_id";

pub struct Storage {
Expand Down Expand Up @@ -463,7 +478,7 @@ impl BlockMetadata {
}

fn construct_storage_key(prefix: StoragePrefix, key: &[u8]) -> Vec<u8> {
[&[VERSION], &[prefix as u8], key].concat()
[&[VERSION], &[u8::from(prefix)], key].concat()
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
}

fn construct_engine_key(key: &[u8], block_height: u64, transaction_position: u16) -> Vec<u8> {
Expand Down
124 changes: 107 additions & 17 deletions engine-standalone-storage/src/relayer_db/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,54 @@ pub struct BlockRow {
pub receipts_root: H256,
}

struct BlockRowSize(i32);

impl From<BlockRowSize> for u32 {
fn from(value: BlockRowSize) -> Self {
// set negative values to 0
let res: u32 = value.0.max(0).try_into().unwrap();
hskang9 marked this conversation as resolved.
Show resolved Hide resolved
res
}
}

struct BlockRowChain(i32);

impl From<BlockRowChain> for u64 {
fn from(value: BlockRowChain) -> Self {
// set negative values to 0
let res: u64 = value.0.max(0).try_into().unwrap();
res
}
}

struct BlockRowId(i64);

impl From<BlockRowId> for u64 {
fn from(value: BlockRowId) -> Self {
// set negative values to 0
let res: u64 = value.0.max(0).try_into().unwrap();
res
}
}

struct BlockRowBlock(i64);

impl From<BlockRowBlock> for u64 {
fn from(value: BlockRowBlock) -> Self {
// set negative values to 0
let res: u64 = value.0.max(0).try_into().unwrap();
res
}
}

impl From<postgres::Row> for BlockRow {
fn from(row: postgres::Row) -> Self {
let chain: i32 = row.get("chain");
let id: i64 = row.get("id");
let chain: BlockRowChain = BlockRowChain(row.get("chain"));
let id: BlockRowId = BlockRowId(row.get("id"));
let hash = get_hash(&row, "hash");
let near_hash: Option<&[u8]> = row.get("near_hash");
let timestamp = get_timestamp(&row, "timestamp");
let size: i32 = row.get("size");
let size: BlockRowSize = BlockRowSize(row.get("size"));
let gas_limit = get_numeric(&row, "gas_limit");
let gas_used = get_numeric(&row, "gas_used");
let parent_hash = get_hash(&row, "parent_hash");
Expand All @@ -84,12 +124,12 @@ impl From<postgres::Row> for BlockRow {
let receipts_root = get_hash(&row, "receipts_root");

Self {
chain: chain as u64,
id: id as u64,
chain: u64::from(chain),
id: u64::from(id),
hash,
near_hash: near_hash.map(H256::from_slice),
timestamp,
size: size as u32,
size: u32::from(size),
gas_limit,
gas_used,
parent_hash,
Expand Down Expand Up @@ -146,12 +186,41 @@ pub struct TransactionRow {
pub output: Vec<u8>,
}

struct TransactionRowBlock(pub i64);

impl From<TransactionRowBlock> for u64 {
fn from(value: TransactionRowBlock) -> Self {
// set negative values to 0
let res: u64 = value.0.max(0).try_into().unwrap();
hskang9 marked this conversation as resolved.
Show resolved Hide resolved
res
}
}
struct TransactionRowIndex(pub i32);

impl From<TransactionRowIndex> for u16 {
fn from(value: TransactionRowIndex) -> Self {
// set Maximum value to u16::MAX
let res: u16 = value.0.min(u16::MAX.into()).try_into().unwrap();
res
}
}

struct TransactionRowId(pub i64);

impl From<TransactionRowId> for u64 {
fn from(value: TransactionRowId) -> Self {
// set negative values to 0
let res: u64 = value.0.max(0).try_into().unwrap();
res
}
}

impl From<postgres::Row> for TransactionRow {
fn from(row: postgres::Row) -> Self {
let block: i64 = row.get("block");
let block: TransactionRowBlock = TransactionRowBlock(row.get("block"));
let block_hash = get_hash(&row, "block_hash");
let index: i32 = row.get("index");
let id: i64 = row.get("id");
let index: TransactionRowIndex = TransactionRowIndex(row.get("index"));
hskang9 marked this conversation as resolved.
Show resolved Hide resolved
let id: TransactionRowId = TransactionRowId(row.get("id"));
let hash = get_hash(&row, "hash");
let near_hash = get_hash(&row, "near_hash");
let near_receipt_hash = get_hash(&row, "near_receipt_hash");
Expand All @@ -170,10 +239,10 @@ impl From<postgres::Row> for TransactionRow {
let output: Option<Vec<u8>> = row.get("output");

Self {
block: block as u64,
block: u64::from(block),
block_hash,
index: index as u16,
id: id as u64,
index: u16::from(index),
id: u64::from(id),
hash,
near_hash,
near_receipt_hash,
Expand Down Expand Up @@ -229,11 +298,22 @@ fn get_address(row: &postgres::Row, field: &str) -> Address {
Address::try_from_slice(value).unwrap()
}

struct TransactionDuration(pub u128);

impl From<TransactionDuration> for u64 {
fn from(value: TransactionDuration) -> Self {
// use a bitwise AND operation with the mask 0xFFFFFFFFFFFFFFFF to extract the lower 64 bits of the u128 value,
// then cast the result to a u64
let value_as_u64: u64 = (value.0 & 0xFFFFFFFFFFFFFFFF).try_into().unwrap();
value_as_u64
}
}

fn get_timestamp(row: &postgres::Row, field: &str) -> Option<u64> {
let timestamp: Option<SystemTime> = row.get(field);
timestamp
.and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok())
.map(|d| d.as_nanos() as u64)
.map(|d| u64::from(TransactionDuration(d.as_nanos())))
}

struct PostgresNumeric {
Expand All @@ -259,6 +339,16 @@ enum PostgresNumericSign {
NaN = 0xc000,
}

impl From<PostgresNumericSign> for u16 {
fn from(sign: PostgresNumericSign) -> Self {
match sign {
PostgresNumericSign::Positive => 0x0000,
PostgresNumericSign::Negative => 0x4000,
PostgresNumericSign::NaN => 0xc000,
}
}
}

impl TryFrom<PostgresNumeric> for U256 {
type Error = NumericToU256Error;

Expand Down Expand Up @@ -318,18 +408,18 @@ impl<'a> postgres::types::FromSql<'a> for PostgresNumeric {
let weight = read_i16(&mut cursor)?;

let sign_raw = read_u16(&mut cursor)?;
let sign = if sign_raw == PostgresNumericSign::Positive as u16 {
let sign = if sign_raw == u16::from(PostgresNumericSign::Positive) {
PostgresNumericSign::Positive
} else if sign_raw == PostgresNumericSign::Negative as u16 {
} else if sign_raw == u16::from(PostgresNumericSign::Negative) {
PostgresNumericSign::Negative
} else if sign_raw == PostgresNumericSign::NaN as u16 {
} else if sign_raw == u16::from(PostgresNumericSign::NaN) {
PostgresNumericSign::NaN
} else {
panic!("Unexpected Numeric Sign value");
};

let scale = read_u16(&mut cursor)?;
let mut groups = Vec::with_capacity(num_groups as usize);
let mut groups = Vec::with_capacity(usize::from(num_groups));
for _ in 0..num_groups {
groups.push(read_u16(&mut cursor)?);
}
Expand Down
8 changes: 8 additions & 0 deletions engine-standalone-storage/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ fn non_submit_execute<'db>(
let mut pauser = EnginePrecompilesPauser::from_io(io);
pauser.resume_precompiles(precompiles_to_resume);

None
}
TransactionKind::SetOwner(args) => {
let mut prev = state::get_state(&io)?;

prev.owner_id = args.clone().new_owner;
state::set_state(&mut io, prev)?;

None
}
};
Expand Down
6 changes: 6 additions & 0 deletions engine-standalone-storage/src/sync/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub enum TransactionKind {
StorageUnregister(Option<bool>),
/// FT storage standard method
StorageWithdraw(parameters::StorageWithdrawCallArgs),
/// Admin only method; used to transfer administration
SetOwner(parameters::SetOwnerArgs),
/// Admin only method
SetPausedFlags(parameters::PauseEthConnectorCallArgs),
/// Ad entry mapping from address to relayer NEAR account
Expand Down Expand Up @@ -339,6 +341,7 @@ impl TransactionKind {
TransactionKind::Unknown => Self::no_evm_execution("unknown"),
Self::PausePrecompiles(_) => Self::no_evm_execution("pause_precompiles"),
Self::ResumePrecompiles(_) => Self::no_evm_execution("resume_precompiles"),
TransactionKind::SetOwner(_) => Self::no_evm_execution("set_owner"),
}
}

Expand Down Expand Up @@ -504,6 +507,7 @@ enum BorshableTransactionKind<'a> {
PausePrecompiles(Cow<'a, parameters::PausePrecompilesCallArgs>),
ResumePrecompiles(Cow<'a, parameters::PausePrecompilesCallArgs>),
Unknown,
SetOwner(Cow<'a, parameters::SetOwnerArgs>),
}

impl<'a> From<&'a TransactionKind> for BorshableTransactionKind<'a> {
Expand Down Expand Up @@ -544,6 +548,7 @@ impl<'a> From<&'a TransactionKind> for BorshableTransactionKind<'a> {
TransactionKind::Unknown => Self::Unknown,
TransactionKind::PausePrecompiles(x) => Self::PausePrecompiles(Cow::Borrowed(x)),
TransactionKind::ResumePrecompiles(x) => Self::ResumePrecompiles(Cow::Borrowed(x)),
TransactionKind::SetOwner(x) => Self::SetOwner(Cow::Borrowed(x)),
}
}
}
Expand Down Expand Up @@ -601,6 +606,7 @@ impl<'a> TryFrom<BorshableTransactionKind<'a>> for TransactionKind {
BorshableTransactionKind::ResumePrecompiles(x) => {
Ok(Self::ResumePrecompiles(x.into_owned()))
}
BorshableTransactionKind::SetOwner(x) => Ok(Self::SetOwner(x.into_owned())),
}
}
}
20 changes: 20 additions & 0 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub unsafe fn on_alloc_error(_: core::alloc::Layout) -> ! {
#[cfg(feature = "contract")]
mod contract {
use borsh::{BorshDeserialize, BorshSerialize};
use parameters::SetOwnerArgs;

use crate::connector::{self, EthConnectorContract};
use crate::engine::{self, Engine};
Expand Down Expand Up @@ -142,6 +143,25 @@ mod contract {
io.return_output(state.owner_id.as_bytes());
}

/// Set owner account id for this contract.
#[no_mangle]
pub extern "C" fn set_owner() {
birchmd marked this conversation as resolved.
Show resolved Hide resolved
let mut io = Runtime;
let mut state = state::get_state(&io).sdk_unwrap();
require_owner_only(&state, &io.predecessor_account_id());
let args: SetOwnerArgs = io.read_input_borsh().sdk_unwrap();
hskang9 marked this conversation as resolved.
Show resolved Hide resolved
if state.owner_id == args.new_owner {
// Would be a no-op to set, do nothing and return false
hskang9 marked this conversation as resolved.
Show resolved Hide resolved
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
io.return_output(b"false");
} else {
state.owner_id = args.new_owner;
state::set_state(&mut io, state).sdk_unwrap();
io.return_output(b"true");
}
// return true as bytes
io.return_output(b"true");
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
}

/// Get bridge prover id for this contract.
#[no_mangle]
pub extern "C" fn get_bridge_prover() {
Expand Down
6 changes: 6 additions & 0 deletions engine/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub struct NewCallArgs {
pub upgrade_delay_blocks: u64,
}

/// Borsh-encoded parameters for the `set_owner` function.
#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
pub struct SetOwnerArgs {
pub new_owner: AccountId,
}

/// Borsh-encoded (genesis) account balance used by the `begin_chain` function.
#[cfg(feature = "evm_bully")]
#[derive(BorshSerialize, BorshDeserialize)]
Expand Down