github.com/kotalco/kotal@v0.3.0/clients/ethereum/geth_client_test.go (about)

     1  package ethereum
     2  
     3  import (
     4  	"fmt"
     5  
     6  	ethereumv1alpha1 "github.com/kotalco/kotal/apis/ethereum/v1alpha1"
     7  	sharedAPI "github.com/kotalco/kotal/apis/shared"
     8  	"github.com/kotalco/kotal/controllers/shared"
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  )
    13  
    14  var _ = Describe("Geth Client", func() {
    15  
    16  	enode := ethereumv1alpha1.Enode("enode://2281549869465d98e90cebc45e1d6834a01465a990add7bcf07a49287e7e66b50ca27f9c70a46190cef7ad746dd5d5b6b9dfee0c9954104c8e9bd0d42758ec58@10.5.0.2:30300")
    17  	coinbase := "0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c"
    18  
    19  	Context("general", func() {
    20  		node := &ethereumv1alpha1.Node{
    21  			ObjectMeta: metav1.ObjectMeta{
    22  				Name: "gneral",
    23  			},
    24  			Spec: ethereumv1alpha1.NodeSpec{
    25  				Client: ethereumv1alpha1.GethClient,
    26  				StaticNodes: []ethereumv1alpha1.Enode{
    27  					enode,
    28  				},
    29  			},
    30  		}
    31  		client, _ := NewClient(node)
    32  
    33  		It("should return correct home directory", func() {
    34  			Expect(client.HomeDir()).To(Equal(GethHomeDir))
    35  		})
    36  
    37  		It("should encode static nodes correctly", func() {
    38  
    39  			Expect(client.EncodeStaticNodes()).To(Equal(fmt.Sprintf("[Node.P2P]\nStaticNodes = [\"%s\"]", string(enode))))
    40  		})
    41  	})
    42  
    43  	Context("Joining mainnet", func() {
    44  		node := &ethereumv1alpha1.Node{
    45  			ObjectMeta: metav1.ObjectMeta{
    46  				Name: "geth-mainnet-node",
    47  			},
    48  			Spec: ethereumv1alpha1.NodeSpec{
    49  				Network:                  ethereumv1alpha1.MainNetwork,
    50  				Client:                   ethereumv1alpha1.GethClient,
    51  				Bootnodes:                []ethereumv1alpha1.Enode{enode},
    52  				NodePrivateKeySecretName: "geth-mainnet-nodekey",
    53  				StaticNodes:              []ethereumv1alpha1.Enode{enode},
    54  				P2PPort:                  3333,
    55  				SyncMode:                 ethereumv1alpha1.LightSynchronization,
    56  				Logging:                  sharedAPI.WarnLogs,
    57  				Hosts:                    []string{"whitelisted.host.com"},
    58  				CORSDomains:              []string{"allowed.domain.com"},
    59  				RPC:                      true,
    60  				RPCPort:                  8888,
    61  				RPCAPI: []ethereumv1alpha1.API{
    62  					ethereumv1alpha1.NetworkAPI,
    63  					ethereumv1alpha1.AdminAPI,
    64  					ethereumv1alpha1.DebugAPI,
    65  				},
    66  				Engine:        true,
    67  				EnginePort:    8552,
    68  				JWTSecretName: "jwt-secret",
    69  				WS:            true,
    70  				WSPort:        7777,
    71  				WSAPI: []ethereumv1alpha1.API{
    72  					ethereumv1alpha1.ETHAPI,
    73  					ethereumv1alpha1.TransactionPoolAPI,
    74  				},
    75  				GraphQL:     true,
    76  				GraphQLPort: 9999,
    77  			},
    78  		}
    79  		node.Default()
    80  
    81  		It("should generate correct arguments", func() {
    82  
    83  			client, err := NewClient(node)
    84  
    85  			Expect(err).To(BeNil())
    86  			Expect(client.Args()).To(ContainElements(
    87  				GethDataDir,
    88  				shared.PathData(client.HomeDir()),
    89  				GethDisableIPC,
    90  				fmt.Sprintf("--%s", ethereumv1alpha1.MainNetwork),
    91  				GethLogging,
    92  				"2", // warn logs
    93  				GethNodeKey,
    94  				fmt.Sprintf("%s/nodekey", shared.PathSecrets(client.HomeDir())),
    95  				GethConfig,
    96  				fmt.Sprintf("%s/config.toml", shared.PathConfig(client.HomeDir())),
    97  				GethBootnodes,
    98  				string(enode),
    99  				GethP2PPort,
   100  				"3333",
   101  				GethSyncMode,
   102  				string(ethereumv1alpha1.LightSynchronization),
   103  				GethRPCHTTPEnabled,
   104  				GethRPCHTTPHost,
   105  				"0.0.0.0",
   106  				GethRPCHTTPPort,
   107  				"8888",
   108  				GethRPCHTTPAPI,
   109  				"net,admin,debug",
   110  				GethRPCWSEnabled,
   111  				GethRPCWSHost,
   112  				"0.0.0.0",
   113  				GethAuthRPCAddress,
   114  				"0.0.0.0",
   115  				GethAuthRPCPort,
   116  				"8552",
   117  				GethAuthRPCHosts,
   118  				"whitelisted.host.com",
   119  				GethAuthRPCJwtSecret,
   120  				fmt.Sprintf("%s/jwt.secret", shared.PathSecrets(client.HomeDir())),
   121  				GethRPCWSPort,
   122  				"7777",
   123  				GethRPCWSAPI,
   124  				"eth,txpool",
   125  				GethGraphQLHTTPEnabled,
   126  				GethRPCHostWhitelist,
   127  				"whitelisted.host.com",
   128  				GethGraphQLHostWhitelist,
   129  				"whitelisted.host.com",
   130  				GethRPCHTTPCorsOrigins,
   131  				"allowed.domain.com",
   132  				GethGraphQLHTTPCorsOrigins,
   133  				"allowed.domain.com",
   134  				GethWSOrigins,
   135  				"allowed.domain.com",
   136  			))
   137  		})
   138  	})
   139  
   140  	Context("miner in private PoW network", func() {
   141  		node := &ethereumv1alpha1.Node{
   142  			ObjectMeta: metav1.ObjectMeta{
   143  				Name: "geth-pow-node",
   144  			},
   145  			Spec: ethereumv1alpha1.NodeSpec{
   146  				Genesis: &ethereumv1alpha1.Genesis{
   147  					ChainID:   12345,
   148  					NetworkID: 12345,
   149  					Ethash:    &ethereumv1alpha1.Ethash{},
   150  				},
   151  				Client:   ethereumv1alpha1.GethClient,
   152  				Miner:    true,
   153  				Coinbase: sharedAPI.EthereumAddress(coinbase),
   154  				Import: &ethereumv1alpha1.ImportedAccount{
   155  					PrivateKeySecretName: "geth-pow-account-key",
   156  					PasswordSecretName:   "geth-pow-account-password",
   157  				},
   158  			},
   159  		}
   160  		node.Default()
   161  
   162  		It("should generate correct arguments", func() {
   163  
   164  			client, err := NewClient(node)
   165  
   166  			Expect(err).To(BeNil())
   167  			Expect(client.Args()).To(ContainElements(
   168  				GethMinerEnabled,
   169  				GethMinerCoinbase,
   170  				coinbase,
   171  				GethUnlock,
   172  				coinbase,
   173  				GethPassword,
   174  				fmt.Sprintf("%s/account.password", shared.PathSecrets(client.HomeDir())),
   175  				GethNetworkID,
   176  				"12345",
   177  				GethNoDiscovery,
   178  			))
   179  		})
   180  
   181  	})
   182  
   183  	Context("signer in private PoA network", func() {
   184  		node := &ethereumv1alpha1.Node{
   185  			ObjectMeta: metav1.ObjectMeta{
   186  				Name: "geth-poa-node",
   187  			},
   188  			Spec: ethereumv1alpha1.NodeSpec{
   189  				Genesis: &ethereumv1alpha1.Genesis{
   190  					ChainID:   12345,
   191  					NetworkID: 12345,
   192  					Clique: &ethereumv1alpha1.Clique{
   193  						Signers: []sharedAPI.EthereumAddress{
   194  							"0xcF2C3fB8F36A863FD1A8c72E2473f81744B4CA6C",
   195  							"0x1990E5760d9f8Ae0ec55dF8B0819C77e59846Ff2",
   196  							"0xB87c1c66b36D98D1A74a9875EbA12c001e0bcEda",
   197  						},
   198  					},
   199  				},
   200  				Client:   ethereumv1alpha1.GethClient,
   201  				SyncMode: ethereumv1alpha1.FullSynchronization,
   202  				Miner:    true,
   203  				Coinbase: sharedAPI.EthereumAddress(coinbase),
   204  				Import: &ethereumv1alpha1.ImportedAccount{
   205  					PrivateKeySecretName: "geth-poa-account-key",
   206  					PasswordSecretName:   "geth-poa-account-password",
   207  				},
   208  			},
   209  		}
   210  		node.Default()
   211  
   212  		It("should generate correct arguments", func() {
   213  
   214  			client, err := NewClient(node)
   215  
   216  			Expect(err).To(BeNil())
   217  			Expect(client.Args()).To(ContainElements(
   218  				GethSyncMode,
   219  				string(node.Spec.SyncMode),
   220  				GethCachePreImages,
   221  				GethHistoryTxs,
   222  				"0",
   223  				GethMinerEnabled,
   224  				GethMinerCoinbase,
   225  				coinbase,
   226  				GethUnlock,
   227  				coinbase,
   228  				GethPassword,
   229  				fmt.Sprintf("%s/account.password", shared.PathSecrets(client.HomeDir())),
   230  				GethNetworkID,
   231  				"12345",
   232  				GethNoDiscovery,
   233  			))
   234  		})
   235  
   236  	})
   237  
   238  })