Skip to content

Commit bd4cecb

Browse files
roman-khimovAnnaShaleva
authored andcommitted
block: drop staterootinheader, make it a block version 1
Signed-off-by: Roman Khimov <[email protected]>
1 parent 945e210 commit bd4cecb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+361
-387
lines changed

cli/server/cli_server_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ func TestServerStart(t *testing.T) {
6161
})
6262
e.RunWithError(t, baseCmd...)
6363
})
64-
t.Run("stateroot service is on && StateRootInHeader=true", func(t *testing.T) {
65-
saveCfg(t, func(cfg *config.Config) {
66-
cfg.ApplicationConfiguration.StateRoot.Enabled = true
67-
cfg.ProtocolConfiguration.StateRootInHeader = true
68-
})
69-
e.RunWithError(t, baseCmd...)
70-
})
7164
t.Run("invalid Oracle config", func(t *testing.T) {
7265
saveCfg(t, func(cfg *config.Config) {
7366
cfg.ApplicationConfiguration.Oracle.Enabled = true

cli/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func restoreDB(ctx *cli.Context) error {
276276
}
277277

278278
var buf []byte
279-
if versionOrLen == block.VersionInitial {
279+
if versionOrLen == block.VersionInitial || versionOrLen == block.VersionFaun {
280280
// Block length > 0 => we read block version, so we have ordinary chain dump.
281281
buf = binary.LittleEndian.AppendUint32(buf, allBlocks)
282282
allBlocks = start

cli/vm/cli_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ func newTestVMClIWithState(t *testing.T) *executor {
129129
store, err := storage.NewLevelDBStore(opts)
130130
require.NoError(t, err)
131131
customConfig := func(c *config.Blockchain) {
132-
c.StateRootInHeader = true // Need for P2PStateExchangeExtensions check.
132+
c.Hardforks = map[string]uint32{
133+
config.HFFaun.String(): 0, // Need for P2PStateExchangeExtensions check.
134+
}
133135
}
134136
bc, validators, committee, err := chain.NewMultiWithCustomConfigAndStoreNoCheck(t, customConfig, store)
135137
require.NoError(t, err)
@@ -148,7 +150,6 @@ func newTestVMClIWithState(t *testing.T) *executor {
148150
require.NoError(t, err)
149151
cfg.ApplicationConfiguration.DBConfiguration.Type = dbconfig.LevelDB
150152
cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions = opts
151-
cfg.ProtocolConfiguration.StateRootInHeader = protoCfg.StateRootInHeader
152153
cfg.ProtocolConfiguration.P2PStateExchangeExtensions = protoCfg.P2PStateExchangeExtensions
153154
cfg.ProtocolConfiguration.Hardforks = protoCfg.Hardforks
154155
return newTestVMCLIWithLogoAndCustomConfig(t, false, &cfg)

config/protocol.privnet.docker.single.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ ProtocolConfiguration:
1313
- node_single:20333
1414
VerifyTransactions: true
1515
P2PSigExtensions: false
16+
Hardforks:
17+
Aspidochelone: 5
18+
Basilisk: 10
19+
Cockatrice: 15
20+
Domovoi: 20
21+
Echidna: 50
1622

1723
ApplicationConfiguration:
1824
SkipBlockVerification: false

config/protocol.testnet.neofs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ ProtocolConfiguration:
1616
- 0272350def90715494b857315c9b9c70181739eeec52d777424fef2891c3396cad
1717
- 03a8cee2d3877bcce5b4595578714d77ca2d47673150b8b9cd4e391b7c73b6bda3
1818
- 0215e735a657f6e23478728d1d0718d516bf50c06c2abd92ec7c00eba2bd7a2552
19-
StateRootInHeader: false
2019
ValidatorsCount: 7
2120
SeedList:
2221
- morph1.t5.fs.neo.org:50333

config/protocol.unit_testnet.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ProtocolConfiguration:
2525
Cockatrice: 3
2626
Domovoi: 4
2727
Echidna: 5
28+
Faun: 6
2829

2930
ApplicationConfiguration:
3031
SkipBlockVerification: false

internal/fakechain/fakechain.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ type FakeStateSync struct {
5656
AddMPTNodesFunc func(nodes [][]byte) error
5757
}
5858

59+
// IsHardforkEnabled implements Blockchainer interface.
60+
func (s *FakeStateSync) IsHardforkEnabled(hf *config.Hardfork, blockHeight uint32) bool {
61+
return false
62+
}
63+
5964
// HeaderHeight returns the height of the latest stored header.
6065
func (s *FakeStateSync) HeaderHeight() uint32 {
6166
return 0

pkg/compiler/interop_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func TestAppCall(t *testing.T) {
373373
}
374374

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

379379
t.Run("valid script", func(t *testing.T) {

pkg/config/protocol_config.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ type (
5656

5757
SeedList []string `yaml:"SeedList"`
5858
StandbyCommittee []string `yaml:"StandbyCommittee"`
59-
// StateRootInHeader enables storing state root in block header.
60-
StateRootInHeader bool `yaml:"StateRootInHeader"`
6159
// StateSyncInterval is the number of blocks between state heights available for MPT state data synchronization.
6260
// It is valid only if P2PStateExchangeExtensions are enabled.
6361
StateSyncInterval int `yaml:"StateSyncInterval"`
@@ -243,7 +241,6 @@ func (p *ProtocolConfiguration) Equals(o *ProtocolConfiguration) bool {
243241
p.P2PSigExtensions != o.P2PSigExtensions ||
244242
p.P2PStateExchangeExtensions != o.P2PStateExchangeExtensions ||
245243
p.ReservedAttributes != o.ReservedAttributes ||
246-
p.StateRootInHeader != o.StateRootInHeader ||
247244
p.StateSyncInterval != o.StateSyncInterval ||
248245
p.TimePerBlock != o.TimePerBlock ||
249246
p.Genesis.MaxValidUntilBlockIncrement != o.Genesis.MaxValidUntilBlockIncrement ||

pkg/consensus/consensus.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Ledger interface {
4444
GetStateRoot(height uint32) (*state.MPTRoot, error)
4545
GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error)
4646
ComputeNextBlockValidators() []*keys.PublicKey
47+
IsHardforkEnabled(hf *config.Hardfork, blockHeight uint32) bool
4748
GetMillisecondsPerBlock() uint32
4849
PoolTx(t *transaction.Transaction, pools ...*mempool.Pool) error
4950
SubscribeForBlocks(ch chan *coreb.Block)
@@ -216,27 +217,24 @@ var (
216217
)
217218

218219
// NewPayload creates a new consensus payload for the provided network.
219-
func NewPayload(m netmode.Magic, stateRootEnabled bool) *Payload {
220+
func NewPayload(m netmode.Magic) *Payload {
220221
return &Payload{
221222
Extensible: npayload.Extensible{
222223
Category: npayload.ConsensusCategory,
223224
},
224-
message: message{
225-
stateRootEnabled: stateRootEnabled,
226-
},
227225
network: m,
228226
}
229227
}
230228

231229
func (s *service) newPayload(c *dbft.Context[util.Uint256], t dbft.MessageType, msg any) dbft.ConsensusPayload[util.Uint256] {
232-
cp := NewPayload(s.ProtocolConfiguration.Magic, s.ProtocolConfiguration.StateRootInHeader)
230+
cp := NewPayload(s.ProtocolConfiguration.Magic)
233231
cp.BlockIndex = c.BlockIndex
234232
cp.message.ValidatorIndex = byte(c.MyIndex)
235233
cp.message.ViewNumber = c.ViewNumber
236234
cp.message.Type = messageType(t)
237235
if pr, ok := msg.(*prepareRequest); ok {
238236
pr.prevHash = s.dbft.PrevHash
239-
pr.version = coreb.VersionInitial
237+
pr.version = coreb.VersionInitial // TODO: ?
240238
}
241239
cp.payload = msg.(io.Serializable)
242240

@@ -249,12 +247,14 @@ func (s *service) newPayload(c *dbft.Context[util.Uint256], t dbft.MessageType,
249247

250248
func (s *service) newPrepareRequest(ts uint64, nonce uint64, transactionsHashes []util.Uint256) dbft.PrepareRequest[util.Uint256] {
251249
r := &prepareRequest{
250+
version: coreb.VersionInitial,
252251
timestamp: ts / nsInMs,
253252
nonce: nonce,
254253
transactionHashes: transactionsHashes,
255254
}
256-
if s.ProtocolConfiguration.StateRootInHeader {
257-
r.stateRootEnabled = true
255+
var f = config.HFFaun
256+
if s.Chain.IsHardforkEnabled(&f, s.dbft.BlockIndex) {
257+
r.version = coreb.VersionFaun
258258
if sr, err := s.Chain.GetStateRoot(s.dbft.BlockIndex - 1); err == nil {
259259
r.stateRoot = sr.Root
260260
} else {
@@ -291,9 +291,7 @@ func (s *service) newRecoveryRequest(ts uint64) dbft.RecoveryRequest {
291291
}
292292

293293
func (s *service) newRecoveryMessage() dbft.RecoveryMessage[util.Uint256] {
294-
return &recoveryMessage{
295-
stateRootEnabled: s.ProtocolConfiguration.StateRootInHeader,
296-
}
294+
return new(recoveryMessage)
297295
}
298296

299297
// Name returns service name.
@@ -464,9 +462,6 @@ func (s *service) getKeyPair(pubs []dbft.PublicKey) (int, dbft.PrivateKey, dbft.
464462
func (s *service) payloadFromExtensible(ep *npayload.Extensible) *Payload {
465463
return &Payload{
466464
Extensible: *ep,
467-
message: message{
468-
stateRootEnabled: s.ProtocolConfiguration.StateRootInHeader,
469-
},
470465
}
471466
}
472467

@@ -597,16 +592,28 @@ func (s *service) verifyRequest(p dbft.ConsensusPayload[util.Uint256]) error {
597592
if req.prevHash != s.dbft.PrevHash {
598593
return errInvalidPrevHash
599594
}
600-
if req.version != coreb.VersionInitial {
601-
return errInvalidVersion
595+
var (
596+
f = config.HFFaun
597+
expectedVersion = coreb.VersionInitial
598+
)
599+
if s.Chain.IsHardforkEnabled(&f, s.dbft.BlockIndex) {
600+
expectedVersion = coreb.VersionFaun
601+
}
602+
if req.version != expectedVersion {
603+
return fmt.Errorf("%w: expected %d, got %d", errInvalidVersion, expectedVersion, req.version)
602604
}
603-
if s.ProtocolConfiguration.StateRootInHeader {
605+
if req.version == coreb.VersionFaun {
604606
sr, err := s.Chain.GetStateRoot(s.dbft.BlockIndex - 1)
605607
if err != nil {
606608
return err
607609
} else if sr.Root != req.stateRoot {
608610
return fmt.Errorf("%w: %s != %s", errInvalidStateRoot, sr.Root, req.stateRoot)
609611
}
612+
} else {
613+
// TODO: port this check to C# node:
614+
if !req.stateRoot.Equals(util.Uint256{}) {
615+
return fmt.Errorf("stateroot is not empty, but Faun is not enabled yet: %s", req.stateRoot)
616+
}
610617
}
611618
if len(req.TransactionHashes()) > int(s.ProtocolConfiguration.MaxTransactionsPerBlock) {
612619
return fmt.Errorf("%w: max = %d, got %d", errInvalidTransactionsCount, s.ProtocolConfiguration.MaxTransactionsPerBlock, len(req.TransactionHashes()))
@@ -756,17 +763,23 @@ func convertKeys(validators []dbft.PublicKey) (pubs []*keys.PublicKey) {
756763
}
757764

758765
func (s *service) newBlockFromContext(ctx *dbft.Context[util.Uint256]) dbft.Block[util.Uint256] {
759-
block := &neoBlock{network: s.ProtocolConfiguration.Magic}
766+
var (
767+
blockVersion = coreb.VersionInitial
768+
block = &neoBlock{network: s.ProtocolConfiguration.Magic}
769+
)
760770

771+
hff, ok := s.ProtocolConfiguration.Hardforks[config.HFFaun.String()]
772+
if ok && hff <= ctx.BlockIndex {
773+
blockVersion = coreb.VersionFaun
774+
}
761775
block.Block.Timestamp = ctx.Timestamp / nsInMs
762776
block.Nonce = ctx.Nonce
763777
block.Block.Index = ctx.BlockIndex
764-
if s.ProtocolConfiguration.StateRootInHeader {
778+
if blockVersion > coreb.VersionInitial {
765779
sr, err := s.Chain.GetStateRoot(ctx.BlockIndex - 1)
766780
if err != nil {
767781
s.log.Fatal(fmt.Sprintf("failed to get state root: %s", err.Error()))
768782
}
769-
block.StateRootEnabled = true
770783
block.PrevStateRoot = sr.Root
771784
}
772785

@@ -787,7 +800,7 @@ func (s *service) newBlockFromContext(ctx *dbft.Context[util.Uint256]) dbft.Bloc
787800
}
788801
block.NextConsensus = hash.Hash160(script)
789802
block.Block.PrevHash = ctx.PrevHash
790-
block.Version = coreb.VersionInitial
803+
block.Version = blockVersion
791804

792805
primaryIndex := byte(ctx.PrimaryIndex)
793806
block.PrimaryIndex = primaryIndex

0 commit comments

Comments
 (0)