github.com/annchain/OG@v0.0.9/consensus/annsensus/archive/verifiers.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  //
    17  //import (
    18  //	"bytes"
    19  //	"github.com/annchain/OG/types/token"
    20  //	"github.com/annchain/OG/types/tx_types"
    21  //	"time"
    22  //
    23  //	"github.com/annchain/OG/common/ogcrypto"
    24  //	"github.com/annchain/kyber/v3/pairing/bn256"
    25  //)
    26  //
    27  //// consensus related verification
    28  //func (a *AnnSensus) VerifyTermChange(t *tx_types.TermChange) bool {
    29  //	if a.disable {
    30  //		log.WithField("t ", t).Warn("annsensus disabled")
    31  //		return true
    32  //	}
    33  //	//check balance
    34  //	if t.TermID <= a.term.ID() {
    35  //		//small term id senstors will be dropped , just verify format and bls keys
    36  //		log.Debug("small term id")
    37  //	} else {
    38  //		if a.GetCandidate(t.Sender()) == nil {
    39  //			log.WithField("candidates ", a.term.Candidates()).WithField("addr ", t.Issuer.TerminalString()).Warn("not found campaign for termChange")
    40  //			return false
    41  //		}
    42  //	}
    43  //	if len(t.SigSet) < a.dkg.GetParticipantNumber() {
    44  //		log.WithField("len ", len(t.SigSet)).WithField("need ",
    45  //			a.dkg.GetParticipantNumber()).Warn("not eoungh sigsets")
    46  //		return false
    47  //	}
    48  //	signer := ogcrypto.NewSigner(a.cryptoType)
    49  //	for _, sig := range t.SigSet {
    50  //		if sig == nil {
    51  //			log.Warn("nil sig")
    52  //			return false
    53  //		}
    54  //		pk := ogcrypto.PublicKeyFromBytes(a.cryptoType, sig.PublicKey)
    55  //		if !signer.Verify(pk, ogcrypto.SignatureFromBytes(a.cryptoType, sig.Signature), t.PkBls) {
    56  //			log.WithField("sig ", sig).Warn("Verify Signature for sigsets fail")
    57  //			return false
    58  //		}
    59  //	}
    60  //	log.WithField("tc ", t).Trace("verify ok")
    61  //	return true
    62  //
    63  //}
    64  //
    65  //// consensus related verification
    66  //func (a *AnnSensus) VerifySequencer(seq *tx_types.Sequencer) bool {
    67  //
    68  //	if a.disable {
    69  //		log.WithField("seq ", seq).Debug("annsensus disabled")
    70  //		return true
    71  //	}
    72  //
    73  //	if senator := a.term.GetSenator(seq.Sender()); senator == nil {
    74  //		log.WithField("address ", seq.Issuer.ShortString()).Warn("not found senator for address")
    75  //		return false
    76  //	}
    77  //	if seq.Proposing {
    78  //		log.WithField("hash ", seq).Debug("proposing seq")
    79  //		return true
    80  //	}
    81  //	log.WithField("hash ", seq).Debug("normal seq")
    82  //
    83  //	if !a.term.Started() {
    84  //		if seq.Height > 1 {
    85  //			log.Warn("dkg is not ready yet")
    86  //			return false
    87  //		}
    88  //		//wait 2 second
    89  //		for {
    90  //			select {
    91  //			case <-time.After(50 * time.Millisecond):
    92  //				if a.term.Started() {
    93  //					break
    94  //				}
    95  //			case <-a.close:
    96  //				return false
    97  //			case <-time.After(2 * time.Second):
    98  //				log.Warn("dkg is not ready yet")
    99  //				return false
   100  //			}
   101  //		}
   102  //
   103  //	}
   104  //	ok := a.dkg.VerifyBlsSig(seq.GetHash().ToBytes(), seq.Signature, seq.PublicKey, a.bft.DKGTermId)
   105  //
   106  //	if !ok {
   107  //		return false
   108  //	}
   109  //	//TODO more consensus verifications
   110  //	return true
   111  //}
   112  //
   113  //// consensus related verification
   114  //func (a *AnnSensus) VerifyCampaign(cp *tx_types.Campaign) bool {
   115  //
   116  //	if a.disable {
   117  //		log.WithField("cp", cp).Warn("annsensus disabled")
   118  //		return true
   119  //	}
   120  //
   121  //	//check balance
   122  //	balance := a.Idag.GetBalance(*cp.Issuer, token.OGTokenID)
   123  //	if balance.Value.Cmp(campaigningMinBalance.Value) < 0 {
   124  //		log.WithField("addr ", cp.Issuer).Warn("your balance is not enough to generate campaign")
   125  //		return false
   126  //	}
   127  //
   128  //	err := a.VrfVerify(cp.Vrf.Vrf, cp.Vrf.PublicKey, cp.Vrf.Message, cp.Vrf.Proof)
   129  //	if err != nil {
   130  //		log.WithError(err).Debug("vrf verify failed")
   131  //		return false
   132  //	}
   133  //
   134  //	err = cp.UnmarshalDkgKey(bn256.UnmarshalBinaryPointG2)
   135  //	if err != nil {
   136  //		log.WithField("cp", cp).WithError(err).Debug("dkg Public key  verify failed")
   137  //		return false
   138  //	}
   139  //	if cp.GetDkgPublicKey() == nil {
   140  //		log.WithField("cp", cp).WithField("data ", cp.PublicKey).Warn("dkgPub is nil")
   141  //		return false
   142  //	}
   143  //	if a.HasCampaign(cp.Sender()) {
   144  //		log.WithField("campaign", cp).Debug("duplicate campaign")
   145  //		//todo  if a node dose not cacht up yet  returning false will cause fail
   146  //		//return false
   147  //	}
   148  //	log.WithField("cp ", cp).Trace("verify ok")
   149  //	return true
   150  //}
   151  //
   152  //func (a *AnnSensus) VerifyRequestedTermChange(t *tx_types.TermChange) bool {
   153  //
   154  //	if a.disable {
   155  //		log.WithField("t ", t).Warn("annsensus disabled")
   156  //		return true
   157  //	}
   158  //
   159  //	if len(t.SigSet) < a.dkg.GetParticipantNumber() {
   160  //		log.WithField("len ", len(t.SigSet)).WithField("need ",
   161  //			a.dkg.GetParticipantNumber()).Warn("not enough sigsets")
   162  //		return false
   163  //	}
   164  //	signer := ogcrypto.NewSigner(a.cryptoType)
   165  //	for _, sig := range t.SigSet {
   166  //		if sig == nil {
   167  //			log.Warn("nil sig")
   168  //			return false
   169  //		}
   170  //		pk := ogcrypto.PublicKeyFromBytes(a.cryptoType, sig.PublicKey)
   171  //		var ok bool
   172  //		for _, gPk := range a.genesisAccounts {
   173  //			if bytes.Equal(pk.KeyBytes, gPk.KeyBytes) {
   174  //				ok = true
   175  //			}
   176  //		}
   177  //		if !ok {
   178  //			log.WithField("pk ", pk).Warn("not a consensus participater")
   179  //			return false
   180  //		}
   181  //		if !signer.Verify(pk, ogcrypto.SignatureFromBytes(a.cryptoType, sig.Signature), t.PkBls) {
   182  //			log.WithField("sig ", sig).Warn("Verify Signature for sigsets fail")
   183  //			return false
   184  //		}
   185  //	}
   186  //	log.WithField("tc", t).Trace("verify ok")
   187  //	return true
   188  //}