Publicación
Comparte tu conocimiento.
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
Respuestas
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.
Sabes la respuesta?
Inicie sesión y compártalo.
Move is an executable bytecode language used to implement custom transactions and smart contracts.
Gana tu parte de 1000 Sui
Gana puntos de reputación y obtén recompensas por ayudar a crecer a la comunidad de Sui.

- ... SUIMatthardy+2185
- ... SUIacher+1752
- ... SUIKurosakisui+1365
- ... SUIChubbycheeks +1176
- ... SUIjakodelarin+1142
- ... SUITucker+1087
- ... SUIzerus+888