Skip to content

Commit

Permalink
Reduce "shim types", introduce agreement.Block
Browse files Browse the repository at this point in the history
agreement.Block is similar to what was agreement.ProposableBlock, but
rather than being an interface it is a concrete type.  Therefore,
there can be no confusion: A validated block is not an agreement.Block
just because it has a Block() method.

Instead, FinalizeBlock methods explicit copy their internal Block()
objects into agreement.Block objects during finalization.

This PR also eliminates node.validatedBlock as
*ledgercore.ValidatedBlock implements agreement.ValidatedBlock
already, no wrapper is needed.  Blocks remain as immutable as before.
  • Loading branch information
jannotti committed Apr 12, 2024
1 parent 58a70e6 commit a46890d
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 40 deletions.
17 changes: 7 additions & 10 deletions agreement/abstractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,20 @@ type BlockFactory interface {
// An UnfinishedBlock represents a Block produced by a BlockFactory
// and must be finalized before being proposed by agreement.
type UnfinishedBlock interface {
// WithSeed creates a copy of this UnfinishedBlock with its
// cryptographically random seed set to the given value.
// FinishBlock creates a Proposaable block, having set the cryptographically
// random seed and payout related fields.
//
// Calls to Seed() or to Digest() on the copy's Block must
// reflect the value of the new seed.
FinishBlock(seed committee.Seed, proposer basics.Address, eligible bool) ProposableBlock
FinishBlock(seed committee.Seed, proposer basics.Address, eligible bool) Block

Round() basics.Round
}

// An ProposableBlock represents a Block produced by a BlockFactory,
// that was later finalized by providing the seed and the proposer,
// and can now be proposed by agreement.
type ProposableBlock interface {
// Block returns the underlying block that has been assembled.
Block() bookkeeping.Block
}
// A Block (in agreement) represents an UnfinishedBlock produced by a
// BlockFactory, that was later finalized by providing the seed and the
// proposer, and can now be proposed by agreement.
type Block bookkeeping.Block

// A Ledger represents the sequence of Entries agreed upon by the protocol.
// The Ledger consists of two parts: a LedgerReader and a LedgerWriter, which
Expand Down
4 changes: 2 additions & 2 deletions agreement/agreementtest/simulate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func (b testValidatedBlock) Round() basics.Round {
return b.Inside.Round()
}

func (b testValidatedBlock) FinishBlock(s committee.Seed, proposer basics.Address, eligible bool) agreement.ProposableBlock {
func (b testValidatedBlock) FinishBlock(s committee.Seed, proposer basics.Address, eligible bool) agreement.Block {
b.Inside.BlockHeader.Seed = s
b.Inside.BlockHeader.Proposer = proposer
if !eligible {
b.Inside.BlockHeader.ProposerPayout = basics.MicroAlgos{}
}
return b
return agreement.Block(b.Inside)
}

type testBlockValidator struct{}
Expand Down
6 changes: 3 additions & 3 deletions agreement/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ func (b testValidatedBlock) Round() basics.Round {
return b.Inside.Round()
}

func (b testValidatedBlock) FinishBlock(s committee.Seed, proposer basics.Address, eligible bool) ProposableBlock {
func (b testValidatedBlock) FinishBlock(s committee.Seed, proposer basics.Address, eligible bool) Block {
b.Inside.BlockHeader.Seed = s
b.Inside.BlockHeader.Proposer = proposer
if !eligible {
b.Inside.BlockHeader.ProposerPayout = basics.MicroAlgos{}
}
return b
return Block(b.Inside)
}

type testBlockValidator struct{}
Expand Down Expand Up @@ -538,7 +538,7 @@ func (v *voteMakerHelper) MakeRandomProposalPayload(t *testing.T, r round) (*pro
pb := ub.FinishBlock(committee.Seed{}, basics.Address{}, false)

var payload unauthenticatedProposal
payload.Block = pb.Block()
payload.Block = bookkeeping.Block(pb)
payload.SeedProof = randomVRFProof()

propVal := proposalValue{
Expand Down
4 changes: 2 additions & 2 deletions agreement/fuzzer/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ func (b testValidatedBlock) Round() basics.Round {
return b.Inside.Round()
}

func (b testValidatedBlock) FinishBlock(s committee.Seed, proposer basics.Address, eligible bool) agreement.ProposableBlock {
func (b testValidatedBlock) FinishBlock(s committee.Seed, proposer basics.Address, eligible bool) agreement.Block {
b.Inside.BlockHeader.Seed = s
b.Inside.BlockHeader.Proposer = proposer
if !eligible {
b.Inside.BlockHeader.ProposerPayout = basics.MicroAlgos{}
}
return b
return agreement.Block(b.Inside)
}

type testBlockValidator struct{}
Expand Down
3 changes: 2 additions & 1 deletion agreement/player_permutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/data/committee"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/test/partitiontest"
Expand All @@ -36,7 +37,7 @@ func makeRandomProposalPayload(r round) *proposal {
pb := ub.FinishBlock(committee.Seed{}, basics.Address{}, false)

var payload unauthenticatedProposal
payload.Block = pb.Block()
payload.Block = bookkeeping.Block(pb)
payload.SeedProof = crypto.VRFProof{}

return &proposal{unauthenticatedProposal: payload}
Expand Down
4 changes: 2 additions & 2 deletions agreement/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ type proposal struct {
validatedAt time.Duration
}

func makeProposalFromProposableBlock(blk ProposableBlock, pf crypto.VrfProof, origPer period, origProp basics.Address) proposal {
e := blk.Block()
func makeProposalFromProposableBlock(blk Block, pf crypto.VrfProof, origPer period, origProp basics.Address) proposal {
e := bookkeeping.Block(blk)
var payload unauthenticatedProposal
payload.Block = e
payload.SeedProof = pf
Expand Down
5 changes: 3 additions & 2 deletions node/impls.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/ledger"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/network"
"github.com/algorand/go-algorand/util/execpool"
Expand Down Expand Up @@ -59,7 +60,7 @@ func (i blockValidatorImpl) Validate(ctx context.Context, e bookkeeping.Block) (
return nil, err
}

return validatedBlock{vb: lvb}, nil
return lvb, nil
}

// agreementLedger implements the agreement.Ledger interface.
Expand All @@ -86,7 +87,7 @@ func (l agreementLedger) EnsureBlock(e bookkeeping.Block, c agreement.Certificat

// EnsureValidatedBlock implements agreement.LedgerWriter.EnsureValidatedBlock.
func (l agreementLedger) EnsureValidatedBlock(ve agreement.ValidatedBlock, c agreement.Certificate) {
l.Ledger.EnsureValidatedBlock(ve.(validatedBlock).vb, c)
l.Ledger.EnsureValidatedBlock(ve.(*ledgercore.ValidatedBlock), c)
// let the network know that we've made some progress.
l.n.OnNetworkAdvance()
}
Expand Down
20 changes: 2 additions & 18 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,33 +1289,17 @@ func (node *AlgorandFullNode) SetCatchpointCatchupMode(catchpointCatchupMode boo

}

// validatedBlock satisfies agreement.ValidatedBlock
type validatedBlock struct {
vb *ledgercore.ValidatedBlock
}

// unfinishedBlock satisfies agreement.UnfinishedBlock
type unfinishedBlock struct {
blk *ledgercore.UnfinishedBlock
}

// proposableBlock satisfies agreement.ProposableBlock
type proposableBlock struct {
blk bookkeeping.Block
}

// Block satisfies the agreement.ValidatedBlock interface.
func (vb validatedBlock) Block() bookkeeping.Block { return vb.vb.Block() }

// Round satisfies the agreement.UnfinishedBlock interface.
func (ub unfinishedBlock) Round() basics.Round { return ub.blk.Round() }

// Block satisfies the agreement.ProposableBlock interface.
func (ab proposableBlock) Block() bookkeeping.Block { return ab.blk }

// FinishBlock satisfies the agreement.UnfinishedBlock interface.
func (ub unfinishedBlock) FinishBlock(s committee.Seed, proposer basics.Address, eligible bool) agreement.ProposableBlock {
return proposableBlock{blk: ub.blk.FinishBlock(s, proposer, eligible)}
func (ub unfinishedBlock) FinishBlock(s committee.Seed, proposer basics.Address, eligible bool) agreement.Block {
return agreement.Block(ub.blk.FinishBlock(s, proposer, eligible))
}

// AssembleBlock implements Ledger.AssembleBlock.
Expand Down

0 comments on commit a46890d

Please sign in to comment.