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

feat: Added ability to use SourceScan #134

Merged
merged 42 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d66aaf0
start
Feb 23, 2024
f97e3f7
updated build_command for deployment
Feb 24, 2024
0025435
fixed
Feb 24, 2024
d4ce54d
refactored docker_cmd
Feb 26, 2024
ede1bf9
Merge branch 'main' into source-scan-integration
FroVolod Feb 26, 2024
6a1f839
refactored clone_contract_cmd
Feb 27, 2024
01b2f71
added check for repository
Feb 29, 2024
6759549
moved "check repository" to "deploy"
Mar 1, 2024
2b51349
added remote repo check
Mar 5, 2024
f2b30f9
refactored remote_repo_url()
Mar 6, 2024
3294d93
added "--locked" to cargo_args
Mar 16, 2024
52b2aff
forward interrupt from docker subprocess to `sh -c` (from `<Ctrl-C>`…
dj8yfo Apr 4, 2024
d3301d2
replace `std::fs::rename` with `std::fs::rename`, handle error (#138)
dj8yfo Apr 4, 2024
b8a8bdf
Source scan integration pass uid and gid to container (#140)
Canvinus Apr 6, 2024
7afc79b
Source scan integration configurable image (#142)
dj8yfo Apr 8, 2024
1bd466b
feat(source-scan): logging, refactor, image with same `cargo-near` em…
dj8yfo Apr 13, 2024
2c80183
allign container code path with nep-330 (#147)
Canvinus Apr 13, 2024
02b3db6
chore(source-scan): merge from `main` branch (#150)
dj8yfo Apr 17, 2024
63d89e5
feat(source-scan): implicit `--locked` by default, add `--no-locked` …
dj8yfo Apr 18, 2024
a421525
feat(source-scan): similar behaviour of build destination with `--no-…
dj8yfo Apr 23, 2024
80f45c2
Merge branch 'main' into source-scan-integration
FroVolod Apr 23, 2024
f3a3af1
Merge branch 'main' into source-scan-integration
FroVolod Apr 23, 2024
7c51acd
feat(source-scan): build command in metadata (#157)
dj8yfo Apr 24, 2024
7585514
feat(source-scan): precise docker build wasm input/destination (#158)
dj8yfo Apr 24, 2024
bf8696b
feat(source-scan): git checks, git metadata (#159)
dj8yfo May 10, 2024
55d9a4f
git(source-scan): resolve conflicts with `main` (#162)
dj8yfo May 13, 2024
a54047e
Merge branch 'main'
May 13, 2024
48cef28
Merge pull request #163 from dj8yfo/source-scan-merge-2-1
frol May 14, 2024
21bd5bc
feat(source-scan): docker checks (#166)
dj8yfo Jun 19, 2024
14deda8
Merge remote-tracking branch 'origin/main' into source-scan-integration
frol Jun 27, 2024
b38aa01
test: add docker build integration test (#169)
dj8yfo Jul 5, 2024
85bb289
Merge branch 'main'
Jul 5, 2024
fe23c51
Merge pull request #173 from dj8yfo/merge-main-3
frol Jul 5, 2024
d0b0252
Merge branch 'main'
Jul 9, 2024
a04e05e
feat: allow `--no-locked` for docker build, forbid for docker deploy;…
dj8yfo Jul 11, 2024
ae34c0b
feat(source-scan): handle missing meta, --no-docker suggestion, meta …
dj8yfo Jul 19, 2024
e8928a7
Merge branch 'main'
Jul 22, 2024
5c45224
Merge branch 'main'
Jul 22, 2024
a3632d2
feat(source-scan): coerce `cargo-near` version in nested builds (for …
dj8yfo Jul 22, 2024
797ea83
chore(source-scan): fix docker hello-world, add `--rm` flag to keep c…
dj8yfo Jul 22, 2024
c9730d1
Merge branch 'main' into source-scan-integration
frol Jul 23, 2024
e305331
chore: add linux in wsl docker site link; correct Cargo.lock (#189)
dj8yfo Jul 26, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
run: sudo apt-get update && sudo apt-get install --assume-yes libudev-dev

- name: Run tests
run: cargo test --workspace --verbose
run: cargo test --workspace

lint:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Build files
/target

/integration-tests/docker-build-template/target
# Code Editor related files
.idea
.vscode
Expand Down
61 changes: 56 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 56 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ npm install cargo-near
<summary>Compile and install from source code (Cargo)</summary>

```sh
cargo install cargo-near
cargo install --locked cargo-near
```

or, install the most recent version from git repository:

```sh
$ git clone https://github.com/near/cargo-near
$ cargo install --path cargo-near
$ cargo install --locked --path cargo-near
```
</details>

Expand All @@ -71,32 +71,85 @@ cargo near

Starts interactive mode that will allow to explore all the available commands.

---

```console
cargo near new
```

Initializes a new project skeleton to create a contract from a template.

---

```console
cargo near build
```

Builds a NEAR smart contract along with its [ABI](https://github.com/near/abi) (while in the directory containing contract's Cargo.toml).

You can also make this command embed ABI into your WASM artifact by adding `--embed-abi` parameter. Once deployed, this will allow you to call a view function `__contract_abi` to retrieve a [ZST](https://facebook.github.io/zstd/)-compressed ABI.
By default, this runs a reproducible build in a [Docker](https://docs.docker.com/) container, which:

1. runs against source code version, committed to git, ignoring any uncommitted changes
2. requires that `Cargo.lock` of project is created (e.g. via `cargo update`) and added to git.
- this enables `--locked` build by downstream `cargo` command.
3. will use configuration in [`[package.metadata.near.reproducible_build]`](https://github.com/near/cargo-near/blob/main/cargo-near/src/commands/new/new-project-template/Cargo.toml.template#L14-L25)
section of contract's `Cargo.toml` and [`package.repository`](https://github.com/near/cargo-near/blob/main/cargo-near/src/commands/new/new-project-template/Cargo.toml.template#L9) field
- default values for this section can also be found in `Cargo.toml` of
template project, generated by `cargo near new`

Important flags:

1. `--no-docker`
- flag can be used to perform a regular build with rust toolchain installed onto host, running the `cargo-near` cli.
- *NO*-Docker builds run against actual state of code in filesystem and not against a version, committed to source control.

2. `--no-locked`
- flag is allowed in *NO*-Docker builds, e.g. to generate a `Cargo.lock` *and* simultaneously build the contract.
- flag is allowed in Docker builds, but
- such builds are not reproducible due to potential update of dependencies and compiled `wasm` mismatch as the result.

---

```console
cargo near abi
```

Generates NEAR smart contract's [ABI](https://github.com/near/abi) (while in the directory containing contract's Cargo.toml).

Once contract is deployed, this will allow you to call a view function `__contract_abi` to retrieve a [ZST](https://facebook.github.io/zstd/)-compressed ABI.

---

```console
cargo near create-dev-account
```

Guides you through creation of a new NEAR account on [testnet](https://explorer.testnet.near.org).

---

```console
cargo near deploy
```

Builds the smart contract (equivalent to `cargo near build`) and guides you to deploy it to the blockchain.

By default, this runs a reproducible build in a Docker container.

`deploy` command from Docker build requires that contract's source code:

1. doesn't have any modified tracked files, any staged changes or any untracked content.
2. has been pushed to remote repository, identified by
[`package.repository`](https://github.com/near/cargo-near/blob/main/cargo-near/src/commands/new/new-project-template/Cargo.toml.template#L9).

Important flags:

1. `--no-docker`
- flag can be used to perform a regular *NO*-Docker build *and* deploy.
- Similar to `build` command, in this case none of the git-related concerns and restrictions apply.

2. `--no-locked`
- flag is declined for deploy, due to its effects on `build` result

## Contribution

Expand Down
14 changes: 13 additions & 1 deletion cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ eula = false

[dependencies]
bs58 = "0.5"
hex = "0.4.3"
camino = "1.1.1"
cargo_metadata = "0.18"
cargo_metadata = "0.18.1"
clap = { version = "4.0.18", features = ["derive", "env"] }
colored = "2.0"
env_logger = "0.11"
log = "0.4"
rustc_version = "0.4"
serde = "1.0.197"
serde_json = "1.0"
sha2 = "0.10"
symbolic-debuginfo = "8.8"
Expand All @@ -52,6 +54,16 @@ interactive-clap = "0.2.10"
interactive-clap-derive = "0.2.10"
near-cli-rs = { version = "0.11.0", default-features = false }
dunce = "1"
tempfile = "3.10.1"
git2 = "0.19"
home = "0.5.9"
pathdiff = { version = "0.2.1", features = ["camino"]}
unix_path = "1.0.1"
url = { version = "2.5.0", features = ["serde"]}
tmp_env = "0.1.1"

[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.29.0", features = ["user", "process"] }

[features]
default = ["ledger"]
Expand Down
61 changes: 61 additions & 0 deletions cargo-near/src/build_extended.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::{BuildArtifact, BuildOpts};

mod build_script;
pub use build_script::BuildScriptOpts;
use rustc_version::Version;

#[derive(Debug, Clone)]
pub struct OptsExtended<'a> {
pub workdir: &'a str,
/// vector of key-value pairs of temporary env overrides during build process
pub env: Vec<(&'a str, &'a str)>,
pub build_opts: BuildOpts,
pub build_script_opts: BuildScriptOpts<'a>,
}

pub fn build(args: OptsExtended) -> Result<BuildArtifact, Box<dyn std::error::Error>> {
let actual_version = rustc_version::version()?;
let (artifact, skipped) = args.skip_or_compile(&actual_version)?;

args.build_script_opts
.post_build(skipped, &artifact, args.workdir, &actual_version)?;
Ok(artifact)
}

impl<'a> OptsExtended<'a> {
pub fn skip_or_compile(
&self,
version: &Version,
) -> Result<(BuildArtifact, bool), Box<dyn std::error::Error>> {
let _tmp_workdir = tmp_env::set_current_dir(self.workdir)?;
let result = if self.build_script_opts.should_skip(version) {
let artifact = self.build_script_opts.create_empty_stub()?;
(artifact, true)
} else {
let artifact = self.compile_near_artifact()?;
(artifact, false)
};
Ok(result)
}

/// `CARGO_TARGET_DIR` export is needed to avoid attempt to acquire same `target/<profile-path>/.cargo-lock`
/// as the `cargo` process, which is running the build-script
pub fn compile_near_artifact(&self) -> Result<BuildArtifact, Box<dyn std::error::Error>> {
let mut tmp_envs = vec![];
for (env_key, value) in self.env.iter() {
let tmp_env = tmp_env::set_var(env_key, value);
tmp_envs.push(tmp_env);
}

let artifact = if let Some(distinct_target_dir) = self.build_script_opts.distinct_target_dir
{
let _tmp_cargo_target_env = tmp_env::set_var("CARGO_TARGET_DIR", distinct_target_dir);

crate::build(self.build_opts.clone())?
} else {
crate::build(self.build_opts.clone())?
};

Ok(artifact)
}
}
Loading
Loading