From 6e432a1a46e69fe8f1af188bca81a1d22eefb184 Mon Sep 17 00:00:00 2001 From: Matt <98158711+BedrockSquirrel@users.noreply.github.com> Date: Fri, 24 May 2024 10:41:03 +0100 Subject: [PATCH] Gateway: add net_version support and test (#1934) --- go/enclave/components/batch_executor.go | 2 +- go/enclave/crosschain/common.go | 2 +- go/enclave/genesis/testnet_genesis.go | 6 ++-- go/enclave/nodetype/common.go | 2 +- go/enclave/nodetype/sequencer.go | 1 + .../networktest/tests/gateway/gateway_test.go | 31 +++++++++++++++++++ integration/networktest/userwallet/gateway.go | 4 +++ tools/walletextension/rpcapi/net_api.go | 17 ++++++++++ .../walletextension_container.go | 3 ++ 9 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 tools/walletextension/rpcapi/net_api.go diff --git a/go/enclave/components/batch_executor.go b/go/enclave/components/batch_executor.go index 77aae4e75f..cfead4334b 100644 --- a/go/enclave/components/batch_executor.go +++ b/go/enclave/components/batch_executor.go @@ -379,7 +379,7 @@ func (executor *batchExecutor) populateOutboundCrossChainData(ctx context.Contex encodedTree, err := json.Marshal(xchainTree) if err != nil { - panic(err) //todo: figure out what to do + panic(err) // todo: figure out what to do } batch.Header.CrossChainTree = encodedTree diff --git a/go/enclave/crosschain/common.go b/go/enclave/crosschain/common.go index b6ee881c89..1d64083dbb 100644 --- a/go/enclave/crosschain/common.go +++ b/go/enclave/crosschain/common.go @@ -234,7 +234,7 @@ func (ms MessageStructs) HashPacked(index int) gethcommon.Hash { }, } - //todo @siliev: err + // todo @siliev: err packed, _ := args.Pack(messageStruct.Sender, messageStruct.Sequence, messageStruct.Nonce, messageStruct.Topic, messageStruct.Payload, messageStruct.ConsistencyLevel) hash := crypto.Keccak256Hash(packed) return hash diff --git a/go/enclave/genesis/testnet_genesis.go b/go/enclave/genesis/testnet_genesis.go index 49b48c16ca..fed93c6f0d 100644 --- a/go/enclave/genesis/testnet_genesis.go +++ b/go/enclave/genesis/testnet_genesis.go @@ -9,8 +9,10 @@ import ( ) const TestnetPrefundedPK = "8dfb8083da6275ae3e4f41e3e8a8c19d028d32c9247e24530933782f2a05035b" // The genesis main account private key. -var GasBridgingKeys, _ = crypto.GenerateKey() // todo - make static -var GasWithdrawalKeys, _ = crypto.GenerateKey() // todo - make static +var ( + GasBridgingKeys, _ = crypto.GenerateKey() // todo - make static + GasWithdrawalKeys, _ = crypto.GenerateKey() // todo - make static +) var TestnetGenesis = Genesis{ Accounts: []Account{ diff --git a/go/enclave/nodetype/common.go b/go/enclave/nodetype/common.go index 197fef08d1..a99dc0f690 100644 --- a/go/enclave/nodetype/common.go +++ b/go/enclave/nodetype/common.go @@ -40,6 +40,6 @@ func ExportCrossChainData(ctx context.Context, storage storage.Storage, fromSeqN L1BlockHash: block.Hash(), L1BlockNum: big.NewInt(0).Set(block.Header().Number), CrossChainRootHashes: crossChainHashes, - } //todo: check fromSeqNo + } // todo: check fromSeqNo return bundle, nil } diff --git a/go/enclave/nodetype/sequencer.go b/go/enclave/nodetype/sequencer.go index a0283c2312..16e5117590 100644 --- a/go/enclave/nodetype/sequencer.go +++ b/go/enclave/nodetype/sequencer.go @@ -468,6 +468,7 @@ func (s *sequencer) signCrossChainBundle(bundle *common.ExtCrossChainBundle) err } return nil } + func (s *sequencer) OnL1Block(ctx context.Context, block *types.Block, result *components.BlockIngestionType) error { // nothing to do return nil diff --git a/integration/networktest/tests/gateway/gateway_test.go b/integration/networktest/tests/gateway/gateway_test.go index eb8d5e9c13..6a2bc1985d 100644 --- a/integration/networktest/tests/gateway/gateway_test.go +++ b/integration/networktest/tests/gateway/gateway_test.go @@ -1,12 +1,15 @@ package gateway import ( + "context" + "fmt" "math/big" "testing" "github.com/ten-protocol/go-ten/integration/networktest" "github.com/ten-protocol/go-ten/integration/networktest/actions" "github.com/ten-protocol/go-ten/integration/networktest/env" + "github.com/ten-protocol/go-ten/integration/networktest/userwallet" "github.com/ten-protocol/go-ten/integration/simulation/devnetwork" ) @@ -37,6 +40,34 @@ func TestGatewayHappyPath(t *testing.T) { &actions.VerifyBalanceAfterTest{UserID: 1, ExpectedBalance: _transferAmount}, &actions.VerifyBalanceDiffAfterTest{UserID: 0, Snapshot: actions.SnapAfterAllocation, ExpectedDiff: big.NewInt(0).Neg(_transferAmount)}, + + // test net_version works through the gateway + actions.VerifyOnlyAction(func(ctx context.Context, network networktest.NetworkConnector) error { + user, err := actions.FetchTestUser(ctx, 0) + if err != nil { + return err + } + // verify user is a gateway user + gwUser, ok := user.(*userwallet.GatewayUser) + if !ok { + return fmt.Errorf("user is not a gateway user") + } + ethClient := gwUser.Client() + rpcClient := ethClient.Client() + // check net_version response + var result string + err = rpcClient.CallContext(ctx, &result, "net_version") + if err != nil { + return fmt.Errorf("failed to get net_version: %w", err) + } + fmt.Println("net_version response:", result) + expectedResult := "443" + if result != expectedResult { + return fmt.Errorf("expected net_version to be %s but got %s", expectedResult, result) + } + + return nil + }), ), ) } diff --git a/integration/networktest/userwallet/gateway.go b/integration/networktest/userwallet/gateway.go index 03fa361a9d..9408c4c06c 100644 --- a/integration/networktest/userwallet/gateway.go +++ b/integration/networktest/userwallet/gateway.go @@ -114,6 +114,10 @@ func (g *GatewayUser) Wallet() wallet.Wallet { return g.wal } +func (g *GatewayUser) Client() *ethclient.Client { + return g.client +} + func (g *GatewayUser) WSClient() (*ethclient.Client, error) { if g.wsClient == nil { var err error diff --git a/tools/walletextension/rpcapi/net_api.go b/tools/walletextension/rpcapi/net_api.go new file mode 100644 index 0000000000..855c5b7a42 --- /dev/null +++ b/tools/walletextension/rpcapi/net_api.go @@ -0,0 +1,17 @@ +package rpcapi + +import ( + "context" +) + +type NetAPI struct { + we *Services +} + +func NewNetAPI(we *Services) *NetAPI { + return &NetAPI{we} +} + +func (api *NetAPI) Version(ctx context.Context) (*string, error) { + return UnauthenticatedTenRPCCall[string](ctx, api.we, &CacheCfg{CacheType: LongLiving}, "net_version") +} diff --git a/tools/walletextension/walletextension_container.go b/tools/walletextension/walletextension_container.go index 084044af54..96e322b08c 100644 --- a/tools/walletextension/walletextension_container.go +++ b/tools/walletextension/walletextension_container.go @@ -83,6 +83,9 @@ func NewContainerFromConfig(config wecommon.Config, logger gethlog.Logger) *Cont }, { Namespace: "eth", Service: rpcapi.NewFilterAPI(walletExt), + }, { + Namespace: "net", + Service: rpcapi.NewNetAPI(walletExt), }, })