github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/u2u/legacy_serialization.go (about)

     1  package u2u
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  
     7  	"github.com/unicornultrafoundation/go-u2u/rlp"
     8  )
     9  
    10  type GasRulesRLPV0 struct {
    11  	MaxEventGas  uint64
    12  	EventGas     uint64
    13  	ParentGas    uint64
    14  	ExtraDataGas uint64
    15  }
    16  
    17  // EncodeRLP is for RLP serialization.
    18  func (r Rules) EncodeRLP(w io.Writer) error {
    19  	// write the type
    20  	rType := uint8(0)
    21  	if r.Upgrades != (Upgrades{}) {
    22  		rType = 1
    23  		_, err := w.Write([]byte{rType})
    24  		if err != nil {
    25  			return err
    26  		}
    27  	}
    28  	// write the main body
    29  	rlpR := RulesRLP(r)
    30  	err := rlp.Encode(w, &rlpR)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	// write additional fields, depending on the type
    35  	if rType > 0 {
    36  		err := rlp.Encode(w, &r.Upgrades)
    37  		if err != nil {
    38  			return err
    39  		}
    40  	}
    41  	return nil
    42  }
    43  
    44  // DecodeRLP is for RLP serialization.
    45  func (r *Rules) DecodeRLP(s *rlp.Stream) error {
    46  	kind, _, err := s.Kind()
    47  	if err != nil {
    48  		return err
    49  	}
    50  	// read rType
    51  	rType := uint8(0)
    52  	if kind == rlp.Byte {
    53  		var b []byte
    54  		if b, err = s.Bytes(); err != nil {
    55  			return err
    56  		}
    57  		if len(b) == 0 {
    58  			return errors.New("empty typed")
    59  		}
    60  		rType = b[0]
    61  		if rType == 0 || rType > 1 {
    62  			return errors.New("unknown type")
    63  		}
    64  	}
    65  	// decode the main body
    66  	rlpR := RulesRLP{}
    67  	err = s.Decode(&rlpR)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	*r = Rules(rlpR)
    72  	// decode additional fields, depending on the type
    73  	if rType >= 1 {
    74  		err = s.Decode(&r.Upgrades)
    75  		if err != nil {
    76  			return err
    77  		}
    78  	}
    79  	return nil
    80  }
    81  
    82  // EncodeRLP is for RLP serialization.
    83  func (u Upgrades) EncodeRLP(w io.Writer) error {
    84  	bitmap := struct {
    85  		V uint64
    86  	}{}
    87  	if u.Berlin {
    88  		bitmap.V |= berlinBit
    89  	}
    90  	if u.London {
    91  		bitmap.V |= londonBit
    92  	}
    93  	if u.Llr {
    94  		bitmap.V |= llrBit
    95  	}
    96  	return rlp.Encode(w, &bitmap)
    97  }
    98  
    99  // DecodeRLP is for RLP serialization.
   100  func (u *Upgrades) DecodeRLP(s *rlp.Stream) error {
   101  	bitmap := struct {
   102  		V uint64
   103  	}{}
   104  	err := s.Decode(&bitmap)
   105  	if err != nil {
   106  		return err
   107  	}
   108  	u.Berlin = (bitmap.V & berlinBit) != 0
   109  	u.London = (bitmap.V & londonBit) != 0
   110  	u.Llr = (bitmap.V & llrBit) != 0
   111  	return nil
   112  }
   113  
   114  // EncodeRLP is for RLP serialization.
   115  func (r GasRules) EncodeRLP(w io.Writer) error {
   116  	// write the type
   117  	rType := uint8(0)
   118  	if r.EpochVoteGas != 0 || r.MisbehaviourProofGas != 0 || r.BlockVotesBaseGas != 0 || r.BlockVoteGas != 0 {
   119  		rType = 1
   120  		_, err := w.Write([]byte{rType})
   121  		if err != nil {
   122  			return err
   123  		}
   124  	}
   125  	if rType == 0 {
   126  		return rlp.Encode(w, &GasRulesRLPV0{
   127  			MaxEventGas:  r.MaxEventGas,
   128  			EventGas:     r.EventGas,
   129  			ParentGas:    r.ParentGas,
   130  			ExtraDataGas: r.ExtraDataGas,
   131  		})
   132  	} else {
   133  		return rlp.Encode(w, (*GasRulesRLPV1)(&r))
   134  	}
   135  }
   136  
   137  // DecodeRLP is for RLP serialization.
   138  func (r *GasRules) DecodeRLP(s *rlp.Stream) error {
   139  	kind, _, err := s.Kind()
   140  	if err != nil {
   141  		return err
   142  	}
   143  	// read rType
   144  	rType := uint8(0)
   145  	if kind == rlp.Byte {
   146  		var b []byte
   147  		if b, err = s.Bytes(); err != nil {
   148  			return err
   149  		}
   150  		if len(b) == 0 {
   151  			return errors.New("empty typed")
   152  		}
   153  		rType = b[0]
   154  		if rType == 0 || rType > 1 {
   155  			return errors.New("unknown type")
   156  		}
   157  	}
   158  	// decode the main body
   159  	if rType == 0 {
   160  		rlpR := GasRulesRLPV0{}
   161  		err = s.Decode(&rlpR)
   162  		if err != nil {
   163  			return err
   164  		}
   165  		*r = GasRules{
   166  			MaxEventGas:  rlpR.MaxEventGas,
   167  			EventGas:     rlpR.EventGas,
   168  			ParentGas:    rlpR.ParentGas,
   169  			ExtraDataGas: rlpR.ExtraDataGas,
   170  		}
   171  		return nil
   172  	} else {
   173  		return s.Decode((*GasRulesRLPV1)(r))
   174  	}
   175  }