mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
91cc76f186
sharding: beginning proposer service tests Former-commit-id: 40ced960afcf8f07fd946780a2bbcd4a5f057d40 [formerly bbe6aa3bf36354edcbeb998417c490ee82df56c3] Former-commit-id: 518cfdb51459b4b450d4b14666edf656414894ae
95 lines
3.6 KiB
Go
95 lines
3.6 KiB
Go
package proposer
|
|
|
|
import (
|
|
"fmt"
|
|
"math/big"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/ethereum/go-ethereum/sharding"
|
|
"github.com/ethereum/go-ethereum/sharding/node"
|
|
)
|
|
|
|
// createCollation creates collation base struct with header
|
|
// and body. Header consists of shardID, ChunkRoot, period,
|
|
// proposer addr and signatures. Body contains serialized blob
|
|
// of a collations transactions.
|
|
func createCollation(n node.Node, shardId *big.Int, period *big.Int, txs []*types.Transaction) (*sharding.Collation, error) {
|
|
// shardId has to be within range
|
|
if shardId.Cmp(big.NewInt(0)) < 0 || shardId.Cmp(big.NewInt(sharding.ShardCount)) > 0 {
|
|
return nil, fmt.Errorf("can't create collation for shard %v. Must be between 0 and %v", shardId, sharding.ShardCount)
|
|
}
|
|
|
|
// period can't be less than current period.
|
|
//ctx := context.Background()
|
|
//b, _ := n.ChainReader().BlockByNumber(ctx, nil)
|
|
b := big.NewInt(5)
|
|
currentPeriod := new(big.Int).Div(b, big.NewInt(sharding.PeriodLength))
|
|
if period.Cmp(currentPeriod) < 0 {
|
|
return nil, fmt.Errorf("can't create collation with period %v less than current period %v", period, currentPeriod)
|
|
}
|
|
|
|
// serialized tx to blob for collation body.
|
|
blobs, err := sharding.SerializeTxToBlob(txs)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("can't create collation, serialization to blob failed: %v", err)
|
|
}
|
|
|
|
// construct the header, leave chunkRoot and signature fields empty, to be filled later.
|
|
addr := n.Account().Address
|
|
header := sharding.NewCollationHeader(shardId, nil, period, &addr, nil)
|
|
|
|
// construct the body with header, blobs(serialized txs) and txs.
|
|
collation := sharding.NewCollation(header, blobs, txs)
|
|
collation.CalculateChunkRoot()
|
|
sig, err := n.Sign(collation.Header().Hash())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("can't create collation, sign collationHeader failed: %v", err)
|
|
}
|
|
|
|
// add proposer signature to collation header.
|
|
collation.Header().AddSig(sig)
|
|
|
|
return collation, nil
|
|
}
|
|
|
|
// addHeader adds the collation header to the main chain by sending
|
|
// an addHeader transaction to the sharding manager contract.
|
|
// There can only exist one header per period per shard, it's proposer's
|
|
// responsibility to check if a header has been added.
|
|
func addHeader(n node.Node, collation sharding.Collation) error {
|
|
log.Info("Adding header to SMC")
|
|
|
|
txOps, err := n.CreateTXOpts(big.NewInt(0))
|
|
if err != nil {
|
|
return fmt.Errorf("unable to initiate add header transaction: %v", err)
|
|
}
|
|
|
|
var churnkRoot [32]byte
|
|
copy(churnkRoot[:], collation.Header().ChunkRoot().String())
|
|
|
|
_, err = n.SMCTransactor().AddHeader(txOps, collation.Header().ShardID(), collation.Header().Period(), churnkRoot)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to add header to SMC: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkHeaderAvailability checks if a collation header has already
|
|
// added to the main chain. There can only be one header per shard
|
|
// per period, proposer should check if anyone else has added the header.
|
|
// checkHeaderAvailability returns true if it's available, false if it's unavailable.
|
|
func checkHeaderAvailability(n node.Node, shardId big.Int, period big.Int) (bool, error) {
|
|
log.Info("Checking header in shard: %d, period: %d", shardId, period)
|
|
|
|
// Get the period of the last header.
|
|
lastPeriod, err := n.SMCCaller().LastSubmittedCollation(&bind.CallOpts{}, &shardId)
|
|
if err != nil {
|
|
return false, fmt.Errorf("unable to get the period of last submitted collation: %v", err)
|
|
}
|
|
|
|
// True if current period is greater than last added period.
|
|
return period.Cmp(lastPeriod) > 0, nil
|
|
}
|