o1js icon indicating copy to clipboard operation
o1js copied to clipboard

Using `Struct` as state for a contract does not work properly inside `reducer-composite test`

Open MartinMinkov opened this issue 10 months ago • 1 comments

Given the following example contract:

class Action extends Struct({
  isIncrement: Bool,
  data: Field,
}) {
  static increment(count: number) {
    return new this({
      isIncrement: Bool(true),
      data: Field(count),
    });
  }

  static other(data: Field) {
    return new this({
      isIncrement: Bool(false),
      data,
    });
  }
}

class CounterState extends Struct({
  count: Field,
  rollup: Field, // helper field to store the point in the action history that our on-chain state is at
}) {
  static initial = new this({
    count: Field(0),
    rollup: Reducer.initialActionState,
  });
}

class Counter extends SmartContract {
  // the "reducer" field describes a type of action that we can dispatch, and reduce later
  reducer = Reducer({ actionType: Action });

  // on-chain version of our state. it will typically lag behind the
  // version that's implicitly represented by the list of actions
  @state(CounterState) state = State<CounterState>();

When we call contract.state.get().count, we get incorrect values inside the reducer-composite test -- see this commit as an example here https://github.com/o1-labs/o1js/pull/1567/commits/442c3402594275d5709807fb8c5ed4fcbdc34158

MartinMinkov avatar Apr 17 '24 18:04 MartinMinkov

since the reducer logic was completely rewritten since this, would be interesting to test it again and see if it works now 😅

mitschabaude avatar Apr 19 '24 10:04 mitschabaude