Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Merge #108
Browse files Browse the repository at this point in the history
108: Deploy's body hash fix. r=zie1ony a=zie1ony

#

Co-authored-by: Maciej Zielinski <[email protected]>
  • Loading branch information
bors[bot] and zie1ony authored Feb 9, 2021
2 parents 0472c8a + 49e2f11 commit b9935be
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 49 deletions.
13 changes: 12 additions & 1 deletion packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to casper-client-sdk.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.18]

### Added

- Partial support for the Contract object under StoredValue.

### Fixed

- Deploy's body hash derivation.

## [1.0.17]

### Added
Expand All @@ -13,7 +23,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- Default `gasPrice` changed from `10` to `1`.
- Casper balances checks return `BigNumber` now.
- Casper balances checks return `BigNumber` now.

## [1.0.15]

### Added
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-client-sdk",
"version": "1.0.17",
"version": "1.0.18",
"license": "Apache 2.0",
"description": "SDK to interact with the Casper blockchain",
"main": "dist/index.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/lib/DeployUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class StoredContractByHash extends ExecutableDeployItemInternal {
Uint8Array.from([this.tag]),
toBytesBytesArray(this.hash),
toBytesString(this.entryPoint),
toBytesArrayU8(this.args.toBytes())
toBytesBytesArray(this.args.toBytes())
]);
}
}
Expand Down Expand Up @@ -321,7 +321,7 @@ export class StoredContractByName extends ExecutableDeployItemInternal {
Uint8Array.from([this.tag]),
toBytesString(this.name),
toBytesString(this.entryPoint),
toBytesArrayU8(this.args.toBytes())
toBytesBytesArray(this.args.toBytes())
]);
}
}
Expand Down Expand Up @@ -370,7 +370,7 @@ export class StoredVersionedContractByName extends ExecutableDeployItemInternal
toBytesString(this.name),
serializedVersion.toBytes(),
toBytesString(this.entryPoint),
toBytesArrayU8(this.args.toBytes())
toBytesBytesArray(this.args.toBytes())
]);
}
}
Expand Down Expand Up @@ -429,7 +429,7 @@ export class StoredVersionedContractByHash extends ExecutableDeployItemInternal
toBytesBytesArray(this.hash),
serializedVersion.toBytes(),
toBytesString(this.entryPoint),
toBytesArrayU8(this.args.toBytes())
toBytesBytesArray(this.args.toBytes())
]);
}
}
Expand Down
21 changes: 18 additions & 3 deletions packages/sdk/src/lib/StoredValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,29 @@ export class SeigniorageAllocation {
}

/**
* Auction metdata. Intended to be recorded at each era.
* Auction metadata. Intended to be recorded at each era.
*/
@jsonObject
export class EraInfoJson {
@jsonArrayMember(SeigniorageAllocation, { name: 'seigniorage_allocations' })
public seigniorageAllocations: SeigniorageAllocation[];
}

/**
* Contract metadata.
*/
@jsonObject
export class ContractMetadataJson {
@jsonMember({ name: 'contract_package_hash', constructor: String })
public contractPackageHash: string;

@jsonMember({ name: 'contract_wasm_hash', constructor: String })
public contractWasmHash: string;

@jsonMember({ name: 'protocol_version', constructor: String })
public protocolVersion: string;
}

