github.com/amazechain/amc@v0.1.3/cmd/evmsdk/common_test.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package evmsdk
    18  
    19  import (
    20  	"crypto/rand"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"github.com/amazechain/amc/common/crypto"
    24  	"testing"
    25  )
    26  
    27  func TestGetNetInfos(t *testing.T) {
    28  	tests := []struct {
    29  		name string
    30  		want string
    31  	}{
    32  		// TODO: Add test cases.
    33  	}
    34  	for _, tt := range tests {
    35  		t.Run(tt.name, func(t *testing.T) {
    36  			if got := GetNetInfos(); got != tt.want {
    37  				t.Errorf("GetNetInfos() = %v, want %v", got, tt.want)
    38  			} else {
    39  				t.Log(got)
    40  				fmt.Println(got)
    41  			}
    42  		})
    43  	}
    44  }
    45  
    46  //func TestEmit(t *testing.T) {
    47  //	type args struct {
    48  //		jsonText string
    49  //	}
    50  //
    51  //	//settttting
    52  //	aa := Emit(`{"type":"test","val":{"app_base_path":"/Users/xan/misc"}}`)
    53  //	fmt.Println(aa)
    54  //
    55  //	// tests := []struct {
    56  //	// 	name string
    57  //	// 	args args
    58  //	// }{{
    59  //	// 	name: "t1",
    60  //	// 	args: args{
    61  //	// 		jsonText: `{"type":"start"}`,
    62  //	// 	},
    63  //	// }, {
    64  //	// 	name: "t2",
    65  //	// 	args: args{
    66  //	// 		jsonText: `{"type":"test"}`,
    67  //	// 	},
    68  //	// },
    69  //	// // 	{
    70  //	// // 		name: "t3",
    71  //	// // 		args: args{
    72  //	// // 			jsonText: `{"type":"list"}`,
    73  //	// // 		},
    74  //	// // 	},
    75  //	// }
    76  //	// for _, tt := range tests {
    77  //	// 	t.Run(tt.name, func(t *testing.T) {
    78  //	// 		got := Emit(tt.args.jsonText)
    79  //	// 		if len(got) == 0 {
    80  //	// 			t.Errorf("Emit() = %v", got)
    81  //	// 		}
    82  //	// 		t.Log(got)
    83  //	// 		fmt.Println(got)
    84  //	// 	})
    85  //	// }
    86  //
    87  //	// <-time.After(60 * time.Second)
    88  //}
    89  
    90  func TestBlsSign(t *testing.T) {
    91  	pk := make([]byte, 32)
    92  	_, err := rand.Read(pk)
    93  	if err != nil {
    94  		t.Error(err)
    95  	}
    96  
    97  	req := `
    98  {
    99  	"type":"blssign",
   100  	"val":{
   101  		"priv_key":"` + hex.EncodeToString(pk) + `",
   102  		"msg":"123123"
   103  	}
   104  }
   105  	`
   106  	resp := Emit(req)
   107  	_ = resp
   108  	fmt.Println(resp)
   109  }
   110  
   111  //func TestEvmEngine_Start(t *testing.T) {
   112  //	type fields struct {
   113  //		mu          sync.Mutex
   114  //		ctx         context.Context
   115  //		cancelFunc  context.CancelFunc
   116  //		Account     string
   117  //		AppBasePath string
   118  //		State       string
   119  //		BlockChan   chan string
   120  //		PrivKey     string
   121  //		ServerUri   string
   122  //	}
   123  //	tests := []struct {
   124  //		name    string
   125  //		fields  fields
   126  //		wantErr bool
   127  //	}{
   128  //		{
   129  //			name: "t1",
   130  //			fields: fields{
   131  //				PrivKey:   `b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAaAAAABNlY2RzYS1zaGEyLW5pc3RwMjU2AAAACG5pc3RwMjU2AAAAQQSgodcFy6hKA9enyiAzEKfpg7Rib5AFx3w33V0NsMYOAoUXtbQtFIOIIgNsTBj9ei1DdGJ4QSbCgw3w37X7oXvkAAAAqAK68x8CuvMfAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKCh1wXLqEoD16fKIDMQp+mDtGJvkAXHfDfdXQ2wxg4ChRe1tC0Ug4giA2xMGP16LUN0YnhBJsKDDfDftfuhe+QAAAAhAMhPG7U/g7k9+YWm66Yk1yDhUvwkHmLMWvV/bTtGeElWAAAACW1hY0Bib2dvbgECAwQFBg==`,
   132  //				ServerUri: "ws://127.0.0.1:20013",
   133  //				Account:   "0x01",
   134  //			},
   135  //			wantErr: false,
   136  //		},
   137  //	}
   138  //	for _, tt := range tests {
   139  //		t.Run(tt.name, func(t *testing.T) {
   140  //			e := &EvmEngine{
   141  //				mu:          tt.fields.mu,
   142  //				ctx:         tt.fields.ctx,
   143  //				cancelFunc:  tt.fields.cancelFunc,
   144  //				Account:     tt.fields.Account,
   145  //				AppBasePath: tt.fields.AppBasePath,
   146  //				EngineState: tt.fields.State,
   147  //				BlockChan:   tt.fields.BlockChan,
   148  //				PrivKey:     tt.fields.PrivKey,
   149  //				ServerUri:   tt.fields.ServerUri,
   150  //			}
   151  //			if err := e.Start(); (err != nil) != tt.wantErr {
   152  //				t.Errorf("EvmEngine.Start() error = %v, wantErr %v", err, tt.wantErr)
   153  //			}
   154  //		})
   155  //	}
   156  //}
   157  
   158  //func TestEvmEngine_vertify(t *testing.T) {
   159  //	type fields struct {
   160  //		mu          sync.Mutex
   161  //		ctx         context.Context
   162  //		cancelFunc  context.CancelFunc
   163  //		Account     string
   164  //		AppBasePath string
   165  //		State       string
   166  //		BlockChan   chan string
   167  //		PrivKey     string
   168  //		ServerUri   string
   169  //	}
   170  //	type args struct {
   171  //		in []byte
   172  //	}
   173  //	tests := []struct {
   174  //		name    string
   175  //		fields  fields
   176  //		args    args
   177  //		want    []byte
   178  //		wantErr bool
   179  //	}{
   180  //		{
   181  //			name: "t1",
   182  //			fields: fields{
   183  //				PrivKey:   `37d15846af852c47649005fd6dfe33483d394aaa60c14e7c56f4deadb116329e`,
   184  //				ServerUri: "ws://127.0.0.1:20013",
   185  //				Account:   "0x01",
   186  //			},
   187  //			args: args{
   188  //				in: []byte(`
   189  //			{
   190  //				"entire":
   191  //				{
   192  //					"header":{
   193  //						"parentHash":"0xeab8f2dca7682e72c594bfe9da4d83fb623741b21c729f57a7ff06a69aa3be38",
   194  //						"sha3Uncles":"0xeab8f2dca7682e72c594bfe9da4d83fb623741b21c729f57a7ff06a69aa3be38",
   195  //						"miner":"0x588639773Bc6F163aa262245CDa746c120676431",
   196  //						"stateRoot":"0xf9e3e616232713a12a90a2abc2302d5fd2b6360764c204ebe2acda342390841b",
   197  //						"transactionsRoot":"0xf9e3e616232713a12a90a2abc2302d5fd2b6360764c204ebe2acda342390841b",
   198  //						"receiptsRoot":"0xf9e3e616232713a12a90a2abc2302d5fd2b6360764c204ebe2acda342390841b",
   199  //						"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   200  //						"difficulty":"0x2a65b3",
   201  //						"number":"0x1532d9",
   202  //						"gasLimit":"0x1c9c380",
   203  //						"gasUsed": "0x0",
   204  //						"timestamp":"0x638f0e18",
   205  //						"extraData": "0xd883010a17846765746888676f312e31382e35856c696e757800000000000000ab9c084cefea3b41830c827c91b8908b12f64b08a243ee4fabd28bdd4556154f00d9a08f7c4a6c2794a63100d92910595e6747db4af69d576c224373c707db4400"
   206  //					},
   207  //					"uncles":[],
   208  //					"transactions":[],
   209  //					"pprof":"0xf9e3e616232713a12a90a2abc2302d5fd2b6360764c204ebe2acda342390841b",
   210  //					"senders":[]
   211  //				},
   212  //				"codes":[]
   213  //			}`),
   214  //			},
   215  //			want:    []byte("321123bb"),
   216  //			wantErr: false,
   217  //		},
   218  //	}
   219  //	for _, tt := range tests {
   220  //		t.Run(tt.name, func(t *testing.T) {
   221  //			e := &EvmEngine{
   222  //				mu:          tt.fields.mu,
   223  //				ctx:         tt.fields.ctx,
   224  //				cancelFunc:  tt.fields.cancelFunc,
   225  //				Account:     tt.fields.Account,
   226  //				AppBasePath: tt.fields.AppBasePath,
   227  //				EngineState: tt.fields.State,
   228  //				BlockChan:   tt.fields.BlockChan,
   229  //				PrivKey:     tt.fields.PrivKey,
   230  //				ServerUri:   tt.fields.ServerUri,
   231  //			}
   232  //			got, err := e.vertify(tt.args.in)
   233  //			if (err != nil) != tt.wantErr {
   234  //				t.Errorf("EvmEngine.vertify() error = %v, wantErr %v", err, tt.wantErr)
   235  //				return
   236  //			}
   237  //			if !reflect.DeepEqual(got, tt.want) {
   238  //				t.Errorf("EvmEngine.vertify() = %v, want %v", got, tt.want)
   239  //			}
   240  //		})
   241  //	}
   242  //}
   243  
   244  //func TestEngineStart(t *testing.T) {
   245  //	EE.Setting(&EmitRequest{
   246  //		Typ: "setting",
   247  //		Val: map[string]interface{}{
   248  //			"app_base_path": "/Users/xan/misc",
   249  //			"priv_key":      "37d15846af852c47649005fd6dfe33483d394aaa60c14e7c56f4deadb116329e", //2d09d9f4e166f35a4ab0a2edd599e2a23bbe86b312b2e05b34d9fbe5693b1e48
   250  //			"server_uri":    "ws://127.0.0.1:20013",
   251  //			"account":       "0x588639773bc6f163aa262245cda746c120676431", //0x7Ac869Ff8b6232f7cfC4370A2df4a81641Cba3d9
   252  //		},
   253  //	})
   254  //	if err := EE.Start(); err != nil {
   255  //		t.Error(err)
   256  //	}
   257  //	select {}
   258  //}
   259  
   260  /*
   261  0xb1fd8daE392D8E7c5aA0c8D405EFC56e1F55c42c
   262  732d6346fc020087ac84bd1a42adb444e0f783fe907759ba7fa977aab3bf66fc
   263  */
   264  //func TestEmitComplexEmulation(t *testing.T) {
   265  //	EE.Setting(&EmitRequest{
   266  //		Typ: "setting",
   267  //		Val: map[string]interface{}{
   268  //			"app_base_path": "/Users/mac/misc",
   269  //			"priv_key":      "de4b76c3dca3d8e10aea7644f77b316a68a6476fbd119d441ead5c6131aa42a7", //2d09d9f4e166f35a4ab0a2edd599e2a23bbe86b312b2e05b34d9fbe5693b1e48
   270  //			"server_uri":    "ws://54.175.247.94:20013",
   271  //			"account":       "0x65081DBA9E7B5398ec4d40f1794003c54dB11B79", //0x7Ac869Ff8b6232f7cfC4370A2df4a81641Cba3d9
   272  //		},
   273  //	})
   274  //	if s := Emit(`{"type":"start"}`); len(s) != 0 {
   275  //		fmt.Println(s)
   276  //	}
   277  //
   278  //	// i := 0
   279  //	// for {
   280  //	// 	if s := Emit(`{"type":"state"}`); len(s) != 0 {
   281  //	// 		fmt.Println(s)
   282  //	// 	}
   283  //	// 	<-time.After(1 * time.Second)
   284  //	// 	if i%20 == 0 {
   285  //	// 		Emit(`{"type":"start"}`)
   286  //	// 	}
   287  //	// 	i++
   288  //	// }
   289  //
   290  //	select {}
   291  //}
   292  
   293  //func TestEngineState(t *testing.T) {
   294  //	EE.Setting(&EmitRequest{
   295  //		Typ: "setting",
   296  //		Val: map[string]interface{}{
   297  //			"app_base_path": "/Users/xan/misc",
   298  //			"priv_key":      "37d15846af852c47649005fd6dfe33483d394aaa60c14e7c56f4deadb116329e",
   299  //			"server_uri":    "ws://127.0.0.1:20013",
   300  //			"account":       "0x7Ac869Ff8b6232f7cfC4370A2df4a81641Cba3d9",
   301  //		},
   302  //	})
   303  //	if ss := EE.State(); ss != "stopped" {
   304  //		t.Error("unstopped")
   305  //	}
   306  //	if err := EE.Start(); err != nil {
   307  //		t.Error(err)
   308  //	}
   309  //	if ss := EE.State(); ss != "started" {
   310  //		t.Error("unstarted")
   311  //	}
   312  //	select {}
   313  //}
   314  
   315  //func TestUnmarshalInput(t *testing.T) {
   316  //	EE.Setting(&EmitRequest{
   317  //		Typ: "setting",
   318  //		Val: map[string]interface{}{
   319  //			"app_base_path": "/Users/xan/misc",
   320  //			"priv_key":      "37d15846af852c47649005fd6dfe33483d394aaa60c14e7c56f4deadb116329e",
   321  //			"server_uri":    "ws://127.0.0.1:20013",
   322  //			"account":       "0x7Ac869Ff8b6232f7cfC4370A2df4a81641Cba3d9",
   323  //		},
   324  //	})
   325  //	if ss := EE.State(); ss != "stopped" {
   326  //		t.Error("unstopped")
   327  //	}
   328  //	if err := EE.Start(); err != nil {
   329  //		t.Error(err)
   330  //	}
   331  //	if ss := EE.State(); ss != "started" {
   332  //		t.Error("unstarted")
   333  //	}
   334  //	select {}
   335  //}
   336  
   337  func TestBlssign(t *testing.T) {
   338  	resp := Emit(`
   339  {"type": "blssign","val": {"priv_key": "202cbf36864a88e348c8f573aa0bc79f5a7119e58251c3580bb08af70cb2dfed", "msg" : "8ac7230489e80000"}}
   340  	`)
   341  	_ = resp
   342  }
   343  
   344  func TestEngineStop(t *testing.T) {
   345  	if err := EE.Stop(); err != nil {
   346  		t.Error(err)
   347  	}
   348  }
   349  
   350  /*
   351  {
   352      "jsonrpc": "2.0",
   353      "method": "eth_subscription",
   354      "params": {
   355          "subscription": "0x7e6684b10d6c906c47253690b8e9c7fa",
   356          "result": {
   357              "Entire": {
   358                  "entire": {
   359                      "header": {
   360                          "parentHash": "0x76b286a66229e34455f5238b9f9c0d516903c222b4e5874a6662820e25dcde98",
   361                          "sha3Uncles": "0x0000000000000000000000000000000000000000000000000000000000000000",
   362                          "miner": "0x0000000000000000000000000000000000000000",
   363                          "stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
   364                          "transactionsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
   365                          "receiptsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
   366                          "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   367                          "difficulty": "0x2",
   368                          "number": "0xd2",
   369                          "gasLimit": "0x18462971",
   370                          "gasUsed": "0x0",
   371                          "timestamp": "0x63a15be4",
   372                          "extraData": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   373                          "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
   374                          "nonce": "0x0000000000000000",
   375                          "baseFeePerGas": null,
   376                          "hash": "0x051510ec6c9998e61126d285169ed6b24114b27c264ce0e5dad5e0a7bbac2f42"
   377                      },
   378                      "uncles": null,
   379                      "transactions": [],
   380                      "snap": {
   381                          "items": [
   382                              {
   383                                  "key": "uelEd/X4i16Noul+hQbW5PzwTls=",
   384                                  "value": "CAEaEzB4NjgxNTVhNDM2NzZlMDAwMDAiIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKiDF0kYBhvcjPJJ+fbLcxwPA5QC2U8qCJzt7+tgEXYWkcA=="
   385                              },
   386                              {
   387                                  "key": "m6M2g1Qiuu/FN9dWQpWdKoZlAKM=",
   388                                  "value": "CAEaEzB4NjgxNTVhNDM2NzZlMDAwMDAiIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKiDF0kYBhvcjPJJ+fbLcxwPA5QC2U8qCJzt7+tgEXYWkcA=="
   389                              },
   390                              {
   391                                  "key": "RUH8HMtOBCo7rf5GkE+dItEntoI=",
   392                                  "value": "CAEaEzB4NjgxNTVhNDM2NzZlMDAwMDAiIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKiDF0kYBhvcjPJJ+fbLcxwPA5QC2U8qCJzt7+tgEXYWkcA=="
   393                              }
   394                          ],
   395                          "outHash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
   396                      },
   397                      "proof": "0x0000000000000000000000000000000000000000000000000000000000000000",
   398                      "senders": null
   399                  },
   400                  "codes": [],
   401                  "headers": [],
   402                  "rewards": {
   403                      "0x4541fc1ccb4e042a3badfe46904f9d22d127b682": {
   404                          "Int": "0x20f5b1eaad8d80000"
   405                      },
   406                      "0x9ba336835422baefc537d75642959d2a866500a3": {
   407                          "Int": "0x1d7d843dc3b480000"
   408                      },
   409                      "0xb9e94477f5f88b5e8da2e97e8506d6e4fcf04e5b": {
   410                          "Int": "0x1bc16d674ec800000"
   411                      }
   412                  }
   413              }
   414          }
   415      }
   416  }
   417  */
   418  
   419  func TestBeanchMark(t *testing.T) {
   420  
   421  	var (
   422  		key, _ = crypto.HexToECDSA("d6d8d19bd786d6676819b806694b1100a4414a94e51e9a82a351bd8f7f3f3658")
   423  		addr   = crypto.PubkeyToAddress(key.PublicKey)
   424  		//signer  = new(types.HomesteadSigner)
   425  		//content = context.Background()
   426  	)
   427  
   428  	fmt.Println(addr)
   429  }