github.com/annchain/OG@v0.0.9/consensus_interface/interface.go (about) 1 package consensus_interface 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/annchain/commongo/utilfuncs" 7 ) 8 9 // OgLedgerAccount represents a full account of a user. 10 type ConsensusAccount interface { 11 Id() string 12 //PubKey() crypto.PubKey 13 } 14 15 type CommitteeMember struct { 16 PeerIndex int // order of peer in the committee 17 MemberId string // peer identifier. current use address 18 TransportPeerId string // for transport only. In the future this should not be revealed. 19 ConsensusAccount ConsensusAccount // account public key to verify messages 20 } 21 22 type Committee struct { 23 Peers []*CommitteeMember 24 Version int 25 } 26 27 type ProposalContext struct { 28 CurrentRound int64 29 HighQC *QC 30 TC *TC 31 } 32 33 func (p ProposalContext) String() string { 34 s, err := json.Marshal(p) 35 utilfuncs.PanicIfError(err, "marshal proposal") 36 return string(s) 37 } 38 39 type VerifyResult struct { 40 Ok bool 41 } 42 43 type ExecutionResult struct { 44 BlockId string 45 ExecuteStateId string 46 Err error 47 } 48 49 func (e *ExecutionResult) String() string { 50 return fmt.Sprintf("BlockId:%s ExecuteStateId:%s Err:%s", e.BlockId, e.ExecuteStateId, e.Err) 51 } 52 53 type ConsensusState struct { 54 LastVoteRound int64 55 PreferredRound int64 56 HighQC *QC 57 LastTC *TC 58 } 59 60 func (c ConsensusState) String() string { 61 return fmt.Sprintf("LastVoteRound %d PreferredRound %d HighQC %s", c.LastVoteRound, c.PreferredRound, c.HighQC) 62 } 63 64 type ConsensusAccountProvider interface { 65 ProvideAccount() (ConsensusAccount, error) 66 Generate() (account ConsensusAccount, err error) 67 Load() (account ConsensusAccount, err error) 68 Save() (err error) 69 } 70 71 type ProposalContextProvider interface { 72 GetProposalContext() *ProposalContext 73 } 74 75 // ProposalGenerator provides a proposal whenever needed 76 type ProposalGenerator interface { 77 GenerateProposal(context *ProposalContext) *ContentProposal 78 GenerateProposalAsync(context *ProposalContext, callback func(*ContentProposal)) 79 } 80 81 type ProposalVerifier interface { 82 VerifyProposal(proposal *ContentProposal) *VerifyResult 83 VerifyProposalAsync(proposal *ContentProposal) 84 } 85 86 type ProposalExecutor interface { 87 ExecuteProposal(block *Block) (executionResult ExecutionResult) 88 ExecuteProposalAsync(block *Block) 89 } 90 91 type CommitteeProvider interface { 92 InitCommittee(version int, peers []CommitteeMember, myAccount ConsensusAccount) 93 GetVersion() int 94 GetAllMemberTransportIds() []string 95 GetAllMemberPeedIds() []string 96 GetAllMembers() []CommitteeMember 97 GetMyPeerId() string 98 GetMyPeerIndex() int 99 GetLeader(round int64) CommitteeMember 100 GetPeerIndex(id string) (index int, err error) 101 GetThreshold() int 102 AmILeader(round int64) bool 103 AmIIn() bool 104 IsIn(id string) bool 105 } 106 type ConsensusSigner interface { 107 Sign(msg []byte, account ConsensusAccount) Signature 108 } 109 110 type SignatureCollector interface { 111 GetThreshold() int 112 GetCurrentCount() int 113 GetSignature(index int) (v Signature, ok bool) 114 GetJointSignature() JointSignature 115 Collected() bool 116 Collect(sig Signature, index int) 117 Has(index int) bool 118 } 119 120 type Ledger interface { 121 // Speculate applies cmds speculatively 122 Speculate(prevBlockId string, block *Block) (executionResult ExecutionResult) 123 // GetState finds the pending state for the given BlockId or nil if not present 124 GetState(blockId string) (stateId string) 125 // Commit commits the pending prefix of the given BlockId and prune other branches 126 Commit(blockId string) 127 SaveConsensusState(*ConsensusState) 128 GetConsensusState() *ConsensusState 129 CurrentHeight() int64 130 CurrentCommittee() *Committee 131 } 132 133 type Hasher interface { 134 Hash(s string) string 135 } 136 137 //type PendingTreeOrganizer interface { 138 // // 139 // Commit(BlockId string) 140 //}