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.

124 lines
4.0 KiB

  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 blockchain
  17. /*
  18. import log "github.com/sirupsen/logrus"
  19. import "github.com/arnaucode/derosuite/crypto"
  20. import "github.com/arnaucode/derosuite/block"
  21. import "github.com/arnaucode/derosuite/transaction"
  22. //import "github.com/arnaucode/derosuite/blockchain/mempool"
  23. */
  24. // DERO blockchain has been designed/developed as a state machine ( single-threaded)
  25. // The state machine cannot change until there is external input
  26. // the blockchain has 2 events as input
  27. // 1) new block
  28. // 2) new transaction
  29. // So, the blockchain just waits for events from 2 sources
  30. // 1) p2p layer ( network did a trasaction or found new block after mining)
  31. // 2) user side ( user did a transaction or found new block after mining)
  32. // the design has been simplified so as smart contracts can be integrated easily
  33. // NOTE that adding a block is an atomic event in DERO blockchain
  34. //
  35. //
  36. // This is the global event handler for block
  37. // any block whether mined locally or via network must be dispatched using this channel
  38. //var Incoming_Block_Channel = make(chan *block.Complete_Block, 512) // upto 500 blocks can be queued
  39. //var Incoming_Transaction_Channel = make(chan *transaction.Transaction, 512) // upto 500 transactions can be queued
  40. /*
  41. // infinite looping function to process incoming block
  42. // below event loops are never terminated, not even while exiting
  43. // but we take the lock of chain, so as state/db cannot be changed
  44. // also note that p2p layer is stopped earlier, so input cannot appear
  45. func (chain *Blockchain) Handle_Block_Event_Loop(){
  46. for{
  47. select{
  48. case <-chain.Exit_Event:
  49. logger.Debugf("Exiting Block event loop")
  50. return
  51. default:
  52. }
  53. select{
  54. case <-chain.Exit_Event:
  55. logger.Debugf("Exiting Block event loop")
  56. return
  57. case complete_bl := <- Incoming_Block_Channel:
  58. logger.Debugf("Incoming New Block")
  59. func (){
  60. var blid crypto.Hash
  61. _ = blid
  62. // defer func() { // safety so if anything wrong happens, verification fails
  63. /// if r := recover(); r != nil {
  64. // logger.WithFields( log.Fields{"blid": blid}).Warnf("Recovered while processing incoming block")
  65. // }}()
  66. blid = complete_bl.Bl.GetHash()
  67. chain.add_Complete_Block(complete_bl)
  68. }()
  69. //default:
  70. //case <- Exit_Event:
  71. }
  72. }
  73. }
  74. // infinite looping function to process incoming block
  75. func (chain *Blockchain) Handle_Transaction_Event_Loop(){
  76. for{
  77. select {
  78. case <-chain.Exit_Event:
  79. logger.Debugf("Exiting Block event loop")
  80. return
  81. case tx := <- Incoming_Transaction_Channel:
  82. logger.Debugf("Incoming New Transaction")
  83. func() {
  84. var txid crypto.Hash
  85. defer func() { // safety so if anything wrong happens, verification fails
  86. if r := recover(); r != nil {
  87. logger.WithFields( log.Fields{"txid": txid}).Warnf("Recovered while Verifying transaction, failed verification")
  88. //result = false
  89. }}()
  90. txid = tx.GetHash()
  91. }()
  92. // case <- Exit_Event:
  93. }
  94. }
  95. }
  96. */