|
| 1 | +// (c) 2024, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package types |
| 5 | + |
| 6 | +import ( |
| 7 | + "io" |
| 8 | + |
| 9 | + ethtypes "github.com/ava-labs/libevm/core/types" |
| 10 | + "github.com/ava-labs/libevm/rlp" |
| 11 | +) |
| 12 | + |
| 13 | +// BodyExtra is a struct that contains extra fields used by Avalanche |
| 14 | +// in the body. |
| 15 | +// This type uses BodySerializable to encode and decode the extra fields |
| 16 | +// along with the upstream type for compatibility with existing network blocks. |
| 17 | +type BodyExtra struct { |
| 18 | + Version uint32 |
| 19 | + ExtData *[]byte |
| 20 | + |
| 21 | + // Fields removed from geth: |
| 22 | + // - withdrawals Withdrawals |
| 23 | +} |
| 24 | + |
| 25 | +func (b *BodyExtra) EncodeRLP(eth *ethtypes.Body, writer io.Writer) error { |
| 26 | + out := new(bodySerializable) |
| 27 | + |
| 28 | + out.updateFromEth(eth) |
| 29 | + out.updateFromExtras(b) |
| 30 | + |
| 31 | + return rlp.Encode(writer, out) |
| 32 | +} |
| 33 | + |
| 34 | +func (b *BodyExtra) DecodeRLP(eth *ethtypes.Body, stream *rlp.Stream) error { |
| 35 | + in := new(bodySerializable) |
| 36 | + if err := stream.Decode(in); err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + in.updateToEth(eth) |
| 41 | + in.updateToExtras(b) |
| 42 | + |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// bodySerializable defines the body in the Ethereum blockchain, |
| 47 | +// as it is to be serialized into RLP. |
| 48 | +type bodySerializable struct { |
| 49 | + Transactions []*Transaction |
| 50 | + Uncles []*Header |
| 51 | + Version uint32 |
| 52 | + ExtData *[]byte `rlp:"nil"` |
| 53 | +} |
| 54 | + |
| 55 | +// updateFromEth updates the [*bodySerializable] from the [*ethtypes.Body]. |
| 56 | +func (b *bodySerializable) updateFromEth(eth *ethtypes.Body) { |
| 57 | + b.Transactions = eth.Transactions |
| 58 | + b.Uncles = eth.Uncles |
| 59 | +} |
| 60 | + |
| 61 | +// updateToEth updates the [*ethtypes.Body] from the [*bodySerializable]. |
| 62 | +func (b *bodySerializable) updateToEth(eth *ethtypes.Body) { |
| 63 | + eth.Transactions = b.Transactions |
| 64 | + eth.Uncles = b.Uncles |
| 65 | +} |
| 66 | + |
| 67 | +// updateFromExtras updates the [*bodySerializable] from the [*BodyExtra]. |
| 68 | +func (b *bodySerializable) updateFromExtras(extras *BodyExtra) { |
| 69 | + b.Version = extras.Version |
| 70 | + b.ExtData = extras.ExtData |
| 71 | +} |
| 72 | + |
| 73 | +// updateToExtras updates the [*BodyExtra] from the [*bodySerializable]. |
| 74 | +func (b *bodySerializable) updateToExtras(extras *BodyExtra) { |
| 75 | + extras.Version = b.Version |
| 76 | + extras.ExtData = b.ExtData |
| 77 | +} |
0 commit comments