Skip to content

Commit

Permalink
Gateway: add net_version support and test (#1934)
Browse files Browse the repository at this point in the history
  • Loading branch information
BedrockSquirrel authored May 24, 2024
1 parent 4f39832 commit 6e432a1
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/crosschain/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go/enclave/genesis/testnet_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/nodetype/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions go/enclave/nodetype/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions integration/networktest/tests/gateway/gateway_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
}),
),
)
}
4 changes: 4 additions & 0 deletions integration/networktest/userwallet/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tools/walletextension/rpcapi/net_api.go
Original file line number Diff line number Diff line change
@@ -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")
}
3 changes: 3 additions & 0 deletions tools/walletextension/walletextension_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
})

Expand Down

0 comments on commit 6e432a1

Please sign in to comment.