Avatar
0
monkey Enlightened
monkey Enlightened
Go đoạn lệnh này có nghĩa là gì?
ev, ok := ev.(*types.DuplicateVoteEvidence)
có nghĩa là gì nhỉ? Nó là gọi hàm hay là gì nhỉ?
type Evidence interface {
	ABCI() []abci.Evidence // forms individual evidence to be sent to the application
	Bytes() []byte         // bytes which comprise the evidence
	Hash() []byte          // hash of the evidence
	Height() int64         // height of the infraction
	String() string        // string format of the evidence
	Time() time.Time       // time of the infraction
	ValidateBasic() error  // basic consistency check
}

type DuplicateVoteEvidence struct {
	VoteA *Vote `json:"vote_a"`
	VoteB *Vote `json:"vote_b"`

	// abci specific information
	TotalVotingPower int64
	ValidatorPower   int64
	Timestamp        time.Time
}

evidenceFromEachValidator := make([]types.Evidence, nValidators)
// set giá trị cho evidenceFromEachValidator

for idx, ev := range evidenceFromEachValidator {
    ev, ok := ev.(*types.DuplicateVoteEvidence)
}		
  • Answer
go
Remain: 5
2 Answers
Avatar
ducnt114 Pundit
ducnt114 Pundit
ev, ok := ev.(*types.DuplicateVoteEvidence)
có nghĩa là cast biến ev sang kiểu *types.DuplicateVoteEvidence, nếu không cast được thì biến ok sẽ trả về false, ngược lại sẽ là true và biến ev sẽ được đổi sang kiểu *types.DuplicateVoteEvidence
  • 0
  • Reply
Nhưng nhìn trong ví dụ thì DuplicateVoteEvidence và Evidence không có sự liên hệ nào với nhau, có vẻ như việc ép kiểu này luôn sai?
 –  monkey 1634845639000
Avatar
ducnt114 Pundit
ducnt114 Pundit
The Best Answer
Trường hợp như thế này thì sẽ cast được
package main

import (
	"fmt"
	"time"
)

type Vote struct {

}

type Evidence interface {
	ABCI() []Evidence // forms individual evidence to be sent to the application
	Bytes() []byte         // bytes which comprise the evidence
	Hash() []byte          // hash of the evidence
	Height() int64         // height of the infraction
	String() string        // string format of the evidence
	Time() time.Time       // time of the infraction
	ValidateBasic() error  // basic consistency check
}

type DuplicateVoteEvidence struct {
	VoteA *Vote `json:"vote_a"`
	VoteB *Vote `json:"vote_b"`

	// abci specific information
	TotalVotingPower int64
	ValidatorPower   int64
	Timestamp        time.Time
}

func NewEvidence() Evidence {
	return &DuplicateVoteEvidence{}
}

func (this *DuplicateVoteEvidence) ABCI() []Evidence {	return nil}
func (this *DuplicateVoteEvidence)Bytes() []byte{return nil}         // bytes which comprise the evidence
func (this *DuplicateVoteEvidence)Hash() []byte   {return nil}       // hash of the evidence
func (this *DuplicateVoteEvidence)Height() int64    {return 0}     // height of the infraction
func (this *DuplicateVoteEvidence)String() string    {return ""}    // string format of the evidence
func (this *DuplicateVoteEvidence)Time() time.Time   {return time.Time{}}    // time of the infraction
func (this *DuplicateVoteEvidence)ValidateBasic() error{return nil}  // basic consistency check

func main() {
	evidenceFromEachValidator := make([]Evidence, 0)
	evidenceFromEachValidator = append(evidenceFromEachValidator, NewEvidence())
	// set giá trị cho evidenceFromEachValidator

	for _, ev := range evidenceFromEachValidator {
		_, ok := ev.(*DuplicateVoteEvidence)
		if ok {
			fmt.Println("Cast success")
		} else {
			fmt.Println("Cast fail")
		}
	}
}

  • 0
  • Reply