Move.

Пост

Поделитесь своими знаниями.

BigDev.
Aug 24, 2025
Экспертные Вопросы и Ответы

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
5
1
Поделиться
Комментарии
.

Ответы

1
jakodelarin.
Aug 24 2025, 20:09

Code 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.

4
Комментарии
.

Знаете ответ?

Пожалуйста, войдите в систему и поделитесь им.

Move is an executable bytecode language used to implement custom transactions and smart contracts.

249Посты550Ответы
Sui.X.Peera.

Заработай свою долю из 1000 Sui

Зарабатывай очки репутации и получай награды за помощь в развитии сообщества Sui.

Кампания вознагражденийСентябрь
Посты с вознаграждением