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.

236 lines
7.4KB

  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. // this package builds up a testnet/mainnet simulator with temporary databases for wide variety of simulation and testing
  17. // this is required for further development and testing of blockchain,smart-contracts and related DAPPs
  18. // the simulator will have hard-coded difficulty set to 1
  19. // the simulator will expose the RPC interface, so as the explorer and other programs can be developed/tested
  20. package simulator
  21. //import "fmt"
  22. //import _ "unsafe" // enable golink to enable difficulty package in simulation mode
  23. //import mrand "math/rand"
  24. //import "crypto/rand"
  25. //import "encoding/binary"
  26. import "github.com/sirupsen/logrus"
  27. //import "github.com/vmihailenco/msgpack"
  28. import "github.com/deroproject/derosuite/block"
  29. import "github.com/deroproject/derosuite/config"
  30. import "github.com/deroproject/derosuite/globals"
  31. import "github.com/deroproject/derosuite/address"
  32. import "github.com/deroproject/derosuite/walletapi"
  33. import "github.com/deroproject/derosuite/blockchain"
  34. //import "github.com/deroproject/derosuite/block"
  35. import "github.com/deroproject/derosuite/blockchain/rpcserver"
  36. type Simulator struct {
  37. Chain *blockchain.Blockchain
  38. rpc *rpcserver.RPCServer
  39. }
  40. // use unsafe to bring difficulty in simulation mode
  41. ////go:linkname difficulty_simulation_mode github.com/deroproject/derosuite/difficulty.simulation
  42. //var difficulty_simulation_mode bool
  43. // initialize a simulator instance
  44. func Init_Simulator(c config.CHAIN_CONFIG) (*Simulator, error) {
  45. var sim Simulator
  46. // manually initialize globals ( simulator does not need socks proxy )
  47. globals.Config = c
  48. globals.Logger = logrus.New()
  49. globals.Logger.SetLevel(logrus.DebugLevel)
  50. // enable difficulty over-ride to hard-code value 1
  51. //difficulty_simulation_mode = true // set difficulty to simulation mode
  52. params := map[string]interface{}{}
  53. params["--disable-checkpoints"] = true // do NOT enforce checkpoints while simulating
  54. params["--simulator"] = true
  55. chain, _ := blockchain.Blockchain_Start(params)
  56. params["chain"] = chain
  57. sim.Chain = chain
  58. sim.rpc, _ = rpcserver.RPCServer_Start(params)
  59. return &sim, nil
  60. }
  61. // this will mine a new block with random miner address ( miner reward cannot be claimed )
  62. func (sim *Simulator) Mine_New_Block() bool {
  63. account, _ := walletapi.Generate_Keys_From_Random() // create a random address
  64. return sim.Mine_New_Block_Address(account.GetAddress()) // mine new block at that address
  65. }
  66. // this will mine a new block with specific miner address
  67. func (sim *Simulator) Mine_New_Block_Address(addr address.Address) bool {
  68. junk := block.Block{}
  69. _ = junk
  70. tips := sim.Chain.Get_TIPS()
  71. height := sim.Chain.Calculate_Height_At_Tips(nil, tips)
  72. _ = height
  73. cbl, bl := sim.Chain.Create_new_miner_block(addr)
  74. cbl.Bl = &bl
  75. /*
  76. // bl.Major_Version = uint64(sim.Chain.Get_Current_Version_at_Height(height))
  77. //bl.Minor_Version = uint64(sim.Chain.Get_Ideal_Version_at_Height(height))
  78. //bl.Timestamp = 0 // first block timestamp
  79. bl.Tips = bl.Tips[:0]
  80. b := make([]byte, 4)
  81. switch len(tips) {
  82. case 1 :
  83. bl.Tips = append(bl.Tips,tips[0])
  84. // add a random nonce
  85. rand.Read(b) // assumming it will never fail
  86. mrand.Read(b)
  87. cbl.Bl.Nonce = binary.LittleEndian.Uint32(b)
  88. sim.Chain.Add_Complete_Block(cbl) // add 1 block
  89. var err error
  90. bl.Miner_TX,err = blockchain.Create_Miner_TX2(int64(bl.Major_Version),height,addr)
  91. if err != nil {
  92. fmt.Printf("Error while creating miner tx , err %s", err)
  93. }
  94. case 2 : // randomly include one or the tips
  95. if mrand.Intn(3) == 0 {
  96. bl.Tips = tips
  97. }else{
  98. bl.Tips = append(bl.Tips,tips[mrand.Intn(len(tips))])
  99. // also choose another tip from other nodes past
  100. for i := range tips{
  101. if tips[i] != bl.Tips[0] {
  102. // load tips from pasts tips
  103. bl2,err := sim.Chain.Load_BL_FROM_ID(nil,tips[i])
  104. if err != nil {
  105. panic(fmt.Sprintf("Block NOT found %s", tips[i]))
  106. }
  107. bl.Tips = append(bl.Tips,bl2.Tips[0])
  108. }
  109. }
  110. }
  111. case 3:
  112. bl.Tips = tips
  113. }
  114. //cbl, _ := sim.Chain.Create_new_miner_block(sim.Chain.Get_Top_ID(), addr, 0) // create a new block with
  115. // TODO enable mechanisms to patch the block here
  116. // TODO add any tx from the pool if required
  117. rand.Read(b) // assumming it will never fail
  118. mrand.Read(b)
  119. cbl.Bl.Nonce = binary.LittleEndian.Uint32(b)
  120. */
  121. // mine the block means extend the chain , simulator mode difficulty is always 1 so everything passes thorough
  122. _, status := sim.Chain.Add_Complete_Block(cbl)
  123. return status
  124. }
  125. // this function will attach alt count blocks with the current tip
  126. // this gives the oppurtunity for easier testing
  127. // all the blocks except first one will be alt blocks
  128. // mempool will be processed and will be common while building the blocks
  129. func (sim *Simulator) Mine_New_Block_Address_Alternate(addr address.Address, alt_count int) bool {
  130. var cbl_array []*block.Complete_Block
  131. for i := 0; i < alt_count; i++ {
  132. cbl, _ := sim.Chain.Create_new_miner_block(addr)
  133. cbl_array = append(cbl_array, cbl)
  134. }
  135. for i := range cbl_array {
  136. sim.Chain.Add_Complete_Block(cbl_array[i])
  137. }
  138. return true
  139. }
  140. // stop the simulator instance freeing resource
  141. func (sim *Simulator) Stop() {
  142. sim.rpc.RPCServer_Stop() // stop rpc server
  143. sim.Chain.Shutdown() // shutdown chain subsysem
  144. }
  145. // rescan entire blockchain, locate and parse outputs
  146. // this scans the chain from the restart everytime, so can be slow
  147. func (sim *Simulator) Rescan_Wallet(w *walletapi.Wallet) {
  148. if w == nil {
  149. return
  150. }
  151. /*
  152. // load output indices limit of top block
  153. top_id := sim.Chain.Get_Top_ID()
  154. biggest_output_index := sim.Chain.Block_Count_Vout(nil,top_id) + sim.Chain.Get_Block_Output_Index(nil,top_id)
  155. for i := uint64(0); i < biggest_output_index; i++ {
  156. data, err := sim.Chain.Read_output_index(nil,i)
  157. if err != nil {
  158. fmt.Printf("err while reading output %d err: %s\n", i, err)
  159. break
  160. }
  161. var output globals.TX_Output_Data
  162. err = msgpack.Unmarshal(data, &output)
  163. if err != nil {
  164. fmt.Printf("err while decoding output %d err: %s\n", i, err)
  165. }
  166. // feed to wallet
  167. w.Add_Transaction_Record_Funds(&output)
  168. }
  169. */
  170. }