github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/state/statedb_candidate.go (about)

     1  package state
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"sort"
     8  
     9  	"github.com/neatlab/neatio/utilities/common"
    10  	"github.com/neatlab/neatio/utilities/rlp"
    11  )
    12  
    13  // ----- candidate Set
    14  
    15  // MarkAddressCandidate adds the specified object to the dirty map
    16  func (self *StateDB) MarkAddressCandidate(addr common.Address) {
    17  	if _, exist := self.GetCandidateSet()[addr]; !exist {
    18  		self.candidateSet[addr] = struct{}{}
    19  		self.candidateSetDirty = true
    20  	}
    21  }
    22  
    23  func (self *StateDB) ClearCandidateSetByAddress(addr common.Address) {
    24  	fmt.Printf("candidate set bug, clear candidate set by address, %v\n", self.candidateSet)
    25  	delete(self.candidateSet, addr)
    26  	self.candidateSetDirty = true
    27  }
    28  
    29  func (self *StateDB) GetCandidateSet() CandidateSet {
    30  	fmt.Printf("candidate set bug, get candidate set 1, %v\n", self.candidateSet)
    31  	if len(self.candidateSet) != 0 {
    32  		return self.candidateSet
    33  	}
    34  	// Try to get from Trie
    35  	enc, err := self.trie.TryGet(candidateSetKey)
    36  	if err != nil {
    37  		self.setError(err)
    38  		return nil
    39  	}
    40  	var value CandidateSet
    41  	if len(enc) > 0 {
    42  		err := rlp.DecodeBytes(enc, &value)
    43  		if err != nil {
    44  			self.setError(err)
    45  		}
    46  		self.candidateSet = value
    47  	}
    48  	fmt.Printf("candidate set bug, get candidate set 2, %v\n", value)
    49  	return value
    50  }
    51  
    52  func (self *StateDB) commitCandidateSet() {
    53  	fmt.Printf("candidate set bug, commit candidate set, %v\n", self.candidateSet)
    54  	data, err := rlp.EncodeToBytes(self.candidateSet)
    55  	if err != nil {
    56  		panic(fmt.Errorf("can't encode candidate set : %v", err))
    57  	}
    58  	self.setError(self.trie.TryUpdate(candidateSetKey, data))
    59  }
    60  
    61  // Store the Candidate Address Set
    62  
    63  var candidateSetKey = []byte("CandidateSet")
    64  
    65  type CandidateSet map[common.Address]struct{}
    66  
    67  func (set CandidateSet) EncodeRLP(w io.Writer) error {
    68  	var list []common.Address
    69  	for addr := range set {
    70  		list = append(list, addr)
    71  	}
    72  	sort.Slice(list, func(i, j int) bool {
    73  		return bytes.Compare(list[i].Bytes(), list[j].Bytes()) == 1
    74  	})
    75  	return rlp.Encode(w, list)
    76  }
    77  
    78  func (set *CandidateSet) DecodeRLP(s *rlp.Stream) error {
    79  	var list []common.Address
    80  	if err := s.Decode(&list); err != nil {
    81  		return err
    82  	}
    83  	candidateSet := make(CandidateSet, len(list))
    84  	for _, addr := range list {
    85  		candidateSet[addr] = struct{}{}
    86  	}
    87  	*set = candidateSet
    88  	return nil
    89  }