github.com/annchain/OG@v0.0.9/og/protocol/ogmessage/archive/tx.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/arefactor/og/types"
    19  	"github.com/annchain/OG/common"
    20  	"math/rand"
    21  	"strings"
    22  	"time"
    23  
    24  	"github.com/annchain/OG/common/math"
    25  )
    26  
    27  //go:generate msgp
    28  
    29  //msgp:tuple Txs
    30  type Txs []*Tx
    31  
    32  //msgp:tuple Tx
    33  type Tx struct {
    34  	TxBase
    35  	From    *common.Address
    36  	To      common.Address
    37  	Value   *math.BigInt
    38  	TokenId int32
    39  	Data    []byte
    40  	confirm time.Time
    41  }
    42  
    43  func (t *Tx) GetConfirm() time.Duration {
    44  	return time.Since(t.confirm)
    45  }
    46  
    47  func (t *Tx) Setconfirm() {
    48  	t.confirm = time.Now()
    49  }
    50  
    51  func (t *Tx) String() string {
    52  	if t.GetSender() == nil {
    53  		return fmt.Sprintf("%s-[nil]-%d-Tx", t.TxBase.String(), t.AccountNonce)
    54  	} else {
    55  		return fmt.Sprintf("%s-[%.10s]-%d-Tx", t.TxBase.String(), t.Sender().String(), t.AccountNonce)
    56  	}
    57  
    58  }
    59  
    60  func SampleTx() *Tx {
    61  	v, _ := math.NewBigIntFromString("-1234567890123456789012345678901234567890123456789012345678901234567890", 10)
    62  	from := common.HexToAddress("0x99")
    63  	return &Tx{TxBase: TxBase{
    64  		Height:       12,
    65  		ParentsHash:  types.Hashes{types.HexToHash("0xCCDD"), types.HexToHash("0xEEFF")},
    66  		Type:         TxBaseTypeNormal,
    67  		AccountNonce: 234,
    68  	},
    69  		From:  &from,
    70  		To:    common.HexToAddress("0x88"),
    71  		Value: v,
    72  	}
    73  }
    74  
    75  func RandomTx() *Tx {
    76  	from := types.RandomAddress()
    77  	return &Tx{TxBase: TxBase{
    78  		Hash:         types.RandomHash(),
    79  		Height:       uint64(rand.Int63n(1000)),
    80  		ParentsHash:  types.Hashes{types.RandomHash(), types.RandomHash()},
    81  		Type:         TxBaseTypeNormal,
    82  		AccountNonce: uint64(rand.Int63n(50000)),
    83  		Weight:       uint64(rand.Int31n(2000)),
    84  	},
    85  		From:  &from,
    86  		To:    types.RandomAddress(),
    87  		Value: math.NewBigInt(rand.Int63()),
    88  	}
    89  }
    90  
    91  func (t *Tx) Sender() common.Address {
    92  	return *t.From
    93  }
    94  
    95  func (t *Tx) GetSender() *common.Address {
    96  	return t.From
    97  }
    98  
    99  func (t *Tx) SetSender(addr common.Address) {
   100  	t.From = &addr
   101  }
   102  
   103  func (t *Tx) RawTx() *RawTx {
   104  	if t == nil {
   105  		return nil
   106  	}
   107  	rawTx := &RawTx{
   108  		TxBase:  t.TxBase,
   109  		To:      t.To,
   110  		Value:   t.Value,
   111  		Data:    t.Data,
   112  		TokenId: t.TokenId,
   113  	}
   114  	return rawTx
   115  }
   116  
   117  func (t Txs) String() string {
   118  	var strs []string
   119  	for _, v := range t {
   120  		strs = append(strs, v.String())
   121  	}
   122  	return strings.Join(strs, ", ")
   123  }
   124  
   125  func (t Txs) ToRawTxs() RawTxs {
   126  	if len(t) == 0 {
   127  		return nil
   128  	}
   129  	var rawTxs []*RawTx
   130  	for _, v := range t {
   131  		rasTx := v.RawTx()
   132  		rawTxs = append(rawTxs, rasTx)
   133  	}
   134  	return rawTxs
   135  }
   136  
   137  func (r *Txs) Len() int {
   138  	if r == nil {
   139  		return 0
   140  	}
   141  	return len(*r)
   142  }
   143  
   144  func (c *Tx) RawTxi() RawTxi {
   145  	return c.RawTx()
   146  }