@jsonObject
export class StoredValue {
// StoredVale
Expand All @@ -167,8 +182,8 @@ export class StoredValue {
public ContractWASM?: string;

// Methods and type signatures supported by a contract
@jsonMember({ constructor: String })
public Contract?: string;
@jsonMember({ constructor: ContractMetadataJson })
public Contract?: ContractMetadataJson;

// A contract definition, metadata, and security container
@jsonMember({ constructor: String })
Expand Down
17 changes: 14 additions & 3 deletions packages/sdk/test/lib/StoredValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,25 @@ describe('StoredValue', () => {

it('should parse Contract stored value correctly', function () {
const mockJson = {
Contract:
'ea058d32053f59e9f66dd3d4de4594a8a3de36c65c87417efe79cdc7c1b926b4b1575b26a474dc52c03c2a7207db951fa4f82214ee4257c4b99bbab603908d5900000000010000000b00000063616c6c5f6661756365740b00000063616c6c5f66617563657402000000060000007461726765740f2000000006000000616d6f756e7408090100010000000000000000000000'
Contract: {
contract_package_hash: 'package-uref',
contract_wasm_hash: 'wasm-hash-uref',
protocol_version: '1.0.0'
}
};

const serializer = new TypedJSON(StoredValue);
const storedValue = serializer.parse(mockJson);
expect(storedValue?.Contract).to.not.eq(undefined);
expect(storedValue?.Contract).to.eq(mockJson.Contract);
expect(storedValue?.Contract?.contractPackageHash).to.eq(
mockJson.Contract.contract_package_hash
);
expect(storedValue?.Contract?.contractWasmHash).to.eq(
mockJson.Contract.contract_wasm_hash
);
expect(storedValue?.Contract?.protocolVersion).to.eq(
mockJson.Contract.protocol_version
);
});

it('should parse ContractPackage stored value correctly', function () {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/@types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ interface AccountBalance {
checkedAt: Date;
blockHashBase16: string;
// undefine means the account didn't exist.
balance: number | undefined;
balance: BigNumber | undefined;
}
10 changes: 0 additions & 10 deletions packages/ui/src/components/App.test.tsx

This file was deleted.

5 changes: 3 additions & 2 deletions packages/ui/src/components/BlockDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RefreshableComponent, SuccessIcon, FailIcon, CSPR } from './Utils';
import { shortHash } from './Utils';
import ObservableValueMap, { ObservableValue } from '../lib/ObservableValueMap';
import { BlockResult, DeployResult, JsonBlock } from 'casper-client-sdk';
import { BigNumber } from '@ethersproject/bignumber';

// https://www.pluralsight.com/guides/react-router-typescript

Expand Down Expand Up @@ -122,7 +123,7 @@ const DeploysTable = observer(
</td>
<td>{shortHash(accountId)}</td>
<td className="text-right">
<CSPR motes={deploy.cost} />
<CSPR motes={BigNumber.from(deploy.cost)} />
</td>
<td className="text-right">
<span>{deploy.state}</span>
Expand Down Expand Up @@ -165,7 +166,7 @@ export const Balance = observer(
(props: { balance: ObservableValue<number> }) => {
const value = props.balance.value;
if (value == null) return null;
return <CSPR motes={value} />;
return <CSPR motes={BigNumber.from(value)} />;
}
);

Expand Down
9 changes: 4 additions & 5 deletions packages/ui/src/components/Utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { observer } from 'mobx-react';
import AuthContainer from '../containers/AuthContainer';
import { encodeBase16 } from 'casper-client-sdk';
import { Helmet } from 'react-helmet';
import { BigNumber } from '@ethersproject/bignumber';

export const Spinner = (msg: String) => (
<div className="text-center">
Expand Down Expand Up @@ -232,9 +233,7 @@ export const Title = (props: { title: string }) => (
</Helmet>
);

export const CSPR = (props: { motes: number }) => {
const s = (props.motes / 1000_000_000).toLocaleString(undefined, {
maximumFractionDigits: 5
});
return <span>{s} CSPR</span>;
export const CSPR = (props: { motes: BigNumber }) => {
let val = props.motes.div(BigNumber.from(1000_000_000));
return <span>{val.toString()} CSPR</span>;
};
2 changes: 1 addition & 1 deletion packages/ui/src/containers/AuthContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class AuthContainer {
this.balances.set(accountHash, {
checkedAt: now,
blockHashBase16: latestBlockHash,
balance: latestAccountBalance?.toNumber()
balance: latestAccountBalance
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions smart-contract/faucet-stored/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ doctest = false
test = false

[features]
std = ["casperlabs-contract/std", "casperlabs-types/std"]
std = ["casper-contract/std", "casper-types/std"]

[dependencies]
casperlabs-contract = "0.6.0"
casperlabs-types = "0.6.0"
faucet = { path = "../faucet", package = "faucet" }
casper-contract = "0.7.0"
casper-types = "0.7.0"
faucet = { path = "../faucet" }
6 changes: 3 additions & 3 deletions smart-contract/faucet-stored/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ extern crate alloc;

use alloc::{string::ToString, vec};

use casperlabs_contract::contract_api::{runtime, storage};
use casperlabs_types::{
account::AccountHash, CLType, CLTyped, ContractHash, ContractVersion, EntryPoint,
use casper_contract::contract_api::{runtime, storage};
use casper_types::{
account::AccountHash, contracts::ContractHash, CLType, CLTyped, ContractVersion, EntryPoint,
EntryPointAccess, EntryPointType, EntryPoints, Parameter,
};

Expand Down
6 changes: 3 additions & 3 deletions smart-contract/faucet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ test = false
bench = false

[features]
std = ["casperlabs-contract/std", "casperlabs-types/std"]
std = ["casper-contract/std", "casper-types/std"]

[dependencies]
casperlabs-contract = "0.6.0"
casperlabs-types = "0.6.0"
casper-contract = "0.7.0"
casper-types = "0.7.0"
15 changes: 8 additions & 7 deletions smart-contract/faucet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![no_std]

use casperlabs_contract::{
use casper_contract::{
contract_api::{runtime, storage, system},
unwrap_or_revert::UnwrapOrRevert,
};
use casperlabs_types::{account::AccountHash, ApiError, U512};
use casper_types::{account::AccountHash, ApiError, Key, U512};

const ARG_TARGET: &str = "target";
const ARG_AMOUNT: &str = "amount";
Expand All @@ -24,15 +24,16 @@ pub fn delegate() {
let amount: U512 = runtime::get_named_arg(ARG_AMOUNT);

// Maybe we will decide to allow multiple funds up until some maximum value.
let already_funded = storage::read_local::<AccountHash, U512>(&account_hash)
.unwrap_or_default()
.is_some();
let already_funded = runtime::get_key(&account_hash.to_formatted_string()).is_some();

if already_funded {
runtime::revert(ApiError::User(CustomError::AlreadyFunded as u16));
} else {
system::transfer_to_account(account_hash, amount).unwrap_or_revert();
system::transfer_to_account(account_hash, amount, None).unwrap_or_revert();
// Transfer successful; Store the fact of funding in the local state.
storage::write_local(account_hash, amount);
runtime::put_key(
&account_hash.to_formatted_string(),
Key::URef(storage::new_uref(())),
)
}
}
2 changes: 1 addition & 1 deletion smart-contract/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2020-03-19
nightly-2020-12-16

0 comments on commit b9935be

Please sign in to comment.