github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/blockchain/rpcserver/getinfo.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  //import "fmt"
    20  //import "time"
    21  import "context"
    22  
    23  //import	"log"
    24  //import 	"net/http"
    25  
    26  import "github.com/intel-go/fastjson"
    27  import "github.com/osamingo/jsonrpc"
    28  
    29  import "github.com/deroproject/derosuite/config"
    30  import "github.com/deroproject/derosuite/globals"
    31  import "github.com/deroproject/derosuite/structures"
    32  
    33  type GetInfo_Handler struct{}
    34  
    35  // TODO
    36  func (h GetInfo_Handler) ServeJSONRPC(c context.Context, params *fastjson.RawMessage) (interface{}, *jsonrpc.Error) {
    37  	var result structures.GetInfo_Result
    38  
    39  	//top_id := chain.Get_Top_ID()
    40  
    41  	//result.Difficulty = chain.Get_Difficulty_At_Block(top_id)
    42  	result.Height = chain.Get_Height()
    43  	result.StableHeight = chain.Get_Stable_Height()
    44  	result.TopoHeight = chain.Load_TOPO_HEIGHT(nil)
    45  
    46  	blid, err := chain.Load_Block_Topological_order_at_index(nil, result.TopoHeight)
    47  	if err == nil {
    48  		result.Difficulty = chain.Get_Difficulty_At_Tips(nil, chain.Get_TIPS()).Uint64()
    49  	}
    50  
    51  	result.Status = "OK"
    52  	result.Version = config.Version.String()
    53  	result.Top_block_hash = blid.String()
    54  	result.Target = chain.Get_Current_BlockTime()
    55  
    56  	if result.TopoHeight > 50 {
    57  		blid50, err := chain.Load_Block_Topological_order_at_index(nil, result.TopoHeight-50)
    58  		if err == nil {
    59  			now := chain.Load_Block_Timestamp(nil, blid)
    60  			now50 := chain.Load_Block_Timestamp(nil, blid50)
    61  			result.AverageBlockTime50 = float32(now-now50) / 50.0
    62  		}
    63  	}
    64  
    65  	//result.Target_Height = uint64(chain.Get_Height())
    66  	result.Tx_pool_size = uint64(len(chain.Mempool.Mempool_List_TX()))
    67  	// get dynamic fees per kb, used by wallet for tx creation
    68  	result.Dynamic_fee_per_kb = config.FEE_PER_KB
    69  	result.Median_Block_Size = config.CRYPTONOTE_MAX_BLOCK_SIZE
    70  
    71  	result.Total_Supply = chain.Load_Already_Generated_Coins_for_Topo_Index(nil, result.TopoHeight)
    72  	if result.Total_Supply > (1000000 * 1000000000000) {
    73  		result.Total_Supply -= (1000000 * 1000000000000) // remove  premine
    74  	}
    75  	result.Total_Supply = result.Total_Supply / 1000000000000
    76  
    77  	if globals.Config.Name != config.Mainnet.Name { // anything other than mainnet is testnet at this point in time
    78  		result.Testnet = true
    79  	}
    80  
    81  	return result, nil
    82  }