github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/blockchain/rpcserver/getblock_template.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 rpcserver
    18  
    19  // get block template handler not implemented
    20  
    21  //import "fmt"
    22  import "time"
    23  import "context"
    24  
    25  //import	"log"
    26  //import 	"net/http"
    27  
    28  import "github.com/intel-go/fastjson"
    29  import "github.com/osamingo/jsonrpc"
    30  
    31  import "golang.org/x/time/rate"
    32  
    33  import "github.com/deroproject/derosuite/config"
    34  import "github.com/deroproject/derosuite/address"
    35  import "github.com/deroproject/derosuite/structures"
    36  import "github.com/deroproject/derosuite/transaction"
    37  
    38  type GetBlockTemplate_Handler struct{}
    39  
    40  // rate limiter is deployed, in case RPC is exposed over internet
    41  // someone should not be just giving fake inputs and delay chain syncing
    42  var get_block_limiter = rate.NewLimiter(16.0, 8) // 16 req per sec, burst of 8 req is okay
    43  
    44  
    45  func (h GetBlockTemplate_Handler) ServeJSONRPC(c context.Context, params *fastjson.RawMessage) (interface{}, *jsonrpc.Error) {
    46  	var p structures.GetBlockTemplate_Params
    47  	if err := jsonrpc.Unmarshal(params, &p); err != nil {
    48  		return nil, err
    49  	}
    50  	
    51  	/*
    52  	if !get_block_limiter.Allow() { // if rate limiter allows, then add block to chain
    53  		logger.Warnf("Too many get block template requests per sec rejected by chain.")
    54                  
    55                  return nil,&jsonrpc.Error{
    56  		Code:    jsonrpc.ErrorCodeInvalidRequest,
    57  		Message: "Too many get block template requests per sec rejected by chain.",
    58  	}
    59  	
    60              
    61  	}
    62      */
    63  
    64  	/*
    65  		return structures.GetBlockTemplate_Result{
    66  				Status: "GetBlockTemplate_Handler NOT implemented",
    67  			}, nil
    68  
    69  	*/
    70  
    71  	// validate address
    72  	miner_address, err := address.NewAddress(p.Wallet_Address)
    73  	if err != nil {
    74  		return structures.GetBlockTemplate_Result{
    75  			Status: "Wallet address could not be parsed",
    76  		}, nil
    77  	}
    78  
    79  	if p.Reserve_size > 255 || p.Reserve_size < 1 {
    80  		return structures.GetBlockTemplate_Result{
    81  			Status: "Reserve size should be > 0 and < 255",
    82  		}, nil
    83  	}
    84  	
    85  	bl, block_hashing_blob_hex, block_template_hex, reserved_pos := chain.Create_new_block_template_mining(chain.Get_Top_ID(), *miner_address, int(p.Reserve_size))
    86  
    87  	prev_hash := ""
    88  	for i := range bl.Tips {
    89  		prev_hash = prev_hash + bl.Tips[i].String()
    90  	}
    91  	return structures.GetBlockTemplate_Result{
    92  		Blocktemplate_blob: block_template_hex,
    93  		Blockhashing_blob:  block_hashing_blob_hex,
    94  		Reserved_Offset:    uint64(reserved_pos),
    95  		Expected_reward:    0, // fill in actual reward
    96  		Height:             bl.Miner_TX.Vin[0].(transaction.Txin_gen).Height,
    97  		Prev_Hash:          prev_hash,
    98  		Epoch:              uint64(uint64(time.Now().UTC().Unix()) + config.BLOCK_TIME), // expiry time of this block
    99  		Difficulty:         chain.Get_Difficulty_At_Tips(nil, bl.Tips).Uint64(),
   100  		Status:             "OK",
   101  	}, nil
   102  
   103  }