github.com/annchain/OG@v0.0.9/consensus/campaign/campaign.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 campaign
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/annchain/OG/common"
    19  	"github.com/annchain/OG/common/byteutil"
    20  	"github.com/annchain/OG/consensus/vrf"
    21  	"github.com/annchain/OG/og/protocol/ogmessage/archive"
    22  	"github.com/annchain/OG/og/types"
    23  	"github.com/annchain/OG/types"
    24  	"github.com/annchain/kyber/v3"
    25  	"strings"
    26  
    27  	"github.com/annchain/OG/common/hexutil"
    28  )
    29  
    30  //go:generate msgp
    31  
    32  //msgp:tuple Campaign
    33  type Campaign struct {
    34  	types.TxBase
    35  	DkgPublicKey []byte
    36  	Vrf          vrf.VrfInfo
    37  	Issuer       *common.Address
    38  	dkgPublicKey kyber.Point
    39  }
    40  
    41  //msgp:tuple Campaigns
    42  type Campaigns []*Campaign
    43  
    44  func (c *Campaign) GetBase() *types.TxBase {
    45  	return &c.TxBase
    46  }
    47  
    48  func (c *Campaign) Sender() common.Address {
    49  	return *c.Issuer
    50  }
    51  
    52  func (tc *Campaign) GetSender() *common.Address {
    53  	return tc.Issuer
    54  }
    55  
    56  func (c *Campaign) Compare(tx types.Txi) bool {
    57  	switch tx := tx.(type) {
    58  	case *Campaign:
    59  		if c.GetHash().Cmp(tx.GetHash()) == 0 {
    60  			return true
    61  		}
    62  		return false
    63  	default:
    64  		return false
    65  	}
    66  }
    67  
    68  func (c *Campaign) Dump() string {
    69  	var phashes []string
    70  	for _, p := range c.ParentsHash {
    71  		phashes = append(phashes, p.Hex())
    72  	}
    73  	return fmt.Sprintf("hash: %s, pHash: [%s], issuer: %s, nonce: %d , signatute: %s, pubkey %s ,"+
    74  		"PkDkg: %x, PkVrf: %x, Data: %x, Vrf: %x, Proof: %x", c.Hash.Hex(),
    75  		strings.Join(phashes, " ,"), c.Issuer, c.AccountNonce, hexutil.Encode(c.Signature), hexutil.Encode(c.PublicKey),
    76  		c.DkgPublicKey, c.Vrf.PublicKey, c.Vrf.Message, c.Vrf.Vrf, c.Vrf.Proof)
    77  }
    78  
    79  func (c *Campaign) SignatureTargets() []byte {
    80  	// add parents infornmation.
    81  	w := byteutil.NewBinaryWriter()
    82  
    83  	w.Write(c.DkgPublicKey, c.Vrf.Vrf, c.Vrf.PublicKey, c.Vrf.Proof, c.Vrf.Message, c.AccountNonce)
    84  
    85  	if !archive.CanRecoverPubFromSig {
    86  		w.Write(c.Issuer.Bytes)
    87  	}
    88  
    89  	return w.Bytes()
    90  }
    91  
    92  func (c *Campaign) String() string {
    93  	if c.GetSender() == nil {
    94  		return fmt.Sprintf("%s-[nil]-%d-%s-Cp", c.TxBase.String(), c.AccountNonce, hexutil.Encode(c.DkgPublicKey[:5]))
    95  	}
    96  	return fmt.Sprintf("%s-[%.10s]-%d-%s-Cp", c.TxBase.String(), c.Sender().String(), c.AccountNonce, hexutil.Encode(c.DkgPublicKey[:5]))
    97  }
    98  
    99  func (c Campaigns) String() string {
   100  	var strs []string
   101  	for _, v := range c {
   102  		strs = append(strs, v.String())
   103  	}
   104  	return strings.Join(strs, ", ")
   105  }
   106  
   107  func (c *Campaign) RawCampaign() *RawCampaign {
   108  	if c == nil {
   109  		return nil
   110  	}
   111  	rc := &RawCampaign{
   112  		TxBase:       c.TxBase,
   113  		DkgPublicKey: c.DkgPublicKey,
   114  		Vrf:          c.Vrf,
   115  	}
   116  	return rc
   117  }
   118  
   119  func (cs Campaigns) RawCampaigns() RawCampaigns {
   120  	if len(cs) == 0 {
   121  		return nil
   122  	}
   123  	var rawCps RawCampaigns
   124  	for _, v := range cs {
   125  		rasSeq := v.RawCampaign()
   126  		rawCps = append(rawCps, rasSeq)
   127  	}
   128  	return rawCps
   129  }
   130  
   131  func (c *Campaign) GetDkgPublicKey() kyber.Point {
   132  	return c.dkgPublicKey
   133  }
   134  
   135  func (c *Campaign) UnmarshalDkgKey(unmarshalFunc func(b []byte) (kyber.Point, error)) error {
   136  	p, err := unmarshalFunc(c.DkgPublicKey)
   137  	if err != nil {
   138  		return err
   139  	}
   140  	c.dkgPublicKey = p
   141  	return nil
   142  }
   143  
   144  func (c *Campaign) MarshalDkgKey() error {
   145  	d, err := c.dkgPublicKey.MarshalBinary()
   146  	if err != nil {
   147  		return nil
   148  	}
   149  	c.DkgPublicKey = d
   150  	return nil
   151  }
   152  
   153  func (c *Campaign) RawTxi() archive.RawTxi {
   154  	return c.RawCampaign()
   155  }
   156  
   157  func (t *Campaign) SetSender(addr common.Address) {
   158  	t.Issuer = &addr
   159  }
   160  
   161  func (r *Campaigns) Len() int {
   162  	if r == nil {
   163  		return 0
   164  	}
   165  	return len(*r)
   166  }