github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/core/state/resource.go (about)

     1  package state
     2  
     3  import (
     4  	"github.com/insight-chain/inb-go/params"
     5  	"math/big"
     6  	"strconv"
     7  	"sync/atomic"
     8  	"time"
     9  )
    10  
    11  var (
    12  	PerINBGainNetNum *big.Int
    13  	PerINBGainCpuNum *big.Int
    14  )
    15  
    16  type record struct {
    17  	rddData RdDdataInterface
    18  	hash    atomic.Value
    19  	size    atomic.Value
    20  	from    atomic.Value
    21  }
    22  type RdDdataInterface interface {
    23  }
    24  type BaseRecordData struct {
    25  	AccountNonce uint64   `json:"nonce"        gencodec:"required"`
    26  	CpuPrice     *big.Int `json:"netPrice"     gencodec:"required"`
    27  	NetPrice     *big.Int `json:"cpuPrice"     gencodec:"required"`
    28  	Amount       *big.Int `json:"value"        gencodec:"required"` //inb number
    29  	CpuAmount    *big.Int `json:"CpuAmount"    gencodec:"required"` //cpu number
    30  	NetAmount    *big.Int `json:"NetAmount"    gencodec:"required"` //net number
    31  	//sign
    32  	V *big.Int `json:"v" gencodec:"required"`
    33  	R *big.Int `json:"r" gencodec:"required"`
    34  	S *big.Int `json:"s" gencodec:"required"`
    35  }
    36  type Mortgagtion struct {
    37  	BaseRecordData
    38  	mortgagtionDate time.Time
    39  }
    40  type Unmortgagtion struct {
    41  	BaseRecordData
    42  	UnmortgagtionDate time.Time
    43  	expectedDate      time.Time
    44  }
    45  
    46  //1 inb can be exchanged for Net
    47  func (c *StateDB) PerInbIsNet() *big.Int {
    48  
    49  	as := c.GetMortgageStateObject().data.Res.StakingValue
    50  	asString := as.Set(as).String()
    51  	asValue, err := strconv.ParseInt(asString, 10, 64)
    52  	if err != nil {
    53  
    54  	}
    55  
    56  	if as == big.NewInt(0) || asValue == 0 {
    57  		PerINBGainNetNum = big.NewInt(1).Div(params.TxConfig.Net, params.TxConfig.Circulation)
    58  	} else {
    59  		PerINBGainNetNum = as.Div(params.TxConfig.Net, as)
    60  	}
    61  	return PerINBGainNetNum
    62  }
    63  
    64  //achilles improve net using
    65  // ratio that 1e * 14 wei exchange into net
    66  func (c *StateDB) UnitConvertNet() *big.Int {
    67  
    68  	//get mortgaged inbs with whole network
    69  	totalMortgageInb := big.NewInt(0)
    70  	mortgageObject := c.GetMortgagePreviousStateObject()
    71  	if mortgageObject != nil {
    72  		totalMortgageInb = c.GetMortgagePreviousStateObject().data.Balance
    73  	}
    74  	mortgage := totalMortgageInb.Div(totalMortgageInb, params.TxConfig.Wei)
    75  	if mortgage.Cmp(params.TxConfig.MortgageInbLimit) < 0 {
    76  		totalMortgageInb = params.TxConfig.MortgageInbLimit
    77  	}
    78  	if mortgage.Cmp(big.NewInt(1e+10)) > 0 {
    79  		totalMortgageInb = big.NewInt(1e+10)
    80  	}
    81  	weiToNet := big.NewInt(1).Div(params.TxConfig.Net, totalMortgageInb)
    82  	temp := big.NewInt(1).Div(params.TxConfig.Wei, params.TxConfig.WeiOfUseNet)
    83  	weiToNet.Div(weiToNet, temp)
    84  	return weiToNet
    85  }
    86  
    87  //mortgage how much NET inb gets
    88  func (c *StateDB) GainNumberOfNet(inbNumber *big.Int) *big.Int {
    89  
    90  	everyINBGainNetNum := c.PerInbIsNet()
    91  
    92  	if everyINBGainNetNum == big.NewInt(0) {
    93  		return big.NewInt(0)
    94  	} else {
    95  		if inbNumber != nil {
    96  			return big.NewInt(1).Mul(inbNumber, everyINBGainNetNum)
    97  		}
    98  		return big.NewInt(0)
    99  	}
   100  }