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

Local state using remote registers #562

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
tests/db-test
tests/web3js/node_modules
tests/web3js/package-lock.json
tests/e2e-network/node_modules
tests/e2e-network/package-lock.json
db
Expand Down
6 changes: 3 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"github.com/onflow/flow-evm-gateway/metrics"
"github.com/onflow/flow-evm-gateway/models"
errs "github.com/onflow/flow-evm-gateway/models/errors"
"github.com/onflow/flow-evm-gateway/services/evm"
"github.com/onflow/flow-evm-gateway/services/logs"
"github.com/onflow/flow-evm-gateway/services/requester"
"github.com/onflow/flow-evm-gateway/storage"
)

Expand Down Expand Up @@ -83,7 +83,7 @@ func SupportedAPIs(
type BlockChainAPI struct {
logger zerolog.Logger
config *config.Config
evm requester.Requester
evm evm.EVMClient
blocks storage.BlockIndexer
transactions storage.TransactionIndexer
receipts storage.ReceiptIndexer
Expand All @@ -96,7 +96,7 @@ type BlockChainAPI struct {
func NewBlockChainAPI(
logger zerolog.Logger,
config *config.Config,
evm requester.Requester,
evm evm.EVMClient,
blocks storage.BlockIndexer,
transactions storage.TransactionIndexer,
receipts storage.ReceiptIndexer,
Expand Down
19 changes: 10 additions & 9 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/onflow/flow-evm-gateway/metrics"
"github.com/onflow/flow-evm-gateway/models"
errs "github.com/onflow/flow-evm-gateway/models/errors"
"github.com/onflow/flow-evm-gateway/services/evm"
"github.com/onflow/flow-evm-gateway/services/ingestion"
"github.com/onflow/flow-evm-gateway/services/requester"
"github.com/onflow/flow-evm-gateway/services/traces"
"github.com/onflow/flow-evm-gateway/storage"
"github.com/onflow/flow-evm-gateway/storage/pebble"
Expand All @@ -44,7 +44,7 @@ type Publishers struct {
type Bootstrap struct {
logger zerolog.Logger
config *config.Config
client *requester.CrossSporkClient
client *evm.CrossSporkClient
storages *Storages
publishers *Publishers
collector metrics.Collector
Expand Down Expand Up @@ -193,9 +193,9 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error {
case b.config.COAKey != nil:
signer, err = crypto.NewInMemorySigner(b.config.COAKey, crypto.SHA3_256)
case b.config.COAKeys != nil:
signer, err = requester.NewKeyRotationSigner(b.config.COAKeys, crypto.SHA3_256)
signer, err = evm.NewKeyRotationSigner(b.config.COAKeys, crypto.SHA3_256)
case len(b.config.COACloudKMSKeys) > 0:
signer, err = requester.NewKMSKeyRotationSigner(
signer, err = evm.NewKMSKeyRotationSigner(
ctx,
b.config.COACloudKMSKeys,
b.logger,
Expand All @@ -208,14 +208,15 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error {
}

// create transaction pool
txPool := requester.NewTxPool(b.client, b.publishers.Transaction, b.logger)
txPool := evm.NewTxPool(b.client, b.publishers.Transaction, b.logger)

evm, err := requester.NewEVM(
evm, err := evm.NewEVM(
b.client,
b.config,
signer,
b.logger,
b.storages.Blocks,
b.storages.Receipts,
txPool,
b.collector,
)
Expand Down Expand Up @@ -393,7 +394,7 @@ func startEngine(
}

// setupCrossSporkClient sets up a cross-spork AN client.
func setupCrossSporkClient(config *config.Config, logger zerolog.Logger) (*requester.CrossSporkClient, error) {
func setupCrossSporkClient(config *config.Config, logger zerolog.Logger) (*evm.CrossSporkClient, error) {
// create access client with cross-spork capabilities
currentSporkClient, err := grpc.NewClient(
config.AccessNodeHost,
Expand All @@ -419,7 +420,7 @@ func setupCrossSporkClient(config *config.Config, logger zerolog.Logger) (*reque
}

// initialize cross spork client to the access nodes
client, err := requester.NewCrossSporkClient(
client, err := evm.NewCrossSporkClient(
currentSporkClient,
pastSporkClients,
logger,
Expand All @@ -436,7 +437,7 @@ func setupCrossSporkClient(config *config.Config, logger zerolog.Logger) (*reque
// in case such a height doesn't already exist in the database.
func setupStorage(
config *config.Config,
client *requester.CrossSporkClient,
client *evm.CrossSporkClient,
logger zerolog.Logger,
) (*Storages, error) {
// create pebble storage from the provided database root directory
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ require (
github.com/cockroachdb/pebble v1.1.1
github.com/goccy/go-json v0.10.2
github.com/google/uuid v1.6.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/onflow/atree v0.8.0-rc.6
github.com/onflow/cadence v1.0.0-preview.52
github.com/onflow/flow-go v0.37.10
github.com/onflow/flow-go v0.37.10-util-ensure-checkpoint-exists.0.20240914104351-c2d9833c3357
github.com/onflow/flow-go-sdk v1.0.0-preview.56
github.com/onflow/flow/protobuf/go/flow v0.4.6
github.com/onflow/flow/protobuf/go/flow v0.4.7
github.com/onflow/go-ethereum v1.14.7
github.com/prometheus/client_golang v1.18.0
github.com/rs/cors v1.8.0
Expand Down Expand Up @@ -89,6 +88,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.3.0 // indirect
Expand Down Expand Up @@ -142,7 +142,6 @@ require (
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 // indirect
github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect
github.com/onflow/sdks v0.6.0-preview.1 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.18.1 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
Expand Down
14 changes: 9 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,10 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:Iju5GlWwrvL6UBg4zJJt3btmonfrMlCDdsejg4CZE7c=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 h1:pRhl55Yx1eC7BZ1N+BBWwnKaMyD8uC+34TLdndZMAKk=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0/go.mod h1:XKMd7iuf/RGPSMJ/U4HP0zS2Z9Fh8Ps9a+6X26m/tmI=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
Expand Down Expand Up @@ -1862,8 +1865,8 @@ github.com/onflow/flow-ft/lib/go/contracts v1.0.0 h1:mToacZ5NWqtlWwk/7RgIl/jeKB/
github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A=
github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs=
github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE=
github.com/onflow/flow-go v0.37.10 h1:Nz2Gp63+0ubb9FuQaEZgCsXNXM5WsXq/j0ukC74N5Vw=
github.com/onflow/flow-go v0.37.10/go.mod h1:bfOCsCk0v1J93vXd+zrYkCmRIVOaL9oAXvNFWgVOujE=
github.com/onflow/flow-go v0.37.10-util-ensure-checkpoint-exists.0.20240914104351-c2d9833c3357 h1:7gJ5RVKZEsUqPSKglpMXUBn+hceJ1cd/PsmLVsd5uzQ=
github.com/onflow/flow-go v0.37.10-util-ensure-checkpoint-exists.0.20240914104351-c2d9833c3357/go.mod h1:Gdqw1ptnAUuB0izif88PWMK8abe655Hr8iEkXXuUJl4=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-preview.56 h1:ZnFznUXI1V8iZ+cKxoJRIeQwJTHItriKpnoKf8hFFso=
github.com/onflow/flow-go-sdk v1.0.0-preview.56/go.mod h1:rBRNboXaTprn7M0MeO6/R1bxNpctbrx66I2FLp0V6fM=
Expand All @@ -1872,8 +1875,8 @@ github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkp
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
github.com/onflow/flow-nft/lib/go/templates v1.2.0/go.mod h1:p+2hRvtjLUR3MW1NsoJe5Gqgr2eeH49QB6+s6ze00w0=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231121210617-52ee94b830c2/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.6 h1:KE/CsRVfyG5lGBtm1aNcjojMciQyS5GfPF3ixOWRfi0=
github.com/onflow/flow/protobuf/go/flow v0.4.6/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.7 h1:iP6DFx4wZ3ETORsyeqzHu7neFT3d1CXF6wdK+AOOjmc=
github.com/onflow/flow/protobuf/go/flow v0.4.7/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/go-ethereum v1.14.7 h1:gg3awYqI02e3AypRdpJKEvNTJ6kz/OhAqRti0h54Wlc=
github.com/onflow/go-ethereum v1.14.7/go.mod h1:zV14QLrXyYu5ucvcwHUA0r6UaqveqbXaehAVQJlSW+I=
github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU=
Expand All @@ -1886,8 +1889,9 @@ github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.13.0/go.mod h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1lskyM0=
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
Expand Down
47 changes: 7 additions & 40 deletions models/block.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"fmt"
"math/big"

"github.com/onflow/cadence"
Expand Down Expand Up @@ -73,11 +72,13 @@ func (b *Block) Hash() (gethCommon.Hash, error) {
func decodeBlockEvent(event cadence.Event) (*Block, error) {
payload, err := events.DecodeBlockEventPayload(event)
if err != nil {
if block, err := decodeLegacyBlockEvent(event); err == nil {
return block, nil
}
return nil, err
}

return nil, fmt.Errorf("failed to cadence decode block [%s]: %w", event.String(), err)
var fixedHash *string
if payload.PrevRandao == gethCommon.HexToHash("0x0") {
hash := payload.Hash.String()
fixedHash = &hash
}
Comment on lines +79 to 82
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-Peter @ramtinms I don't understand this part. Can you help me out?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janezpodhostnik This is a part which is necessary for calculating the hash of blocks, when there are changes to its format (e.g. addition/removal of fields). The hash calculation can only be made on demand, with a function (https://github.com/onflow/flow-go/blob/master/fvm/evm/types/block.go#L54-L63). When testnet was launched, the block format had certain fields. After some time, we made an HCU to introduce a new field, PrevRandao. This naturally caused the Hash function to return different values for past blocks. And this part is basically an approach to mitigate that. We fix the block hash, to the value that was emitted in the EVM.BlockExecuted event.


return &Block{
Expand All @@ -91,41 +92,7 @@ func decodeBlockEvent(event cadence.Event) (*Block, error) {
TotalGasUsed: payload.TotalGasUsed,
PrevRandao: payload.PrevRandao,
},
}, nil
}

// todo remove this after updated in flow-go
type blockEventPayloadV0 struct {
Height uint64 `cadence:"height"`
Hash gethCommon.Hash `cadence:"hash"`
Timestamp uint64 `cadence:"timestamp"`
TotalSupply cadence.Int `cadence:"totalSupply"`
TotalGasUsed uint64 `cadence:"totalGasUsed"`
ParentBlockHash gethCommon.Hash `cadence:"parentHash"`
ReceiptRoot gethCommon.Hash `cadence:"receiptRoot"`
TransactionHashRoot gethCommon.Hash `cadence:"transactionHashRoot"`
}

// DecodeBlockEventPayload decodes Cadence event into block event payload.
func decodeLegacyBlockEvent(event cadence.Event) (*Block, error) {
var block blockEventPayloadV0
err := cadence.DecodeFields(event, &block)
if err != nil {
return nil, err
}

h := block.Hash.String()
return &Block{
Block: &types.Block{
ParentBlockHash: block.ParentBlockHash,
Height: block.Height,
Timestamp: block.Timestamp,
TotalSupply: block.TotalSupply.Value,
ReceiptRoot: block.ReceiptRoot,
TransactionHashRoot: block.TransactionHashRoot,
TotalGasUsed: block.TotalGasUsed,
},
FixedHash: &h,
FixedHash: fixedHash,
}, nil
}

Expand Down
7 changes: 2 additions & 5 deletions models/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func Test_DecodingLegacyBlockExecutedEvent(t *testing.T) {
hashToCadenceArrayValue(block.TransactionHashRoot),
}).WithType(eventType)

b, err := decodeLegacyBlockEvent(legacyEvent)
b, err := decodeBlockEvent(legacyEvent)
require.NoError(t, err)

require.Equal(t, block.ParentBlockHash, b.ParentBlockHash)
Expand All @@ -160,10 +160,7 @@ func Test_DecodingLegacyBlockExecutedEvent(t *testing.T) {
dech, err := b.Hash()
require.NoError(t, err)
require.Equal(t, bh, dech)

b2, err := decodeBlockEvent(legacyEvent)
require.NoError(t, err)
require.Equal(t, b, b2)
require.NotNil(t, b.FixedHash)
}

func Test_Hash(t *testing.T) {
Expand Down
File renamed without changes.
Loading
Loading