-
Notifications
You must be signed in to change notification settings - Fork 51
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 deployment of common contracts to Blockchain
type
#367
Expose deployment of common contracts to Blockchain
type
#367
Conversation
Codecov Report
@@ Coverage Diff @@
## master #367 +/- ##
==========================================
- Coverage 47.54% 46.98% -0.56%
==========================================
Files 26 26
Lines 3418 3418
==========================================
- Hits 1625 1606 -19
- Misses 1662 1688 +26
+ Partials 131 124 -7
Flags with carried forward coverage won't be shown. Click here to find out more.
|
I think we need a different injection system for this. I think blockchain should not know about contracts but should get them as parameter somehow. Ideally we would have fvm bootstrap those. We should have an issue on that. |
Thanks for the comment @bluesign 🙏 |
I think first step we can send them via struct at first ( like DeployDescription here ) to blockchain. emulator.WithContracts can took Then maybe we can bump the issue: onflow/flow-go#2812 If fvm can expose us contracts in bootstrap, it maybe better than to deploy them afterwards. ( So we will have them all deployed even when block height is zero ) But I think we can merge this if we just change it to |
Perfect, much thanks for the detailed explanation 🙇 |
We expose the DeployContracts method to the public, and allow users to pass in their contracts of choice, for deployment. We also expose the CommonContracts global variable, which pre-configures the most common contracts. We also introduce a new option, called WithContracts, in order to programmatically specify which contracts should be deployed by the Blockchain.
a96675f
to
e45a531
Compare
blockchain.go
Outdated
@@ -158,6 +157,7 @@ type config struct { | |||
TransactionValidationEnabled bool | |||
ChainID flowgo.ChainID | |||
CoverageReportingEnabled bool | |||
WithContracts []ContractDescription |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe drop the With prefix, or rename to something like InitialContracts, or StartupContracts, or just Contracts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I wanted it to be on par with the server flag, which has a boolean value. But since we changed that, I forgot to update accordingly. I would go with just Contracts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in b4ab4b8
blockchain.go
Outdated
@@ -449,6 +461,12 @@ func NewBlockchain(opts ...Option) (*Blockchain, error) { | |||
if err != nil { | |||
return nil, err | |||
} | |||
if conf.WithContracts != nil && len(conf.WithContracts) > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just check for len(), because if nil it would just be 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good suggestion 🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in b4ab4b8
@@ -943,7 +961,7 @@ func (b *Blockchain) GetTransactionResult(ID sdk.Identifier) (*sdk.TransactionRe | |||
// GetAccountByIndex returns the account for the given address. | |||
func (b *Blockchain) GetAccountByIndex(index uint) (*sdk.Account, error) { | |||
|
|||
generator := flow.NewAddressGenerator(sdk.ChainID(b.vmCtx.Chain.ChainID())) | |||
generator := sdk.NewAddressGenerator(sdk.ChainID(b.vmCtx.Chain.ChainID())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both flow and sdk were actually the same package imported twice, and the editor displayed a linting error. So I kept only one of the imports. Did I misunderstood this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, nice.
nftstorefront "github.com/onflow/nft-storefront/lib/go/contracts" | ||
) | ||
|
||
var CommonContracts = func() []ContractDescription { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: I think this could be a variable not a function. But I'm not sure if better since then you could modify it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am new to Golang 😅. Could I have a global variable definition, with a body? Because I need the chain and service address intermediate variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also kinda new to Go, but I think yes. What you could do is:
var chain = flowgo.Emulator.Chain()
var ftAddress = flowsdk.HexToAddress(fvm.FungibleTokenAddress(chain).HexWithPrefix())
var serviceAddress = flowsdk.HexToAddress(chain.ServiceAddress().HexWithPrefix())
var CommonContracts = []ContractDescription{ ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is NIT, so decide however you feel like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally like the function version, because all the intermediate variables, are not exposed to the rest of the file 😇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some minor comments, otherwise looks great, thank you!
Blockchain
typeBlockchain
type
This is the necessary ground-work, for the Deployed Contracts improvement. import ArrayUtils from "ArrayUtils.cdc"
import FUSD from 0xf8d6e0586b0a20c7
pub contract StringUtils {
...
} Once this is merged, I will open up a follow-up PR on the blockchain.useConfiguration(Test.Configuration({
"NonFungibleToken": blockchain.serviceAccount().address,
...
})) Exposing the |
@m-Peter good to merge? |
@sideninja I think yes, we can proceed with merging this 🙏 |
Work towards: onflow/developer-grants#148
Description
We introduce a new option, called
Contracts
, in order to programmatically specify which contracts should aBlockchain
deploy (this is mostly for the common contracts, it does not affect the system contracts, which are essential to the function of the emulator/blockchain).The end goal is to be able to utilize it in other libraries, for example the
cadence-tools/test
repo, with something like:This will enable the Cadence Testing Framework to have the common contracts available by default. e.g:
NonFungibleToken
/MetadataViews
/FUSD
/ExampleNFT
/NFTStorefront
/NFTStorefrontV2
etc.For contributor use:
master
branchFiles changed
in the GitHub PR explorer