Bài viết
Chia sẻ kiến thức của bạn.
Object Versioning and Transaction Ordering
Sui’s object versioning ensures deterministic outcomes in parallel transaction execution by tracking object versions and resolving conflicts via consensus (Narwhal/Bullshark). Intermediate developers must handle versioning to avoid conflicts in shared objects.
module leaderboard::shared_leaderboard {
use sui::object::{Self, UID};
use sui::tx_context::{Self, TxContext};
use sui::transfer;
use sui::dynamic_field as df;
struct Leaderboard has key {
id: UID,
}
// Create shared leaderboard
public entry fun create_leaderboard(ctx: &mut TxContext) {
let leaderboard = Leaderboard { id: object::new(ctx) };
transfer::share_object(leaderboard);
}
// Update user score (Sui versioning ensures atomic updates)
public entry fun update_score(leaderboard: &mut Leaderboard, user: address, score: u64) {
df::add(&mut leaderboard.id, user, score);
// Sui’s consensus ensures versioned updates are ordered
}
// Get user score
public fun get_score(leaderboard: &Leaderboard, user: address): u64 {
if (df::exists_(&leaderboard.id, user)) {
*df::borrow(&leaderboard.id, user)
} else {
0
}
}
}
How does Sui’s object versioning prevent race conditions? Write a Move module for a shared leaderboard where users update scores, and explain how Sui ensures consistency if multiple users update simultaneously.
- Move CLI
Câu trả lời
1Code Block Explanation:
The Leaderboard struct is a shared object (via transfer::share_object) that stores user scores as dynamic fields, using the user’s address as the key and their score as the value. The create_leaderboard function initializes it, while update_score adds or updates a user’s score. Sui’s object versioning assigns a unique version to the Leaderboard object each time it’s modified. If multiple transactions try to update the same user’s score, Sui’s consensus ensures one transaction is applied first, and others referencing an outdated version fail, preventing race conditions. The get_score function safely retrieves scores, returning 0 for non-existent users.
Bạn có biết câu trả lời không?
Hãy đăng nhập và chia sẻ nó.
Move is an executable bytecode language used to implement custom transactions and smart contracts.
Kiếm phần của bạn từ 1000 Sui
Tích lũy điểm danh tiếng và nhận phần thưởng khi giúp cộng đồng Sui phát triển.

- ... SUIMatthardy+2095
- ... SUIacher+1666
- ... SUIjakodelarin+1092
- ... SUIChubbycheeks +1081
- ... SUITucker+1047
- ... SUIKurosakisui+1034
- ... SUIzerus+890