1

I have a single object on a blockchain that is going to get updated from time to time. I'd like to track these changes. How do I describe such a struct Vec<(u32, u32)> and initialize it at the start? For now I have:

encoding_struct! {
    struct AC {
        const SIZE = 16;
        field s: Vec<u32> [00 => 08]
        field o: Vec<u32> [08 => 16]
    }
}

and then wait for a special empty init transaction

message! {
    struct TxInitAC {
        const TYPE = SERVICE_ID;
        const ID = TX_INIT_AC;
        const SIZE = 0;
    }
}

with the execute method

fn execute(&self, view: &mut Fork) {
    let mut schema = CurrencySchema { view };                   
    let ac = AC::new(vec![], vec![]);        
    schema.access_control().push(ac);
}
defuz
  • 26,721
  • 10
  • 38
  • 60
Victor Ermolaev
  • 721
  • 1
  • 5
  • 16
  • My understanding was that items on the blockchain are *never* changed; that's part of what makes them interesting and useful. Beyond that, your question doesn't have enough detail for me to even understand what you are truly asking. Does your question need to involve Exonum? Could you phrase the question with just plain Rust, creating a [MCVE]? – Shepmaster Sep 24 '17 at 14:42
  • Items do change. Think about cryptocurrency: wallet balances are not fixed. What's important is that changes to the balances (transactions) are immutable. W.r.t. the minimal example: even the minimal example in the Exonum doc is several pages long. I am afraid the question cannot be rephrased in plain Rust, I was hoping that developers of Exonum would take a look at this question. They specifically offer help on their website (https://exonum.com/faq) – Victor Ermolaev Sep 24 '17 at 16:54

1 Answers1

1

After having a conversation with the developers on Gitter, I came up with a solution.

To describe a compound object in encoding_struct! one has to describe each component in a corresponding encoding_struct!. In the case of the question that would be:

encoding_struct! {
    struct Pair {
        const SIZE = 8;
        field s: u32 [00 => 04]
        field o: u32 [04 => 08]
    }
}

encoding_struct! {
    struct AC {
        const SIZE = 8;
        field inner : Vec<Pair> [00 => 08]
    }
}

To initialize the blockchain db, one has to implement initialize function in the Service trait, e.g., initialize with an empty vector:

impl Service for MService {
//...
    fn initialize(&self, fork: &mut Fork) -> Value {
        let mut schema = MatrixSchema { view: fork };
        let matrix = AC::new(vec![]);
        // assume method ac() is implemented for the schema
        schema.ac().set(ac);        
        Value::Null
    }
}
Victor Ermolaev
  • 721
  • 1
  • 5
  • 16