Skip to content

Commit

Permalink
Merge branch 'master' into signer
Browse files Browse the repository at this point in the history
  • Loading branch information
dnut committed Dec 17, 2021
2 parents f2a3494 + 561f7cd commit 86acda9
Show file tree
Hide file tree
Showing 74 changed files with 1,054 additions and 810 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/tests.yaml
Expand Up @@ -35,10 +35,16 @@ jobs:
- run: cargo build
- run: cargo fmt -- --check
- run: cargo clippy --all-targets -- -D warnings
- run: rustup toolchain install nightly --profile minimal --component clippy
- run: cargo +nightly clippy --all-targets -- -D warnings
- run: cargo test
- run: cd ts && yarn
- run: cd ts && yarn test
- run: cd ts && yarn lint
- run: cd examples/tutorial && yarn
- run: cd examples/tutorial && yarn lint
- run: cd tests && yarn
- run: cd tests && yarn lint

setup-anchor-cli:
name: Setup Anchor cli
Expand Down Expand Up @@ -258,7 +264,7 @@ jobs:
path: tests/cashiers-check
- cmd: cd tests/typescript && anchor test
path: tests/typescript
- cmd: cd tests/zero-copy && anchor test
- cmd: cd tests/zero-copy && anchor test && cd programs/zero-copy && cargo test-bpf
path: tests/zero-copy
- cmd: cd tests/chat && anchor test
path: tests/chat
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,10 @@ incremented for features.

## [Unreleased]

### Fixes

* ts: fix `translateAddress` which currently leads to failing browser code. Now uses `PublicKey` constructor instead of prototype chain constructor name checking which doesn't work in the presence of code minifying/mangling([1138](https://github.com/project-serum/anchor/pull/1138))

### Features

