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

Emulator .git compatibility for --persist #91

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
45 changes: 35 additions & 10 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (conf config) GetServiceKey() ServiceKey {
return serviceKey
}

const defaultGenesisTokenSupply = "1000000000.0"
const defaultGenesisTokenSupply = "10000000000.0"
const defaultScriptGasLimit = 100000
const defaultTransactionMaxGasLimit = flowgo.DefaultMaxTransactionGasLimit

Expand Down Expand Up @@ -321,6 +321,7 @@ func NewBlockchain(opts ...Option) (*Blockchain, error) {
blocks := newBlocks(b)

b.vm, b.vmCtx, err = configureFVM(conf, blocks)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -687,7 +688,7 @@ func (b *Blockchain) GetAccount(address sdk.Address) (*sdk.Account, error) {
return nil, err
}

return &sdkAccount, err
return &sdkAccount, nil
}

// getAccount returns the account for the given address.
Expand All @@ -697,22 +698,46 @@ func (b *Blockchain) getAccount(address flowgo.Address) (*flowgo.Account, error)
return nil, err
}

view := b.storage.LedgerViewByHeight(latestBlock.Header.Height)
return b.getAccountAtBlock(address, latestBlock.Header.Height)
}

// GetAccountAtBlock returns the account for the given address at specified block height.
func (b *Blockchain) GetAccountAtBlock(address sdk.Address, blockHeight uint64) (*sdk.Account, error) {
b.mu.RLock()
defer b.mu.RUnlock()

flowAddress := sdkconvert.SDKAddressToFlow(address)

account, err := b.getAccountAtBlock(flowAddress, blockHeight)
if err != nil {
return nil, err
}

sdkAccount, err := sdkconvert.FlowAccountToSDK(*account)
if err != nil {
return nil, err
}

return &sdkAccount, nil
}

// GetAccountAtBlock returns the account for the given address at specified block height.
func (b *Blockchain) getAccountAtBlock(address flowgo.Address, blockHeight uint64) (*flowgo.Account, error) {

account, err := b.vm.GetAccount(
b.vmCtx,
address,
b.storage.LedgerViewByHeight(blockHeight),
programs.NewEmptyPrograms(),
)

programs := programs.NewEmptyPrograms()
account, err := b.vm.GetAccount(b.vmCtx, address, view, programs)
if fvmerrors.IsAccountNotFoundError(err) {
return nil, &AccountNotFoundError{Address: address}
}

return account, nil
}

// TODO: Implement
func (b *Blockchain) GetAccountAtBlock(address sdk.Address, blockHeight uint64) (*sdk.Account, error) {
panic("not implemented")
}

// GetEventsByHeight returns the events in the block at the given height, optionally filtered by type.
func (b *Blockchain) GetEventsByHeight(blockHeight uint64, eventType string) ([]sdk.Event, error) {
flowEvents, err := b.storage.EventsByHeight(blockHeight, eventType)
Expand Down
32 changes: 23 additions & 9 deletions cmd/emulator/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/hex"
"fmt"
"os"
"strings"
"time"

"github.com/onflow/cadence"
Expand All @@ -39,7 +40,10 @@ import (
type Config struct {
Port int `default:"3569" flag:"port,p" info:"port to run RPC server"`
HTTPPort int `default:"8080" flag:"http-port" info:"port to run HTTP server"`
WalletPort int `default:"3000" flag:"wallet-port" info:"port to run Dev Wallet server"`
DevWalletEnabled bool `default:"false" flag:"dev-wallet" info:"enable local Dev Wallet server"`
Verbose bool `default:"false" flag:"verbose,v" info:"enable verbose logging"`
LogFormat string `default:"text" flag:"log-format" info:"logging output format. Valid values (text, JSON)"`
BlockTime time.Duration `flag:"block-time,b" info:"time between sealed blocks, e.g. '300ms', '-1.5h' or '2h45m'. Valid units are 'ns', 'us' (or 'µs'), 'ms', 's', 'm', 'h'"`
ServicePrivateKey string `flag:"service-priv-key" info:"service account private key"`
ServicePublicKey string `flag:"service-pub-key" info:"service account public key"`
Expand Down Expand Up @@ -89,6 +93,8 @@ func Cmd(getServiceKey serviceKeyFunc) *cobra.Command {
serviceKeySigAlgo = crypto.StringToSignatureAlgorithm(conf.ServiceKeySigAlgo)
serviceKeyHashAlgo = crypto.StringToHashAlgorithm(conf.ServiceKeyHashAlgo)

logger := initLogger()

if len(conf.ServicePublicKey) > 0 {
checkKeyAlgorithms(serviceKeySigAlgo, serviceKeyHashAlgo)

Expand Down Expand Up @@ -143,13 +149,16 @@ func Cmd(getServiceKey serviceKeyFunc) *cobra.Command {
}

serverConf := &server.Config{
GRPCPort: conf.Port,
GRPCDebug: conf.GRPCDebug,
HTTPPort: conf.HTTPPort,
GRPCPort: conf.Port,
GRPCDebug: conf.GRPCDebug,
HTTPPort: conf.HTTPPort,
WalletPort: conf.WalletPort,
DevWalletEnabled: conf.DevWalletEnabled,
// TODO: allow headers to be parsed from environment
HTTPHeaders: nil,
BlockTime: conf.BlockTime,
ServicePublicKey: servicePublicKey,
ServicePrivateKey: servicePrivateKey,
ServiceKeySigAlgo: serviceKeySigAlgo,
ServiceKeyHashAlgo: serviceKeyHashAlgo,
Persist: conf.Persist,
Expand All @@ -174,14 +183,19 @@ func Cmd(getServiceKey serviceKeyFunc) *cobra.Command {
return cmd
}

func init() {
initLogger()
}
func initLogger() *logrus.Logger {
var logger = logrus.New()

switch strings.ToLower(conf.LogFormat) {
case "json":
logger.Formatter = new(logrus.JSONFormatter)
default:
logger.Formatter = new(logrus.TextFormatter)
}

func initLogger() {
logger = logrus.New()
logger.Formatter = new(logrus.TextFormatter)
logger.Out = os.Stdout

return logger
}

func initConfig(cmd *cobra.Command) {
Expand Down
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
module github.com/onflow/flow-emulator

go 1.13
go 1.16

require (
github.com/HdrHistogram/hdrhistogram-go v1.1.0 // indirect
github.com/dgraph-io/badger/v2 v2.0.3
github.com/fxamacker/cbor/v2 v2.2.1-0.20210510192846-c3f3c69e7bc8
github.com/go-git/go-billy v4.2.0+incompatible // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git v4.7.0+incompatible // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/golang/mock v1.6.0
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/improbable-eng/grpc-web v0.12.0
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b // indirect
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/onflow/cadence v0.19.1
github.com/onflow/flow-go v0.21.3
Expand All @@ -25,4 +33,5 @@ require (
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
google.golang.org/grpc v1.38.0
gopkg.in/src-d/go-git.v4 v4.13.1 // indirect
)
Loading