Skip to content

Commit

Permalink
fix(lib/babe): add pre-runtime digest before calling initialize_block (
Browse files Browse the repository at this point in the history
  • Loading branch information
noot authored and timwu20 committed Dec 6, 2021
1 parent d2197b0 commit f0060ae
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
5 changes: 5 additions & 0 deletions dot/types/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func NewEmptyDigest() Digest {
return []DigestItem{}
}

// NewDigest returns a new Digest from the given DigestItems
func NewDigest(items ...DigestItem) Digest {
return items
}

// Encode returns the SCALE encoded digest
func (d *Digest) Encode() ([]byte, error) {
enc, err := scale.Encode(big.NewInt(int64(len(*d))))
Expand Down
5 changes: 1 addition & 4 deletions lib/babe/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (b *Service) buildBlock(parent *types.Header, slot Slot) (*types.Block, err

// create new block header
number := big.NewInt(0).Add(parent.Number, big.NewInt(1))
header, err := types.NewHeader(parent.Hash(), common.Hash{}, common.Hash{}, number, types.NewEmptyDigest())
header, err := types.NewHeader(parent.Hash(), common.Hash{}, common.Hash{}, number, types.NewDigest(preDigest))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,9 +87,6 @@ func (b *Service) buildBlock(parent *types.Header, slot Slot) (*types.Block, err
header.ParentHash = parent.Hash()
header.Number.Add(parent.Number, big.NewInt(1))

// add BABE header to digest
header.Digest = append(header.Digest, preDigest)

// create seal and add to digest
seal, err := b.buildBlockSeal(header)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions lib/babe/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,16 @@ func TestBuildBlock_ok(t *testing.T) {
Digest: types.Digest{preDigest},
}

// remove seal from built block, since we can't predict the signature
block.Header.Digest = block.Header.Digest[:1]
require.Equal(t, expectedBlockHeader.ParentHash, block.Header.ParentHash)
require.Equal(t, expectedBlockHeader.Number, block.Header.Number)
require.NotEqual(t, block.Header.StateRoot, emptyHash)
require.NotEqual(t, block.Header.ExtrinsicsRoot, emptyHash)
require.Equal(t, expectedBlockHeader.Digest, block.Header.Digest)
require.Equal(t, 3, len(block.Header.Digest))
require.Equal(t, preDigest, block.Header.Digest[0])
require.Equal(t, types.PreRuntimeDigestType, block.Header.Digest[0].Type())
require.Equal(t, types.ConsensusDigestType, block.Header.Digest[1].Type())
require.Equal(t, types.SealDigestType, block.Header.Digest[2].Type())
require.Equal(t, types.NextEpochDataType, block.Header.Digest[1].(*types.ConsensusDigest).DataType())

// confirm block body is correct
extsRes, err := block.Body.AsExtrinsics()
Expand Down
18 changes: 7 additions & 11 deletions lib/runtime/wasmer/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func (in *Instance) FinalizeBlock() (*types.Header, error) {
func (in *Instance) ExecuteBlock(block *types.Block) ([]byte, error) {
// copy block since we're going to modify it
b := block.DeepCopy()
b.Header.Digest = types.NewEmptyDigest()

if in.version == nil {
var err error
Expand All @@ -158,17 +157,14 @@ func (in *Instance) ExecuteBlock(block *types.Block) ([]byte, error) {
}
}

// TODO: hack since substrate node_runtime can't seem to handle BABE pre-runtime digests
// with type prefix (ie Primary, Secondary...)
if bytes.Equal(in.version.SpecName(), []byte("kusama")) || bytes.Equal(in.version.SpecName(), []byte("polkadot")) {
// remove seal digest only
for _, d := range block.Header.Digest {
if d.Type() == types.SealDigestType {
continue
}

b.Header.Digest = append(b.Header.Digest, d)
// remove seal digest only
b.Header.Digest = types.NewEmptyDigest()
for _, d := range block.Header.Digest {
if d.Type() == types.SealDigestType {
continue
}

b.Header.Digest = append(b.Header.Digest, d)
}

bdEnc, err := b.Encode()
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/wasmer/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ func TestInstance_ExecuteBlock_NodeRuntime(t *testing.T) {
require.NoError(t, err)
instance.SetContextStorage(parentState)

block.Header.Digest = types.NewEmptyDigest()
_, err = instance.ExecuteBlock(block)
require.NoError(t, err)
}
Expand Down

0 comments on commit f0060ae

Please sign in to comment.