Skip to content

Commit

Permalink
Merge branch 'master' into janez/change-key-index-to-uint32
Browse files Browse the repository at this point in the history
  • Loading branch information
janezpodhostnik committed Jul 17, 2024
2 parents afefbf5 + 08e28b9 commit d5d2e60
Show file tree
Hide file tree
Showing 42 changed files with 1,050 additions and 640 deletions.
6 changes: 4 additions & 2 deletions engine/execution/computation/computer/computer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {

// include all fees. System chunk should ignore them
contextOptions := []fvm.Option{
fvm.WithEVMEnabled(true),
fvm.WithTransactionFeesEnabled(true),
fvm.WithAccountStorageLimit(true),
fvm.WithBlocks(&environment.NoopBlockFinder{}),
Expand Down Expand Up @@ -1223,6 +1224,7 @@ func (f *FixedAddressGenerator) AddressCount() uint64 {
func Test_ExecutingSystemCollection(t *testing.T) {

execCtx := fvm.NewContext(
fvm.WithEVMEnabled(true),
fvm.WithChain(flow.Localnet.Chain()),
fvm.WithBlocks(&environment.NoopBlockFinder{}),
)
Expand All @@ -1245,8 +1247,8 @@ func Test_ExecutingSystemCollection(t *testing.T) {

noopCollector := metrics.NewNoopCollector()

expectedNumberOfEvents := 3
expectedEventSize := 1497
expectedNumberOfEvents := 4
expectedEventSize := 1961

// bootstrapping does not cache programs
expectedCachedPrograms := 0
Expand Down
1 change: 1 addition & 0 deletions engine/execution/computation/programs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func TestPrograms_TestBlockForks(t *testing.T) {
chain := flow.Emulator.Chain()
vm := fvm.NewVirtualMachine()
execCtx := fvm.NewContext(
fvm.WithEVMEnabled(true),
fvm.WithBlockHeader(block.Header),
fvm.WithBlocks(blockProvider{map[uint64]*flow.Block{0: &block}}),
fvm.WithChain(chain))
Expand Down
2 changes: 1 addition & 1 deletion engine/execution/state/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBootstrapLedger(t *testing.T) {
}

func TestBootstrapLedger_ZeroTokenSupply(t *testing.T) {
expectedStateCommitmentBytes, _ := hex.DecodeString("8084ec7253e5de8fa98c501f4acbcf448aafea3d9d11760d6982bb813926f4ef")
expectedStateCommitmentBytes, _ := hex.DecodeString("a738c835be7196f62900595d186557ba56d5ee4221b0a108782967c5d1d110d7")
expectedStateCommitment, err := flow.ToStateCommitment(expectedStateCommitmentBytes)
require.NoError(t, err)

Expand Down
6 changes: 6 additions & 0 deletions fvm/blueprints/scripts/systemChunkTransactionTemplate.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import FlowEpoch from "FlowEpoch"
import NodeVersionBeacon from "NodeVersionBeacon"
import RandomBeaconHistory from "RandomBeaconHistory"
import EVM from "EVM"

transaction {
prepare(serviceAccount: auth(BorrowValue) &Account) {
Expand All @@ -17,5 +18,10 @@ transaction {
.borrow<&RandomBeaconHistory.Heartbeat>(from: RandomBeaconHistory.HeartbeatStoragePath)
?? panic("Couldn't borrow RandomBeaconHistory.Heartbeat Resource")
randomBeaconHistoryHeartbeat.heartbeat(randomSourceHistory: randomSourceHistory())

let evmHeartbeat = serviceAccount.storage.borrow<&EVM.Heartbeat>(from: /storage/EVMHeartbeat)
if evmHeartbeat != nil { // skip if not available
evmHeartbeat!.heartbeat()
}
}
}
27 changes: 21 additions & 6 deletions fvm/blueprints/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blueprints

import (
_ "embed"
"strings"

"github.com/onflow/flow-core-contracts/lib/go/templates"

Expand All @@ -17,17 +18,31 @@ const SystemChunkTransactionGasLimit = 100_000_000
//go:embed scripts/systemChunkTransactionTemplate.cdc
var systemChunkTransactionTemplate string

// TODO: when the EVM contract is moved to the flow-core-contracts, we can
// just directly use the replace address functionality of the templates package.

var placeholderEVMAddress = "\"EVM\""

func prepareSystemContractCode(chainID flow.ChainID) string {
sc := systemcontracts.SystemContractsForChain(chainID)
code := templates.ReplaceAddresses(
systemChunkTransactionTemplate,
sc.AsTemplateEnv(),
)
code = strings.ReplaceAll(
code,
placeholderEVMAddress,
sc.EVMContract.Address.HexWithPrefix(),
)
return code
}

// SystemChunkTransaction creates and returns the transaction corresponding to the
// system chunk for the given chain.
func SystemChunkTransaction(chain flow.Chain) (*flow.TransactionBody, error) {
contracts := systemcontracts.SystemContractsForChain(chain.ChainID())

tx := flow.NewTransactionBody().
SetScript(
[]byte(templates.ReplaceAddresses(
systemChunkTransactionTemplate,
contracts.AsTemplateEnv(),
)),
[]byte(prepareSystemContractCode(chain.ChainID())),
).
// The heartbeat resources needed by the system tx have are on the service account,
// therefore, the service account is the only authorizer needed.
Expand Down
8 changes: 4 additions & 4 deletions fvm/blueprints/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func TestSystemChunkTransactionHash(t *testing.T) {

// this is formatted in a way that the resulting error message is easy to copy-paste into the test.
expectedHashes := []chainHash{
{chainId: "flow-mainnet", expectedHash: "0a7ea89ad32d79a30b91b4c1202230a1e29310e1b92e01c76d036d2e3839159b"},
{chainId: "flow-testnet", expectedHash: "368434cb7c792c3c35647f30aa90aae5798a45efcf2ff6abb7123b70c1e7850c"},
{chainId: "flow-previewnet", expectedHash: "e90268cb6e8385d9eb50f2956f47c1c5f77a7b3111de2f66756b2a48855e05ce"},
{chainId: "flow-emulator", expectedHash: "c6ccd6b805adcfaa6f9719f1dc71c831c40712977f12d82332ba23e2cb499475"},
{chainId: "flow-mainnet", expectedHash: "0e56f890392ad3f2a0dcc7a0e859b6d41f3c17556aa85a0f8831ae25a6537e39"},
{chainId: "flow-testnet", expectedHash: "69c922177af520e8ecaf0ba97cb174a3ebbd03de6d5b5d1f6b6c043a9638dba3"},
{chainId: "flow-previewnet", expectedHash: "2ec3b6b7e7d70a651600eb80f775cb57613a0a168c847b70040c469f09066a55"},
{chainId: "flow-emulator", expectedHash: "7dcc0daaecebc7be33b068ed5c8da0620c89fd896abfc498fc0cc32a261aab1f"},
}

var actualHashes []chainHash
Expand Down
8 changes: 4 additions & 4 deletions fvm/evm/debug/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ const (

type EVMTracer interface {
WithBlockID(identifier flow.Identifier)
TxTracer() tracers.Tracer
TxTracer() *tracers.Tracer
Collect(txID gethCommon.Hash)
}

var _ EVMTracer = &CallTracer{}

type CallTracer struct {
logger zerolog.Logger
tracer tracers.Tracer
tracer *tracers.Tracer
uploader Uploader
blockID flow.Identifier
}
Expand All @@ -48,7 +48,7 @@ func NewEVMCallTracer(uploader Uploader, logger zerolog.Logger) (*CallTracer, er
}, nil
}

func (t *CallTracer) TxTracer() tracers.Tracer {
func (t *CallTracer) TxTracer() *tracers.Tracer {
return t.tracer
}

Expand Down Expand Up @@ -102,7 +102,7 @@ var _ EVMTracer = &nopTracer{}

type nopTracer struct{}

func (n nopTracer) TxTracer() tracers.Tracer {
func (n nopTracer) TxTracer() *tracers.Tracer {
return nil
}

Expand Down
15 changes: 10 additions & 5 deletions fvm/evm/debug/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/core/vm"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -34,15 +35,19 @@ func Test_CallTracer(t *testing.T) {

from := gethCommon.HexToAddress("0x01")
to := gethCommon.HexToAddress("0x02")
nonce := uint64(10)
data := []byte{0x02, 0x04}
amount := big.NewInt(1)

tr := tracer.TxTracer()
require.NotNil(t, tr)

tr.CaptureStart(nil, from, to, true, []byte{0x01, 0x02}, 10, big.NewInt(1))
tr.CaptureTxStart(100)
tr.CaptureEnter(vm.ADD, from, to, []byte{0x02, 0x04}, 20, big.NewInt(2))
tr.CaptureTxEnd(500)
tr.CaptureEnd([]byte{0x02}, 200, nil)
tr.OnEnter(0, 0, from, to, []byte{0x01, 0x02}, 10, big.NewInt(1))
tx := gethTypes.NewTransaction(nonce, to, amount, 100, big.NewInt(10), data)
tr.OnTxStart(nil, tx, from)
tr.OnEnter(1, byte(vm.ADD), from, to, data, 20, big.NewInt(2))
tr.OnTxEnd(&gethTypes.Receipt{}, nil)
tr.OnExit(0, []byte{0x02}, 200, nil, false)

res, err = tr.GetResult()
require.NoError(t, err)
Expand Down
16 changes: 13 additions & 3 deletions fvm/evm/debug/uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"cloud.google.com/go/storage"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/core/vm"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -64,9 +65,18 @@ func Test_TracerUploaderIntegration(t *testing.T) {
tr := tracer.TxTracer()
require.NotNil(t, tr)

tr.CaptureTxStart(1000)
tr.CaptureEnter(vm.ADD, gethCommon.HexToAddress("0x01"), gethCommon.HexToAddress("0x02"), []byte{0x01}, 10, big.NewInt(2))
tr.CaptureTxEnd(500)
from := gethCommon.HexToAddress("0x01")
to := gethCommon.HexToAddress("0x02")
nonce := uint64(10)
gas := uint64(100)
gasPrice := big.NewInt(1)
value := big.NewInt(2)
input := []byte{0x01}
tx := gethTypes.NewTransaction(nonce, to, value, gas, gasPrice, input)

tr.OnTxStart(nil, tx, from)
tr.OnEnter(0, byte(vm.ADD), from, to, input, gas, value)
tr.OnTxEnd(&gethTypes.Receipt{}, nil)

traces, err := tr.GetResult()
require.NoError(t, err)
Expand Down
7 changes: 5 additions & 2 deletions fvm/evm/emulator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var DefaultChainConfig = &gethParams.ChainConfig{
ShanghaiTime: &zero, // already on Shanghai
CancunTime: &zero, // already on Cancun
PragueTime: nil, // not on Prague
VerkleTime: nil, // not on Verkle
}

// Default config supports the dynamic fee structure (EIP-1559)
Expand Down Expand Up @@ -213,9 +214,11 @@ func WithRandom(rand *gethCommon.Hash) Option {
}

// WithTransactionTracer sets a transaction tracer
func WithTransactionTracer(tracer tracers.Tracer) Option {
func WithTransactionTracer(tracer *tracers.Tracer) Option {
return func(c *Config) *Config {
c.EVMConfig.Tracer = tracer
if tracer != nil {
c.EVMConfig.Tracer = tracer.Hooks
}
return c
}
}
Loading

0 comments on commit d5d2e60

Please sign in to comment.