github.com/annchain/OG@v0.0.9/node/delegate.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 node 15 16 import ( 17 "fmt" 18 "github.com/annchain/OG/account" 19 "github.com/annchain/OG/common" 20 "github.com/annchain/OG/common/crypto" 21 "github.com/annchain/OG/common/math" 22 "github.com/annchain/OG/og/types" 23 core2 "github.com/annchain/OG/ogcore/ledger" 24 "github.com/annchain/OG/ogcore/pool" 25 "github.com/annchain/OG/ogcore_test" 26 27 "github.com/annchain/OG/og/txmaker" 28 "github.com/sirupsen/logrus" 29 ) 30 31 type TxRequest struct { 32 AddrFrom common.Address 33 AddrTo common.Address 34 PrivateKey crypto.PrivateKey 35 Value *math.BigInt 36 Nonce uint64 37 TokenId int32 38 } 39 40 type insertTxsFn func(seq *types.Sequencer, txs types.Txis) error 41 42 type Delegate struct { 43 TxCreator *txmaker.OGTxCreator 44 ReceivedNewTxsChan chan []types.Txi 45 ReceivedNewTxChan chan types.Txi 46 TxPool *pool.TxPool 47 Dag *core2.Dag 48 OnNewTxiGenerated []chan types.Txi 49 InsertSyncBuffer insertTxsFn 50 } 51 52 func (d *Delegate) GetTxNum() uint32 { 53 return d.TxPool.GetTxNum() 54 } 55 56 func (d *Delegate) TooMoreTx() bool { 57 if d.GetTxNum() > 6000 { 58 return true 59 } 60 return false 61 } 62 63 func (c *Delegate) GenerateTx(r ogcore.SignedTxBuildRequest) (tx types.Txi, err error) { 64 tx = c.TxCreator.NewSignedTx(r) 65 66 if ok := c.TxCreator.SealTx(tx, nil); !ok { 67 logrus.Warn("delegate failed to seal tx") 68 err = fmt.Errorf("delegate failed to seal tx") 69 return 70 } 71 logrus.WithField("tx", tx).Debugf("tx generated") 72 return 73 } 74 75 func (c *Delegate) GenerateArchive(data []byte) (tx types.Txi, err error) { 76 tx, err = c.TxCreator.NewArchiveWithSeal(data) 77 if err != nil { 78 logrus.WithField("tx", tx).Debugf("tx generated") 79 } 80 return 81 } 82 83 type SeqRequest struct { 84 Issuer common.Address 85 PrivateKey crypto.PrivateKey 86 Nonce uint64 87 Height uint64 88 } 89 90 //discarded function 91 func (c *Delegate) GenerateSequencer(r SeqRequest) (seq *types.Sequencer, err error) { 92 // TODO: why twice? 93 seq, err, _ = c.TxCreator.GenerateSequencer(r.Issuer, r.Height, r.Nonce, &r.PrivateKey, nil) 94 if err != nil { 95 return 96 } 97 //if seq == nil && genAgain { 98 // seq, genAgain = c.TxCreator.GenerateSequencer(r.Issuer, r.Height, r.Nonce, &r.PrivateKey, nil) 99 //} 100 logrus.WithField("seq", seq).Infof("sequencer generated") 101 if ok := c.TxCreator.SealTx(seq, &r.PrivateKey); !ok { 102 err = fmt.Errorf("delegate failed to seal seq") 103 return 104 } 105 logrus.WithField("seq", seq).Infof("sequencer connected") 106 return seq, nil 107 } 108 109 func (c *Delegate) GetLatestAccountNonce(addr common.Address) (uint64, error) { 110 noncePool, errPool := c.TxPool.GetLatestNonce(addr) 111 if errPool == nil { 112 return noncePool, errPool 113 } 114 logrus.WithError(errPool).WithField("addr", addr.String()).Trace("txpool nonce not found") 115 116 nonceDag, errDag := c.Dag.GetLatestNonce(addr) 117 if errDag == nil { 118 return nonceDag, errDag 119 } 120 logrus.WithError(errDag).WithField("addr", addr.String()).Trace("dag nonce not found") 121 122 return 0, fmt.Errorf("nonce for address not found") 123 } 124 125 func (c *Delegate) GetLatestDagSequencer() *types.Sequencer { 126 latestSeq := c.Dag.LatestSequencer() 127 return latestSeq 128 } 129 130 func (c *Delegate) Announce(txi types.Txi) { 131 for _, ch := range c.OnNewTxiGenerated { 132 ch <- txi 133 } 134 } 135 136 func (c *Delegate) JudgeNonce(me *account.Account) uint64 { 137 138 var n uint64 139 //NonceSelfDiscipline 140 // fetch from db every time 141 n, err := c.GetLatestAccountNonce(me.Address) 142 me.SetNonce(n) 143 if err != nil { 144 // not exists, set to 0 145 return 0 146 } else { 147 n, _ = me.ConsumeNonce() 148 return n 149 } 150 }