github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/blockchain/block_chain_input.go (about)

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