-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Manuel-yang/main
[Add ]使用anchor build 依賴衝突 文章
- Loading branch information
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
slug: error-after-running-anchor-build | ||
title: 使用anchor build 依賴衝突 | ||
authors: [YanAemons] | ||
tags: [blog, blockchain, solana, anchor] | ||
--- | ||
|
||
# 情景 | ||
|
||
## 報錯日志 | ||
|
||
在使用solana-cli時候,鑑於一些依賴版本限制,會用到cli14.xx(主網版本),而不是16.xx(測試網版本) | ||
|
||
例如,在使用solana-cli版本爲`1.14.17`, anchor版本爲`0.26.0`的環境中, `anchor init`創建一個新項目後運行 `anchor build`會發生以下錯誤: | ||
|
||
```shell | ||
error: package constant_time_eq v0.3.0 cannot be built because it requires rustc 1.66.0 or newer, while the currently active rustc version is 1.62.0-dev | ||
``` | ||
## 報錯原因 | ||
使用的solana-cli版本在14.xxx, cli內自帶的rustc版本過老,無法編譯較新的依賴 | ||
## 解決方案 | ||
### 1. 升級solana-cli至最新版本 | ||
```shell | ||
solana-install update | ||
``` | ||
### 2.指定依賴包版本 | ||
需要在Cargo.toml文件下指定以下依賴版本 | ||
```rust | ||
getrandom = { version = "0.2.9", features = ["custom"] } | ||
solana-program = "=1.14.17" | ||
winnow="=0.4.1" | ||
toml_datetime="=0.6.1" | ||
blake3 = "=1.3.1" | ||
``` | ||
運行`cargo clean`後重新運行`anchor build`即可解決 | ||
## | ||
## 監聽程序log監聽到兩次 | ||
在使用`program.addEventListener()`有可能聽到兩次相同的事件,其中一次的txSign會是“1111111111111111111111111111111111111111111111111111111111111111”, 這是因爲監聽到了模擬時的交易哈系,我們只需要在監聽到該交易哈系時拋棄即可 | ||
```ts | ||
program.addEventListener("eventName", (event, slot, signature) => { | ||
if (signature === '1111111111111111111111111111111111111111111111111111111111111111') return | ||
// do ur stuff | ||
}) | ||
``` | ||
然而,有時websocket訂閱也會多次返回實際簽名。如果是這種情況,您可以使用一些緩存解決方案。例如,創建一個具有一定長度限制的集合,在此處添加簽名並檢查該集合中是否存在新簽名: | ||
```ts | ||
const handledSignatures = new Set<string>() | ||
const maxHandledSignaturesLen = 100 | ||
program.addEventListener("eventName", (event, slot, signature) => { | ||
if (signature === '1111111111111111111111111111111111111111111111111111111111111111') return | ||
if (handledSignatures.has(signature)) return | ||
// do ur stuff | ||
handledSignatures.add(signature) | ||
if (handledSignatures.size > maxHandledSignaturesLen) { | ||
handledSignatures.delete(handledSignatures.values().next().value) | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters