github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/random_testcgo.go (about)

     1  package nvm
     2  
     3  /*
     4  #include <stdlib.h>
     5  #include "v8/lib/nvm_error.h"
     6  
     7  */
     8  import "C"
     9  
    10  import (
    11  	"fmt"
    12  	"testing"
    13  	"unsafe"
    14  
    15  	"github.com/nebulasio/go-nebulas/core"
    16  	"github.com/nebulasio/go-nebulas/crypto"
    17  	"github.com/nebulasio/go-nebulas/crypto/keystore"
    18  	"github.com/nebulasio/go-nebulas/crypto/keystore/secp256k1"
    19  	"github.com/nebulasio/go-nebulas/util"
    20  	"github.com/nebulasio/go-nebulas/util/byteutils"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func newUint128FromIntWrapper2(a int64) *util.Uint128 {
    25  	b, _ := util.NewUint128FromInt(a)
    26  	return b
    27  }
    28  
    29  func mockNormalTransaction2(from, to, value string) *core.Transaction {
    30  
    31  	fromAddr, _ := core.AddressParse(from)
    32  	toAddr, _ := core.AddressParse(to)
    33  	payload, _ := core.NewBinaryPayload(nil).ToBytes()
    34  	gasPrice, _ := util.NewUint128FromString("1000000")
    35  	gasLimit, _ := util.NewUint128FromString("2000000")
    36  	v, _ := util.NewUint128FromString(value)
    37  	tx, _ := core.NewTransaction(1, fromAddr, toAddr, v, 1, core.TxPayloadBinaryType, payload, gasPrice, gasLimit)
    38  
    39  	priv1 := secp256k1.GeneratePrivateKey()
    40  	signature, _ := crypto.NewSignature(keystore.SECP256K1)
    41  	signature.InitSign(priv1)
    42  	tx.Sign(signature)
    43  	return tx
    44  }
    45  
    46  type testBlock2 struct {
    47  }
    48  
    49  // Coinbase mock
    50  func (block *testBlock2) Coinbase() *core.Address {
    51  	addr, _ := core.AddressParse("n1FkntVUMPAsESuCAAPK711omQk19JotBjM")
    52  	return addr
    53  }
    54  
    55  // Hash mock
    56  func (block *testBlock2) Hash() byteutils.Hash {
    57  	return []byte("59fc526072b09af8a8ca9732dae17132c4e9127e43cf2232")
    58  }
    59  
    60  // Height mock
    61  func (block *testBlock2) Height() uint64 {
    62  	return core.NebCompatibility.NvmMemoryLimitWithoutInjectHeight()
    63  }
    64  
    65  // RandomSeed mock
    66  func (block *testBlock2) RandomSeed() string {
    67  	return "59fc526072b09af8a8ca9732dae17132c4e9127e43cf2232"
    68  }
    69  
    70  // RandomAvailable mock
    71  func (block *testBlock2) RandomAvailable() bool {
    72  	return true
    73  }
    74  
    75  // DateAvailable
    76  func (block *testBlock2) DateAvailable() bool {
    77  	return true
    78  }
    79  
    80  // GetTransaction mock
    81  func (block *testBlock2) GetTransaction(hash byteutils.Hash) (*core.Transaction, error) {
    82  	return nil, nil
    83  }
    84  
    85  // RecordEvent mock
    86  func (block *testBlock2) RecordEvent(txHash byteutils.Hash, topic, data string) error {
    87  	return nil
    88  }
    89  
    90  func (block *testBlock2) Timestamp() int64 {
    91  	return int64(0)
    92  }
    93  
    94  func (block *testBlock2) NR() core.NR {
    95  	return nil
    96  }
    97  
    98  func mockBlock2() Block {
    99  	block := &testBlock2{}
   100  	return block
   101  }
   102  
   103  func testRandomFunc(t *testing.T, context WorldState) {
   104  	contractAddr, err := core.AddressParse("n1FkntVUMPAsESuCAAPK711omQk19JotBjM")
   105  	assert.Nil(t, err)
   106  	contract, _ := context.CreateContractAccount(contractAddr.Bytes(), nil, nil)
   107  	contract.AddBalance(newUint128FromIntWrapper2(5))
   108  
   109  	tx := mockNormalTransaction2("n1FkntVUMPAsESuCAAPK711omQk19JotBjM", "n1TV3sU6jyzR4rJ1D7jCAmtVGSntJagXZHC", "0")
   110  	ctx, err := NewContext(mockBlock2(), tx, contract, context)
   111  	assert.Nil(t, err)
   112  
   113  	// execute.
   114  	engine := NewV8Engine(ctx)
   115  	assert.Nil(t, engine.ctx.contextRand.rand)
   116  
   117  	var cnt C.size_t
   118  	var result *C.char
   119  	var exception *C.char
   120  	r1 := GetTxRandomFunc(unsafe.Pointer(uintptr(engine.lcsHandler)), &cnt, &result, &exception)
   121  	fmt.Printf("r1:%v\n", r1)
   122  	assert.Equal(t, r1, C.NVM_SUCCESS)
   123  	assert.NotNil(t, result)
   124  	assert.NotNil(t, engine.ctx.contextRand.rand)
   125  	rs1 := C.GoString(result)
   126  	if result != nil {
   127  		C.free(unsafe.Pointer(result))
   128  	}
   129  	if exception != nil {
   130  		C.free(unsafe.Pointer(exception))
   131  	}
   132  	result = nil
   133  	exception = nil
   134  	r2 := GetTxRandomFunc(unsafe.Pointer(uintptr(engine.lcsHandler)), &cnt, &result, &exception)
   135  	assert.Equal(t, r2, C.NVM_SUCCESS)
   136  	assert.NotNil(t, result)
   137  	rs2 := C.GoString(result)
   138  
   139  	assert.NotEqual(t, rs1, rs2)
   140  
   141  	fmt.Println(rs1, rs2)
   142  	if result != nil {
   143  		C.free(unsafe.Pointer(result))
   144  	}
   145  	if exception != nil {
   146  		C.free(unsafe.Pointer(exception))
   147  	}
   148  	engine.Dispose()
   149  }