Dero Web Wallet derosuite. This repo contains changes required for webwallet. With this changes any Dero daemon can become web wallet. All changes are related to daemon rpc server and wallet to make it light and faster.
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.

206 lines
6.3KB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package main
  17. import "os"
  18. import "fmt"
  19. import "time"
  20. import "testing"
  21. import "path/filepath"
  22. import "runtime/pprof"
  23. //import "encoding/hex"
  24. import "github.com/deroproject/derosuite/simulator"
  25. import "github.com/deroproject/derosuite/config"
  26. import "github.com/deroproject/derosuite/crypto"
  27. //import "github.com/deroproject/derosuite/globals"
  28. import "github.com/deroproject/derosuite/address"
  29. import "github.com/deroproject/derosuite/walletapi"
  30. import "github.com/deroproject/derosuite/blockchain"
  31. func main() {
  32. var t testing.T
  33. f, err := os.Create("cpu.prof")
  34. if err != nil {
  35. fmt.Printf("%s\n", err)
  36. return
  37. }
  38. pprof.StartCPUProfile(f)
  39. defer pprof.StopCPUProfile()
  40. QTest_Basic_Simulator(&t)
  41. }
  42. // testing basic functionality of block chain, whether the chain can be extended and balances decoded
  43. // mem pool okay etc
  44. func QTest_Basic_Simulator(t *testing.T) {
  45. /*// override simulation hard fork
  46. blockchain.Simulation_hard_forks = []blockchain.Hard_fork{
  47. {1, 0, 0, 0, 0, true}, // version 1 hard fork where genesis block landed
  48. {6, 1, 0, 0, 0, true}, // version 6 hard fork where we started , it's mandatory
  49. {7, 40, 31, 60, 00, false}, // version 7 hard fork where we started , threshold is 60 % means atleast 18 votes should be in favor, from block 10 till block 40
  50. }*/
  51. // start the simulator ( mine genesis block )
  52. sim, _ := simulator.Init_Simulator(config.Testnet)
  53. //defer sim.Stop()
  54. // create 3 wallets, w1,w2,w3
  55. temp_db := filepath.Join(os.TempDir(), "test_w1.db")
  56. os.Remove(temp_db)
  57. w1, err := walletapi.Create_Encrypted_Wallet(temp_db, "QWER", crypto.HexToKey("366c28ae6fcdf4885c6c830de792e630ec5c2e61c4dc5cca9259ab2006ee1b05"))
  58. if err != nil {
  59. t.Fatalf("Cannot create encrypted wallet, err %s", err)
  60. }
  61. temp_db = filepath.Join(os.TempDir(), "test_w2.db")
  62. os.Remove(temp_db)
  63. w2, err := walletapi.Create_Encrypted_Wallet(temp_db, "QWER", crypto.HexToKey("f3abe1ad37402b6f026e2ced1fd44d6eea692d68c43ae9886752ee62752d5706"))
  64. if err != nil {
  65. t.Fatalf("Cannot create encrypted wallet, err %s", err)
  66. }
  67. temp_db = filepath.Join(os.TempDir(), "test_w3.db")
  68. os.Remove(temp_db)
  69. w3, err := walletapi.Create_Encrypted_Wallet(temp_db, "QWER", crypto.HexToKey("108059799dcd40b9a9f91d9f1dda5710ae0a10d7b355adf04f751788a813100e"))
  70. if err != nil {
  71. t.Fatalf("Cannot create encrypted wallet, err %s", err)
  72. }
  73. _ = w2
  74. _ = w3
  75. // mine the first block and get reward
  76. sim.Mine_New_Block_Address(w1.GetAddress())
  77. /*sim.Rescan_Wallet(w1)
  78. unlocked, locked := w1.Get_Balance()
  79. globals.Logger.Infof("W1 balances %s : %s locked", globals.FormatMoney(unlocked), globals.FormatMoney(locked))
  80. */
  81. t.Logf("Chain height %d", sim.Chain.Get_Height())
  82. for i := 0; i < 70; i++ {
  83. /*sim.Rescan_Wallet(w1)
  84. unlocked, locked := w1.Get_Balance_Rescan()
  85. globals.Logger.Infof("W1 balances %s : %s locked", globals.FormatMoney(unlocked), globals.FormatMoney(locked))
  86. // */
  87. // add a random block to random miner
  88. // random insert 3 ( so called orphan blocks in blockchain tech)
  89. if i%5 == 0 {
  90. sim.Mine_New_Block_Address_Alternate(w1.GetAddress(), 3)
  91. } else {
  92. sim.Mine_New_Block() // mine block after genesis block
  93. }
  94. t.Logf("Chain height %d", sim.Chain.Get_Height())
  95. }
  96. t.Logf("Chain height %d done", sim.Chain.Get_Height())
  97. // now lets rescan a wallet and build a TX
  98. // sync the wallets
  99. w1.Sync_Wallet_With_Daemon()
  100. tx, _, _, _, err := w1.Transfer([]address.Address{w3.GetAddress()}, []uint64{1000000000000}, 0, "", 25037535000, 0)
  101. if err != nil {
  102. t.Fatalf("Error creating transaction err %s", err)
  103. }
  104. if !sim.Chain.Mempool.Mempool_Add_TX(tx, 0) {
  105. t.Fatalf("Add TX to pool failed ")
  106. }
  107. sim.Mine_New_Block_Address_Alternate(w1.GetAddress(), 2) // mine the TX in 2 alt blocks
  108. sim.Mine_New_Block() // lets join the tips together
  109. sim.Mine_New_Block() // lets mine another block to trigger client_protocol in reverse and then fix it up
  110. /* for i := 0; i <100; i++{
  111. sim.Mine_New_Block()
  112. }
  113. */
  114. w3.Sync_Wallet_With_Daemon()
  115. time.Sleep(10 * time.Second)
  116. unlocked, locked := w3.Get_Balance_Rescan()
  117. fmt.Printf("Balance %d locked %d unlocked ", locked, unlocked)
  118. /* blocks := sim.Chain.Get_Blocks_At_Height(3)
  119. past := sim.Chain.Get_Past_Unsettled(blocks[0])
  120. // past := sim.Chain.Get_Past_Unsettled(crypto.HashHexToHash("78845caac717c18e34682c2390b49de7e73e69633cc5f7eb2ce3f62948ddb8cd"))
  121. t.Logf("Past of %s is %+v\n", blocks[0],past)
  122. for k,_ := range past {
  123. t.Logf("Past %s\n", k)
  124. }
  125. */
  126. //blocks = sim.Chain.Get_Blocks_At_Height(7)
  127. /*
  128. future := sim.Chain.Get_Future_Unsettled(blocks[0])
  129. t.Logf("Future of %s is %+v\n", blocks[0],future)
  130. for k,_ := range future {
  131. t.Logf("Future %s\n", k)
  132. }
  133. dag := sim.Chain.Get_DAG_Unsettled_Cached()
  134. for k,_ := range dag { // for all nodes find the anticone
  135. dag_copy := sim.Chain.Get_AntiCone_Unsettled(k)
  136. fmt.Printf("Anticone of %s is %d\n", k, len(dag_copy))
  137. if len(dag_copy) == 0 {
  138. // order := sim.Chain.Generate_Full_Order(k,0)
  139. //
  140. //for i := range order {
  141. // t.Logf("%2d %s", i,order[i])
  142. //}
  143. }
  144. }
  145. */
  146. if sim.Chain.Get_Height() < 100 {
  147. blockchain.WriteBlockChainTree(sim.Chain, "/tmp/graph.dot")
  148. t.Logf("Written graph")
  149. }
  150. time.Sleep(500 * time.Second)
  151. }