* lang: Add `programdata_address: Option<Pubkey>` field to `Program` account. Will be populated if account is a program owned by the upgradable bpf loader ([#1125](https://github.com/project-serum/anchor/pull/1125))
Expand All @@ -19,6 +23,7 @@ incremented for features.
### Breaking

* client: Client::new and Client::new_with_options now accept `Rc<dyn Signer>` instead of `Keypair` ([#975](https://github.com/project-serum/anchor/pull/975)).
* lang, ts: Change error enum name and message for 'wrong program ownership' account validation ([#1154](https://github.com/project-serum/anchor/pull/1154)).

## [0.19.0] - 2021-12-08

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/src/lib.rs
Expand Up @@ -1777,7 +1777,7 @@ fn validator_flags(cfg: &WithPath<Config>) -> Result<Vec<String>> {
if key == "ledger" {
continue;
};
flags.push(format!("--{}", key.replace("_", "-")));
flags.push(format!("--{}", key.replace('_', "-")));
if let serde_json::Value::String(v) = value {
flags.push(v.to_string());
} else {
Expand Down
2 changes: 2 additions & 0 deletions client/src/lib.rs
Expand Up @@ -74,13 +74,15 @@ impl Client {
}

// Internal configuration for a client.
#[derive(Debug)]
struct Config {
cluster: Cluster,
payer: Rc<dyn Signer>,
options: Option<CommitmentConfig>,
}

/// Program is the primary client handle to be used to build and send requests.
#[derive(Debug)]
pub struct Program {
program_id: Pubkey,
cfg: Config,
Expand Down
12 changes: 7 additions & 5 deletions examples/tutorial/basic-0/client.js
Expand Up @@ -2,18 +2,20 @@
// It is not expected users directly test with this example. For a more
// ergonomic example, see `tests/basic-0.js` in this workspace.

const anchor = require('@project-serum/anchor');
const anchor = require("@project-serum/anchor");

// Configure the local cluster.
anchor.setProvider(anchor.Provider.local());

async function main() {
// #region main
// Read the generated IDL.
const idl = JSON.parse(require('fs').readFileSync('./target/idl/basic_0.json', 'utf8'));
const idl = JSON.parse(
require("fs").readFileSync("./target/idl/basic_0.json", "utf8")
);

// Address of the deployed program.
const programId = new anchor.web3.PublicKey('<YOUR-PROGRAM-ID>');
const programId = new anchor.web3.PublicKey("<YOUR-PROGRAM-ID>");

// Generate the program client from IDL.
const program = new anchor.Program(idl, programId);
Expand All @@ -23,5 +25,5 @@ async function main() {
// #endregion main
}

console.log('Running client.');
main().then(() => console.log('Success'));
console.log("Running client.");
main().then(() => console.log("Success"));
42 changes: 22 additions & 20 deletions examples/tutorial/basic-2/tests/basic-2.js
@@ -1,46 +1,48 @@
const assert = require('assert');
const anchor = require('@project-serum/anchor');
const assert = require("assert");
const anchor = require("@project-serum/anchor");
const { SystemProgram } = anchor.web3;

describe('basic-2', () => {
const provider = anchor.Provider.local()
describe("basic-2", () => {
const provider = anchor.Provider.local();

// Configure the client to use the local cluster.
anchor.setProvider(provider)
anchor.setProvider(provider);

// Counter for the tests.
const counter = anchor.web3.Keypair.generate()
const counter = anchor.web3.Keypair.generate();

// Program for the tests.
const program = anchor.workspace.Basic2
const program = anchor.workspace.Basic2;

it('Creates a counter', async () => {
it("Creates a counter", async () => {
await program.rpc.create(provider.wallet.publicKey, {
accounts: {
counter: counter.publicKey,
user: provider.wallet.publicKey,
systemProgram: SystemProgram.programId,
},
signers: [counter],
})
});

let counterAccount = await program.account.counter.fetch(counter.publicKey)
let counterAccount = await program.account.counter.fetch(counter.publicKey);

assert.ok(counterAccount.authority.equals(provider.wallet.publicKey))
assert.ok(counterAccount.count.toNumber() === 0)
})
assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
assert.ok(counterAccount.count.toNumber() === 0);
});

it('Updates a counter', async () => {
it("Updates a counter", async () => {
await program.rpc.increment({
accounts: {
counter: counter.publicKey,
authority: provider.wallet.publicKey,
},
})
});

const counterAccount = await program.account.counter.fetch(counter.publicKey)
const counterAccount = await program.account.counter.fetch(
counter.publicKey
);

assert.ok(counterAccount.authority.equals(provider.wallet.publicKey))
assert.ok(counterAccount.count.toNumber() == 1)
})
})
assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
assert.ok(counterAccount.count.toNumber() == 1);
});
});
8 changes: 4 additions & 4 deletions examples/tutorial/basic-3/tests/basic-3.js
Expand Up @@ -25,10 +25,10 @@ describe("basic-3", () => {

// Invoke the puppet master to perform a CPI to the puppet.
await puppetMaster.rpc.pullStrings(new anchor.BN(111), {
accounts: {
puppet: newPuppetAccount.publicKey,
puppetProgram: puppet.programId,
},
accounts: {
puppet: newPuppetAccount.publicKey,
puppetProgram: puppet.programId,
},
});

// Check the state updated.
Expand Down
7 changes: 6 additions & 1 deletion examples/tutorial/package.json
@@ -1,6 +1,10 @@
{
"name": "anchor-examples",
"private": true,
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
},
"workspaces": [
"basic-0",
"basic-1",
Expand All @@ -12,6 +16,7 @@
"@project-serum/anchor": "^0.19.0"
},
"devDependencies": {
"mocha": "^9.1.3"
"mocha": "^9.1.3",
"prettier": "^2.5.1"
}
}
17 changes: 11 additions & 6 deletions examples/tutorial/yarn.lock
Expand Up @@ -30,17 +30,17 @@
"@ethersproject/logger" "^5.5.0"
hash.js "1.1.7"

"@project-serum/anchor@^0.18.0":
version "0.18.0"
resolved "https://registry.yarnpkg.com/@project-serum/anchor/-/anchor-0.18.0.tgz#867144282e59482230f797f73ee9f5634f846061"
integrity sha512-WTm+UB93MoxyCbjnHIibv/uUEoO/5gL4GEtE/aMioLF8Z4i0vCMPnvAN0xpk9VBu3t7ld2DcCE/L+6Z7dwU++w==
"@project-serum/anchor@^0.19.0":
version "0.19.0"
resolved "https://registry.yarnpkg.com/@project-serum/anchor/-/anchor-0.19.0.tgz#79f1fbe7c3134860ccbfe458a0e09daf79644885"
integrity sha512-cs0LBmJOrL9eJ8MRNqitnzbpCT5QEzVdJmiIjfNV5YaGn1K9vISR7DtISj3Bdl3KBdLqii4CTw1mpHdi8iXUCg==
dependencies:
"@project-serum/borsh" "^0.2.2"
"@solana/web3.js" "^1.17.0"
base64-js "^1.5.1"
bn.js "^5.1.2"
bs58 "^4.0.1"
buffer-layout "^1.2.0"
buffer-layout "^1.2.2"
camelcase "^5.3.1"
crypto-hash "^1.3.0"
eventemitter3 "^4.0.7"
Expand Down Expand Up @@ -257,7 +257,7 @@ bs58@^4.0.0, bs58@^4.0.1:
dependencies:
base-x "^3.0.2"

buffer-layout@^1.2.0:
buffer-layout@^1.2.0, buffer-layout@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/buffer-layout/-/buffer-layout-1.2.2.tgz#b9814e7c7235783085f9ca4966a0cfff112259d5"
integrity sha512-kWSuLN694+KTk8SrYvCqwP2WcgQjoRCiF5b4QDvkkz8EmgD+aWAIceGFKMIAdmF/pH+vpgNV3d3kAKorcdAmWA==
Expand Down Expand Up @@ -812,6 +812,11 @@ picomatch@^2.0.4, picomatch@^2.2.1:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
integrity sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==

prettier@^2.5.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a"
integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==

randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
Expand Down
1 change: 1 addition & 0 deletions lang/Cargo.toml
Expand Up @@ -33,6 +33,7 @@ anchor-attribute-state = { path = "./attribute/state", version = "0.19.0" }
anchor-attribute-interface = { path = "./attribute/interface", version = "0.19.0" }
anchor-attribute-event = { path = "./attribute/event", version = "0.19.0" }
anchor-derive-accounts = { path = "./derive/accounts", version = "0.19.0" }
arrayref = "0.3.6"
base64 = "0.13.0"
borsh = "0.9"
bytemuck = "1.4.0"
Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/account/src/lib.rs
Expand Up @@ -69,7 +69,7 @@ pub fn account(
for arg in args {
let ns = arg
.to_string()
.replace("\"", "")
.replace('\"', "")
.chars()
.filter(|c| !c.is_whitespace())
.collect();
Expand Down
4 changes: 2 additions & 2 deletions lang/src/account.rs → lang/src/accounts/account.rs
Expand Up @@ -38,7 +38,7 @@ impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> Account<'a, T
return Err(ErrorCode::AccountNotInitialized.into());
}
if info.owner != &T::owner() {
return Err(ErrorCode::AccountNotProgramOwned.into());
return Err(ErrorCode::AccountOwnedByWrongProgram.into());
}
let mut data: &[u8] = &info.try_borrow_data()?;
Ok(Account::new(info.clone(), T::try_deserialize(&mut data)?))
Expand All @@ -53,7 +53,7 @@ impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> Account<'a, T
return Err(ErrorCode::AccountNotInitialized.into());
}
if info.owner != &T::owner() {
return Err(ErrorCode::AccountNotProgramOwned.into());
return Err(ErrorCode::AccountOwnedByWrongProgram.into());
}
let mut data: &[u8] = &info.try_borrow_data()?;
Ok(Account::new(
Expand Down
File renamed without changes.
File renamed without changes.
@@ -1,5 +1,5 @@
use crate::error::ErrorCode;
use crate::*;
use crate::{error::ErrorCode, prelude::Account};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::instruction::AccountMeta;
Expand Down
4 changes: 2 additions & 2 deletions lang/src/cpi_state.rs → lang/src/accounts/cpi_state.rs
@@ -1,10 +1,10 @@
use crate::error::ErrorCode;
#[allow(deprecated)]
use crate::{accounts::state::ProgramState, CpiStateContext};
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Key, ToAccountInfo,
ToAccountInfos, ToAccountMetas,
};
#[allow(deprecated)]
use crate::{CpiStateContext, ProgramState};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::instruction::AccountMeta;
Expand Down
20 changes: 9 additions & 11 deletions lang/src/loader.rs → lang/src/accounts/loader.rs
Expand Up @@ -3,6 +3,7 @@ use crate::{
Accounts, AccountsClose, AccountsExit, Key, ToAccountInfo, ToAccountInfos, ToAccountMetas,
ZeroCopy,
};
use arrayref::array_ref;
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::instruction::AccountMeta;
Expand Down Expand Up @@ -58,13 +59,12 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
acc_info: &AccountInfo<'info>,
) -> Result<Loader<'info, T>, ProgramError> {
if acc_info.owner != program_id {
return Err(ErrorCode::AccountNotProgramOwned.into());
return Err(ErrorCode::AccountOwnedByWrongProgram.into());
}
let data: &[u8] = &acc_info.try_borrow_data()?;
// Discriminator must match.
let mut disc_bytes = [0u8; 8];
disc_bytes.copy_from_slice(&data[..8]);
if disc_bytes != T::discriminator() {
let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
}

Expand All @@ -79,7 +79,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
acc_info: &AccountInfo<'info>,
) -> Result<Loader<'info, T>, ProgramError> {
if acc_info.owner != program_id {
return Err(ErrorCode::AccountNotProgramOwned.into());
return Err(ErrorCode::AccountOwnedByWrongProgram.into());
}
Ok(Loader::new(acc_info.clone()))
}
Expand All @@ -89,9 +89,8 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
pub fn load(&self) -> Result<Ref<T>, ProgramError> {
let data = self.acc_info.try_borrow_data()?;

let mut disc_bytes = [0u8; 8];
disc_bytes.copy_from_slice(&data[..8]);
if disc_bytes != T::discriminator() {
let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
}

Expand All @@ -109,9 +108,8 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {

let data = self.acc_info.try_borrow_mut_data()?;

let mut disc_bytes = [0u8; 8];
disc_bytes.copy_from_slice(&data[..8]);
if disc_bytes != T::discriminator() {
let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
}

Expand Down

0 comments on commit 86acda9

Please sign in to comment.