Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make staterootinheader a proper block version 1 #3566

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/protocol.privnet.docker.single.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ ProtocolConfiguration:
- node_single:20333
VerifyTransactions: true
P2PSigExtensions: false
Hardforks:
Aspidochelone: 5
Basilisk: 10
Cockatrice: 15
Domovoi: 20
Echidna: 50

ApplicationConfiguration:
SkipBlockVerification: false
Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func TestAppCall(t *testing.T) {
}

fc := fakechain.NewFakeChain()
ic := interop.NewContext(trigger.Application, fc, dao.NewSimple(storage.NewMemoryStore(), false),
ic := interop.NewContext(trigger.Application, fc, dao.NewSimple(storage.NewMemoryStore()),
interop.DefaultBaseExecFee, native.DefaultStoragePrice, contractGetter, nil, nil, nil, nil, zaptest.NewLogger(t))

t.Run("valid script", func(t *testing.T) {
Expand Down
14 changes: 10 additions & 4 deletions pkg/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,17 +753,23 @@ func convertKeys(validators []dbft.PublicKey) (pubs []*keys.PublicKey) {
}

func (s *service) newBlockFromContext(ctx *dbft.Context[util.Uint256]) dbft.Block[util.Uint256] {
block := &neoBlock{network: s.ProtocolConfiguration.Magic}
var (
blockVersion = uint32(coreb.VersionInitial)
block = &neoBlock{network: s.ProtocolConfiguration.Magic}
)

hfe, ok := s.ProtocolConfiguration.Hardforks[config.HFEchidna.String()]
if ok && hfe <= ctx.BlockIndex {
blockVersion = coreb.VersionEchidna
}
block.Block.Timestamp = ctx.Timestamp / nsInMs
block.Block.Nonce = ctx.Nonce
block.Block.Index = ctx.BlockIndex
if s.ProtocolConfiguration.StateRootInHeader {
if blockVersion > coreb.VersionInitial {
sr, err := s.Chain.GetStateRoot(ctx.BlockIndex - 1)
if err != nil {
s.log.Fatal(fmt.Sprintf("failed to get state root: %s", err.Error()))
}
block.StateRootEnabled = true
block.PrevStateRoot = sr.Root
}

Expand All @@ -784,7 +790,7 @@ func (s *service) newBlockFromContext(ctx *dbft.Context[util.Uint256]) dbft.Bloc
}
block.Block.NextConsensus = hash.Hash160(script)
block.Block.PrevHash = ctx.PrevHash
block.Block.Version = coreb.VersionInitial
block.Block.Version = blockVersion

primaryIndex := byte(ctx.PrimaryIndex)
block.Block.PrimaryIndex = primaryIndex
Expand Down
15 changes: 6 additions & 9 deletions pkg/core/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ func (b *Block) RebuildMerkleRoot() {
// This is commonly used to create a block from stored data.
// Blocks created from trimmed data will have their Trimmed field
// set to true.
func NewTrimmedFromReader(stateRootEnabled bool, br *io.BinReader) (*Block, error) {
func NewTrimmedFromReader(br *io.BinReader) (*Block, error) {
block := &Block{
Header: Header{
StateRootEnabled: stateRootEnabled,
},
Trimmed: true,
}

Expand All @@ -93,11 +90,11 @@ func NewTrimmedFromReader(stateRootEnabled bool, br *io.BinReader) (*Block, erro
return block, br.Err
}

// New creates a new blank block with proper state root setting.
func New(stateRootEnabled bool) *Block {
// New creates a new blank block of appropriate version.
func New(version uint32) *Block {
return &Block{
Header: Header{
StateRootEnabled: stateRootEnabled,
Version: version,
},
}
}
Expand Down Expand Up @@ -213,7 +210,7 @@ func (b *Block) GetExpectedBlockSizeWithoutTransactions(txCount int) int {
size := expectedHeaderSizeWithEmptyWitness - 1 - 1 + // 1 is for the zero-length (new(Header)).Script.Invocation/Verification
io.GetVarSize(&b.Script) +
io.GetVarSize(txCount)
if b.StateRootEnabled {
if b.Version > 0 {
size += util.Uint256Size
}
return size
Expand All @@ -233,7 +230,7 @@ func (b *Block) ToStackItem() stackitem.Item {
stackitem.NewByteArray(b.NextConsensus.BytesBE()),
stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))),
}
if b.StateRootEnabled {
if b.Version > 0 {
items = append(items, stackitem.NewByteArray(b.PrevStateRoot.BytesBE()))
}

Expand Down
24 changes: 15 additions & 9 deletions pkg/core/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestDecodeBlock1(t *testing.T) {
b, err := hex.DecodeString(data["raw"].(string))
require.NoError(t, err)

block := New(false)
block := &Block{}
assert.NoError(t, testserdes.DecodeBinary(b, block))

assert.Equal(t, uint32(data["index"].(float64)), block.Index)
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestTrimmedBlock(t *testing.T) {
require.NoError(t, buf.Err)

r := io.NewBinReaderFromBuf(buf.Bytes())
trimmedBlock, err := NewTrimmedFromReader(false, r)
trimmedBlock, err := NewTrimmedFromReader(r)
require.NoError(t, err)

assert.True(t, trimmedBlock.Trimmed)
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestBinBlockDecodeEncode(t *testing.T) {
rawblock := "AAAAAAwIVa2D6Yha3tArd5XnwkAf7deJBsdyyvpYb2xMZGBbkOUNHAsfre0rKA/F+Ox05/bQSXmcRZnzK3M6Z+/TxJUh0MNFeAEAAAAAAAAAAAAAAQAAAADe7nnBifMAmLC6ai65CzqSWKbH/wHGDEDgwCcXkcaFw5MGOp1cpkgApzDTX2/RxKlmPeXTgWYtfEA8g9svUSbZA4TeoGyWvX8LiN0tJKrzajdMGvTVGqVmDEDp6PBmZmRx9CxswtLht6oWa2Uq4rl5diPsLtqXZeZepMlxUSbaCdlFTB7iWQG9yKXWR5hc0sScevvuVwwsUYdlDEDwlhwZrP07E5fEQKttVMYAiL7edd/eW2yoMGZe6Q95g7yXQ69edVHfQb61fBw3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ELqpN8ghcM/PtfFwo56/BfEasfHuSKECJMYxvU47r2ZtSihg59lGxSZzHsvxTy6nsyvJ22ycNhINdJDECl61cg937N/HujKsLMu2wJMS7C54bzJ3q22Czqllvw3Yp809USgKDs+W+3QD7rI+SFs0OhIn0gooCUU6f/13WjDEDr9XdeT5CGTO8CL0JigzcTcucs0GBcqHs8fToO6zPuuCfS7Wh6dyxSCijT4A4S+7BUdW3dsO7828ke1fj8oNxmkxMMIQIQOn990BZVhZf3lg0nxRakOU/ZaLnmUVXrSwE+QEBAbgwhAqe8Vf6GhOARl2jRBLoweVvcyGYZ6GSt0mFWcj7Rhc1iDCECs2Ir9AF73+MXxYrtX0x1PyBrfbiWBG+n13S7xL9/jcIMIQPZDAffY+aQzneRLhCrUazJRLZoYCN7YIxPj4MJ5x7mmRRBe85spQ=="
rawblockBytes, _ := base64.StdEncoding.DecodeString(rawblock)

b := New(false)
b := &Block{}

assert.NoError(t, testserdes.DecodeBinary(rawblockBytes, b))
expected := map[string]bool{ // 1 trans
Expand Down Expand Up @@ -144,12 +144,12 @@ func TestBinBlockDecodeEncode(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, rawblock, base64.StdEncoding.EncodeToString(data))

testserdes.MarshalUnmarshalJSON(t, b, New(false))
testserdes.MarshalUnmarshalJSON(t, b, &Block{})
}

func TestJSONEmptyTx(t *testing.T) {
jblock := `{"hash":"0x5f3fbb43d1e516fe07771e2e873ebc9e2810662401acf603a775aace486220bd","version":0,"previousblockhash":"0x1f4d1defa46faa5e7b9b8d3f79a06bec777d7c26c4aa5f6f5899a291daa87c15","merkleroot":"0x0000000000000000000000000000000000000000000000000000000000000000","time":1627894840919,"nonce":"BA8E021F1AEEA3F6","index":1,"nextconsensus":"NVg7LjGcUSrgxgjX3zEgqaksfMaiS8Z6e1","primary":0,"witnesses":[{"invocation":"DEAq7W/jUhpMon1t9muqXKfBvNyGwLfFFM1vAxrMKvUl6MqK+LL/lJAJP9uAk/cberIWWhSsdcxUtltkBLemg/VuDECQZGuvP93JlZga2ml8cnbe5cNiGgO0EMrbGYyzvgr8calP5SwMNPSYms10gIHxlsuXDU++EQpZu/vKxfHoxdC5DEDgsA3POVZdfN+i5+ekvtsaIvif42n0GC+dZi3Rp37ETmt4NtkoK2I2UXi+WIjm5yXLJsPhAvEV6cJSrvqBdsQBDEDTS6NU+kB+tgeBe9lWv+6y0L2qcUBIaUxiTCaNWZtLPghQICBvjDz1/9ttJRXG3I5N9CFDjjLKCpdIY842HW4/DEC+wlWjkCzVqzKslvpCKZbEPUGIf87CFAD88xqzl26m/TpTUcT0+D5oI2bVzAk0mcdBTPnyjcNbv17BFmr63+09","verification":"FQwhAkhv0VcCxEkKJnAxEqXMHQkj/Wl6M0Br1aHADgATsJpwDCECTHt/tsMQ/M8bozsIJRnYKWTqk4aNZ2Zi1KWa1UjfDn0MIQKq7DhHD2qtAELG6HfP2Ah9Jnaw9Rb93TYoAbm9OTY5ngwhA7IJ/U9TpxcOpERODLCmu2pTwr0BaSaYnPhfmw+6F6cMDCEDuNnVdx2PUTqghpucyNUJhkA7eMbaNokGOMPUalrc4EoMIQLKDidpe5wkj28W4IX9AGHib0TahbWO6DXBEMql7DulVAwhAt9I9g6PPgHEj/QLm38TENeosqGTGIvv4cLj33QOiVCTF0Ge0Nw6"}],"tx":[]}`
b := New(false)
b := &Block{}
require.NoError(t, json.Unmarshal([]byte(jblock), b))
s, err := json.Marshal(b)
require.NoError(t, err)
Expand All @@ -166,7 +166,7 @@ func TestBlockSizeCalculation(t *testing.T) {
rawBlock := "AAAAAAwIVa2D6Yha3tArd5XnwkAf7deJBsdyyvpYb2xMZGBbkOUNHAsfre0rKA/F+Ox05/bQSXmcRZnzK3M6Z+/TxJUh0MNFeAEAAAAAAAAAAAAAAQAAAADe7nnBifMAmLC6ai65CzqSWKbH/wHGDEDgwCcXkcaFw5MGOp1cpkgApzDTX2/RxKlmPeXTgWYtfEA8g9svUSbZA4TeoGyWvX8LiN0tJKrzajdMGvTVGqVmD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wumouuQs6klimx/8BAF8LAwBA2d2ITQoADBSAzse29bVvUFePc38WLTqxTUZlDQwU3u55wYnzAJiwumouuQs6klimx/8UwB8MCHRyYW5zZmVyDBTPduKL0AYsSkeO41VhARMZ88+k0kFifVtSOQHGDEDWn0D7z2ELqpN8ghcM/PtfFwo56/BfEasfHuSKECJMYxvU47r2ZtSihg59lGxSZzHsvxTy6nsyvJ22ycNhINdJDECl61cg937N/HujKsLMu2wJMS7C54bzJ3q22Czqllvw3Yp809USgKDs+W+3QD7rI+SFs0OhIn0gooCUU6f/13WjDEDr9XdeT5CGTO8CL0JigzcTcucs0GBcqHs8fToO6zPuuCfS7Wh6dyxSCijT4A4S+7BUdW3dsO7828ke1fj8oNxmkxMMIQIQOn990BZVhZf3lg0nxRakOU/ZaLnmUVXrSwE+QEBAbgwhAqe8Vf6GhOARl2jRBLoweVvcyGYZ6GSt0mFWcj7Rhc1iDCECs2Ir9AF73+MXxYrtX0x1PyBrfbiWBG+n13S7xL9/jcIMIQPZDAffY+aQzneRLhCrUazJRLZoYCN7YIxPj4MJ5x7mmRRBe85spQ=="
rawBlockBytes, _ := base64.StdEncoding.DecodeString(rawBlock)

b := New(false)
b := &Block{}
assert.NoError(t, testserdes.DecodeBinary(rawBlockBytes, b))

expected := []struct {
Expand Down Expand Up @@ -243,21 +243,27 @@ func TestGetExpectedBlockSize(t *testing.T) {
check := func(t *testing.T, stateRootEnabled bool) {
t.Run("without transactions", func(t *testing.T) {
b := newDumbBlock()
b.StateRootEnabled = stateRootEnabled
if stateRootEnabled {
b.Version = VersionEchidna
}
b.Transactions = []*transaction.Transaction{}
require.Equal(t, io.GetVarSize(b), b.GetExpectedBlockSize())
require.Equal(t, io.GetVarSize(b), b.GetExpectedBlockSizeWithoutTransactions(0))
})
t.Run("with one transaction", func(t *testing.T) {
b := newDumbBlock()
b.StateRootEnabled = stateRootEnabled
if stateRootEnabled {
b.Version = VersionEchidna
}
expected := io.GetVarSize(b)
require.Equal(t, expected, b.GetExpectedBlockSize())
require.Equal(t, expected-b.Transactions[0].Size(), b.GetExpectedBlockSizeWithoutTransactions(len(b.Transactions)))
})
t.Run("with multiple transactions", func(t *testing.T) {
b := newDumbBlock()
b.StateRootEnabled = stateRootEnabled
if stateRootEnabled {
b.Version = VersionEchidna
}
b.Transactions = make([]*transaction.Transaction, 123)
for i := range b.Transactions {
tx := transaction.New([]byte{byte(opcode.RET)}, int64(i))
Expand Down
18 changes: 10 additions & 8 deletions pkg/core/block/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
"github.com/nspcc-dev/neo-go/pkg/util"
)

// VersionInitial is the default Neo block version.
const VersionInitial uint32 = 0
const (
// VersionInitial is the initial Neo block version.
VersionInitial = iota
// VersionEchidna is StateRoot-enabled Neo block version

Check failure on line 19 in pkg/core/block/header.go

View workflow job for this annotation

GitHub Actions / Lint

Comment should end in a period (godot)
VersionEchidna
)

// Header holds the base info of a block.
type Header struct {
Expand Down Expand Up @@ -45,8 +49,6 @@
// Script used to validate the block
Script transaction.Witness

// StateRootEnabled specifies if the header contains state root.
StateRootEnabled bool
// PrevStateRoot is the state root of the previous block.
PrevStateRoot util.Uint256
// PrimaryIndex is the index of the primary consensus node for this block.
Expand Down Expand Up @@ -126,7 +128,7 @@
bw.WriteU32LE(b.Index)
bw.WriteB(b.PrimaryIndex)
bw.WriteBytes(b.NextConsensus[:])
if b.StateRootEnabled {
if b.Version > 0 {
bw.WriteBytes(b.PrevStateRoot[:])
}
}
Expand All @@ -142,7 +144,7 @@
b.Index = br.ReadU32LE()
b.PrimaryIndex = br.ReadB()
br.ReadBytes(b.NextConsensus[:])
if b.StateRootEnabled {
if b.Version > 0 {
br.ReadBytes(b.PrevStateRoot[:])
}

Expand All @@ -167,7 +169,7 @@
NextConsensus: address.Uint160ToString(b.NextConsensus),
Witnesses: []transaction.Witness{b.Script},
}
if b.StateRootEnabled {
if b.Version > 0 {
aux.PrevStateRoot = &b.PrevStateRoot
}
return json.Marshal(aux)
Expand Down Expand Up @@ -206,7 +208,7 @@
b.PrimaryIndex = aux.PrimaryIndex
b.NextConsensus = nextC
b.Script = aux.Witnesses[0]
if b.StateRootEnabled {
if b.Version > 0 {
if aux.PrevStateRoot == nil {
return errors.New("'previousstateroot' is empty")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/block/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func testHeaderEncodeDecode(t *testing.T, stateRootEnabled bool) {
},
}
if stateRootEnabled {
header.StateRootEnabled = stateRootEnabled
header.Version = VersionEchidna
header.PrevStateRoot = random.Uint256()
}

_ = header.Hash()
headerDecode := &Header{StateRootEnabled: stateRootEnabled}
headerDecode := &Header{}
testserdes.EncodeDecodeBinary(t, &header, headerDecode)

assert.Equal(t, header.Version, headerDecode.Version, "expected both versions to be equal")
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/block/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func getDecodedBlock(t *testing.T, i int) *Block {
b, err := hex.DecodeString(data["raw"].(string))
require.NoError(t, err)

block := New(false)
block := &Block{}
require.NoError(t, testserdes.DecodeBinary(b, block))

return block
Expand Down
33 changes: 10 additions & 23 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,6 @@ func NewBlockchain(s storage.Store, cfg config.Blockchain, log *zap.Logger) (*Bl
zap.Uint32("MaxValidUntilBlockIncrement", cfg.MaxValidUntilBlockIncrement))
}
if cfg.P2PStateExchangeExtensions {
if !cfg.StateRootInHeader {
return nil, errors.New("P2PStatesExchangeExtensions are enabled, but StateRootInHeader is off")
}
if cfg.KeepOnlyLatestState && !cfg.RemoveUntraceableBlocks {
return nil, errors.New("P2PStateExchangeExtensions can be enabled either on MPT-complete node (KeepOnlyLatestState=false) or on light GC-enabled node (RemoveUntraceableBlocks=true)")
}
Expand Down Expand Up @@ -308,8 +305,8 @@ func NewBlockchain(s storage.Store, cfg config.Blockchain, log *zap.Logger) (*Bl
}
bc := &Blockchain{
config: cfg,
dao: dao.NewSimple(s, cfg.StateRootInHeader),
persistent: dao.NewSimple(s, cfg.StateRootInHeader),
dao: dao.NewSimple(s),
persistent: dao.NewSimple(s),
store: s,
stopCh: make(chan struct{}),
runToExitCh: make(chan struct{}),
Expand Down Expand Up @@ -411,7 +408,6 @@ func (bc *Blockchain) init() error {
bc.log.Info("no storage version found! creating genesis block")
ver = dao.Version{
StoragePrefix: storage.STStorage,
StateRootInHeader: bc.config.StateRootInHeader,
P2PSigExtensions: bc.config.P2PSigExtensions,
P2PStateExchangeExtensions: bc.config.P2PStateExchangeExtensions,
KeepOnlyLatestState: bc.config.Ledger.KeepOnlyLatestState,
Expand All @@ -434,10 +430,6 @@ func (bc *Blockchain) init() error {
if ver.Value != version {
return fmt.Errorf("storage version mismatch (expected=%s, actual=%s)", version, ver.Value)
}
if ver.StateRootInHeader != bc.config.StateRootInHeader {
return fmt.Errorf("StateRootInHeader setting mismatch (config=%t, db=%t)",
bc.config.StateRootInHeader, ver.StateRootInHeader)
}
if ver.P2PSigExtensions != bc.config.P2PSigExtensions {
return fmt.Errorf("P2PSigExtensions setting mismatch (old=%t, new=%t)",
ver.P2PSigExtensions, bc.config.P2PSigExtensions)
Expand Down Expand Up @@ -1485,10 +1477,6 @@ func (bc *Blockchain) AddBlock(block *block.Block) error {
if expectedHeight != block.Index {
return fmt.Errorf("expected %d, got %d: %w", expectedHeight, block.Index, ErrInvalidBlockIndex)
}
if bc.config.StateRootInHeader != block.StateRootEnabled {
return fmt.Errorf("%w: %v != %v",
ErrHdrStateRootSetting, bc.config.StateRootInHeader, block.StateRootEnabled)
}

if block.Index == bc.HeaderHeight()+1 {
err := bc.addHeaders(!bc.config.SkipBlockVerification, &block.Header)
Expand Down Expand Up @@ -1749,7 +1737,8 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error
// because changes applied are the ones from HALTed transactions.
return fmt.Errorf("error while trying to apply MPT changes: %w", err)
}
if bc.config.StateRootInHeader && bc.HeaderHeight() > sr.Index {
var hfe = config.HFEchidna
if bc.isHardforkEnabled(&hfe, sr.Index) && bc.HeaderHeight() > sr.Index {
h, err := bc.GetHeader(bc.GetHeaderHash(sr.Index + 1))
if err != nil {
err = fmt.Errorf("failed to get next header: %w", err)
Expand Down Expand Up @@ -2509,12 +2498,10 @@ var (
)

func (bc *Blockchain) verifyHeader(currHeader, prevHeader *block.Header) error {
if bc.config.StateRootInHeader {
if bc.stateRoot.CurrentLocalHeight() == prevHeader.Index {
if sr := bc.stateRoot.CurrentLocalStateRoot(); currHeader.PrevStateRoot != sr {
return fmt.Errorf("%w: %s != %s",
ErrHdrInvalidStateRoot, currHeader.PrevStateRoot.StringLE(), sr.StringLE())
}
if currHeader.Version > block.VersionInitial {
if sr := bc.stateRoot.CurrentLocalStateRoot(); currHeader.PrevStateRoot != sr {
return fmt.Errorf("%w: %s != %s",
ErrHdrInvalidStateRoot, currHeader.PrevStateRoot.StringLE(), sr.StringLE())
}
}
if prevHeader.Hash() != currHeader.PrevHash {
Expand Down Expand Up @@ -2861,7 +2848,7 @@ func (bc *Blockchain) GetTestHistoricVM(t trigger.Type, tx *transaction.Transact
return nil, fmt.Errorf("failed to retrieve stateroot for height %d: %w", b.Index, err)
}
s := mpt.NewTrieStore(sr.Root, mode, storage.NewPrivateMemCachedStore(bc.dao.Store))
dTrie := dao.NewSimple(s, bc.config.StateRootInHeader)
dTrie := dao.NewSimple(s)
dTrie.Version = bc.dao.Version
// Initialize native cache before passing DAO to interop context constructor, because
// the constructor will call BaseExecFee/StoragePrice policy methods on the passed DAO.
Expand All @@ -2876,7 +2863,7 @@ func (bc *Blockchain) GetTestHistoricVM(t trigger.Type, tx *transaction.Transact

// getFakeNextBlock returns fake block with the specified index and pre-filled Timestamp field.
func (bc *Blockchain) getFakeNextBlock(nextBlockHeight uint32) (*block.Block, error) {
b := block.New(bc.config.StateRootInHeader)
b := &block.Block{}
b.Index = nextBlockHeight
hdr, err := bc.GetHeader(bc.GetHeaderHash(nextBlockHeight - 1))
if err != nil {
Expand Down
Loading
Loading