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.

190 lines
6.0KB

  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. // this also does the double spending test, which executes double spend, however the chain client protocol must be able to handle that
  45. func QTest_Basic_Simulator(t *testing.T) {
  46. /*// override simulation hard fork
  47. blockchain.Simulation_hard_forks = []blockchain.Hard_fork{
  48. {1, 0, 0, 0, 0, true}, // version 1 hard fork where genesis block landed
  49. {6, 1, 0, 0, 0, true}, // version 6 hard fork where we started , it's mandatory
  50. {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
  51. }*/
  52. // start the simulator ( mine genesis block )
  53. sim, _ := simulator.Init_Simulator(config.Testnet)
  54. defer sim.Stop()
  55. // create 3 wallets, w1,w2,w3
  56. temp_db := filepath.Join(os.TempDir(), "test_w1.db")
  57. os.Remove(temp_db)
  58. w1, err := walletapi.Create_Encrypted_Wallet(temp_db, "QWER", *crypto.RandomScalar())
  59. if err != nil {
  60. t.Fatalf("Cannot create encrypted wallet, err %s", err)
  61. }
  62. temp_db = filepath.Join(os.TempDir(), "test_w2.db")
  63. os.Remove(temp_db)
  64. w2, err := walletapi.Create_Encrypted_Wallet(temp_db, "QWER", *crypto.RandomScalar())
  65. if err != nil {
  66. t.Fatalf("Cannot create encrypted wallet, err %s", err)
  67. }
  68. temp_db = filepath.Join(os.TempDir(), "test_w3.db")
  69. os.Remove(temp_db)
  70. w3, err := walletapi.Create_Encrypted_Wallet(temp_db, "QWER", *crypto.RandomScalar())
  71. if err != nil {
  72. t.Fatalf("Cannot create encrypted wallet, err %s", err)
  73. }
  74. _ = w2
  75. _ = w3
  76. // mine the first block and get reward
  77. sim.Mine_New_Block_Address(w1.GetAddress())
  78. /*sim.Rescan_Wallet(w1)
  79. unlocked, locked := w1.Get_Balance()
  80. globals.Logger.Infof("W1 balances %s : %s locked", globals.FormatMoney(unlocked), globals.FormatMoney(locked))
  81. */
  82. t.Logf("Chain height %d", sim.Chain.Get_Height())
  83. for i := 0; i < 70; i++ {
  84. /*sim.Rescan_Wallet(w1)
  85. unlocked, locked := w1.Get_Balance_Rescan()
  86. globals.Logger.Infof("W1 balances %s : %s locked", globals.FormatMoney(unlocked), globals.FormatMoney(locked))
  87. // */
  88. // add a random block to random miner
  89. sim.Mine_New_Block() // mine block after genesis block
  90. t.Logf("Chain height %d", sim.Chain.Get_Height())
  91. }
  92. t.Logf("Chain height %d done", sim.Chain.Get_Height())
  93. // sync the wallets
  94. w1.Sync_Wallet_With_Daemon()
  95. w2.Sync_Wallet_With_Daemon()
  96. w3.Sync_Wallet_With_Daemon()
  97. tx, _, _, _, err := w1.Transfer([]address.Address{w3.GetAddress()}, []uint64{1000}, 0, "", 25037535000, 0)
  98. if err != nil {
  99. t.Fatalf("Error creating transaction err %s", err)
  100. }
  101. if !sim.Chain.Mempool.Mempool_Add_TX(tx, 0) {
  102. t.Fatalf("Add TX to pool failed ")
  103. }
  104. sim.Chain.Mempool.Mempool_Print()
  105. sim.Mine_New_Block()
  106. /* blocks := sim.Chain.Get_Blocks_At_Height(3)
  107. past := sim.Chain.Get_Past_Unsettled(blocks[0])
  108. // past := sim.Chain.Get_Past_Unsettled(crypto.HashHexToHash("78845caac717c18e34682c2390b49de7e73e69633cc5f7eb2ce3f62948ddb8cd"))
  109. t.Logf("Past of %s is %+v\n", blocks[0],past)
  110. for k,_ := range past {
  111. t.Logf("Past %s\n", k)
  112. }
  113. */
  114. //blocks = sim.Chain.Get_Blocks_At_Height(7)
  115. /*
  116. future := sim.Chain.Get_Future_Unsettled(blocks[0])
  117. t.Logf("Future of %s is %+v\n", blocks[0],future)
  118. for k,_ := range future {
  119. t.Logf("Future %s\n", k)
  120. }
  121. dag := sim.Chain.Get_DAG_Unsettled_Cached()
  122. for k,_ := range dag { // for all nodes find the anticone
  123. dag_copy := sim.Chain.Get_AntiCone_Unsettled(k)
  124. fmt.Printf("Anticone of %s is %d\n", k, len(dag_copy))
  125. if len(dag_copy) == 0 {
  126. // order := sim.Chain.Generate_Full_Order(k,0)
  127. //
  128. //for i := range order {
  129. // t.Logf("%2d %s", i,order[i])
  130. //}
  131. }
  132. }
  133. */
  134. if sim.Chain.Get_Height() < 100 {
  135. blockchain.WriteBlockChainTree(sim.Chain, "/tmp/graph.dot")
  136. t.Logf("Written graph")
  137. }
  138. time.Sleep(1000 * time.Second)
  139. }