github.com/MetalBlockchain/subnet-evm@v0.4.9/plugin/evm/status.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package evm
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  )
    10  
    11  var (
    12  	errUnknownStatus = errors.New("unknown status")
    13  )
    14  
    15  // Status ...
    16  type Status uint32
    17  
    18  // List of possible status values
    19  // [Unknown] Zero value, means the status is not known
    20  // [Dropped] means the transaction was in the mempool, but was dropped because it failed verification
    21  // [Processing] means the transaction is in the mempool
    22  // [Accepted] means the transaction was accepted
    23  const (
    24  	Unknown Status = iota
    25  	Dropped
    26  	Processing
    27  	Accepted
    28  )
    29  
    30  // MarshalJSON ...
    31  func (s Status) MarshalJSON() ([]byte, error) {
    32  	if err := s.Valid(); err != nil {
    33  		return nil, err
    34  	}
    35  	return []byte(fmt.Sprintf("%q", s)), nil
    36  }
    37  
    38  // UnmarshalJSON ...
    39  func (s *Status) UnmarshalJSON(b []byte) error {
    40  	str := string(b)
    41  	if str == "null" {
    42  		return nil
    43  	}
    44  	switch str {
    45  	case `"Unknown"`:
    46  		*s = Unknown
    47  	case `"Dropped"`:
    48  		*s = Dropped
    49  	case `"Processing"`:
    50  		*s = Processing
    51  	case `"Accepted"`:
    52  		*s = Accepted
    53  	default:
    54  		return errUnknownStatus
    55  	}
    56  	return nil
    57  }
    58  
    59  // Valid returns nil if the status is a valid status.
    60  func (s Status) Valid() error {
    61  	switch s {
    62  	case Unknown, Dropped, Processing, Accepted:
    63  		return nil
    64  	default:
    65  		return errUnknownStatus
    66  	}
    67  }
    68  
    69  func (s Status) String() string {
    70  	switch s {
    71  	case Unknown:
    72  		return "Unknown"
    73  	case Dropped:
    74  		return "Dropped"
    75  	case Processing:
    76  		return "Processing"
    77  	case Accepted:
    78  		return "Accepted"
    79  	default:
    80  		return "Invalid status"
    81  	}
    82  }