Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose network precondition setters to LocalBlockchain #329

Merged
merged 7 commits into from
Aug 8, 2022

Conversation

Trivo25
Copy link
Member

@Trivo25 Trivo25 commented Aug 5, 2022

Description

Exposes various setters to individually set network preconditions. Network state is now entirely being managed on the side of SnarkyJS and can easily be customized.

Example

class MyContract extends SmartContract {

  @method blockheightEquals(y: UInt32) {
    let length = this.network.blockchainLength.get();
    this.network.blockchainLength.assertEquals(length);

    length.assertEquals(y);
  }

}

// ... create a local blockchain instance

let Local = Mina.LocalBlockchain();
Mina.setActiveInstance(Local);

// ... set the network state
Local.setBlockchainLength(500);


let tx = await Mina.transaction(feePayer, () => {
  zkapp.blockheightEquals(UInt32.from(500));
});
tx.send();

Under the hood

LocalBlockchain has been extended to manage the network state within a new variable networkState, instead of relying on a hard-coded value within OCaml. LocalBlockchain now exposes the following closures:

setTimestamp: (ms: UInt64) => void;
setGlobalSlot: (slot: UInt32) => void;
setGlobalSlotSinceHardfork: (slot: UInt32) => void;
setBlockchainLength: (height: UInt32) => void;
setTotalCurrency: (currency: UInt32) => void;

When a transaction is being processed, the network state is now being passed in as an additional parameter.

applyJsonTransaction(
	txJson: string,
    accountCreationFee: string,
    networkState: string
): Account[];

bindings not included, requires MinaProtocol/mina#11639

Copy link
Contributor

@MartinMinkov MartinMinkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just some small questions

src/lib/mina.ts Outdated Show resolved Hide resolved
src/lib/mina.ts Outdated
setBlockchainLength(height: UInt32) {
networkState.blockchainLength = height;
},
setTotalCurrency(currency: UInt32) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small question, will we ever need a UInt64 for any of these as well as UInt32?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, currency is supposed to be UInt64

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argh.. turns out that UInt32 extends UInt64 (but not the other way). I wonder if this is a bug which we should fix, or if it makes sense in a weird way, because a UInt32 also fits in 64 bits

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thats right, currency is also supposed to be UInt64. I should probably set it to UInt64 as well, since it is also defined like this in all the Precondition interfaces

Copy link
Member

@mitschabaude mitschabaude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great! I appreciate the example

@Trivo25 Trivo25 merged commit 1dfdc04 into main Aug 8, 2022
@Trivo25 Trivo25 deleted the feature/set-updates-LocalBlockchain branch August 8, 2022 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants