github.com/uuosio/chaintester@v0.0.0-20230731100329-1f6fad7372e5/basic_test.go (about)

     1  package chaintester
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  var ctx = context.Background()
    10  
    11  func TestChainTester(t *testing.T) {
    12  	tester := NewChainTester()
    13  	info, _ := tester.GetInfo()
    14  	t.Logf("+++++++++info: %v", info.ToString())
    15  
    16  	key, err := tester.CreateKey()
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  
    21  	t.Logf("+++++++++key: %v", key.ToString())
    22  
    23  	privKey, _ := key.GetString("private")
    24  	t.Logf("+++++++++private key: %v", privKey)
    25  
    26  	pubKey, _ := key.GetString("public")
    27  	t.Logf("+++++++++public key: %v", pubKey)
    28  
    29  	_, err = tester.CreateAccount("hello", "helloworld33", pubKey, pubKey, 10*1024*1024, 10*10000, 10*10000)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	ret, _ := tester.GetAccount("helloworld33")
    35  	t.Logf("+++++++++account info: %v", ret.ToString())
    36  
    37  	tester.SetNativeApply("hello", nil)
    38  	err = tester.DeployContract("hello", "test/test.wasm", "test/test.abi")
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  	tester.ProduceBlock()
    43  
    44  	permissions := `
    45  	{
    46  		"hello": "active"
    47  	}
    48  	`
    49  	args := `
    50  	{
    51  		"name": "go"
    52  	}
    53  	`
    54  	ret, err = tester.PushAction("hello", "inc", args, permissions)
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  	tester.ProduceBlock()
    59  	sender := NewActionSender(tester)
    60  	sender.AddActionWithSigner("hello", "inc", args, "hello")
    61  	ret, err = sender.Send()
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	// t.Logf("%v", ret.ToString())
    66  	ret, err = tester.GetTableRows(true, "hello", "", "counter", "", "", 10)
    67  	if err != nil {
    68  		panic(fmt.Errorf("++++++++error:%v", err))
    69  	}
    70  	t.Logf("%v", ret.ToString())
    71  
    72  	ret, err = tester.GetTableRows(true, "eosio.token", "hello", "accounts", "EOS", "", 1)
    73  	if err != nil {
    74  		panic(fmt.Errorf("++++++++error:%v", err))
    75  	}
    76  
    77  	balance, err := ret.GetString("rows", 0)
    78  	if err != nil {
    79  		panic(err)
    80  	}
    81  	t.Logf("++++++++++= balance: %s", balance)
    82  
    83  	ret, err = tester.PushAction("hello", "test", "", permissions)
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  	tester.ProduceBlock(10)
    88  
    89  	ret, err = tester.PushAction("hello", "test", "", permissions)
    90  	if err != nil {
    91  		panic(err)
    92  	}
    93  	tester.ProduceBlock()
    94  	t.Logf("+++++++balance of hello: %d", tester.GetBalance("hello"))
    95  
    96  	tester.SetNativeApply("hello", native_apply)
    97  	ret, err = tester.PushAction("hello", "test", "", permissions)
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  	tester.ProduceBlock()
   102  }
   103  
   104  func TestApplyCtx(t *testing.T) {
   105  	tester := NewChainTester()
   106  	defer tester.FreeChain()
   107  	tester.SetNativeApply("hello", native_apply)
   108  
   109  	tester.GetInfo()
   110  
   111  	{
   112  		defer func() {
   113  			err := recover()
   114  			if err == nil {
   115  				panic(fmt.Errorf("err should not be nil"))
   116  			}
   117  			t.Logf("++++%v", err)
   118  		}()
   119  		GetVMAPI().Prints(ctx, "hello, world!\n")
   120  	}
   121  }
   122  
   123  func native_apply(receiver uint64, firstReceiver uint64, action uint64) {
   124  	for i := 0; i < 10; i++ {
   125  		GetVMAPI().Prints(ctx, "hello, world!\n")
   126  	}
   127  }