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

fix: test function for compatibility #119

Merged
merged 17 commits into from
Sep 5, 2024
Merged
4 changes: 2 additions & 2 deletions api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ typedef struct {
U8SliceView,
U8SliceView,
uint64_t,
uint64_t*,
UnmanagedVector*,
UnmanagedVector*);
int32_t (*share_to_amount)(const api_t*,
U8SliceView,
U8SliceView,
uint64_t,
U8SliceView,
uint64_t*,
UnmanagedVector*);
int32_t (*unbond_timestamp)(const api_t*, uint64_t*, UnmanagedVector*);
Expand Down
22 changes: 11 additions & 11 deletions api/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ typedef GoError (*scan_db_fn)(db_t *ptr, U8SliceView prefix, U8SliceView start,
// and api
typedef GoError (*query_fn)(api_t *ptr, U8SliceView request, uint64_t gasBalance, UnmanagedVector *response, uint64_t *usedGas, UnmanagedVector *errOut);
typedef GoError (*get_account_info_fn)(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, uint8_t *account_type, bool *is_blocked, UnmanagedVector *errOut);
typedef GoError (*amount_to_share_fn)(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, uint64_t *share, UnmanagedVector *errOut);
typedef GoError (*share_to_amount_fn)(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t share, uint64_t *amount, UnmanagedVector *errOut);
typedef GoError (*amount_to_share_fn)(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, UnmanagedVector *share, UnmanagedVector *errOut);
typedef GoError (*share_to_amount_fn)(api_t *ptr, U8SliceView validator, U8SliceView metadata, U8SliceView share, uint64_t *amount, UnmanagedVector *errOut);
typedef GoError (*unbond_timestamp_fn)(api_t *ptr, uint64_t *unbondTimestamp, UnmanagedVector *errOut);
typedef GoError (*get_price_fn)(api_t *ptr, U8SliceView pairId, UnmanagedVector *price, uint64_t *updatedAt, uint64_t *decimals, UnmanagedVector *errOut);
// and iterator
Expand All @@ -29,8 +29,8 @@ GoError cScan_cgo(db_t *ptr, U8SliceView prefix, U8SliceView start, U8SliceView
// api
GoError cQuery_cgo(api_t *ptr, U8SliceView request, uint64_t gasBalance, UnmanagedVector *response, uint64_t *usedGas, UnmanagedVector *errOut);
GoError cGetAccountInfo_cgo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, uint8_t *account_type, bool *is_blocked, UnmanagedVector *errOut);
GoError cAmountToShare_cgo(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, uint64_t *share, UnmanagedVector *errOut);
GoError cShareToAmount_cgo(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t share, uint64_t *amount, UnmanagedVector *errOut);
GoError cAmountToShare_cgo(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, UnmanagedVector *share, UnmanagedVector *errOut);
GoError cShareToAmount_cgo(api_t *ptr, U8SliceView validator, U8SliceView metadata, U8SliceView share, uint64_t *amount, UnmanagedVector *errOut);
GoError cUnbondTimestamp_cgo(api_t *ptr, uint64_t *unbondTimestamp, UnmanagedVector *errOut);
GoError cGetPrice_cgo(api_t *ptr, U8SliceView pairId, UnmanagedVector *price, uint64_t *updatedAt, uint64_t *decimals, UnmanagedVector *errOut);
// iterator
Expand Down Expand Up @@ -303,8 +303,8 @@ func cNext(ref C.iterator_t, key *C.UnmanagedVector, errOut *C.UnmanagedVector)
type GoAPI interface {
Query(types.QueryRequest, uint64) ([]byte, uint64, error)
GetAccountInfo(types.AccountAddress) (bool /* found */, uint64 /* account number */, uint64 /* sequence */, uint8 /* account type */, bool /* is blocked */)
AmountToShare([]byte, types.AccountAddress, uint64) (uint64, error)
ShareToAmount([]byte, types.AccountAddress, uint64) (uint64, error)
AmountToShare([]byte, types.AccountAddress, uint64) (string, error)
ShareToAmount([]byte, types.AccountAddress, string) (uint64, error)
UnbondTimestamp() uint64
GetPrice(string) ([]byte, uint64, uint64, error)
}
Expand Down Expand Up @@ -402,7 +402,7 @@ func cGetAccountInfo(ptr *C.api_t, addr C.U8SliceView, found *C.bool, account_nu
}

//export cAmountToShare
func cAmountToShare(ptr *C.api_t, validator C.U8SliceView, metadata C.U8SliceView, amount C.uint64_t, share *C.uint64_t, errOut *C.UnmanagedVector) (ret C.GoError) {
func cAmountToShare(ptr *C.api_t, validator C.U8SliceView, metadata C.U8SliceView, amount C.uint64_t, share *C.UnmanagedVector, errOut *C.UnmanagedVector) (ret C.GoError) {
defer recoverPanic(&ret)

if share == nil {
Expand Down Expand Up @@ -433,12 +433,12 @@ func cAmountToShare(ptr *C.api_t, validator C.U8SliceView, metadata C.U8SliceVie
return C.GoError_User
}

*share = C.uint64_t(s)
*share = newUnmanagedVector([]byte(s))
return C.GoError_None
}

//export cShareToAmount
func cShareToAmount(ptr *C.api_t, validator C.U8SliceView, metadata C.U8SliceView, share C.uint64_t, amount *C.uint64_t, errOut *C.UnmanagedVector) (ret C.GoError) {
func cShareToAmount(ptr *C.api_t, validator C.U8SliceView, metadata C.U8SliceView, share C.U8SliceView, amount *C.uint64_t, errOut *C.UnmanagedVector) (ret C.GoError) {
defer recoverPanic(&ret)

if amount == nil {
Expand All @@ -455,15 +455,15 @@ func cShareToAmount(ptr *C.api_t, validator C.U8SliceView, metadata C.U8SliceVie

v := copyU8Slice(validator)
m := copyU8Slice(metadata)
s := uint64(share)
s := copyU8Slice(share)

t, err := types.BcsDeserializeAccountAddress(m)
if err != nil {
*errOut = newUnmanagedVector([]byte(err.Error()))
return C.GoError_User
}

a, err := api.ShareToAmount(v, t, s)
a, err := api.ShareToAmount(v, t, string(s))
if err != nil {
*errOut = newUnmanagedVector([]byte(err.Error()))
return C.GoError_User
Expand Down
8 changes: 4 additions & 4 deletions api/callbacks_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ GoError cScan(db_t *ptr, U8SliceView prefix, U8SliceView start, U8SliceView end,
// imports (api)
GoError cQuery(api_t *ptr, U8SliceView request, uint64_t gasBalance, UnmanagedVector *response, uint64_t *usedGas, UnmanagedVector *errOut);
GoError cGetAccountInfo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, uint8_t *account_type, bool *is_blocked, UnmanagedVector *errOut);
GoError cAmountToShare(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, uint64_t *share, UnmanagedVector *errOut);
GoError cShareToAmount(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t share, uint64_t *amount, UnmanagedVector *errOut);
GoError cAmountToShare(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, UnmanagedVector *share, UnmanagedVector *errOut);
GoError cShareToAmount(api_t *ptr, U8SliceView validator, U8SliceView metadata, U8SliceView share, uint64_t *amount, UnmanagedVector *errOut);
GoError cUnbondTimestamp(api_t *ptr, uint64_t *unbondTimestamp, UnmanagedVector *errOut);
GoError cGetPrice(api_t *ptr, U8SliceView pairId, UnmanagedVector *price, uint64_t *updatedAt, uint64_t *decimals, UnmanagedVector *errOut);
// imports (iterator)
Expand Down Expand Up @@ -45,10 +45,10 @@ GoError cQuery_cgo(api_t *ptr, U8SliceView request, uint64_t gasBalance, Unmanag
GoError cGetAccountInfo_cgo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, uint8_t *account_type, bool *is_blocked, UnmanagedVector *errOut) {
return cGetAccountInfo(ptr, addr, found, account_number, sequence, account_type, is_blocked, errOut);
}
GoError cAmountToShare_cgo(api_t *ptr, U8SliceView validator, U8SliceView coinType, uint64_t amount, uint64_t *share, UnmanagedVector *errOut) {
GoError cAmountToShare_cgo(api_t *ptr, U8SliceView validator, U8SliceView coinType, uint64_t amount, UnmanagedVector *share, UnmanagedVector *errOut) {
return cAmountToShare(ptr, validator, coinType, amount, share, errOut);
}
GoError cShareToAmount_cgo(api_t *ptr, U8SliceView validator, U8SliceView coinType, uint64_t share, uint64_t *amount, UnmanagedVector *errOut) {
GoError cShareToAmount_cgo(api_t *ptr, U8SliceView validator, U8SliceView coinType, U8SliceView share, uint64_t *amount, UnmanagedVector *errOut) {
return cShareToAmount(ptr, validator, coinType, share, amount, errOut);
}
GoError cUnbondTimestamp_cgo(api_t *ptr, uint64_t *unbondTimestamp, UnmanagedVector *errOut) {
Expand Down
21 changes: 11 additions & 10 deletions api/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"errors"

"cosmossdk.io/math"
dbm "github.com/cosmos/cosmos-db"
"github.com/initia-labs/movevm/types"
)
Expand Down Expand Up @@ -122,11 +123,11 @@ func (m MockAPI) GetAccountInfo(addr types.AccountAddress) (bool, uint64, uint64
return m.AccountAPI.GetAccountInfo(addr)
}

func (m MockAPI) AmountToShare(validator []byte, metadata types.AccountAddress, amount uint64) (uint64, error) {
func (m MockAPI) AmountToShare(validator []byte, metadata types.AccountAddress, amount uint64) (string, error) {
return m.StakingAPI.AmountToShare(validator, metadata, amount)
}

func (m MockAPI) ShareToAmount(validator []byte, metadata types.AccountAddress, share uint64) (uint64, error) {
func (m MockAPI) ShareToAmount(validator []byte, metadata types.AccountAddress, share string) (uint64, error) {
return m.StakingAPI.ShareToAmount(validator, metadata, share)
}

Expand Down Expand Up @@ -163,7 +164,7 @@ func (m MockAccountAPI) GetAccountInfo(addr types.AccountAddress) (bool, uint64,
}

type ShareAmountRatio struct {
share uint64
share string
amount uint64
}

Expand All @@ -178,7 +179,7 @@ func NewMockStakingAPI() MockStakingAPI {
}
}

func (m *MockStakingAPI) SetShareRatio(validator []byte, metadata types.AccountAddress, share uint64, amount uint64) {
func (m *MockStakingAPI) SetShareRatio(validator []byte, metadata types.AccountAddress, share string, amount uint64) {
if ratios, ok := m.validators[string(validator)]; ok {
ratios[metadata] = ShareAmountRatio{share, amount}
} else {
Expand All @@ -187,21 +188,21 @@ func (m *MockStakingAPI) SetShareRatio(validator []byte, metadata types.AccountA
}
}

func (m MockStakingAPI) AmountToShare(validator []byte, metadata types.AccountAddress, amount uint64) (uint64, error) {
func (m MockStakingAPI) AmountToShare(validator []byte, metadata types.AccountAddress, amount uint64) (string, error) {
ratios, ok := m.validators[string(validator)]
if !ok {
return 0, errors.New("validator not found")
return "0", errors.New("validator not found")
}

ratio, ok := ratios[metadata]
if !ok {
return 0, errors.New("metadata not found")
return "0", errors.New("metadata not found")
}

return amount * ratio.share / ratio.amount, nil
return math.LegacyMustNewDecFromStr(ratio.share).MulInt64(int64(amount)).QuoInt64(int64(ratio.amount)).String(), nil
}

func (m MockStakingAPI) ShareToAmount(validator []byte, metadata types.AccountAddress, share uint64) (uint64, error) {
func (m MockStakingAPI) ShareToAmount(validator []byte, metadata types.AccountAddress, share string) (uint64, error) {
ratios, ok := m.validators[string(validator)]
if !ok {
return 0, errors.New("validator not found")
Expand All @@ -212,7 +213,7 @@ func (m MockStakingAPI) ShareToAmount(validator []byte, metadata types.AccountAd
return 0, errors.New("metadata not found")
}

return share * ratio.amount / ratio.share, nil
return math.LegacyMustNewDecFromStr(share).MulInt64(int64(ratio.amount)).Quo(math.LegacyMustNewDecFromStr(ratio.share)).TruncateInt().Uint64(), nil
}

type MockQueryAPI struct {
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ impl StakingAPI for BlankStakingAPIImpl {
_validator: &[u8],
_metadata: AccountAddress,
_amount: u64,
) -> anyhow::Result<u64> {
) -> anyhow::Result<String> {
Err(anyhow!("validator not found"))
}

fn share_to_amount(
&self,
_validator: &[u8],
_metadata: AccountAddress,
_share: u64,
_share: String,
) -> anyhow::Result<u64> {
Err(anyhow!("validator not found"))
}
Expand Down
1 change: 1 addition & 0 deletions crates/e2e-move-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ thiserror = { workspace = true }
once_cell = { workspace = true }
sha3 = { workspace = true }
bytes = { workspace = true }
bigdecimal = { workspace = true }

initia-move-types = { workspace = true }
initia-move-vm = { workspace = true }
Expand Down
22 changes: 14 additions & 8 deletions crates/e2e-move-tests/src/test_utils/mock_chain.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use bigdecimal::{num_bigint::ToBigInt, BigDecimal};
use bytes::Bytes;
use initia_move_storage::{state_view::StateView, table_resolver::TableResolver};
use std::{
collections::BTreeMap,
ops::{Bound, RangeBounds},
str::FromStr,
};

use initia_move_natives::{
Expand Down Expand Up @@ -266,7 +268,7 @@ impl StakingAPI for MockAPI {
validator: &[u8],
metadata: AccountAddress,
amount: u64,
) -> anyhow::Result<u64> {
) -> anyhow::Result<String> {
self.staking_api
.amount_to_share(validator, metadata, amount)
}
Expand All @@ -275,7 +277,7 @@ impl StakingAPI for MockAPI {
&self,
validator: &[u8],
metadata: AccountAddress,
share: u64,
share: String,
) -> anyhow::Result<u64> {
self.staking_api.share_to_amount(validator, metadata, share)
}
Expand Down Expand Up @@ -392,10 +394,10 @@ impl MockStakingAPI {
validator: &[u8],
metadata: AccountAddress,
amount: u64,
) -> anyhow::Result<u64> {
) -> anyhow::Result<String> {
match self.validators.get(validator) {
Some(ratios) => match ratios.get(&metadata) {
Some((s, a)) => Ok(amount * s / a),
Some((s, a)) => Ok((BigDecimal::from(amount) * s / a).to_string()),
None => Err(anyhow!("ratio not found")),
},
None => Err(anyhow!("validator not found")),
Expand All @@ -406,11 +408,15 @@ impl MockStakingAPI {
&self,
validator: &[u8],
metadata: AccountAddress,
share: u64,
share: String,
) -> anyhow::Result<u64> {
match self.validators.get(validator) {
Some(ratios) => match ratios.get(&metadata) {
Some((s, a)) => Ok(share * a / s),
Some((s, a)) => Ok((BigDecimal::from_str(&share).unwrap() * a / s)
.to_bigint()
.unwrap()
.try_into()
.unwrap()),
None => Err(anyhow!("ratio not found")),
},
None => Err(anyhow!("validator not found")),
Expand Down Expand Up @@ -568,15 +574,15 @@ impl StakingAPI for BlankStakingAPIImpl {
_validator: &[u8],
_metadata: AccountAddress,
_amount: u64,
) -> anyhow::Result<u64> {
) -> anyhow::Result<String> {
Err(anyhow!("validator not found"))
}

fn share_to_amount(
&self,
_validator: &[u8],
_metadata: AccountAddress,
_share: u64,
_share: String,
) -> anyhow::Result<u64> {
Err(anyhow!("validator not found"))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/e2e-move-tests/src/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeMap;
use move_core_types::{account_address::AccountAddress, vm_status::VMStatus};

type VMOutput = (VMStatus, MessageOutput, Option<String>);
type StakingDelta = (Vec<u8>, Vec<(AccountAddress, (u64, u64))>);
type StakingDelta = (Vec<u8>, Vec<(AccountAddress, (u64, String))>);

pub struct ExpectedOutput(pub Vec<ExpectedOutputItem>);

Expand Down Expand Up @@ -66,7 +66,7 @@ impl ExpectedOutput {
pub enum ExpectedOutputItem {
VMStatusReturn(VMStatus),
Response(String),
StakingChange(BTreeMap<Vec<u8>, BTreeMap<AccountAddress, (u64, u64)>>),
StakingChange(BTreeMap<Vec<u8>, BTreeMap<AccountAddress, (u64, String)>>),
CosmosMessages(Vec<CosmosMessage>),
}

Expand Down
4 changes: 2 additions & 2 deletions crates/e2e-move-tests/src/tests/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn test_simple_staking() {
None,
Some(vec![(
val_addr.clone(),
vec![(staking_metadata, (1_000_000u64, 0u64))],
vec![(staking_metadata, (1_000_000u64, "0".to_string()))],
)]),
None,
),
Expand Down Expand Up @@ -207,7 +207,7 @@ fn test_simple_staking() {
None,
Some(vec![(
val_addr.clone(),
vec![(staking_metadata, (0u64, 500_000u64))],
vec![(staking_metadata, (0u64, "500000".to_string()))],
)]),
None,
),
Expand Down
Loading
Loading