github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/storelaw/account_state_law.go (about)

     1  package storelaw
     2  
     3  import (
     4  	"io"
     5  
     6  	"math/big"
     7  
     8  	"sort"
     9  
    10  	"github.com/sixexorg/magnetic-ring/common"
    11  )
    12  
    13  //AccountStater
    14  type AccountStater interface {
    15  	Hash() common.Hash
    16  	Serialize(w io.Writer) error
    17  	Deserialize(r io.Reader) error
    18  	Serialization(w io.Writer) error
    19  	GetKey() []byte
    20  	Account() common.Address
    21  	League() common.Address
    22  	BalanceAdd(num *big.Int)
    23  	BalanceSub(num *big.Int)
    24  	EnergyAdd(num *big.Int)
    25  	EnergySub(num *big.Int)
    26  	NonceSet(num uint64)
    27  	Balance() *big.Int
    28  	Energy() *big.Int
    29  	Nonce() uint64
    30  	BonusHeight() uint64
    31  	BonusHSet(num uint64)
    32  	HeightSet(height uint64)
    33  	common.FTreer
    34  }
    35  
    36  type AccountStaters []AccountStater
    37  
    38  func (s AccountStaters) Len() int           { return len(s) }
    39  func (s AccountStaters) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    40  func (s AccountStaters) Less(i, j int) bool { return s[i].Hash().String() < s[j].Hash().String() }
    41  func (s AccountStaters) 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  func (s AccountStaters) GetAccounts() []common.Address {
    50  	addrs := make([]common.Address, 0, s.Len())
    51  	for _, v := range s {
    52  		addrs = append(addrs, v.Account())
    53  	}
    54  	return addrs
    55  }