github.com/annchain/OG@v0.0.9/og/protocol/ogmessage/archive/sequencer.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package archive
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/annchain/OG/common"
    19  	"github.com/annchain/OG/og/protocol/dagmessage"
    20  	"strings"
    21  
    22  	"github.com/annchain/OG/common/hexutil"
    23  )
    24  
    25  //go:generate msgp
    26  
    27  ////msgp:tuple Sequencer
    28  //type Sequencer struct {
    29  //	// TODO: need more states in sequencer to differentiate multiple chains
    30  //	TxBase
    31  //	Issuer         *common.Address
    32  //	Signature    hexutil.KeyBytes
    33  //	PublicKey hexutil.KeyBytes
    34  //	StateRoot      common.Hash
    35  //	Proposing      bool `msg:"-"` // is the sequencer is proposal ,did't commit yet ,use this flag to avoid bls sig verification failed
    36  //}
    37  
    38  //msgp:tuple SequencerJson
    39  type SequencerJson struct {
    40  	TxBaseJson
    41  	Issuer         *common.Address `json:"issuer"`
    42  	BlsJointSig    hexutil.Bytes   `json:"bls_joint_sig"`
    43  	BlsJointPubKey hexutil.Bytes   `json:"bls_joint_pub_key"`
    44  	Proposing      bool            `msg:"-",json:"-"`
    45  }
    46  
    47  //func (s *Sequencer) ToSmallCaseJson() ([]byte, error) {
    48  //	if s == nil {
    49  //		return nil, nil
    50  //	}
    51  //	j := SequencerJson{
    52  //		TxBaseJson:     *s.TxBase.ToSmallCase(),
    53  //		Issuer:         s.Issuer,
    54  //		Signature:    s.Signature,
    55  //		PublicKey: s.PublicKey,
    56  //		Proposing:      s.Proposing,
    57  //	}
    58  //
    59  //	return json.Marshal(&j)
    60  //}
    61  
    62  //msgp:tuple BlsSigSet
    63  type BlsSigSet struct {
    64  	PublicKey    []byte
    65  	BlsSignature []byte
    66  }
    67  
    68  //msgp:tuple Sequencers
    69  type Sequencers []*Sequencer
    70  
    71  func (t *Sequencer) GetHead() *dagmessage.SequencerHeader {
    72  	return dagmessage.NewSequencerHead(t.GetHash(), t.Height)
    73  }
    74  
    75  func (t *Sequencer) Dump() string {
    76  	var phashes []string
    77  	for _, p := range t.ParentsHash {
    78  		phashes = append(phashes, p.Hex())
    79  	}
    80  	return fmt.Sprintf("pHash:[%s], Issuer : %s , Height: %d, Weight: %d, nonce : %d , blspub: %s, signatute : %s, pubkey:  %s root: %s",
    81  		strings.Join(phashes, " ,"),
    82  		t.Issuer.Hex(),
    83  		t.Height,
    84  		t.Weight,
    85  		t.AccountNonce,
    86  		t.BlsJointPubKey,
    87  		hexutil.Encode(t.PublicKey),
    88  		hexutil.Encode(t.Signature),
    89  		t.StateRoot.Hex(),
    90  	)
    91  }
    92  
    93  func (s *Sequencer) RawSequencer() *RawSequencer {
    94  	if s == nil {
    95  		return nil
    96  	}
    97  	return &RawSequencer{
    98  		TxBase:         s.TxBase,
    99  		BlsJointSig:    s.BlsJointSig,
   100  		BlsJointPubKey: s.BlsJointPubKey,
   101  		StateRoot:      s.StateRoot,
   102  	}
   103  }
   104  
   105  func (s Sequencers) String() string {
   106  	var strs []string
   107  	for _, v := range s {
   108  		strs = append(strs, v.String())
   109  	}
   110  	return strings.Join(strs, ", ")
   111  }
   112  
   113  func (s Sequencers) ToHeaders() dagmessage.SequencerHeaders {
   114  	if len(s) == 0 {
   115  		return nil
   116  	}
   117  	var headers dagmessage.SequencerHeaders
   118  	for _, v := range s {
   119  		head := dagmessage.NewSequencerHead(v.Hash, v.Height)
   120  		headers = append(headers, head)
   121  	}
   122  	return headers
   123  }
   124  
   125  func (seqs Sequencers) ToRawSequencers() RawSequencers {
   126  	if len(seqs) == 0 {
   127  		return nil
   128  	}
   129  	var rawSeqs RawSequencers
   130  	for _, v := range seqs {
   131  		rasSeq := v.RawSequencer()
   132  		rawSeqs = append(rawSeqs, rasSeq)
   133  	}
   134  	return rawSeqs
   135  }
   136  
   137  func (c *Sequencer) RawTxi() RawTxi {
   138  	return c.RawSequencer()
   139  }
   140  
   141  func (t *Sequencer) SetSender(addr common.Address) {
   142  	t.Issuer = &addr
   143  }
   144  
   145  func (r *Sequencers) Len() int {
   146  	if r == nil {
   147  		return 0
   148  	}
   149  	return len(*r)
   150  }