Follow the same strategy for header versioning, except put everything into the same "./core/types" package – for now. Adaptation of the existing code outside the package to come next.pull/1546/head
parent
1a1caa422d
commit
070a42c387
@ -0,0 +1,31 @@ |
||||
package types |
||||
|
||||
import "github.com/harmony-one/harmony/block" |
||||
|
||||
// BodyFieldSetter is a body field setter.
|
||||
type BodyFieldSetter struct { |
||||
b *Body |
||||
} |
||||
|
||||
// Transactions sets the Transactions field of the body.
|
||||
func (bfs BodyFieldSetter) Transactions(newTransactions []*Transaction) BodyFieldSetter { |
||||
bfs.b.SetTransactions(newTransactions) |
||||
return bfs |
||||
} |
||||
|
||||
// Uncles sets the Uncles field of the body.
|
||||
func (bfs BodyFieldSetter) Uncles(newUncles []*block.Header) BodyFieldSetter { |
||||
bfs.b.SetUncles(newUncles) |
||||
return bfs |
||||
} |
||||
|
||||
// IncomingReceipts sets the IncomingReceipts field of the body.
|
||||
func (bfs BodyFieldSetter) IncomingReceipts(newIncomingReceipts CXReceiptsProofs) BodyFieldSetter { |
||||
bfs.b.SetIncomingReceipts(newIncomingReceipts) |
||||
return bfs |
||||
} |
||||
|
||||
// Body ends the field setter chain and returns the underlying body itself.
|
||||
func (bfs BodyFieldSetter) Body() *Body { |
||||
return bfs.b |
||||
} |
@ -0,0 +1,84 @@ |
||||
package types |
||||
|
||||
import ( |
||||
"io" |
||||
|
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
|
||||
"github.com/harmony-one/harmony/block" |
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
) |
||||
|
||||
// BodyV0 is the V0 block body
|
||||
type BodyV0 struct { |
||||
f bodyFieldsV0 |
||||
} |
||||
|
||||
type bodyFieldsV0 struct { |
||||
Transactions []*Transaction |
||||
Uncles []*block.Header |
||||
} |
||||
|
||||
// Transactions returns the list of transactions.
|
||||
//
|
||||
// The returned list is a deep copy; the caller may do anything with it without
|
||||
// affecting the original.
|
||||
func (b *BodyV0) Transactions() (txs []*Transaction) { |
||||
for _, tx := range b.f.Transactions { |
||||
txs = append(txs, tx.Copy()) |
||||
} |
||||
return txs |
||||
} |
||||
|
||||
// SetTransactions sets the list of transactions with a deep copy of the given
|
||||
// list.
|
||||
func (b *BodyV0) SetTransactions(newTransactions []*Transaction) { |
||||
var txs []*Transaction |
||||
for _, tx := range newTransactions { |
||||
txs = append(txs, tx.Copy()) |
||||
} |
||||
b.f.Transactions = txs |
||||
} |
||||
|
||||
// Uncles returns a deep copy of the list of uncle headers of this block.
|
||||
func (b *BodyV0) Uncles() (uncles []*block.Header) { |
||||
for _, uncle := range b.f.Uncles { |
||||
uncles = append(uncles, CopyHeader(uncle)) |
||||
} |
||||
return uncles |
||||
} |
||||
|
||||
// SetUncles sets the list of uncle headers with a deep copy of the given list.
|
||||
func (b *BodyV0) SetUncles(newUncle []*block.Header) { |
||||
var uncles []*block.Header |
||||
for _, uncle := range newUncle { |
||||
uncles = append(uncles, CopyHeader(uncle)) |
||||
} |
||||
b.f.Uncles = uncles |
||||
} |
||||
|
||||
// IncomingReceipts returns a deep copy of the list of incoming cross-shard
|
||||
// transaction receipts of this block.
|
||||
func (b *BodyV0) IncomingReceipts() (incomingReceipts CXReceiptsProofs) { |
||||
return nil |
||||
} |
||||
|
||||
// SetIncomingReceipts sets the list of incoming cross-shard transaction
|
||||
// receipts of this block with a dep copy of the given list.
|
||||
func (b *BodyV0) SetIncomingReceipts(newIncomingReceipts CXReceiptsProofs) { |
||||
if len(newIncomingReceipts) > 0 { |
||||
utils.Logger().Warn(). |
||||
Msg("cannot store incoming CX receipts in v0 block body") |
||||
} |
||||
} |
||||
|
||||
// EncodeRLP RLP-encodes the block body into the given writer.
|
||||
func (b *BodyV0) EncodeRLP(w io.Writer) error { |
||||
return rlp.Encode(w, &b.f) |
||||
} |
||||
|
||||
// DecodeRLP RLP-decodes a block body from the given RLP stream into the
|
||||
// receiver.
|
||||
func (b *BodyV0) DecodeRLP(s *rlp.Stream) error { |
||||
return s.Decode(&b.f) |
||||
} |
@ -0,0 +1,81 @@ |
||||
package types |
||||
|
||||
import ( |
||||
"io" |
||||
|
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
|
||||
"github.com/harmony-one/harmony/block" |
||||
) |
||||
|
||||
// BodyV1 is the V1 block body
|
||||
type BodyV1 struct { |
||||
f bodyFieldsV1 |
||||
} |
||||
|
||||
type bodyFieldsV1 struct { |
||||
Transactions []*Transaction |
||||
Uncles []*block.Header |
||||
IncomingReceipts CXReceiptsProofs |
||||
} |
||||
|
||||
// Transactions returns the list of transactions.
|
||||
//
|
||||
// The returned list is a deep copy; the caller may do anything with it without
|
||||
// affecting the original.
|
||||
func (b *BodyV1) Transactions() (txs []*Transaction) { |
||||
for _, tx := range b.f.Transactions { |
||||
txs = append(txs, tx.Copy()) |
||||
} |
||||
return txs |
||||
} |
||||
|
||||
// SetTransactions sets the list of transactions with a deep copy of the given
|
||||
// list.
|
||||
func (b *BodyV1) SetTransactions(newTransactions []*Transaction) { |
||||
var txs []*Transaction |
||||
for _, tx := range newTransactions { |
||||
txs = append(txs, tx.Copy()) |
||||
} |
||||
b.f.Transactions = txs |
||||
} |
||||
|
||||
// Uncles returns a deep copy of the list of uncle headers of this block.
|
||||
func (b *BodyV1) Uncles() (uncles []*block.Header) { |
||||
for _, uncle := range b.f.Uncles { |
||||
uncles = append(uncles, CopyHeader(uncle)) |
||||
} |
||||
return uncles |
||||
} |
||||
|
||||
// SetUncles sets the list of uncle headers with a deep copy of the given list.
|
||||
func (b *BodyV1) SetUncles(newUncle []*block.Header) { |
||||
var uncles []*block.Header |
||||
for _, uncle := range newUncle { |
||||
uncles = append(uncles, CopyHeader(uncle)) |
||||
} |
||||
b.f.Uncles = uncles |
||||
} |
||||
|
||||
// IncomingReceipts returns a deep copy of the list of incoming cross-shard
|
||||
// transaction receipts of this block.
|
||||
func (b *BodyV1) IncomingReceipts() (incomingReceipts CXReceiptsProofs) { |
||||
return b.f.IncomingReceipts.Copy() |
||||
} |
||||
|
||||
// SetIncomingReceipts sets the list of incoming cross-shard transaction
|
||||
// receipts of this block with a dep copy of the given list.
|
||||
func (b *BodyV1) SetIncomingReceipts(newIncomingReceipts CXReceiptsProofs) { |
||||
b.f.IncomingReceipts = newIncomingReceipts.Copy() |
||||
} |
||||
|
||||
// EncodeRLP RLP-encodes the block body into the given writer.
|
||||
func (b *BodyV1) EncodeRLP(w io.Writer) error { |
||||
return rlp.Encode(w, &b.f) |
||||
} |
||||
|
||||
// DecodeRLP RLP-decodes a block body from the given RLP stream into the
|
||||
// receiver.
|
||||
func (b *BodyV1) DecodeRLP(s *rlp.Stream) error { |
||||
return s.Decode(&b.f) |
||||
} |
Loading…
Reference in new issue