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

Bluesign: Internal Emulator #6592

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ go-math-rand-check:
# - "onflow/crypto/random" for deterministic randomness
grep --include=\*.go \
--exclude=*test* --exclude=*helper* --exclude=*example* --exclude=*fixture* --exclude=*benchmark* --exclude=*profiler* \
--exclude-dir=*test* --exclude-dir=*helper* --exclude-dir=*example* --exclude-dir=*fixture* --exclude-dir=*benchmark* --exclude-dir=*profiler* -rnw '"math/rand"'; \
--exclude-dir=*test* --exclude-dir=*helper* --exclude-dir=*example* --exclude-dir=*fixture* --exclude-dir=*benchmark* --exclude-dir=*profiler* --exclude-dir=*emulator* -rnw '"math/rand"'; \
if [ $$? -ne 1 ]; then \
echo "[Error] Go production code should not use math/rand package"; exit 1; \
fi
Expand Down
4 changes: 2 additions & 2 deletions integration/benchmark/load/load_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/ccf"
convert2 "github.com/onflow/flow-emulator/convert"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"

Expand All @@ -28,6 +27,7 @@ import (
"github.com/onflow/flow-go/integration/benchmark/common"
"github.com/onflow/flow-go/integration/benchmark/load"
"github.com/onflow/flow-go/integration/convert"
"github.com/onflow/flow-go/integration/emulator"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/unittest"
)
Expand Down Expand Up @@ -254,7 +254,7 @@ func (t *testTransactionSender) Send(tx *sdk.Transaction) (sdk.TransactionResult
Error: result.Err,
BlockID: sdk.EmptyID,
BlockHeight: 0,
TransactionID: convert2.FlowIdentifierToSDK(txBody.ID()),
TransactionID: emulator.FlowIdentifierToSDK(txBody.ID()),
CollectionID: sdk.EmptyID,
}

