github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/extstates/league_account.go (about)

     1  package extstates
     2  
     3  import (
     4  	"encoding/binary"
     5  	"math/big"
     6  	"sort"
     7  
     8  	"github.com/sixexorg/magnetic-ring/common/sink"
     9  
    10  	"io"
    11  
    12  	"io/ioutil"
    13  
    14  	"bytes"
    15  
    16  	"github.com/sixexorg/magnetic-ring/common"
    17  	"github.com/sixexorg/magnetic-ring/errors"
    18  	mcom "github.com/sixexorg/magnetic-ring/store/mainchain/common"
    19  )
    20  
    21  type LeagueAccountState struct {
    22  	Address  common.Address
    23  	LeagueId common.Address
    24  	hash     common.Hash
    25  	Height   uint64
    26  	Data     *Account
    27  }
    28  
    29  type Account struct {
    30  	Nonce       uint64
    31  	Balance     *big.Int
    32  	EnergyBalance *big.Int
    33  	BonusHeight uint64
    34  }
    35  
    36  type LeagueAccountStates []*LeagueAccountState
    37  
    38  func (s LeagueAccountStates) Len() int           { return len(s) }
    39  func (s LeagueAccountStates) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    40  func (s LeagueAccountStates) Less(i, j int) bool { return s[i].hash.String() < s[j].hash.String() }
    41  func (s LeagueAccountStates) GetHashRoot() common.Hash {
    42  	sort.Sort(s)
    43  	hashes := make([]common.Hash, 0, s.Len())
    44  	for _, v := range s {
    45  		hashes = append(hashes, v.Hash())
    46  	}
    47  	return common.ComputeMerkleRoot(hashes)
    48  }
    49  
    50  func (this *LeagueAccountState) Hash() common.Hash {
    51  	buff := new(bytes.Buffer)
    52  	this.Serialization(buff)
    53  	hash, _ := common.ParseHashFromBytes(common.Sha256(buff.Bytes()))
    54  	this.hash = hash
    55  	return hash
    56  }
    57  func (this *LeagueAccountState) Serialize(w io.Writer) error {
    58  	sk := sink.NewZeroCopySink(nil)
    59  	sk.WriteUint64(this.Data.Nonce)
    60  	c1, _ := sink.BigIntToComplex(this.Data.Balance)
    61  	sk.WriteComplex(c1)
    62  	c2, _ := sink.BigIntToComplex(this.Data.EnergyBalance)
    63  	sk.WriteComplex(c2)
    64  	sk.WriteUint64(this.Data.BonusHeight)
    65  	w.Write(sk.Bytes())
    66  	return nil
    67  }
    68  func (this *LeagueAccountState) Serialization(w io.Writer) error {
    69  	sk := sink.NewZeroCopySink(nil)
    70  	sk.WriteAddress(this.Address)
    71  	sk.WriteUint64(this.Height)
    72  	sk.WriteUint64(this.Data.Nonce)
    73  	c1, _ := sink.BigIntToComplex(this.Data.Balance)
    74  	sk.WriteComplex(c1)
    75  	c2, _ := sink.BigIntToComplex(this.Data.EnergyBalance)
    76  	sk.WriteComplex(c2)
    77  	sk.WriteUint64(this.Data.BonusHeight)
    78  	w.Write(sk.Bytes())
    79  	//fmt.Println("⛔️ hash ", this.Address.ToString(), this.Height, this.Data.Balance.Uint64(), this.Data.EnergyBalance.Uint64(), this.Data.BonusHeight)
    80  	return nil
    81  }
    82  
    83  func (this *LeagueAccountState) Deserialize(r io.Reader) error {
    84  	buff, err := ioutil.ReadAll(r)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	var eof bool
    89  	source := sink.NewZeroCopySource(buff)
    90  	_, eof = source.NextByte()
    91  	this.LeagueId, eof = source.NextAddress()
    92  	this.Address, eof = source.NextAddress()
    93  	this.Height, eof = source.NextUint64()
    94  	this.Data = &Account{}
    95  	this.Data.Nonce, eof = source.NextUint64()
    96  	bal, eof := source.NextComplex()
    97  	this.Data.Balance, err = bal.ComplexToBigInt()
    98  	if err != nil {
    99  		return err
   100  	}
   101  	bbl, eof := source.NextComplex()
   102  	this.Data.EnergyBalance, err = bbl.ComplexToBigInt()
   103  	if err != nil {
   104  		return err
   105  	}
   106  	this.Data.BonusHeight, eof = source.NextUint64()
   107  	if eof {
   108  		return errors.ERR_TXRAW_EOF
   109  	}
   110  	return nil
   111  }
   112  
   113  func (this *LeagueAccountState) GetKey() []byte {
   114  	buff := make([]byte, 1+2*common.AddrLength+8)
   115  	buff[0] = byte(mcom.EXT_LEAGUE_ACCOUNT)
   116  	copy(buff[1:common.AddrLength+1], this.LeagueId[:])
   117  	copy(buff[common.AddrLength+1:common.AddrLength*2+1], this.Address[:])
   118  	binary.LittleEndian.PutUint64(buff[common.AddrLength*2+1:], this.Height)
   119  	return buff
   120  }
   121  func (this *LeagueAccountState) Account() common.Address {
   122  	return this.Address
   123  }
   124  func (this *LeagueAccountState) League() common.Address {
   125  	return this.LeagueId
   126  }
   127  func (this *LeagueAccountState) BalanceAdd(num *big.Int) {
   128  	this.Data.Balance.Add(this.Data.Balance, num)
   129  }
   130  func (this *LeagueAccountState) BalanceSub(num *big.Int) {
   131  	this.Data.Balance.Sub(this.Data.Balance, num)
   132  }
   133  func (this *LeagueAccountState) EnergyAdd(num *big.Int) {
   134  	this.Data.EnergyBalance.Add(this.Data.EnergyBalance, num)
   135  }
   136  func (this *LeagueAccountState) EnergySub(num *big.Int) {
   137  	this.Data.EnergyBalance.Sub(this.Data.EnergyBalance, num)
   138  }
   139  func (this *LeagueAccountState) NonceSet(num uint64) {
   140  	this.Data.Nonce = num
   141  }
   142  func (this *LeagueAccountState) Balance() *big.Int {
   143  	return this.Data.Balance
   144  }
   145  func (this *LeagueAccountState) Energy() *big.Int {
   146  	return this.Data.EnergyBalance
   147  }
   148  func (this *LeagueAccountState) Nonce() uint64 {
   149  	return this.Data.Nonce
   150  }
   151  func (this *LeagueAccountState) BonusHeight() uint64 {
   152  	return this.Data.BonusHeight
   153  }
   154  func (this *LeagueAccountState) GetHeight() uint64 {
   155  	return this.Height
   156  }
   157  func (this *LeagueAccountState) GetVal() interface{} {
   158  	return this.Data.Balance
   159  }
   160  func (this *LeagueAccountState) BonusHSet(height uint64) {
   161  	this.Data.BonusHeight = height
   162  }
   163  func (this *LeagueAccountState) HeightSet(height uint64) {
   164  	this.Height = height
   165  }