NAC_Blockchain/charter-std/sovereignty/rules.ch

593 lines
16 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

///! # 主权规则系统
///!
///! Sovereignty Rules System
///! 定义7种主权类别及其规则
///!
///! **版本**: v1.0
///! **模块**: charter-std/sovereignty/rules.ch
use asset::gnacs::GNACSCode;
// ============================================================================
// 主权类别枚举
// ============================================================================
/// 主权类别
///
/// NAC定义的7种主权类别
pub enum SovereigntyType {
A0, // 绝对所有权 (Absolute Ownership)
B1, // 使用权 (Usage Rights)
C2, // 收益权 (Revenue Rights)
D0, // 担保主权 (Collateral Sovereignty)
E3, // 知识产权 (Intellectual Property)
F4, // 临时监管权 (Temporary Custody)
G5 // 共有权 (Co-ownership)
}
// ============================================================================
// 主权规则结构
// ============================================================================
/// 主权规则
///
/// 定义每种主权类别的规则和约束
struct SovereigntyRule {
sovereignty_type: SovereigntyType,
name: String,
description: String,
required_fields: Vec<String>,
constraints: Vec<RuleConstraint>,
on_transfer: TransferRule,
on_revenue: RevenueRule,
on_expire: ExpireRule
}
/// 规则约束
struct RuleConstraint {
field_name: String,
constraint_type: ConstraintType,
value: String
}
/// 约束类型
enum ConstraintType {
Required, // 必须字段
MinValue, // 最小值
MaxValue, // 最大值
Pattern, // 模式匹配
Custom // 自定义约束
}
/// 转账规则
enum TransferRule {
Unrestricted, // 无限制
RequireApproval, // 需要批准
RequireMultiSig, // 需要多签
Prohibited // 禁止转账
}
/// 收益规则
enum RevenueRule {
FullToOwner, // 全部归所有者
DistributeByShares, // 按份额分配
ToSpecificBeneficiary, // 归特定受益人
Custom // 自定义分配
}
/// 到期规则
enum ExpireRule {
NoExpiry, // 无到期
AutoRevoke, // 自动撤销
AutoRenew, // 自动续期
RequireRenewal // 需要续期
}
// ============================================================================
// A0: 绝对所有权 (Absolute Ownership)
// ============================================================================
/// A0: 绝对所有权规则
///
/// 特点:
/// - 完全控制权
/// - 无期限限制
/// - 可自由转让
/// - 收益完全归所有者
pub const RULE_A0: SovereigntyRule = SovereigntyRule {
sovereignty_type: SovereigntyType::A0,
name: "绝对所有权",
description: "对资产拥有完全的控制权和处置权",
required_fields: vec!["owner", "asset_id", "gnacs_code"],
constraints: vec![
RuleConstraint {
field_name: "owner",
constraint_type: ConstraintType::Required,
value: "Address"
}
],
on_transfer: TransferRule::Unrestricted,
on_revenue: RevenueRule::FullToOwner,
on_expire: ExpireRule::NoExpiry
};
/// 验证A0规则
pub fn validate_a0(
owner: Address,
asset_id: Hash,
gnacs_code: GNACSCode
) -> bool {
// 检查所有者地址有效性
if !owner.is_valid() {
return false;
}
// 检查资产ID有效性
if asset_id.is_zero() {
return false;
}
// 检查GNACS编码有效性
if !gnacs_code.validate() {
return false;
}
return true;
}
// ============================================================================
// B1: 使用权 (Usage Rights)
// ============================================================================
/// B1: 使用权规则
///
/// 特点:
/// - 有期限使用
/// - 不可转让(除非授权)
/// - 到期自动撤销
/// - 无收益权
pub const RULE_B1: SovereigntyRule = SovereigntyRule {
sovereignty_type: SovereigntyType::B1,
name: "使用权",
description: "在特定期限内使用资产的权利",
required_fields: vec!["user", "asset_id", "start_time", "end_time", "usage_scope"],
constraints: vec![
RuleConstraint {
field_name: "end_time",
constraint_type: ConstraintType::Required,
value: "Timestamp"
},
RuleConstraint {
field_name: "usage_scope",
constraint_type: ConstraintType::Required,
value: "String"
}
],
on_transfer: TransferRule::RequireApproval,
on_revenue: RevenueRule::ToSpecificBeneficiary,
on_expire: ExpireRule::AutoRevoke
};
/// 验证B1规则
pub fn validate_b1(
user: Address,
asset_id: Hash,
start_time: Timestamp,
end_time: Timestamp,
usage_scope: String
) -> bool {
// 检查用户地址有效性
if !user.is_valid() {
return false;
}
// 检查时间有效性
if end_time <= start_time {
return false;
}
// 检查使用范围非空
if usage_scope.is_empty() {
return false;
}
return true;
}
// ============================================================================
// C2: 收益权 (Revenue Rights)
// ============================================================================
/// C2: 收益权规则
///
/// 特点:
/// - 仅享有收益分配权
/// - 无控制权
/// - 可转让
/// - 可能有期限
pub const RULE_C2: SovereigntyRule = SovereigntyRule {
sovereignty_type: SovereigntyType::C2,
name: "收益权",
description: "享有资产产生收益的分配权",
required_fields: vec!["beneficiary", "asset_id", "revenue_share", "start_time"],
constraints: vec![
RuleConstraint {
field_name: "revenue_share",
constraint_type: ConstraintType::MinValue,
value: "0"
},
RuleConstraint {
field_name: "revenue_share",
constraint_type: ConstraintType::MaxValue,
value: "10000" // 100.00% (basis points)
}
],
on_transfer: TransferRule::Unrestricted,
on_revenue: RevenueRule::DistributeByShares,
on_expire: ExpireRule::RequireRenewal
};
/// 验证C2规则
pub fn validate_c2(
beneficiary: Address,
asset_id: Hash,
revenue_share: u16, // 基点0-10000
start_time: Timestamp
) -> bool {
// 检查受益人地址有效性
if !beneficiary.is_valid() {
return false;
}
// 检查收益份额有效性0-10000基点
if revenue_share > 10000 {
return false;
}
return true;
}
// ============================================================================
// D0: 担保主权 (Collateral Sovereignty)
// ============================================================================
/// D0: 担保主权规则
///
/// 特点:
/// - 资产作为抵押品
/// - 限制转让
/// - 设置清算条件
/// - 必须登记
pub const RULE_D0: SovereigntyRule = SovereigntyRule {
sovereignty_type: SovereigntyType::D0,
name: "担保主权",
description: "资产作为债务担保的抵押品",
required_fields: vec!["collateral_asset", "beneficiary", "amount", "term", "liquidation_conditions"],
constraints: vec![
RuleConstraint {
field_name: "amount",
constraint_type: ConstraintType::MinValue,
value: "0"
},
RuleConstraint {
field_name: "liquidation_conditions",
constraint_type: ConstraintType::Required,
value: "LiquidationConditions"
}
],
on_transfer: TransferRule::Prohibited,
on_revenue: RevenueRule::ToSpecificBeneficiary,
on_expire: ExpireRule::AutoRevoke
};
/// 清算条件
struct LiquidationConditions {
trigger_price: u256,
trigger_time: Timestamp,
liquidation_ratio: u16 // 基点
}
/// 验证D0规则
pub fn validate_d0(
collateral_asset: Hash,
beneficiary: Address,
amount: u256,
term: Duration,
liquidation_conditions: LiquidationConditions
) -> bool {
// 检查抵押品资产ID有效性
if collateral_asset.is_zero() {
return false;
}
// 检查受益人地址有效性
if !beneficiary.is_valid() {
return false;
}
// 检查金额有效性
if amount == 0 {
return false;
}
// 检查清算比例有效性
if liquidation_conditions.liquidation_ratio > 10000 {
return false;
}
return true;
}
// ============================================================================
// E3: 知识产权 (Intellectual Property)
// ============================================================================
/// E3: 知识产权规则
///
/// 特点:
/// - 专利、版权、商标等
/// - 可授权使用
/// - 有地域限制
/// - 有时间限制
pub const RULE_E3: SovereigntyRule = SovereigntyRule {
sovereignty_type: SovereigntyType::E3,
name: "知识产权",
description: "专利、版权、商标等无形资产权利",
required_fields: vec!["ip_holder", "ip_type", "registration_number", "jurisdiction", "expiry_date"],
constraints: vec![
RuleConstraint {
field_name: "ip_type",
constraint_type: ConstraintType::Required,
value: "IPType"
},
RuleConstraint {
field_name: "registration_number",
constraint_type: ConstraintType::Required,
value: "String"
}
],
on_transfer: TransferRule::RequireApproval,
on_revenue: RevenueRule::FullToOwner,
on_expire: ExpireRule::RequireRenewal
};
/// 知识产权类型
enum IPType {
Patent, // 专利
Copyright, // 版权
Trademark, // 商标
TradeSecret, // 商业秘密
Other // 其他
}
/// 验证E3规则
pub fn validate_e3(
ip_holder: Address,
ip_type: IPType,
registration_number: String,
jurisdiction: u8,
expiry_date: Timestamp
) -> bool {
// 检查持有人地址有效性
if !ip_holder.is_valid() {
return false;
}
// 检查注册号非空
if registration_number.is_empty() {
return false;
}
// 检查到期日期有效性
if expiry_date <= block.timestamp {
return false;
}
return true;
}
// ============================================================================
// F4: 临时监管权 (Temporary Custody)
// ============================================================================
/// F4: 临时监管权规则
///
/// 特点:
/// - 托管、监管期间
/// - 无处置权
/// - 有明确期限
/// - 必须返还
pub const RULE_F4: SovereigntyRule = SovereigntyRule {
sovereignty_type: SovereigntyType::F4,
name: "临时监管权",
description: "临时托管或监管资产的权利",
required_fields: vec!["custodian", "original_owner", "asset_id", "custody_start", "custody_end", "custody_purpose"],
constraints: vec![
RuleConstraint {
field_name: "custody_end",
constraint_type: ConstraintType::Required,
value: "Timestamp"
},
RuleConstraint {
field_name: "custody_purpose",
constraint_type: ConstraintType::Required,
value: "String"
}
],
on_transfer: TransferRule::Prohibited,
on_revenue: RevenueRule::ToSpecificBeneficiary,
on_expire: ExpireRule::AutoRevoke
};
/// 验证F4规则
pub fn validate_f4(
custodian: Address,
original_owner: Address,
asset_id: Hash,
custody_start: Timestamp,
custody_end: Timestamp,
custody_purpose: String
) -> bool {
// 检查托管人地址有效性
if !custodian.is_valid() {
return false;
}
// 检查原所有者地址有效性
if !original_owner.is_valid() {
return false;
}
// 检查托管期限有效性
if custody_end <= custody_start {
return false;
}
// 检查托管目的非空
if custody_purpose.is_empty() {
return false;
}
return true;
}
// ============================================================================
// G5: 共有权 (Co-ownership)
// ============================================================================
/// G5: 共有权规则
///
/// 特点:
/// - 多方共同所有
/// - 按份额分配
/// - 重大决策需多数同意
/// - 收益按份额分配
pub const RULE_G5: SovereigntyRule = SovereigntyRule {
sovereignty_type: SovereigntyType::G5,
name: "共有权",
description: "多方共同拥有资产的权利",
required_fields: vec!["co_owners", "ownership_shares", "decision_threshold"],
constraints: vec![
RuleConstraint {
field_name: "co_owners",
constraint_type: ConstraintType::MinValue,
value: "2" // 至少2个共有人
},
RuleConstraint {
field_name: "decision_threshold",
constraint_type: ConstraintType::MinValue,
value: "5000" // 至少50%同意
}
],
on_transfer: TransferRule::RequireMultiSig,
on_revenue: RevenueRule::DistributeByShares,
on_expire: ExpireRule::NoExpiry
};
/// 共有人份额
struct OwnershipShare {
owner: Address,
share: u16 // 基点0-10000
}
/// 验证G5规则
pub fn validate_g5(
co_owners: Vec<OwnershipShare>,
decision_threshold: u16 // 基点5000-10000
) -> bool {
// 检查共有人数量至少2人
if co_owners.len() < 2 {
return false;
}
// 检查决策阈值有效性50%-100%
if decision_threshold < 5000 || decision_threshold > 10000 {
return false;
}
// 检查份额总和是否为100%
let mut total_share: u32 = 0;
for share in co_owners {
if !share.owner.is_valid() {
return false;
}
total_share += share.share as u32;
}
if total_share != 10000 {
return false;
}
return true;
}
// ============================================================================
// 辅助函数
// ============================================================================
/// 获取主权规则
pub fn get_sovereignty_rule(sovereignty_type: SovereigntyType) -> SovereigntyRule {
match sovereignty_type {
SovereigntyType::A0 => RULE_A0,
SovereigntyType::B1 => RULE_B1,
SovereigntyType::C2 => RULE_C2,
SovereigntyType::D0 => RULE_D0,
SovereigntyType::E3 => RULE_E3,
SovereigntyType::F4 => RULE_F4,
SovereigntyType::G5 => RULE_G5
}
}
/// 验证主权规则
pub fn validate_sovereignty(
sovereignty_type: SovereigntyType,
data: Map<String, Any>
) -> bool {
match sovereignty_type {
SovereigntyType::A0 => validate_a0(
data.get("owner"),
data.get("asset_id"),
data.get("gnacs_code")
),
SovereigntyType::B1 => validate_b1(
data.get("user"),
data.get("asset_id"),
data.get("start_time"),
data.get("end_time"),
data.get("usage_scope")
),
SovereigntyType::C2 => validate_c2(
data.get("beneficiary"),
data.get("asset_id"),
data.get("revenue_share"),
data.get("start_time")
),
SovereigntyType::D0 => validate_d0(
data.get("collateral_asset"),
data.get("beneficiary"),
data.get("amount"),
data.get("term"),
data.get("liquidation_conditions")
),
SovereigntyType::E3 => validate_e3(
data.get("ip_holder"),
data.get("ip_type"),
data.get("registration_number"),
data.get("jurisdiction"),
data.get("expiry_date")
),
SovereigntyType::F4 => validate_f4(
data.get("custodian"),
data.get("original_owner"),
data.get("asset_id"),
data.get("custody_start"),
data.get("custody_end"),
data.get("custody_purpose")
),
SovereigntyType::G5 => validate_g5(
data.get("co_owners"),
data.get("decision_threshold")
)
}
}