You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

867 lines
28 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "github.com/tendermint/tendermint/account"
  9. . "github.com/tendermint/tendermint/common"
  10. dbm "github.com/tendermint/tendermint/db"
  11. "github.com/tendermint/tendermint/events"
  12. ptypes "github.com/tendermint/tendermint/permission/types"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. /*
  16. Permission Tests:
  17. - SendTx:
  18. x - 1 input, no perm, call perm, create perm
  19. x - 1 input, perm
  20. x - 2 inputs, one with perm one without
  21. - CallTx, CALL
  22. x - 1 input, no perm, send perm, create perm
  23. x - 1 input, perm
  24. x - contract runs call but doesn't have call perm
  25. x - contract runs call and has call perm
  26. x - contract runs call (with perm), runs contract that runs call (without perm)
  27. x - contract runs call (with perm), runs contract that runs call (with perm)
  28. - CallTx for Create, CREATE
  29. x - 1 input, no perm, send perm, call perm
  30. x - 1 input, perm
  31. x - contract runs create but doesn't have create perm
  32. x - contract runs create but has perm
  33. x - contract runs call with empty address (has call and create perm)
  34. - BondTx
  35. x - 1 input, no perm
  36. x - 1 input, perm
  37. x - 1 bonder with perm, input without send or bond
  38. x - 1 bonder with perm, input with send
  39. x - 1 bonder with perm, input with bond
  40. x - 2 inputs, one with perm one without
  41. - SendTx for new account
  42. x - 1 input, 1 unknown ouput, input with send, not create (fail)
  43. x - 1 input, 1 unknown ouput, input with send and create (pass)
  44. x - 2 inputs, 1 unknown ouput, both inputs with send, one with create, one without (fail)
  45. x - 2 inputs, 1 known output, 1 unknown ouput, one input with create, one without (fail)
  46. x - 2 inputs, 1 unknown ouput, both inputs with send, both inputs with create (pass )
  47. x - 2 inputs, 1 known output, 1 unknown ouput, both inputs with create, (pass)
  48. - CALL for new account
  49. x - unknown output, without create (fail)
  50. x - unknown output, with create (pass)
  51. - Gendoug:
  52. - base: has,set,unset
  53. - roles: has, add, r
  54. */
  55. // keys
  56. var user = makeUsers(10)
  57. func makeUsers(n int) []*account.PrivAccount {
  58. accounts := []*account.PrivAccount{}
  59. for i := 0; i < n; i++ {
  60. secret := []byte("mysecret" + strconv.Itoa(i))
  61. user := account.GenPrivAccountFromSecret(secret)
  62. accounts = append(accounts, user)
  63. }
  64. return accounts
  65. }
  66. var (
  67. PermsAllFalse = ptypes.NewAccountPermissions()
  68. )
  69. func newBaseGenDoc(globalPerm, accountPerm *ptypes.AccountPermissions) GenesisDoc {
  70. genAccounts := []GenesisAccount{}
  71. for _, u := range user[:5] {
  72. genAccounts = append(genAccounts, GenesisAccount{
  73. Address: u.Address,
  74. Amount: 1000000,
  75. Permissions: accountPerm.Copy(),
  76. })
  77. }
  78. return GenesisDoc{
  79. GenesisTime: time.Now(),
  80. Params: &GenesisParams{
  81. GlobalPermissions: globalPerm,
  82. },
  83. Accounts: genAccounts,
  84. Validators: []GenesisValidator{
  85. GenesisValidator{
  86. PubKey: user[0].PubKey.(account.PubKeyEd25519),
  87. Amount: 10,
  88. UnbondTo: []GenesisAccount{
  89. GenesisAccount{
  90. Address: user[0].Address,
  91. },
  92. },
  93. },
  94. },
  95. }
  96. }
  97. func TestSendFails(t *testing.T) {
  98. stateDB := dbm.GetDB("state")
  99. genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  100. genDoc.Accounts[1].Permissions.Base.Set(ptypes.Send, true)
  101. genDoc.Accounts[2].Permissions.Base.Set(ptypes.Call, true)
  102. genDoc.Accounts[3].Permissions.Base.Set(ptypes.CreateContract, true)
  103. st := MakeGenesisState(stateDB, &genDoc)
  104. blockCache := NewBlockCache(st)
  105. //-------------------
  106. // send txs
  107. // simple send tx should fail
  108. tx := types.NewSendTx()
  109. if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
  110. t.Fatal(err)
  111. }
  112. tx.AddOutput(user[1].Address, 5)
  113. tx.SignInput(0, user[0])
  114. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  115. t.Fatal("Expected error")
  116. } else {
  117. fmt.Println(err)
  118. }
  119. // simple send tx with call perm should fail
  120. tx = types.NewSendTx()
  121. if err := tx.AddInput(blockCache, user[2].PubKey, 5); err != nil {
  122. t.Fatal(err)
  123. }
  124. tx.AddOutput(user[4].Address, 5)
  125. tx.SignInput(0, user[2])
  126. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  127. t.Fatal("Expected error")
  128. } else {
  129. fmt.Println(err)
  130. }
  131. // simple send tx with create perm should fail
  132. tx = types.NewSendTx()
  133. if err := tx.AddInput(blockCache, user[3].PubKey, 5); err != nil {
  134. t.Fatal(err)
  135. }
  136. tx.AddOutput(user[4].Address, 5)
  137. tx.SignInput(0, user[3])
  138. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  139. t.Fatal("Expected error")
  140. } else {
  141. fmt.Println(err)
  142. }
  143. // simple send tx to unknown account without create_account perm should fail
  144. acc := blockCache.GetAccount(user[3].Address)
  145. acc.Permissions.Base.Set(ptypes.Send, true)
  146. blockCache.UpdateAccount(acc)
  147. tx = types.NewSendTx()
  148. if err := tx.AddInput(blockCache, user[3].PubKey, 5); err != nil {
  149. t.Fatal(err)
  150. }
  151. tx.AddOutput(user[6].Address, 5)
  152. tx.SignInput(0, user[3])
  153. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  154. t.Fatal("Expected error")
  155. } else {
  156. fmt.Println(err)
  157. }
  158. }
  159. func TestCallFails(t *testing.T) {
  160. stateDB := dbm.GetDB("state")
  161. genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  162. genDoc.Accounts[1].Permissions.Base.Set(ptypes.Send, true)
  163. genDoc.Accounts[2].Permissions.Base.Set(ptypes.Call, true)
  164. genDoc.Accounts[3].Permissions.Base.Set(ptypes.CreateContract, true)
  165. st := MakeGenesisState(stateDB, &genDoc)
  166. blockCache := NewBlockCache(st)
  167. //-------------------
  168. // call txs
  169. // simple call tx should fail
  170. tx, _ := types.NewCallTx(blockCache, user[0].PubKey, user[4].Address, nil, 100, 100, 100)
  171. tx.Sign(user[0])
  172. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  173. t.Fatal("Expected error")
  174. } else {
  175. fmt.Println(err)
  176. }
  177. // simple call tx with send permission should fail
  178. tx, _ = types.NewCallTx(blockCache, user[1].PubKey, user[4].Address, nil, 100, 100, 100)
  179. tx.Sign(user[1])
  180. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  181. t.Fatal("Expected error")
  182. } else {
  183. fmt.Println(err)
  184. }
  185. // simple call tx with create permission should fail
  186. tx, _ = types.NewCallTx(blockCache, user[3].PubKey, user[4].Address, nil, 100, 100, 100)
  187. tx.Sign(user[3])
  188. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  189. t.Fatal("Expected error")
  190. } else {
  191. fmt.Println(err)
  192. }
  193. //-------------------
  194. // create txs
  195. // simple call create tx should fail
  196. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, nil, nil, 100, 100, 100)
  197. tx.Sign(user[0])
  198. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  199. t.Fatal("Expected error")
  200. } else {
  201. fmt.Println(err)
  202. }
  203. // simple call create tx with send perm should fail
  204. tx, _ = types.NewCallTx(blockCache, user[1].PubKey, nil, nil, 100, 100, 100)
  205. tx.Sign(user[1])
  206. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  207. t.Fatal("Expected error")
  208. } else {
  209. fmt.Println(err)
  210. }
  211. // simple call create tx with call perm should fail
  212. tx, _ = types.NewCallTx(blockCache, user[2].PubKey, nil, nil, 100, 100, 100)
  213. tx.Sign(user[2])
  214. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  215. t.Fatal("Expected error")
  216. } else {
  217. fmt.Println(err)
  218. }
  219. }
  220. func TestSendPermission(t *testing.T) {
  221. stateDB := dbm.GetDB("state")
  222. genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  223. genDoc.Accounts[0].Permissions.Base.Set(ptypes.Send, true) // give the 0 account permission
  224. st := MakeGenesisState(stateDB, &genDoc)
  225. blockCache := NewBlockCache(st)
  226. // A single input, having the permission, should succeed
  227. tx := types.NewSendTx()
  228. if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
  229. t.Fatal(err)
  230. }
  231. tx.AddOutput(user[1].Address, 5)
  232. tx.SignInput(0, user[0])
  233. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  234. t.Fatal("Transaction failed", err)
  235. }
  236. // Two inputs, one with permission, one without, should fail
  237. tx = types.NewSendTx()
  238. if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
  239. t.Fatal(err)
  240. }
  241. if err := tx.AddInput(blockCache, user[1].PubKey, 5); err != nil {
  242. t.Fatal(err)
  243. }
  244. tx.AddOutput(user[2].Address, 10)
  245. tx.SignInput(0, user[0])
  246. tx.SignInput(1, user[1])
  247. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  248. t.Fatal("Expected error")
  249. } else {
  250. fmt.Println(err)
  251. }
  252. }
  253. func TestCallPermission(t *testing.T) {
  254. stateDB := dbm.GetDB("state")
  255. genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  256. genDoc.Accounts[0].Permissions.Base.Set(ptypes.Call, true) // give the 0 account permission
  257. st := MakeGenesisState(stateDB, &genDoc)
  258. blockCache := NewBlockCache(st)
  259. //------------------------------
  260. // call to simple contract
  261. fmt.Println("##### SIMPLE CONTRACT")
  262. // create simple contract
  263. simpleContractAddr := NewContractAddress(user[0].Address, 100)
  264. simpleAcc := &account.Account{
  265. Address: simpleContractAddr,
  266. Balance: 0,
  267. Code: []byte{0x60},
  268. Sequence: 0,
  269. StorageRoot: Zero256.Bytes(),
  270. Permissions: ptypes.NewAccountPermissions(),
  271. }
  272. st.UpdateAccount(simpleAcc)
  273. // A single input, having the permission, should succeed
  274. tx, _ := types.NewCallTx(blockCache, user[0].PubKey, simpleContractAddr, nil, 100, 100, 100)
  275. tx.Sign(user[0])
  276. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  277. t.Fatal("Transaction failed", err)
  278. }
  279. //----------------------------------------------------------
  280. // call to contract that calls simple contract - without perm
  281. fmt.Println("##### CALL TO SIMPLE CONTRACT (FAIL)")
  282. // create contract that calls the simple contract
  283. contractCode := callContractCode(simpleContractAddr)
  284. caller1ContractAddr := NewContractAddress(user[0].Address, 101)
  285. caller1Acc := &account.Account{
  286. Address: caller1ContractAddr,
  287. Balance: 0,
  288. Code: contractCode,
  289. Sequence: 0,
  290. StorageRoot: Zero256.Bytes(),
  291. Permissions: ptypes.NewAccountPermissions(),
  292. }
  293. blockCache.UpdateAccount(caller1Acc)
  294. // A single input, having the permission, but the contract doesn't have permission
  295. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, caller1ContractAddr, nil, 100, 10000, 100)
  296. tx.Sign(user[0])
  297. // we need to subscribe to the Receive event to detect the exception
  298. _, exception := execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(caller1ContractAddr)) //
  299. if exception == "" {
  300. t.Fatal("Expected exception")
  301. }
  302. //----------------------------------------------------------
  303. // call to contract that calls simple contract - with perm
  304. fmt.Println("##### CALL TO SIMPLE CONTRACT (PASS)")
  305. // A single input, having the permission, and the contract has permission
  306. caller1Acc.Permissions.Base.Set(ptypes.Call, true)
  307. blockCache.UpdateAccount(caller1Acc)
  308. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, caller1ContractAddr, nil, 100, 10000, 100)
  309. tx.Sign(user[0])
  310. // we need to subscribe to the Receive event to detect the exception
  311. _, exception = execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(caller1ContractAddr)) //
  312. if exception != "" {
  313. t.Fatal("Unexpected exception:", exception)
  314. }
  315. //----------------------------------------------------------
  316. // call to contract that calls contract that calls simple contract - without perm
  317. // caller1Contract calls simpleContract. caller2Contract calls caller1Contract.
  318. // caller1Contract does not have call perms, but caller2Contract does.
  319. fmt.Println("##### CALL TO CONTRACT CALLING SIMPLE CONTRACT (FAIL)")
  320. contractCode2 := callContractCode(caller1ContractAddr)
  321. caller2ContractAddr := NewContractAddress(user[0].Address, 102)
  322. caller2Acc := &account.Account{
  323. Address: caller2ContractAddr,
  324. Balance: 1000,
  325. Code: contractCode2,
  326. Sequence: 0,
  327. StorageRoot: Zero256.Bytes(),
  328. Permissions: ptypes.NewAccountPermissions(),
  329. }
  330. caller1Acc.Permissions.Base.Set(ptypes.Call, false)
  331. caller2Acc.Permissions.Base.Set(ptypes.Call, true)
  332. blockCache.UpdateAccount(caller1Acc)
  333. blockCache.UpdateAccount(caller2Acc)
  334. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, caller2ContractAddr, nil, 100, 10000, 100)
  335. tx.Sign(user[0])
  336. // we need to subscribe to the Receive event to detect the exception
  337. _, exception = execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(caller1ContractAddr)) //
  338. if exception == "" {
  339. t.Fatal("Expected exception")
  340. }
  341. //----------------------------------------------------------
  342. // call to contract that calls contract that calls simple contract - without perm
  343. // caller1Contract calls simpleContract. caller2Contract calls caller1Contract.
  344. // both caller1 and caller2 have permission
  345. fmt.Println("##### CALL TO CONTRACT CALLING SIMPLE CONTRACT (PASS)")
  346. caller1Acc.Permissions.Base.Set(ptypes.Call, true)
  347. blockCache.UpdateAccount(caller1Acc)
  348. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, caller2ContractAddr, nil, 100, 10000, 100)
  349. tx.Sign(user[0])
  350. // we need to subscribe to the Receive event to detect the exception
  351. _, exception = execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(caller1ContractAddr)) //
  352. if exception != "" {
  353. t.Fatal("Unexpected exception", exception)
  354. }
  355. }
  356. func TestCreatePermission(t *testing.T) {
  357. stateDB := dbm.GetDB("state")
  358. genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  359. genDoc.Accounts[0].Permissions.Base.Set(ptypes.CreateContract, true) // give the 0 account permission
  360. genDoc.Accounts[0].Permissions.Base.Set(ptypes.Call, true) // give the 0 account permission
  361. st := MakeGenesisState(stateDB, &genDoc)
  362. blockCache := NewBlockCache(st)
  363. //------------------------------
  364. // create a simple contract
  365. fmt.Println("##### CREATE SIMPLE CONTRACT")
  366. contractCode := []byte{0x60}
  367. createCode := wrapContractForCreate(contractCode)
  368. // A single input, having the permission, should succeed
  369. tx, _ := types.NewCallTx(blockCache, user[0].PubKey, nil, createCode, 100, 100, 100)
  370. tx.Sign(user[0])
  371. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  372. t.Fatal("Transaction failed", err)
  373. }
  374. // ensure the contract is there
  375. contractAddr := NewContractAddress(tx.Input.Address, uint64(tx.Input.Sequence))
  376. contractAcc := blockCache.GetAccount(contractAddr)
  377. if contractAcc == nil {
  378. t.Fatalf("failed to create contract %X", contractAddr)
  379. }
  380. if bytes.Compare(contractAcc.Code, contractCode) != 0 {
  381. t.Fatalf("contract does not have correct code. Got %X, expected %X", contractAcc.Code, contractCode)
  382. }
  383. //------------------------------
  384. // create contract that uses the CREATE op
  385. fmt.Println("##### CREATE FACTORY")
  386. contractCode = []byte{0x60}
  387. createCode = wrapContractForCreate(contractCode)
  388. factoryCode := createContractCode()
  389. createFactoryCode := wrapContractForCreate(factoryCode)
  390. // A single input, having the permission, should succeed
  391. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, nil, createFactoryCode, 100, 100, 100)
  392. tx.Sign(user[0])
  393. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  394. t.Fatal("Transaction failed", err)
  395. }
  396. // ensure the contract is there
  397. contractAddr = NewContractAddress(tx.Input.Address, uint64(tx.Input.Sequence))
  398. contractAcc = blockCache.GetAccount(contractAddr)
  399. if contractAcc == nil {
  400. t.Fatalf("failed to create contract %X", contractAddr)
  401. }
  402. if bytes.Compare(contractAcc.Code, factoryCode) != 0 {
  403. t.Fatalf("contract does not have correct code. Got %X, expected %X", contractAcc.Code, factoryCode)
  404. }
  405. //------------------------------
  406. // call the contract (should FAIL)
  407. fmt.Println("###### CALL THE FACTORY (FAIL)")
  408. // A single input, having the permission, should succeed
  409. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, contractAddr, createCode, 100, 100, 100)
  410. tx.Sign(user[0])
  411. // we need to subscribe to the Receive event to detect the exception
  412. _, exception := execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(contractAddr)) //
  413. if exception == "" {
  414. t.Fatal("expected exception")
  415. }
  416. //------------------------------
  417. // call the contract (should PASS)
  418. fmt.Println("###### CALL THE FACTORY (PASS)")
  419. contractAcc.Permissions.Base.Set(ptypes.CreateContract, true)
  420. blockCache.UpdateAccount(contractAcc)
  421. // A single input, having the permission, should succeed
  422. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, contractAddr, createCode, 100, 100, 100)
  423. tx.Sign(user[0])
  424. // we need to subscribe to the Receive event to detect the exception
  425. _, exception = execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(contractAddr)) //
  426. if exception != "" {
  427. t.Fatal("unexpected exception", exception)
  428. }
  429. //--------------------------------
  430. fmt.Println("##### CALL to empty address")
  431. zeroAddr := LeftPadBytes([]byte{}, 20)
  432. code := callContractCode(zeroAddr)
  433. contractAddr = NewContractAddress(user[0].Address, 110)
  434. contractAcc = &account.Account{
  435. Address: contractAddr,
  436. Balance: 1000,
  437. Code: code,
  438. Sequence: 0,
  439. StorageRoot: Zero256.Bytes(),
  440. Permissions: ptypes.NewAccountPermissions(),
  441. }
  442. contractAcc.Permissions.Base.Set(ptypes.Call, true)
  443. contractAcc.Permissions.Base.Set(ptypes.CreateContract, true)
  444. blockCache.UpdateAccount(contractAcc)
  445. // this should call the 0 address but not create ...
  446. tx, _ = types.NewCallTx(blockCache, user[0].PubKey, contractAddr, createCode, 100, 10000, 100)
  447. tx.Sign(user[0])
  448. // we need to subscribe to the Receive event to detect the exception
  449. _, exception = execTxWaitEvent(t, blockCache, tx, types.EventStringAccReceive(zeroAddr)) //
  450. if exception != "" {
  451. t.Fatal("unexpected exception", exception)
  452. }
  453. zeroAcc := blockCache.GetAccount(zeroAddr)
  454. if len(zeroAcc.Code) != 0 {
  455. t.Fatal("the zero account was given code from a CALL!")
  456. }
  457. }
  458. func TestBondPermission(t *testing.T) {
  459. stateDB := dbm.GetDB("state")
  460. genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  461. st := MakeGenesisState(stateDB, &genDoc)
  462. blockCache := NewBlockCache(st)
  463. var bondAcc *account.Account
  464. //------------------------------
  465. // one bonder without permission should fail
  466. tx, _ := types.NewBondTx(user[1].PubKey)
  467. if err := tx.AddInput(blockCache, user[1].PubKey, 5); err != nil {
  468. t.Fatal(err)
  469. }
  470. tx.AddOutput(user[1].Address, 5)
  471. tx.SignInput(0, user[1])
  472. tx.SignBond(user[1])
  473. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  474. t.Fatal("Expected error")
  475. } else {
  476. fmt.Println(err)
  477. }
  478. //------------------------------
  479. // one bonder with permission should pass
  480. bondAcc = blockCache.GetAccount(user[1].Address)
  481. bondAcc.Permissions.Base.Set(ptypes.Bond, true)
  482. blockCache.UpdateAccount(bondAcc)
  483. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  484. t.Fatal("Unexpected error", err)
  485. }
  486. // reset state (we can only bond with an account once ..)
  487. genDoc = newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  488. st = MakeGenesisState(stateDB, &genDoc)
  489. blockCache = NewBlockCache(st)
  490. bondAcc = blockCache.GetAccount(user[1].Address)
  491. bondAcc.Permissions.Base.Set(ptypes.Bond, true)
  492. blockCache.UpdateAccount(bondAcc)
  493. //------------------------------
  494. // one bonder with permission and an input without send should fail
  495. tx, _ = types.NewBondTx(user[1].PubKey)
  496. if err := tx.AddInput(blockCache, user[2].PubKey, 5); err != nil {
  497. t.Fatal(err)
  498. }
  499. tx.AddOutput(user[1].Address, 5)
  500. tx.SignInput(0, user[2])
  501. tx.SignBond(user[1])
  502. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  503. t.Fatal("Expected error")
  504. } else {
  505. fmt.Println(err)
  506. }
  507. // reset state (we can only bond with an account once ..)
  508. genDoc = newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  509. st = MakeGenesisState(stateDB, &genDoc)
  510. blockCache = NewBlockCache(st)
  511. bondAcc = blockCache.GetAccount(user[1].Address)
  512. bondAcc.Permissions.Base.Set(ptypes.Bond, true)
  513. blockCache.UpdateAccount(bondAcc)
  514. //------------------------------
  515. // one bonder with permission and an input with send should pass
  516. sendAcc := blockCache.GetAccount(user[2].Address)
  517. sendAcc.Permissions.Base.Set(ptypes.Send, true)
  518. blockCache.UpdateAccount(sendAcc)
  519. tx, _ = types.NewBondTx(user[1].PubKey)
  520. if err := tx.AddInput(blockCache, user[2].PubKey, 5); err != nil {
  521. t.Fatal(err)
  522. }
  523. tx.AddOutput(user[1].Address, 5)
  524. tx.SignInput(0, user[2])
  525. tx.SignBond(user[1])
  526. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  527. t.Fatal("Unexpected error", err)
  528. }
  529. // reset state (we can only bond with an account once ..)
  530. genDoc = newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  531. st = MakeGenesisState(stateDB, &genDoc)
  532. blockCache = NewBlockCache(st)
  533. bondAcc = blockCache.GetAccount(user[1].Address)
  534. bondAcc.Permissions.Base.Set(ptypes.Bond, true)
  535. blockCache.UpdateAccount(bondAcc)
  536. //------------------------------
  537. // one bonder with permission and an input with bond should pass
  538. sendAcc.Permissions.Base.Set(ptypes.Bond, true)
  539. blockCache.UpdateAccount(sendAcc)
  540. tx, _ = types.NewBondTx(user[1].PubKey)
  541. if err := tx.AddInput(blockCache, user[2].PubKey, 5); err != nil {
  542. t.Fatal(err)
  543. }
  544. tx.AddOutput(user[1].Address, 5)
  545. tx.SignInput(0, user[2])
  546. tx.SignBond(user[1])
  547. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  548. t.Fatal("Unexpected error", err)
  549. }
  550. // reset state (we can only bond with an account once ..)
  551. genDoc = newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  552. st = MakeGenesisState(stateDB, &genDoc)
  553. blockCache = NewBlockCache(st)
  554. bondAcc = blockCache.GetAccount(user[1].Address)
  555. bondAcc.Permissions.Base.Set(ptypes.Bond, true)
  556. blockCache.UpdateAccount(bondAcc)
  557. //------------------------------
  558. // one bonder with permission and an input from that bonder and an input without send or bond should fail
  559. tx, _ = types.NewBondTx(user[1].PubKey)
  560. if err := tx.AddInput(blockCache, user[1].PubKey, 5); err != nil {
  561. t.Fatal(err)
  562. }
  563. if err := tx.AddInput(blockCache, user[2].PubKey, 5); err != nil {
  564. t.Fatal(err)
  565. }
  566. tx.AddOutput(user[1].Address, 5)
  567. tx.SignInput(0, user[1])
  568. tx.SignInput(1, user[2])
  569. tx.SignBond(user[1])
  570. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  571. t.Fatal("Expected error")
  572. }
  573. }
  574. func TestCreateAccountPermission(t *testing.T) {
  575. stateDB := dbm.GetDB("state")
  576. genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse)
  577. genDoc.Accounts[0].Permissions.Base.Set(ptypes.Send, true) // give the 0 account permission
  578. genDoc.Accounts[1].Permissions.Base.Set(ptypes.Send, true) // give the 0 account permission
  579. genDoc.Accounts[0].Permissions.Base.Set(ptypes.CreateAccount, true) // give the 0 account permission
  580. st := MakeGenesisState(stateDB, &genDoc)
  581. blockCache := NewBlockCache(st)
  582. //----------------------------------------------------------
  583. // SendTx to unknown account
  584. // A single input, having the permission, should succeed
  585. tx := types.NewSendTx()
  586. if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
  587. t.Fatal(err)
  588. }
  589. tx.AddOutput(user[6].Address, 5)
  590. tx.SignInput(0, user[0])
  591. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  592. t.Fatal("Transaction failed", err)
  593. }
  594. // Two inputs, both with send, one with create, one without, should fail
  595. tx = types.NewSendTx()
  596. if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
  597. t.Fatal(err)
  598. }
  599. if err := tx.AddInput(blockCache, user[1].PubKey, 5); err != nil {
  600. t.Fatal(err)
  601. }
  602. tx.AddOutput(user[7].Address, 10)
  603. tx.SignInput(0, user[0])
  604. tx.SignInput(1, user[1])
  605. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  606. t.Fatal("Expected error")
  607. } else {
  608. fmt.Println(err)
  609. }
  610. // Two inputs, both with send, one with create, one without, two ouputs (one known, one unknown) should fail
  611. tx = types.NewSendTx()
  612. if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
  613. t.Fatal(err)
  614. }
  615. if err := tx.AddInput(blockCache, user[1].PubKey, 5); err != nil {
  616. t.Fatal(err)
  617. }
  618. tx.AddOutput(user[7].Address, 4)
  619. tx.AddOutput(user[4].Address, 6)
  620. tx.SignInput(0, user[0])
  621. tx.SignInput(1, user[1])
  622. if err := ExecTx(blockCache, tx, true, nil); err == nil {
  623. t.Fatal("Expected error")
  624. } else {
  625. fmt.Println(err)
  626. }
  627. // Two inputs, both with send, both with create, should pass
  628. acc := blockCache.GetAccount(user[1].Address)
  629. acc.Permissions.Base.Set(ptypes.CreateAccount, true)
  630. blockCache.UpdateAccount(acc)
  631. tx = types.NewSendTx()
  632. if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
  633. t.Fatal(err)
  634. }
  635. if err := tx.AddInput(blockCache, user[1].PubKey, 5); err != nil {
  636. t.Fatal(err)
  637. }
  638. tx.AddOutput(user[7].Address, 10)
  639. tx.SignInput(0, user[0])
  640. tx.SignInput(1, user[1])
  641. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  642. t.Fatal("Unexpected error", err)
  643. }
  644. // Two inputs, both with send, both with create, two outputs (one known, one unknown) should pass
  645. tx = types.NewSendTx()
  646. if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
  647. t.Fatal(err)
  648. }
  649. if err := tx.AddInput(blockCache, user[1].PubKey, 5); err != nil {
  650. t.Fatal(err)
  651. }
  652. tx.AddOutput(user[7].Address, 7)
  653. tx.AddOutput(user[4].Address, 3)
  654. tx.SignInput(0, user[0])
  655. tx.SignInput(1, user[1])
  656. if err := ExecTx(blockCache, tx, true, nil); err != nil {
  657. t.Fatal("Unexpected error", err)
  658. }
  659. //----------------------------------------------------------
  660. // CALL to unknown account
  661. acc = blockCache.GetAccount(user[0].Address)
  662. acc.Permissions.Base.Set(ptypes.Call, true)
  663. blockCache.UpdateAccount(acc)
  664. // call to contract that calls unknown account - without create_account perm
  665. // create contract that calls the simple contract
  666. contractCode := callContractCode(user[9].Address)
  667. caller1ContractAddr := NewContractAddress(user[4].Address, 101)
  668. caller1Acc := &account.Account{
  669. Address: caller1ContractAddr,
  670. Balance: 0,
  671. Code: contractCode,
  672. Sequence: 0,
  673. StorageRoot: Zero256.Bytes(),
  674. Permissions: ptypes.NewAccountPermissions(),
  675. }
  676. blockCache.UpdateAccount(caller1Acc)
  677. // A single input, having the permission, but the contract doesn't have permission
  678. txCall, _ := types.NewCallTx(blockCache, user[0].PubKey, caller1ContractAddr, nil, 100, 10000, 100)
  679. txCall.Sign(user[0])
  680. // we need to subscribe to the Receive event to detect the exception
  681. _, exception := execTxWaitEvent(t, blockCache, txCall, types.EventStringAccReceive(caller1ContractAddr)) //
  682. if exception == "" {
  683. t.Fatal("Expected exception")
  684. }
  685. // NOTE: for a contract to be able to CreateAccount, it must be able to call
  686. // NOTE: for a user to be able to CreateAccount, it must be able to send!
  687. caller1Acc.Permissions.Base.Set(ptypes.CreateAccount, true)
  688. caller1Acc.Permissions.Base.Set(ptypes.Call, true)
  689. blockCache.UpdateAccount(caller1Acc)
  690. // A single input, having the permission, but the contract doesn't have permission
  691. txCall, _ = types.NewCallTx(blockCache, user[0].PubKey, caller1ContractAddr, nil, 100, 10000, 100)
  692. txCall.Sign(user[0])
  693. // we need to subscribe to the Receive event to detect the exception
  694. _, exception = execTxWaitEvent(t, blockCache, txCall, types.EventStringAccReceive(caller1ContractAddr)) //
  695. if exception != "" {
  696. t.Fatal("Unexpected exception", exception)
  697. }
  698. }
  699. //-------------------------------------------------------------------------------------
  700. // helpers
  701. // run ExecTx and wait for the Receive event on given addr
  702. // returns the msg data and an error/exception
  703. func execTxWaitEvent(t *testing.T, blockCache *BlockCache, tx types.Tx, eventid string) (interface{}, string) {
  704. evsw := new(events.EventSwitch)
  705. evsw.Start()
  706. ch := make(chan interface{})
  707. evsw.AddListenerForEvent("test", eventid, func(msg interface{}) {
  708. ch <- msg
  709. })
  710. evc := events.NewEventCache(evsw)
  711. go func() {
  712. if err := ExecTx(blockCache, tx, true, evc); err != nil {
  713. ch <- err.Error()
  714. }
  715. evc.Flush()
  716. }()
  717. msg := <-ch
  718. switch ev := msg.(type) {
  719. case types.EventMsgCallTx:
  720. return ev, ev.Exception
  721. case types.EventMsgCall:
  722. return ev, ev.Exception
  723. case string:
  724. return nil, ev
  725. default:
  726. return ev, ""
  727. }
  728. }
  729. // convenience function for contract that calls a given address
  730. func callContractCode(contractAddr []byte) []byte {
  731. // calldatacopy into mem and use as input to call
  732. memOff, inputOff := byte(0x0), byte(0x0)
  733. contractCode := []byte{0x60, memOff, 0x60, inputOff, 0x36, 0x37}
  734. gas1, gas2 := byte(0x1), byte(0x1)
  735. value := byte(0x1)
  736. inOff := byte(0x0)
  737. retOff, retSize := byte(0x0), byte(0x20)
  738. // this is the code we want to run (call a contract and return)
  739. contractCode = append(contractCode, []byte{0x60, retSize, 0x60, retOff, 0x36, 0x60, inOff, 0x60, value, 0x73}...)
  740. contractCode = append(contractCode, contractAddr...)
  741. contractCode = append(contractCode, []byte{0x61, gas1, gas2, 0xf1, 0x60, 0x20, 0x60, 0x0, 0xf3}...)
  742. return contractCode
  743. }
  744. // convenience function for contract that is a factory for the code that comes as call data
  745. func createContractCode() []byte {
  746. // TODO: gas ...
  747. // calldatacopy the calldatasize
  748. memOff, inputOff := byte(0x0), byte(0x0)
  749. contractCode := []byte{0x60, memOff, 0x60, inputOff, 0x36, 0x37}
  750. // create
  751. value := byte(0x1)
  752. contractCode = append(contractCode, []byte{0x60, value, 0x36, 0x60, memOff, 0xf0}...)
  753. return contractCode
  754. }
  755. // wrap a contract in create code
  756. func wrapContractForCreate(contractCode []byte) []byte {
  757. // the is the code we need to return the contractCode when the contract is initialized
  758. lenCode := len(contractCode)
  759. // push code to the stack
  760. code := append([]byte{0x7f}, RightPadWord256(contractCode).Bytes()...)
  761. // store it in memory
  762. code = append(code, []byte{0x60, 0x0, 0x52}...)
  763. // return whats in memory
  764. code = append(code, []byte{0x60, byte(lenCode), 0x60, 0x0, 0xf3}...)
  765. // return init code, contract code, expected return
  766. return code
  767. }