Expand Down
38 changes: 18 additions & 20 deletions integration/dkg/dkg_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import (
"github.com/stretchr/testify/suite"

"github.com/onflow/cadence"

jsoncdc "github.com/onflow/cadence/encoding/json"
emulator "github.com/onflow/flow-emulator/emulator"

"github.com/onflow/crypto"
"github.com/onflow/flow-core-contracts/lib/go/contracts"
"github.com/onflow/flow-core-contracts/lib/go/templates"
Expand All @@ -23,6 +20,7 @@ import (
sdktemplates "github.com/onflow/flow-go-sdk/templates"
"github.com/onflow/flow-go-sdk/test"

emulator "github.com/onflow/flow-go/integration/emulator"
"github.com/onflow/flow-go/integration/utils"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/dkg"
Expand All @@ -34,13 +32,13 @@ type ClientSuite struct {

contractClient *dkg.Client

env templates.Environment
blockchain emulator.Emulator
emulatorClient *utils.EmulatorClient

dkgAddress sdk.Address
dkgAccountKey *sdk.AccountKey
dkgSigner sdkcrypto.Signer
env templates.Environment
blockchain emulator.Emulator
emulatorClient *utils.EmulatorClient
serviceAccountAddress sdk.Address
dkgAddress sdk.Address
dkgAccountKey *sdk.AccountKey
dkgSigner sdkcrypto.Signer
}

func TestDKGClient(t *testing.T) {
Expand All @@ -57,7 +55,7 @@ func (s *ClientSuite) SetupTest() {

s.blockchain = blockchain
s.emulatorClient = utils.NewEmulatorClient(blockchain)

s.serviceAccountAddress = sdk.Address(s.blockchain.ServiceKey().Address)
// deploy contract
s.deployDKGContract()

Expand Down Expand Up @@ -234,16 +232,16 @@ func (s *ClientSuite) setUpAdmin() {
setUpAdminTx := sdk.NewTransaction().
SetScript(templates.GeneratePublishDKGParticipantScript(s.env)).
SetComputeLimit(9999).
SetProposalKey(s.blockchain.ServiceKey().Address, s.blockchain.ServiceKey().Index,
SetProposalKey(s.serviceAccountAddress, s.blockchain.ServiceKey().Index,
s.blockchain.ServiceKey().SequenceNumber).
SetPayer(s.blockchain.ServiceKey().Address).
SetPayer(s.serviceAccountAddress).
AddAuthorizer(s.dkgAddress)

signer, err := s.blockchain.ServiceKey().Signer()
require.NoError(s.T(), err)

s.signAndSubmit(setUpAdminTx,
[]sdk.Address{s.blockchain.ServiceKey().Address, s.dkgAddress},
[]sdk.Address{s.serviceAccountAddress, s.dkgAddress},
[]sdkcrypto.Signer{signer, s.dkgSigner},
)
}
Expand All @@ -262,9 +260,9 @@ func (s *ClientSuite) startDKGWithParticipants(nodeIDs []flow.Identifier) {
startDKGTx := sdk.NewTransaction().
SetScript(templates.GenerateStartDKGScript(s.env)).
SetComputeLimit(9999).
SetProposalKey(s.blockchain.ServiceKey().Address, s.blockchain.ServiceKey().Index,
SetProposalKey(s.serviceAccountAddress, s.blockchain.ServiceKey().Index,
s.blockchain.ServiceKey().SequenceNumber).
SetPayer(s.blockchain.ServiceKey().Address).
SetPayer(s.serviceAccountAddress).
AddAuthorizer(s.dkgAddress)

err := startDKGTx.AddArgument(cadence.NewArray(valueNodeIDs))
Expand All @@ -274,7 +272,7 @@ func (s *ClientSuite) startDKGWithParticipants(nodeIDs []flow.Identifier) {
require.NoError(s.T(), err)

s.signAndSubmit(startDKGTx,
[]sdk.Address{s.blockchain.ServiceKey().Address, s.dkgAddress},
[]sdk.Address{s.serviceAccountAddress, s.dkgAddress},
[]sdkcrypto.Signer{signer, s.dkgSigner},
)

Expand All @@ -293,9 +291,9 @@ func (s *ClientSuite) createParticipant(nodeID flow.Identifier, authoriser sdk.A
createParticipantTx := sdk.NewTransaction().
SetScript(templates.GenerateCreateDKGParticipantScript(s.env)).
SetComputeLimit(9999).
SetProposalKey(s.blockchain.ServiceKey().Address, s.blockchain.ServiceKey().Index,
SetProposalKey(s.serviceAccountAddress, s.blockchain.ServiceKey().Index,
s.blockchain.ServiceKey().SequenceNumber).
SetPayer(s.blockchain.ServiceKey().Address).
SetPayer(s.serviceAccountAddress).
AddAuthorizer(authoriser)

err := createParticipantTx.AddArgument(cadence.NewAddress(s.dkgAddress))
Expand All @@ -310,7 +308,7 @@ func (s *ClientSuite) createParticipant(nodeID flow.Identifier, authoriser sdk.A
require.NoError(s.T(), err)

s.signAndSubmit(createParticipantTx,
[]sdk.Address{s.blockchain.ServiceKey().Address, authoriser},
[]sdk.Address{s.serviceAccountAddress, authoriser},
[]sdkcrypto.Signer{s2, signer},
)

Expand Down
47 changes: 23 additions & 24 deletions integration/dkg/dkg_emulator_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ import (
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-core-contracts/lib/go/contracts"
"github.com/onflow/flow-core-contracts/lib/go/templates"
emulator "github.com/onflow/flow-emulator/emulator"

sdk "github.com/onflow/flow-go-sdk"
sdkcrypto "github.com/onflow/flow-go-sdk/crypto"
sdktemplates "github.com/onflow/flow-go-sdk/templates"
"github.com/onflow/flow-go-sdk/test"

"github.com/onflow/flow-go/module/metrics"

dkgeng "github.com/onflow/flow-go/engine/consensus/dkg"
"github.com/onflow/flow-go/engine/testutil"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/integration/emulator"
"github.com/onflow/flow-go/integration/tests/lib"
"github.com/onflow/flow-go/integration/utils"
"github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/dkg"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/network/stub"
"github.com/onflow/flow-go/state/protocol/events/gadgets"
"github.com/onflow/flow-go/storage/badger"
Expand All @@ -54,10 +53,10 @@ type EmulatorSuite struct {
dkgAccountKey *sdk.AccountKey
dkgSigner sdkcrypto.Signer
checkDKGUnhappy bool // activate log hook for DKGBroker to check if the DKG core is flagging misbehaviours

netIDs flow.IdentityList
nodeAccounts []*nodeAccount
nodes []*node
serviceAccountAddress sdk.Address
netIDs flow.IdentityList
nodeAccounts []*nodeAccount
nodes []*node
}

func (s *EmulatorSuite) SetupTest() {
Expand Down Expand Up @@ -119,7 +118,7 @@ func (s *EmulatorSuite) initEmulator() {
s.Require().NoError(err)

s.blockchain = blockchain

s.serviceAccountAddress = sdk.Address(s.blockchain.ServiceKey().Address)
s.adminEmulatorClient = utils.NewEmulatorClient(blockchain)

s.hub = stub.NewNetworkHub()
Expand Down Expand Up @@ -163,15 +162,15 @@ func (s *EmulatorSuite) setupDKGAdmin() {
SetScript(templates.GeneratePublishDKGParticipantScript(s.env)).
SetComputeLimit(9999).
SetProposalKey(
s.blockchain.ServiceKey().Address,
s.serviceAccountAddress,
s.blockchain.ServiceKey().Index,
s.blockchain.ServiceKey().SequenceNumber).
SetPayer(s.blockchain.ServiceKey().Address).
SetPayer(s.serviceAccountAddress).
AddAuthorizer(s.dkgAddress)
signer, err := s.blockchain.ServiceKey().Signer()
require.NoError(s.T(), err)
_, err = s.prepareAndSubmit(setUpAdminTx,
[]sdk.Address{s.blockchain.ServiceKey().Address, s.dkgAddress},
[]sdk.Address{s.serviceAccountAddress, s.dkgAddress},
[]sdkcrypto.Signer{signer, s.dkgSigner},
)
require.NoError(s.T(), err)
Expand Down Expand Up @@ -229,13 +228,13 @@ func (s *EmulatorSuite) createAndFundAccount(netID bootstrap.NodeInfo) *nodeAcco
sc.FungibleToken.Address.Hex(),
sc.FlowToken.Address.Hex(),
))).
AddAuthorizer(s.blockchain.ServiceKey().Address).
AddAuthorizer(s.serviceAccountAddress).
SetProposalKey(
s.blockchain.ServiceKey().Address,
s.serviceAccountAddress,
s.blockchain.ServiceKey().Index,
s.blockchain.ServiceKey().SequenceNumber,
).
SetPayer(s.blockchain.ServiceKey().Address)
SetPayer(s.serviceAccountAddress)

err = fundAccountTx.AddArgument(cadence.UFix64(1_000_000))
require.NoError(s.T(), err)
Expand All @@ -244,7 +243,7 @@ func (s *EmulatorSuite) createAndFundAccount(netID bootstrap.NodeInfo) *nodeAcco
signer, err := s.blockchain.ServiceKey().Signer()
require.NoError(s.T(), err)
_, err = s.prepareAndSubmit(fundAccountTx,
[]sdk.Address{s.blockchain.ServiceKey().Address},
[]sdk.Address{s.serviceAccountAddress},
[]sdkcrypto.Signer{signer},
)
require.NoError(s.T(), err)
Expand Down Expand Up @@ -307,18 +306,18 @@ func (s *EmulatorSuite) startDKGWithParticipants(accounts []*nodeAccount) {
SetScript(templates.GenerateStartDKGScript(s.env)).
SetComputeLimit(9999).
SetProposalKey(
s.blockchain.ServiceKey().Address,
s.serviceAccountAddress,
s.blockchain.ServiceKey().Index,
s.blockchain.ServiceKey().SequenceNumber).
SetPayer(s.blockchain.ServiceKey().Address).
SetPayer(s.serviceAccountAddress).
AddAuthorizer(s.dkgAddress)

err := startDKGTx.AddArgument(cadence.NewArray(valueNodeIDs))
require.NoError(s.T(), err)
signer, err := s.blockchain.ServiceKey().Signer()
require.NoError(s.T(), err)
_, err = s.prepareAndSubmit(startDKGTx,
[]sdk.Address{s.blockchain.ServiceKey().Address, s.dkgAddress},
[]sdk.Address{s.serviceAccountAddress, s.dkgAddress},
[]sdkcrypto.Signer{signer, s.dkgSigner},
)
require.NoError(s.T(), err)
Expand All @@ -334,7 +333,7 @@ func (s *EmulatorSuite) claimDKGParticipant(node *node) {
SetScript(templates.GenerateCreateDKGParticipantScript(s.env)).
SetComputeLimit(9999).
SetProposalKey(
s.blockchain.ServiceKey().Address,
s.serviceAccountAddress,
s.blockchain.ServiceKey().Index,
s.blockchain.ServiceKey().SequenceNumber,
).
Expand All @@ -350,7 +349,7 @@ func (s *EmulatorSuite) claimDKGParticipant(node *node) {
signer, err := s.blockchain.ServiceKey().Signer()
require.NoError(s.T(), err)
_, err = s.prepareAndSubmit(createParticipantTx,
[]sdk.Address{node.account.accountAddress, s.blockchain.ServiceKey().Address, s.dkgAddress},
[]sdk.Address{node.account.accountAddress, s.serviceAccountAddress, s.dkgAddress},
[]sdkcrypto.Signer{node.account.accountSigner, signer, s.dkgSigner},
)
require.NoError(s.T(), err)
Expand All @@ -371,21 +370,21 @@ func (s *EmulatorSuite) sendDummyTx() (*flow.Block, error) {
createAccountTx, err := sdktemplates.CreateAccount(
[]*sdk.AccountKey{test.AccountKeyGenerator().New()},
[]sdktemplates.Contract{},
s.blockchain.ServiceKey().Address)
s.serviceAccountAddress)
if err != nil {
return nil, err
}
createAccountTx.
SetProposalKey(
s.blockchain.ServiceKey().Address,
s.serviceAccountAddress,
s.blockchain.ServiceKey().Index,
s.blockchain.ServiceKey().SequenceNumber).
SetPayer(s.blockchain.ServiceKey().Address)
SetPayer(s.serviceAccountAddress)

signer, err := s.blockchain.ServiceKey().Signer()
require.NoError(s.T(), err)
block, err := s.prepareAndSubmit(createAccountTx,
[]sdk.Address{s.blockchain.ServiceKey().Address},
[]sdk.Address{s.serviceAccountAddress},
[]sdkcrypto.Signer{signer},
)
return block, err
Expand Down
56 changes: 56 additions & 0 deletions integration/emulator/blockTicker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Flow Emulator
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package emulator

import (
"time"
)

type BlocksTicker struct {
emulator Emulator
ticker *time.Ticker
done chan bool
}

func NewBlocksTicker(
emulator Emulator,
blockTime time.Duration,
) *BlocksTicker {
return &BlocksTicker{
emulator: emulator,
ticker: time.NewTicker(blockTime),
done: make(chan bool, 1),
}
}

func (t *BlocksTicker) Start() error {
for {
select {
case <-t.ticker.C:
_, _ = t.emulator.ExecuteBlock()
_, _ = t.emulator.CommitBlock()
case <-t.done:
return nil
}
}
}

func (t *BlocksTicker) Stop() {
t.done <- true
}
Loading
Loading