Skip to main content

Module 0x3::validator_wrapper

use 0x2::tx_context;
use 0x2::versioned;
use 0x3::validator;

Struct ValidatorWrapper

struct ValidatorWrapper has store
Fields

Constants

const EInvalidVersion: u64 = 0;

Function create_v1

public(friend) fun create_v1(validator: validator::Validator, ctx: &mut tx_context::TxContext): validator_wrapper::ValidatorWrapper
Implementation
public(package) fun create_v1(validator: Validator, ctx: &mut TxContext): ValidatorWrapper {
    ValidatorWrapper {
        inner: versioned::create(1, validator, ctx)
    }
}

Function load_validator_maybe_upgrade

This function should always return the latest supported version. If the inner version is old, we upgrade it lazily in-place.

public(friend) fun load_validator_maybe_upgrade(self: &mut validator_wrapper::ValidatorWrapper): &mut validator::Validator
Implementation
public(package) fun load_validator_maybe_upgrade(self: &mut ValidatorWrapper): &mut Validator {
    upgrade_to_latest(self);
    versioned::load_value_mut(&mut self.inner)
}

Function destroy

Destroy the wrapper and retrieve the inner validator object.

public(friend) fun destroy(self: validator_wrapper::ValidatorWrapper): validator::Validator
Implementation
public(package) fun destroy(self: ValidatorWrapper): Validator {
    upgrade_to_latest(&self);
    let ValidatorWrapper { inner } = self;
    versioned::destroy(inner)
}

Function upgrade_to_latest

fun upgrade_to_latest(self: &validator_wrapper::ValidatorWrapper)
Implementation
fun upgrade_to_latest(self: &ValidatorWrapper) {
    let version = version(self);
    // TODO: When new versions are added, we need to explicitly upgrade here.
    assert!(version == 1, EInvalidVersion);
}

Function version

fun version(self: &validator_wrapper::ValidatorWrapper): u64
Implementation
fun version(self: &ValidatorWrapper): u64 {
    versioned::version(&self.inner)
}