Skip to content

Commit

Permalink
Utilize flow.MonotonicEmulator chain for integration tests
Browse files Browse the repository at this point in the history
This will guarantee that the addresses of created accounts will be
sequential and start from 0x01. Thus, allowing developers to safely
import types of deployed contracts.
  • Loading branch information
m-Peter committed Sep 8, 2023
1 parent b63c712 commit 767d32b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
22 changes: 13 additions & 9 deletions test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,17 @@ type keyInfo struct {
signer crypto.Signer
}

var Chain = flow.MonotonicEmulator.Chain()

var CommonContracts = emulator.NewCommonContracts(Chain)

var systemContracts = func() []common.AddressLocation {
chain := flow.Emulator.Chain()
serviceAddress := chain.ServiceAddress().HexWithPrefix()
serviceAddress := Chain.ServiceAddress().HexWithPrefix()
contracts := map[string]string{
"FlowServiceAccount": serviceAddress,
"FlowToken": fvm.FlowTokenAddress(chain).HexWithPrefix(),
"FungibleToken": fvm.FungibleTokenAddress(chain).HexWithPrefix(),
"FlowFees": environment.FlowFeesAddress(chain).HexWithPrefix(),
"FlowToken": fvm.FlowTokenAddress(Chain).HexWithPrefix(),
"FungibleToken": fvm.FungibleTokenAddress(Chain).HexWithPrefix(),
"FlowFees": environment.FlowFeesAddress(Chain).HexWithPrefix(),
"FlowStorageFees": serviceAddress,
"FlowClusterQC": serviceAddress,
"FlowDKG": serviceAddress,
Expand Down Expand Up @@ -536,7 +539,8 @@ func newBlockchain(
[]emulator.Option{
emulator.WithStorageLimitEnabled(false),
emulator.WithServerLogger(logger),
emulator.Contracts(emulator.CommonContracts),
emulator.Contracts(CommonContracts),
emulator.WithChainID(Chain.ChainID()),
},
opts...,
)...,
Expand Down Expand Up @@ -711,7 +715,7 @@ func excludeCommonLocations(coverageReport *runtime.CoverageReport) {
for _, location := range systemContracts {
coverageReport.ExcludeLocation(location)
}
for _, contract := range emulator.CommonContracts {
for _, contract := range CommonContracts {
address, _ := common.HexToAddress(contract.Address.String())
location := common.AddressLocation{
Address: address,
Expand All @@ -725,7 +729,7 @@ func excludeCommonLocations(coverageReport *runtime.CoverageReport) {
// address mappings for system/common contracts.
func baseConfiguration() *stdlib.Configuration {
addresses := make(map[string]common.Address, 0)
serviceAddress, _ := common.HexToAddress("0xf8d6e0586b0a20c7")
serviceAddress := common.Address(Chain.ServiceAddress())
addresses["NonFungibleToken"] = serviceAddress
addresses["MetadataViews"] = serviceAddress
addresses["ViewResolver"] = serviceAddress
Expand All @@ -734,7 +738,7 @@ func baseConfiguration() *stdlib.Configuration {
address := common.Address(addressLocation.Address)
addresses[contract] = address
}
for _, contractDescription := range emulator.CommonContracts {
for _, contractDescription := range CommonContracts {
contract := contractDescription.Name
address := common.Address(contractDescription.Address)
addresses[contract] = address
Expand Down
51 changes: 28 additions & 23 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2957,7 +2957,7 @@ func TestServiceAccount(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t,
"0xf8d6e0586b0a20c7",
"0x0000000000000001",
serviceAccount.Address.HexWithPrefix(),
)
})
Expand All @@ -2977,7 +2977,7 @@ func TestServiceAccount(t *testing.T) {
// Assert
Test.assertEqual(Type<Address>(), account.address.getType())
Test.assertEqual(Type<PublicKey>(), account.publicKey.getType())
Test.assertEqual(Address(0xf8d6e0586b0a20c7), account.address)
Test.assertEqual(Address(0x0000000000000001), account.address)
}
`

Expand Down Expand Up @@ -3377,7 +3377,7 @@ func TestCoverageReportForIntegrationTests(t *testing.T) {
assert.Equal(t, result2.TestName, "testAddSpecialNumber")
require.NoError(t, result2.Error)

address, err := common.HexToAddress("0x01cf0e2f2f715450")
address, err := common.HexToAddress("0x0000000000000005")
require.NoError(t, err)
location := common.AddressLocation{
Address: address,
Expand All @@ -3400,25 +3400,25 @@ func TestCoverageReportForIntegrationTests(t *testing.T) {
assert.ElementsMatch(
t,
[]string{
"A.0ae53cb6e3f42a79.FlowToken",
"A.ee82856bf20e2aa6.FungibleToken",
"A.e5a8b7f23e8b548f.FlowFees",
"A.f8d6e0586b0a20c7.FlowStorageFees",
"A.f8d6e0586b0a20c7.FlowServiceAccount",
"A.f8d6e0586b0a20c7.FlowClusterQC",
"A.f8d6e0586b0a20c7.FlowDKG",
"A.f8d6e0586b0a20c7.FlowEpoch",
"A.f8d6e0586b0a20c7.FlowIDTableStaking",
"A.f8d6e0586b0a20c7.FlowStakingCollection",
"A.f8d6e0586b0a20c7.LockedTokens",
"A.f8d6e0586b0a20c7.NodeVersionBeacon",
"A.f8d6e0586b0a20c7.StakingProxy",
"A.0000000000000003.FlowToken",
"A.0000000000000002.FungibleToken",
"A.0000000000000004.FlowFees",
"A.0000000000000001.FlowStorageFees",
"A.0000000000000001.FlowServiceAccount",
"A.0000000000000001.FlowClusterQC",
"A.0000000000000001.FlowDKG",
"A.0000000000000001.FlowEpoch",
"A.0000000000000001.FlowIDTableStaking",
"A.0000000000000001.FlowStakingCollection",
"A.0000000000000001.LockedTokens",
"A.0000000000000001.NodeVersionBeacon",
"A.0000000000000001.StakingProxy",
"s.7465737400000000000000000000000000000000000000000000000000000000",
"I.Crypto",
"I.Test",
"A.f8d6e0586b0a20c7.ExampleNFT",
"A.f8d6e0586b0a20c7.NFTStorefrontV2",
"A.f8d6e0586b0a20c7.NFTStorefront",
"A.0000000000000001.ExampleNFT",
"A.0000000000000001.NFTStorefrontV2",
"A.0000000000000001.NFTStorefront",
},
coverageReport.ExcludedLocationIDs(),
)
Expand Down Expand Up @@ -3889,6 +3889,7 @@ func TestGetEventsFromIntegrationTests(t *testing.T) {

const testCode = `
import Test
import FooContract from 0x0000000000000005
pub let blockchain = Test.newEmulatorBlockchain()
pub let account = blockchain.createAccount()
Expand Down Expand Up @@ -3916,7 +3917,7 @@ func TestGetEventsFromIntegrationTests(t *testing.T) {
Test.expect(result, Test.beSucceeded())
Test.assert(result.returnValue! as! Bool)
let typ = CompositeType("A.01cf0e2f2f715450.FooContract.ContractInitialized")!
let typ = Type<FooContract.ContractInitialized>()
let events = blockchain.eventsOfType(typ)
Test.assertEqual(1, events.length)
}
Expand All @@ -3933,10 +3934,14 @@ func TestGetEventsFromIntegrationTests(t *testing.T) {
let result = blockchain.executeTransaction(tx)
Test.expect(result, Test.beSucceeded())
let typ = CompositeType("A.01cf0e2f2f715450.FooContract.NumberAdded")!
let typ = Type<FooContract.NumberAdded>()
let events = blockchain.eventsOfType(typ)
Test.assertEqual(1, events.length)
let event = events[0] as! FooContract.NumberAdded
Test.assertEqual(78557, event.n)
Test.assertEqual("Sierpinski", event.trait)
let evts = blockchain.events()
Test.expect(evts.length, Test.beGreaterThan(1))
}
Expand Down Expand Up @@ -4420,7 +4425,7 @@ func TestReferenceDeployedContractTypes(t *testing.T) {

const testCode = `
import Test
import FooContract from 0x01cf0e2f2f715450
import FooContract from 0x0000000000000005
pub let blockchain = Test.newEmulatorBlockchain()
pub let account = blockchain.createAccount()
Expand Down Expand Up @@ -4543,7 +4548,7 @@ func TestReferenceDeployedContractTypes(t *testing.T) {

const testCode = `
import Test
import FooContract from 0x01cf0e2f2f715450
import FooContract from 0x0000000000000005
pub let blockchain = Test.newEmulatorBlockchain()
pub let account = blockchain.createAccount()
Expand Down
3 changes: 2 additions & 1 deletion test/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ func NewTestRunner() *TestRunner {
logger := zerolog.New(output).With().Timestamp().Logger().Hook(logCollectionHook)
blockchain, err := emulator.New(
emulator.WithStorageLimitEnabled(false),
emulator.Contracts(emulator.CommonContracts),
emulator.Contracts(CommonContracts),
emulator.WithChainID(Chain.ChainID()),
)
if err != nil {
panic(err)
Expand Down

0 comments on commit 767d32b

Please sign in to comment.