Skip to content

Commit

Permalink
sloppy version working
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Sep 25, 2024
1 parent 45a0fca commit b106b5d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/lib/mina/actions/offchain-contract.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ const offchainState = OffchainState(

class StateProof extends offchainState.Proof {}

const offchainStateInstance = offchainState.init();

// example contract that interacts with offchain state
class ExampleContract extends SmartContract {
@state(OffchainState.Commitments) offchainStateCommitments =
offchainState.emptyCommitments();

offchainState = offchainState.init(ExampleContract);
offchainState = offchainStateInstance;

@method
async createAccount(address: PublicKey, amountToMint: UInt64) {
Expand Down Expand Up @@ -68,6 +70,9 @@ class ExampleContract extends SmartContract {
let fromOption = await this.offchainState.fields.accounts.get(from);
let fromBalance = fromOption.assertSome('sender account exists');

Provable.asProver(() => {
console.log('transfer2');
});
let toOption = await this.offchainState.fields.accounts.get(to);
let toBalance = toOption.orElse(0n);

Expand Down Expand Up @@ -117,6 +122,8 @@ class ExampleContract extends SmartContract {
}
}

offchainStateInstance.setContractClass(ExampleContract);

// connect contract to offchain state
// offchainState.setContractClass(ExampleContract);

Expand Down
39 changes: 29 additions & 10 deletions src/lib/mina/actions/offchain-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type OffchainStateInstance<

setContractInstance(contractInstance: OffchainStateContract<Config>): void;

setContractClass(contractClass: OffchainStateContractClass<Config>): void;

/**
* Create a proof that updates the commitments to offchain state: Merkle root and action state.
*/
Expand Down Expand Up @@ -104,9 +106,7 @@ type OffchainState<Config extends { [key: string]: OffchainStateKind }> = {

emptyCommitments(): State<OffchainStateCommitments>;

init(
contractClass: OffchainStateContractClass<Config>
): OffchainStateInstance<Config>;
init(): OffchainStateInstance<Config>;
};

type OffchainStateContract<
Expand Down Expand Up @@ -189,15 +189,15 @@ function OffchainState<
const height = logTotalCapacity + 1;
class IndexedMerkleMapN extends IndexedMerkleMap(height) {}

const emptyMerkleMapRoot = new IndexedMerkleMapN().root;

let rollup = OffchainStateRollup({
logTotalCapacity,
maxActionsPerProof,
maxActionsPerUpdate,
});

function OffchainStateInstance(
contractClass: OffchainStateContractClass<Config>
): OffchainStateInstance<Config> {
function OffchainStateInstance(): OffchainStateInstance<Config> {
type InternalState = {
_contract: OffchainStateContract<Config> | undefined;
_contractClass: OffchainStateContractClass<Config> | undefined;
Expand All @@ -215,9 +215,10 @@ function OffchainState<
};

function defaultInternalState(): InternalState {
console.log('defaultInternalState');
return {
_contract: undefined,
_contractClass: contractClass,
_contractClass: undefined,
merkleMap: new IndexedMerkleMapN(),
valueMap: new Map(),

Expand Down Expand Up @@ -249,10 +250,16 @@ function OffchainState<
};

const merkleMaps = async () => {
if (internal.merkleMap !== undefined && internal.valueMap !== undefined) {
if (
internal.merkleMap.root.toString() !== emptyMerkleMapRoot.toString() ||
internal.valueMap.size > 0
) {
console.log('local merkle maps found');
return { merkleMap: internal.merkleMap, valueMap: internal.valueMap };
}
console.log('local merkle maps not found');
let actionState = await onchainActionState();
console.log('actionState', actionState.toString());
let { merkleMap, valueMap } = await fetchMerkleMap(
height,
internal.contract,
Expand Down Expand Up @@ -413,6 +420,13 @@ function OffchainState<

// push action on account update
let update = getContract().self;
Provable.asProver(() => {
console.log(
'existing actions',
JSON.stringify(update.body.actions)
);
console.log('pushing action', action.toString());
});
update.body.actions = Actions.pushEvent(update.body.actions, action);
},

Expand All @@ -426,6 +440,11 @@ function OffchainState<
return {
setContractInstance(contractInstance) {
internal._contract = contractInstance;
internal._contractClass =
contractInstance.constructor as OffchainStateContractClass<Config>;
},
setContractClass(contractClass) {
internal._contractClass = contractClass;
},
async createSettlementProof() {
let { merkleMap } = await merkleMaps();
Expand Down Expand Up @@ -490,8 +509,8 @@ function OffchainState<
}

return {
init(contractClass: OffchainStateContractClass<Config>) {
return OffchainStateInstance(contractClass);
init() {
return OffchainStateInstance();
},

async compile() {
Expand Down

0 comments on commit b106b5d

Please sign in to comment.