prysm-pulse/sharding/collation.go
Raul Jordan 5dc498f402 sharding: shardDB put, delete can return error now
Former-commit-id: 8d96f95dbe6419d804d86b05dca8f1e90e2b56ed [formerly c42c9e84b82dcc29d50739e0b8eac5bd65f93024]
Former-commit-id: 527cb5170e3eb156bf7c0bb2ec69ef9f32cc8320
2018-05-10 22:00:54 -04:00

111 lines
3.9 KiB
Go

package sharding
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/rlp"
)
// Collation base struct.
type Collation struct {
header *CollationHeader
// body represents the serialized blob of a collation's transactions.
body []byte
// transactions serves as a useful slice to store deserialized chunks from the
// collation's body. Every time this transactions slice is updated, the serialized
// body would need to be recalculated. This will be a useful property for proposers
// in our system.
transactions []*types.Transaction
}
// CollationHeader base struct.
type CollationHeader struct {
// RLP decoding only works on exported properties of structs. In this case, we want
// to keep collation properties as read-only and only accessible through getters.
// We can accomplish this through this nested data property.
data collationHeaderData
}
type collationHeaderData struct {
ShardID *big.Int // the shard ID of the shard.
ChunkRoot *common.Hash // the root of the chunk tree which identifies collation body.
Period *big.Int // the period number in which collation to be Pncluded.
ProposerAddress *common.Address // address of the collation proposer.
ProposerSignature []byte // the proposer's signature for calculating collation hash.
}
// NewCollationHeader initializes a collation header struct.
func NewCollationHeader(shardID *big.Int, chunkRoot *common.Hash, period *big.Int, proposerAddress *common.Address, proposerSignature []byte) *CollationHeader {
data := collationHeaderData{
ShardID: shardID,
ChunkRoot: chunkRoot,
Period: period,
ProposerAddress: proposerAddress,
ProposerSignature: proposerSignature,
}
return &CollationHeader{data}
}
// Hash takes the keccak256 of the collation header's contents.
func (h *CollationHeader) Hash() (hash common.Hash) {
hw := sha3.NewKeccak256()
rlp.Encode(hw, h)
hw.Sum(hash[:0])
return hash
}
// ShardID the collation corresponds to.
func (h *CollationHeader) ShardID() *big.Int { return h.data.ShardID }
// Period the collation corresponds to.
func (h *CollationHeader) Period() *big.Int { return h.data.Period }
// ChunkRoot of the serialized collation body.
func (h *CollationHeader) ChunkRoot() *common.Hash { return h.data.ChunkRoot }
// EncodeRLP gives an encoded representation of the collation header.
func (h *CollationHeader) EncodeRLP() ([]byte, error) {
encoded, err := rlp.EncodeToBytes(&h.data)
if err != nil {
return nil, err
}
return encoded, nil
}
// DecodeRLP uses an RLP Stream to populate the data field of a collation header.
func (h *CollationHeader) DecodeRLP(s *rlp.Stream) error {
return s.Decode(&h.data)
}
// Header returns the collation's header.
func (c *Collation) Header() *CollationHeader { return c.header }
// Body returns the collation's byte body.
func (c *Collation) Body() []byte { return c.body }
// Transactions returns an array of tx's in the collation.
func (c *Collation) Transactions() []*types.Transaction { return c.transactions }
// ProposerAddress is the coinbase addr of the creator for the collation.
func (c *Collation) ProposerAddress() *common.Address {
return c.header.data.ProposerAddress
}
// AddTransaction adds to the collation's body of tx blobs.
func (c *Collation) AddTransaction(tx *types.Transaction) {
// TODO: Include blob serialization instead.
c.transactions = append(c.transactions, tx)
}
// CalculateChunkRoot updates the collation header's chunk root based on the body.
func (c *Collation) CalculateChunkRoot() {
// TODO: this needs to be based on blob serialization.
// For proof of custody we need to split chunks (body) into chunk + salt and
// take the merkle root of that.
chunkRoot := common.BytesToHash(c.body)
c.header.data.ChunkRoot = &chunkRoot
}