func testSNativeCALL(t *testing.T, expectPass bool, blockCache *BlockCache, doug *acm.Account, snativePerm ptypes.PermFlag, snativeAddress, data []byte, f func([]byte) error) { if expectPass { doug.Permissions.Base.Set(snativePerm, true) } var addr []byte contractCode := callContractCode(snativeAddress) doug.Code = contractCode blockCache.UpdateAccount(doug) addr = doug.Address tx, _ := types.NewCallTx(blockCache, user[0].PubKey, addr, data, 100, 10000, 100) tx.Sign(chainID, user[0]) fmt.Println("subscribing to", types.EventStringAccCall(snativeAddress)) ev, exception := execTxWaitEvent(t, blockCache, tx, types.EventStringAccCall(snativeAddress)) if exception == ExceptionTimeOut { t.Fatal("Timed out waiting for event") } if expectPass { if exception != "" { t.Fatal("Unexpected exception", exception) } evv := ev.(types.EventDataCall) ret := evv.Return if err := f(ret); err != nil { t.Fatal(err) } } else { if exception == "" { t.Fatal("Expected exception") } } }
// give a contract perms for an snative, call it, it calls the snative, ensure the check funciton (f) succeeds func testSNativeCALLExpectPass(t *testing.T, blockCache *BlockCache, doug *account.Account, snativeAddress, data []byte, f func([]byte) error) { perm := vm.RegisteredSNativePermissions[LeftPadWord256(snativeAddress)] var addr []byte if doug != nil { contractCode := callContractCode(snativeAddress) doug.Code = contractCode doug.Permissions.Base.Set(perm, true) blockCache.UpdateAccount(doug) addr = doug.Address } else { acc := blockCache.GetAccount(user[0].Address) acc.Permissions.Base.Set(perm, true) blockCache.UpdateAccount(acc) addr = snativeAddress } tx, _ := types.NewCallTx(blockCache, user[0].PubKey, addr, data, 100, 10000, 100) tx.Sign(chainID, user[0]) ev, exception := execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(snativeAddress)) // if exception != "" { t.Fatal("Unexpected exception", exception) } evv := ev.(types.EventMsgCall) ret := evv.Return if err := f(ret); err != nil { t.Fatal(err) } }
// assumes the contract has not been given the permission. calls the it, it calls the snative, expects to fail func testSNativeCALLExpectFail(t *testing.T, blockCache *BlockCache, doug *account.Account, snativeAddress, data []byte) { var addr []byte if doug != nil { contractCode := callContractCode(snativeAddress) doug.Code = contractCode blockCache.UpdateAccount(doug) addr = doug.Address } else { addr = snativeAddress } tx, _ := types.NewCallTx(blockCache, user[0].PubKey, addr, data, 100, 10000, 100) tx.Sign(chainID, user[0]) fmt.Println("subscribing to", types.EventStringAccReceive(snativeAddress)) _, exception := execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(snativeAddress)) if exception == "" { t.Fatal("Expected exception") } }