github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/status/status.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package status
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  	"fmt"
    10  
    11  	"github.com/MetalBlockchain/metalgo/vms/components/verify"
    12  )
    13  
    14  // List of possible status values:
    15  // - [Unknown] The transaction is not known
    16  // - [Committed] The transaction was proposed and committed
    17  // - [Aborted] The transaction was proposed and aborted
    18  // - [Processing] The transaction was proposed and is currently in the preferred chain
    19  // - [Dropped] The transaction was dropped due to failing verification
    20  const (
    21  	Unknown    Status = 0
    22  	Committed  Status = 4
    23  	Aborted    Status = 5
    24  	Processing Status = 6
    25  	Dropped    Status = 8
    26  )
    27  
    28  var (
    29  	errUnknownStatus = errors.New("unknown status")
    30  
    31  	_ json.Marshaler    = Status(0)
    32  	_ verify.Verifiable = Status(0)
    33  	_ fmt.Stringer      = Status(0)
    34  )
    35  
    36  type Status uint32
    37  
    38  func (s Status) MarshalJSON() ([]byte, error) {
    39  	return []byte(`"` + s.String() + `"`), s.Verify()
    40  }
    41  
    42  func (s *Status) UnmarshalJSON(b []byte) error {
    43  	switch string(b) {
    44  	case `"Unknown"`:
    45  		*s = Unknown
    46  	case `"Committed"`:
    47  		*s = Committed
    48  	case `"Aborted"`:
    49  		*s = Aborted
    50  	case `"Processing"`:
    51  		*s = Processing
    52  	case `"Dropped"`:
    53  		*s = Dropped
    54  	case "null":
    55  	default:
    56  		return errUnknownStatus
    57  	}
    58  	return nil
    59  }
    60  
    61  // Verify that this is a valid status.
    62  func (s Status) Verify() error {
    63  	switch s {
    64  	case Unknown, Committed, Aborted, Processing, Dropped:
    65  		return nil
    66  	default:
    67  		return errUnknownStatus
    68  	}
    69  }
    70  
    71  func (s Status) String() string {
    72  	switch s {
    73  	case Unknown:
    74  		return "Unknown"
    75  	case Committed:
    76  		return "Committed"
    77  	case Aborted:
    78  		return "Aborted"
    79  	case Processing:
    80  		return "Processing"
    81  	case Dropped:
    82  		return "Dropped"
    83  	default:
    84  		return "Invalid status"
    85  	}
    86  }