diff --git a/blog/2023-08-28.md b/blog/2023-08-28.md new file mode 100644 index 000000000..57562584c --- /dev/null +++ b/blog/2023-08-28.md @@ -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() +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) + } +}) +``` diff --git a/blog/authors.yml b/blog/authors.yml index c4da709cc..1ba7236db 100644 --- a/blog/authors.yml +++ b/blog/authors.yml @@ -2,4 +2,10 @@ davirain: name: Davirain title: Davirain Blog url: https://github.com/DaviRain-Su - image_url: https://github.com/DaviRain-Su.png \ No newline at end of file + image_url: https://github.com/DaviRain-Su.png + +YanAemons: + name: YanAemons + title: YanAemons's Blog + url: https://github.com/Manuel-yang + image_url: https://avatars.githubusercontent.com/u/66017597?s=400&u=48df8af939edc4b9610ad74638df907ea37acac4&v=4 \ No newline at end of file