github.com/annchain/OG@v0.0.9/og/types/tx.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/annchain/OG/arefactor/og/types"
     6  	"github.com/annchain/OG/common"
     7  	"github.com/annchain/OG/common/byteutil"
     8  	"github.com/annchain/OG/common/crypto"
     9  	"github.com/annchain/OG/common/hexutil"
    10  	"github.com/annchain/OG/common/math"
    11  	"strings"
    12  )
    13  
    14  type Tx struct {
    15  	// graph structure info
    16  	Hash        types.Hash
    17  	ParentsHash types.Hashes
    18  	MineNonce   uint64
    19  
    20  	// tx info
    21  	AccountNonce uint64
    22  	From         common.Address
    23  	To           common.Address
    24  	Value        *math.BigInt
    25  	TokenId      int32
    26  	Data         []byte
    27  	PublicKey    crypto.PublicKey
    28  	Signature    crypto.Signature
    29  	//confirm     time.Time
    30  	// CanRecoverPubFromSig
    31  
    32  	// Derived properties
    33  	Height uint64
    34  	Weight uint64
    35  
    36  	valid bool
    37  }
    38  
    39  func (t *Tx) SetMineNonce(v uint64) {
    40  	t.MineNonce = v
    41  }
    42  
    43  func (t *Tx) SetParents(hashes types.Hashes) {
    44  	t.ParentsHash = hashes
    45  }
    46  
    47  func (t *Tx) SetWeight(weight uint64) {
    48  	t.Weight = weight
    49  }
    50  
    51  func (t *Tx) SetValid(b bool) {
    52  	t.valid = b
    53  }
    54  
    55  func (t *Tx) Valid() bool {
    56  	return t.valid
    57  }
    58  
    59  func (t *Tx) SetSender(addr common.Address) {
    60  	t.From = addr
    61  }
    62  
    63  func (t *Tx) SetHash(h types.Hash) {
    64  	t.Hash = h
    65  }
    66  
    67  func (t *Tx) GetNonce() uint64 {
    68  	return t.AccountNonce
    69  }
    70  
    71  func (t *Tx) Sender() common.Address {
    72  	return t.From
    73  }
    74  
    75  func (t *Tx) SetHeight(height uint64) {
    76  	t.Height = height
    77  }
    78  
    79  func (t *Tx) Dump() string {
    80  	var phashes []string
    81  	for _, p := range t.ParentsHash {
    82  		phashes = append(phashes, p.Hex())
    83  	}
    84  	return fmt.Sprintf("hash %s, pHash:[%s], from: %s, to: %s, value: %s,\n nonce: %d, signatute: %s, pubkey: %s, "+
    85  		"minedNonce: %v, data: %x", t.Hash.Hex(),
    86  		strings.Join(phashes, " ,"), t.From.Hex(), t.To.Hex(), t.Value,
    87  		t.AccountNonce, t.Signature, hexutil.Encode(t.PublicKey.KeyBytes), t.MineNonce, t.Data)
    88  }
    89  
    90  func (t *Tx) SignatureTargets() []byte {
    91  	// log.WithField("tx", t).Tracef("SignatureTargets: %s", t.Dump())
    92  
    93  	w := byteutil.NewBinaryWriter()
    94  
    95  	w.Write(t.AccountNonce)
    96  	w.Write(t.From.Bytes)
    97  	w.Write(t.To.Bytes, t.Value.GetSigBytes(), t.Data, t.TokenId)
    98  	return w.Bytes()
    99  }
   100  
   101  func (t *Tx) GetType() TxBaseType {
   102  	return TxBaseTypeTx
   103  }
   104  
   105  func (t *Tx) GetHeight() uint64 {
   106  	return t.Height
   107  }
   108  
   109  func (t *Tx) GetWeight() uint64 {
   110  	if t.Weight == 0 {
   111  		panic("implementation error: weight not initialized")
   112  	}
   113  	return t.Weight
   114  }
   115  
   116  func (t *Tx) GetHash() types.Hash {
   117  	return t.Hash
   118  }
   119  
   120  func (t *Tx) GetParents() types.Hashes {
   121  	return t.ParentsHash
   122  }
   123  
   124  func (t *Tx) String() string {
   125  	return t.DebugString()
   126  	//return fmt.Sprintf("T-%d-[%.10s]-%d", t.Height, t.GetHash().Hex(), t.Weight)
   127  }
   128  
   129  func (t *Tx) DebugString() string {
   130  	s := t.GetHash().Hex()
   131  	return fmt.Sprintf("Tx-H%d-W%d-N%d-[%s]", t.Height, t.Weight, t.AccountNonce, s[len(s)-8:])
   132  }
   133  
   134  //CalculateWeight  a core algorithm for tx sorting,
   135  //a tx's weight must bigger than any of it's parent's weight  and bigger than any of it's elder transaction's
   136  func (t *Tx) CalculateWeight(parents Txis) uint64 {
   137  	var maxWeight uint64
   138  	for _, p := range parents {
   139  		if p.GetWeight() > maxWeight {
   140  			maxWeight = p.GetWeight()
   141  		}
   142  	}
   143  	return maxWeight + 1
   144  }
   145  
   146  func (t *Tx) Compare(tx Txi) bool {
   147  	switch tx := tx.(type) {
   148  	case *Tx:
   149  		if t.GetHash().Cmp(tx.GetHash()) == 0 {
   150  			return true
   151  		}
   152  		return false
   153  	default:
   154  		return false
   155  	}
   156  }