github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/orgchain/validation/state_validate_vote.go (about) 1 package validation 2 3 import ( 4 "math/big" 5 6 "fmt" 7 8 "github.com/sixexorg/magnetic-ring/common" 9 "github.com/sixexorg/magnetic-ring/core/orgchain/types" 10 "github.com/sixexorg/magnetic-ring/errors" 11 "github.com/sixexorg/magnetic-ring/store/storelaw" 12 ) 13 14 //just sub balance 15 //1. check vote exist and get vote state 16 //2. check vote between start and end 17 //3. check account has voted 18 //4. get the total ut at the time the vote was initiated 19 func (s *StateValidate) verifyVote(oplog *OpLog) error { 20 if oplog.method > AccountMaxLine && oplog.method < VoteMaxLine { 21 aa := oplog.data.(*OPHashAddress) 22 err := s.checkAccountVote(aa.Hash, aa.Address) 23 if err != nil { 24 return err 25 } 26 return nil 27 } 28 return nil 29 } 30 31 type TxUT struct { 32 Tx *types.Transaction 33 UT *big.Int 34 } 35 36 func (s *StateValidate) checkAccountVote(voteId common.Hash, account common.Address) error { 37 fmt.Println("⭕️ checkAccountVote p1") 38 err := s.GetVoteState(voteId) 39 if err != nil { 40 return err 41 } 42 fmt.Println("⭕️ checkAccountVote p2") 43 err = s.GetLeagueUTAndTx(voteId) 44 if err != nil { 45 return err 46 } 47 fmt.Println("⭕️ checkAccountVote p3") 48 err = s.AccountAlreadyVoted(voteId, account) 49 if err != nil { 50 return err 51 } 52 fmt.Println("⭕️ checkAccountVote p4") 53 return nil 54 } 55 56 func (s *StateValidate) GetVoteState(voteId common.Hash) error { 57 if s.MemoVoteState[voteId] != nil { 58 return nil 59 } 60 var ( 61 vs *storelaw.VoteState 62 err error 63 ) 64 vs, err = s.ledgeror.GetVoteState(voteId, s.TargetHeight) 65 if err != nil { 66 fmt.Println("⭕️ GetVoteState err ", err) 67 return err 68 } 69 fmt.Println("⭕️ GetVoteState ", vs.VoteId.String()) 70 s.MemoVoteState[voteId] = vs 71 return nil 72 } 73 74 func (s *StateValidate) GetLeagueUTAndTx(hash common.Hash) error { 75 if s.voteUT[hash] == nil { 76 tx, height, err := s.ledgeror.GetTransaction(hash, s.leagueId) 77 if err != nil { 78 return err 79 } 80 // -- X -- start -- O -- currentHeight -- O -- end -- X -- 81 if tx.TxData.Start > s.TargetHeight || tx.TxData.End < s.TargetHeight { 82 return errors.ERR_VOTE_OUT_OF_RANGE 83 } 84 ut := s.ledgeror.GetUTByHeight(height, s.leagueId) 85 s.voteUT[hash] = &TxUT{ 86 Tx: tx, 87 UT: ut, 88 } 89 } 90 return nil 91 } 92 func (s *StateValidate) AccountAlreadyVoted(voteId common.Hash, account common.Address) error { 93 if s.DirtyVote[voteId] != nil { 94 _, ok := s.DirtyVote[voteId][account] 95 if ok { 96 return errors.ERR_VOTE_ALREADY_VOTED 97 } 98 } else { 99 s.DirtyVote[voteId] = make(map[common.Address]struct{}) 100 } 101 did := s.ledgeror.AlreadyVoted(voteId, account) 102 if did { 103 return errors.ERR_VOTE_ALREADY_VOTED 104 } 105 s.DirtyVote[voteId][account] = struct{}{} 106 return nil 107 }