From 49e2f11f51e810d9710741120d19f8f53e274501 Mon Sep 17 00:00:00 2001 From: Maciej Zielinski Date: Tue, 9 Feb 2021 08:41:14 +0100 Subject: [PATCH] Deploy's body hash fix. --- packages/sdk/CHANGELOG.md | 13 ++++++++++++- packages/sdk/package.json | 2 +- packages/sdk/src/lib/DeployUtil.ts | 8 ++++---- packages/sdk/src/lib/StoredValue.ts | 21 ++++++++++++++++++--- packages/sdk/test/lib/StoredValue.test.ts | 17 ++++++++++++++--- packages/ui/src/@types/models.d.ts | 2 +- packages/ui/src/components/App.test.tsx | 10 ---------- packages/ui/src/components/BlockDetails.tsx | 5 +++-- packages/ui/src/components/Utils.tsx | 9 ++++----- packages/ui/src/containers/AuthContainer.ts | 2 +- smart-contract/faucet-stored/Cargo.toml | 8 ++++---- smart-contract/faucet-stored/src/main.rs | 6 +++--- smart-contract/faucet/Cargo.toml | 6 +++--- smart-contract/faucet/src/lib.rs | 15 ++++++++------- smart-contract/rust-toolchain | 2 +- 15 files changed, 77 insertions(+), 49 deletions(-) delete mode 100644 packages/ui/src/components/App.test.tsx diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md index b2160eae..26d98056 100644 --- a/packages/sdk/CHANGELOG.md +++ b/packages/sdk/CHANGELOG.md @@ -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 @@ -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 diff --git a/packages/sdk/package.json b/packages/sdk/package.json index d25e5ea5..ef34dca6 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -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", diff --git a/packages/sdk/src/lib/DeployUtil.ts b/packages/sdk/src/lib/DeployUtil.ts index 56011db5..d7834e22 100644 --- a/packages/sdk/src/lib/DeployUtil.ts +++ b/packages/sdk/src/lib/DeployUtil.ts @@ -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()) ]); } } @@ -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()) ]); } } @@ -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()) ]); } } @@ -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()) ]); } } diff --git a/packages/sdk/src/lib/StoredValue.ts b/packages/sdk/src/lib/StoredValue.ts index fcac11d2..1cce0b00 100644 --- a/packages/sdk/src/lib/StoredValue.ts +++ b/packages/sdk/src/lib/StoredValue.ts @@ -145,7 +145,7 @@ 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 { @@ -153,6 +153,21 @@ export class EraInfoJson { 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 @@ -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 }) diff --git a/packages/sdk/test/lib/StoredValue.test.ts b/packages/sdk/test/lib/StoredValue.test.ts index d3b4f453..db6b7cba 100644 --- a/packages/sdk/test/lib/StoredValue.test.ts +++ b/packages/sdk/test/lib/StoredValue.test.ts @@ -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 () { diff --git a/packages/ui/src/@types/models.d.ts b/packages/ui/src/@types/models.d.ts index 4bb1dd8a..98a5e2b6 100644 --- a/packages/ui/src/@types/models.d.ts +++ b/packages/ui/src/@types/models.d.ts @@ -38,5 +38,5 @@ interface AccountBalance { checkedAt: Date; blockHashBase16: string; // undefine means the account didn't exist. - balance: number | undefined; + balance: BigNumber | undefined; } diff --git a/packages/ui/src/components/App.test.tsx b/packages/ui/src/components/App.test.tsx deleted file mode 100644 index 1d117335..00000000 --- a/packages/ui/src/components/App.test.tsx +++ /dev/null @@ -1,10 +0,0 @@ -import React from 'react'; -import ReactDOM from 'react-dom'; -import App from './App'; -import CasperContainer from '../containers/CasperContainer'; - -it('renders without crashing', () => { - const div = document.createElement('div'); - ReactDOM.render(, div); - ReactDOM.unmountComponentAtNode(div); -}); diff --git a/packages/ui/src/components/BlockDetails.tsx b/packages/ui/src/components/BlockDetails.tsx index f7dcf435..605009a5 100644 --- a/packages/ui/src/components/BlockDetails.tsx +++ b/packages/ui/src/components/BlockDetails.tsx @@ -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 @@ -122,7 +123,7 @@ const DeploysTable = observer( {shortHash(accountId)} - + {deploy.state} @@ -165,7 +166,7 @@ export const Balance = observer( (props: { balance: ObservableValue }) => { const value = props.balance.value; if (value == null) return null; - return ; + return ; } ); diff --git a/packages/ui/src/components/Utils.tsx b/packages/ui/src/components/Utils.tsx index 70c0beae..f0294524 100755 --- a/packages/ui/src/components/Utils.tsx +++ b/packages/ui/src/components/Utils.tsx @@ -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) => (
@@ -232,9 +233,7 @@ export const Title = (props: { title: string }) => ( ); -export const CSPR = (props: { motes: number }) => { - const s = (props.motes / 1000_000_000).toLocaleString(undefined, { - maximumFractionDigits: 5 - }); - return {s} CSPR; +export const CSPR = (props: { motes: BigNumber }) => { + let val = props.motes.div(BigNumber.from(1000_000_000)); + return {val.toString()} CSPR; }; diff --git a/packages/ui/src/containers/AuthContainer.ts b/packages/ui/src/containers/AuthContainer.ts index 9616aa33..3e38152b 100644 --- a/packages/ui/src/containers/AuthContainer.ts +++ b/packages/ui/src/containers/AuthContainer.ts @@ -157,7 +157,7 @@ export class AuthContainer { this.balances.set(accountHash, { checkedAt: now, blockHashBase16: latestBlockHash, - balance: latestAccountBalance?.toNumber() + balance: latestAccountBalance }); } } diff --git a/smart-contract/faucet-stored/Cargo.toml b/smart-contract/faucet-stored/Cargo.toml index 00c15d76..d8b93150 100644 --- a/smart-contract/faucet-stored/Cargo.toml +++ b/smart-contract/faucet-stored/Cargo.toml @@ -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" } diff --git a/smart-contract/faucet-stored/src/main.rs b/smart-contract/faucet-stored/src/main.rs index 4c00e460..028e9751 100644 --- a/smart-contract/faucet-stored/src/main.rs +++ b/smart-contract/faucet-stored/src/main.rs @@ -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, }; diff --git a/smart-contract/faucet/Cargo.toml b/smart-contract/faucet/Cargo.toml index 67378b7f..0a0320a1 100644 --- a/smart-contract/faucet/Cargo.toml +++ b/smart-contract/faucet/Cargo.toml @@ -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" \ No newline at end of file diff --git a/smart-contract/faucet/src/lib.rs b/smart-contract/faucet/src/lib.rs index 1f4c3742..5b3a3526 100644 --- a/smart-contract/faucet/src/lib.rs +++ b/smart-contract/faucet/src/lib.rs @@ -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"; @@ -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::(&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(())), + ) } } diff --git a/smart-contract/rust-toolchain b/smart-contract/rust-toolchain index 10a652fd..714051aa 100644 --- a/smart-contract/rust-toolchain +++ b/smart-contract/rust-toolchain @@ -1 +1 @@ -nightly-2020-03-19 +nightly-2020-12-16