github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/signer/core/types.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"math/big"
    23  	"strings"
    24  
    25  	"github.com/fff-chain/go-fff/common"
    26  	"github.com/fff-chain/go-fff/common/hexutil"
    27  	"github.com/fff-chain/go-fff/core/types"
    28  )
    29  
    30  type ValidationInfo struct {
    31  	Typ     string `json:"type"`
    32  	Message string `json:"message"`
    33  }
    34  type ValidationMessages struct {
    35  	Messages []ValidationInfo
    36  }
    37  
    38  const (
    39  	WARN = "WARNING"
    40  	CRIT = "CRITICAL"
    41  	INFO = "Info"
    42  )
    43  
    44  func (vs *ValidationMessages) Crit(msg string) {
    45  	vs.Messages = append(vs.Messages, ValidationInfo{CRIT, msg})
    46  }
    47  func (vs *ValidationMessages) Warn(msg string) {
    48  	vs.Messages = append(vs.Messages, ValidationInfo{WARN, msg})
    49  }
    50  func (vs *ValidationMessages) Info(msg string) {
    51  	vs.Messages = append(vs.Messages, ValidationInfo{INFO, msg})
    52  }
    53  
    54  /// getWarnings returns an error with all messages of type WARN of above, or nil if no warnings were present
    55  func (v *ValidationMessages) getWarnings() error {
    56  	var messages []string
    57  	for _, msg := range v.Messages {
    58  		if msg.Typ == WARN || msg.Typ == CRIT {
    59  			messages = append(messages, msg.Message)
    60  		}
    61  	}
    62  	if len(messages) > 0 {
    63  		return fmt.Errorf("validation failed: %s", strings.Join(messages, ","))
    64  	}
    65  	return nil
    66  }
    67  
    68  // SendTxArgs represents the arguments to submit a transaction
    69  type SendTxArgs struct {
    70  	From     common.MixedcaseAddress  `json:"from"`
    71  	To       *common.MixedcaseAddress `json:"to"`
    72  	Gas      hexutil.Uint64           `json:"gas"`
    73  	GasPrice hexutil.Big              `json:"gasPrice"`
    74  	Value    hexutil.Big              `json:"value"`
    75  	Nonce    hexutil.Uint64           `json:"nonce"`
    76  	// We accept "data" and "input" for backwards-compatibility reasons.
    77  	Data  *hexutil.Bytes `json:"data"`
    78  	Input *hexutil.Bytes `json:"input,omitempty"`
    79  
    80  	// For non-legacy transactions
    81  	AccessList *types.AccessList `json:"accessList,omitempty"`
    82  	ChainID    *hexutil.Big      `json:"chainId,omitempty"`
    83  }
    84  
    85  func (args SendTxArgs) String() string {
    86  	s, err := json.Marshal(args)
    87  	if err == nil {
    88  		return string(s)
    89  	}
    90  	return err.Error()
    91  }
    92  
    93  func (args *SendTxArgs) toTransaction() *types.Transaction {
    94  	var input []byte
    95  	if args.Data != nil {
    96  		input = *args.Data
    97  	} else if args.Input != nil {
    98  		input = *args.Input
    99  	}
   100  	var to *common.Address
   101  	if args.To != nil {
   102  		_to := args.To.Address()
   103  		to = &_to
   104  	}
   105  	var data types.TxData
   106  	if args.AccessList == nil {
   107  		data = &types.LegacyTx{
   108  			To:       to,
   109  			Nonce:    uint64(args.Nonce),
   110  			Gas:      uint64(args.Gas),
   111  			GasPrice: (*big.Int)(&args.GasPrice),
   112  			Value:    (*big.Int)(&args.Value),
   113  			Data:     input,
   114  		}
   115  	} else {
   116  		data = &types.AccessListTx{
   117  			To:         to,
   118  			ChainID:    (*big.Int)(args.ChainID),
   119  			Nonce:      uint64(args.Nonce),
   120  			Gas:        uint64(args.Gas),
   121  			GasPrice:   (*big.Int)(&args.GasPrice),
   122  			Value:      (*big.Int)(&args.Value),
   123  			Data:       input,
   124  			AccessList: *args.AccessList,
   125  		}
   126  	}
   127  	return types.NewTx(data)
   128  }