Skill站外收录 @skilllib/skill-2014279386613100545
bch-development
定义 Bitcoin Cash 的专用实现规范,通过结构体嵌入 Bitcoin 基础实现并覆盖特定方法来确保逻辑隔离,所有 BCH 差异必须在独立目录中实现,严禁污染 BTC 原始代码,保障双链维护安全与可追溯性。
区块链 AI 技能
Skills.MD
# BCH (Bitcoin Cash) Development Rules
## 🚨 CRITICAL: Override Pattern for BCH-Specific Logic
**This is a non-negotiable architectural rule.** BCH implementation uses struct embedding with method override pattern.
### Architecture Overview
```go
// internal/infrastructure/api/btc/bch/bitcoin_cash.go
type BitcoinCash struct {
apibtcimpl.Bitcoin // Embeds BTC implementation
}
```
**Key Principle:** `BitcoinCash` embeds `Bitcoin`, inheriting all BTC methods by default.
## Rules
### ✅ DO: Override on BCH Side
When BCH requires different logic from BTC:
1. **Create a new file** in `internal/infrastructure/api/btc/bch/`
2. **Implement the method** with the same name on `BitcoinCash`
3. **This "overrides"** the embedded `Bitcoin` method
### ❌ DON'T: Modify BTC Side for BCH
- **NEVER** modify `internal/infrastructure/api/btc/btc/` for BCH-specific requirements
- **NEVER** add BCH conditionals in BTC code
- **NEVER** add BCH-specific types or logic to the BTC package
## Why This Pattern?
| Reason | Explanation |
|--------|-------------|
| **Separation of Concerns** | BTC code remains pure and focused |
| **Maintainability** | BCH changes don't affect BTC |
| **Clarity** | BCH differences are explicit in BCH directory |
| **Safety** | BTC modifications can't accidentally break BCH |
## Implementation Examples
### Example 1: GetAddressInfo Override
BCH has a different response structure for `getaddressinfo` RPC:
```go
// internal/infrastructure/api/btc/bch/address.go
// BCH-specific response type
type GetAddressInfoResult struct {
Address string `json:"address"`
ScriptPubKey string `json:"scriptPubKey"`
Label string `json:"label,omitempty"` // BCH uses Label (singular)
Labels []struct { // BCH has different Labels structure
Name string `json:"name"`
Purpose string `json:"purpose"`
} `json:"labels"`
// ... other BCH-specific fields
}
// Override GetAddressInfo for BCH
func (b *BitcoinCash) GetAddressInfo(addr string) (*dtobtc.AddressInfo, error) {
// BCH-specific implementation
// ...
}
```
### Example 2: GetAccount Override
BCH requires different logic for getting account info:
```go
// internal/infrastructure/api/btc/bch/account.go
func (b *BitcoinCash) GetAccount(addr string) (string, error) {
// BCH calls GetAddressInfo (which is also overridden)
res, err := b.GetAddressInfo(addr)
if err != nil {
return "", fmt.Errorf("fail to call btc.GetAddressInfo() in bch: %w", err)
}
// BCH-specific label extraction
if len(res.Labels) == 0 {
return "", nil
}
return res.Labels[0], nil
}
```
### Example 3: Chain Parameters Override
BCH has different network magic numbers:
```go
// internal/infrastructure/api/btc/bch/bitcoin_cash.go
const (
MainnetMagic wire.BitcoinNet = 0xe8f3e1e3 // BCH-specific
TestnetMagic wire.BitcoinNet = 0xf4f3e5f4 // BCH-specific
Regtestmagic wire.BitcoinNet = 0xfabfb5da // BCH-specific
)
func (b *BitcoinCash) initChainParams() {
// Override chain parameters for BCH
}
```
## BCH vs BTC: Feature Differences
| Feature | BTC | BCH |
|---------|-----|-----|
| SegWit | ✅ Supported | ❌ Not supported |
| Taproot | ✅ Supported | ❌ Not supported |
| Address Format | Legacy, SegWit, Taproot | Legacy, CashAddr |
| Network Magic | BTC values | BCH-specific values |
## Directory Structure
```
internal/infrastructure/api/btc/
├── btc/ # BTC implementation (DO NOT modify for BCH)
│ ├── bitcoin.go # Bitcoin struct and methods
│ ├── account.go
│ ├── address.go
│ └── ...
├── bch/ # BCH overrides (ADD new files here)
│ ├── bitcoin_cash.go # BitcoinCash struct (embeds Bitcoin)
│ ├── account.go # Override: GetAccount
│ ├── address.go # Override: GetAddressInfo
│ └── ...
└── connection.go # Shared connection logic
```
## Checklist for BCH Changes
When implementing BCH-specific logic:
- [ ] File created in `internal/infrastructure/api/btc/bch/` (NOT in `btc/`)
- [ ] Method has same signature as the BTC method being overridden
- [ ] No changes made to `internal/infrastructure/api/btc/btc/`
- [ ] BCH-specific types defined in BCH package (if needed)
- [ ] Error messages include "bch" for traceability
## Common Mistakes
### ❌ WRONG: Adding BCH Logic to BTC
```go
// internal/infrastructure/api/btc/btc/account.go
func (b *Bitcoin) GetAccount(addr string) (string, error) {
if b.coinTypeCode == domainCoin.BCH { // DON'T DO THIS!
// BCH-specific logic
}
// BTC logic
}
```
### ✅ CORRECT: Override in BCH Package
```go
// internal/infrastructure/api/btc/bch/account.go
func (b *BitcoinCash) GetAccount(addr string) (string, error) {
// BCH-specific logic here
}
```
## Technical Note: How Go Embedding Works
When `BitcoinCash` embeds `Bitcoin`:
1. All `Bitcoin` methods are "promoted" to `BitcoinCash`
2. If `BitcoinCash` defines a method with the same name, it takes precedence
3. The embedded `Bitcoin` methods can still be called via `b.Bitcoin.MethodName()`
```go
// Call overridden method (uses BitcoinCash.GetAddressInfo)
info, _ := bch.GetAddressInfo(addr)
// Call embedded method directly (uses Bitcoin.GetAddressInfo)
info, _ := bch.Bitcoin.GetAddressInfo(addr)
```
## Related Files
| File | Purpose |
|------|---------|
| `internal/infrastructure/api/btc/bch/bitcoin_cash.go` | BitcoinCash struct definition |
| `internal/infrastructure/api/btc/btc/bitcoin.go` | Bitcoin struct (embedded by BCH) |
| `internal/application/ports/btc/interface.go` | Bitcoiner interface |
## Related Documentation
- [BCH Technical Docs](../../../docs/crypto/bch/)
- [BTC Technical Docs](../../../docs/crypto/btc/)
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「bch-development」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次