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

add new blockchain hooks #6319

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a7e9294
add new blockchain hooks
laurci Jul 16, 2024
9ff3ca2
Merge remote-tracking branch 'origin/rc/v1.7.next1' into new-blockcha…
laurci Jul 24, 2024
67a99d6
Merge remote-tracking branch 'origin/rc/v1.7.2' into new-blockchain-h…
laurci Jul 25, 2024
f060fde
use cached epoch start block in vm blockchain hook
laurci Jul 26, 2024
a4b93be
tpn nil epoch start trigger
laurci Jul 26, 2024
c8ef463
use round handler to get round duration in blockchain hook
laurci Jul 26, 2024
9334561
fix nil pointer epoch start block header
laurci Jul 26, 2024
b9eef0c
nil checks for RoundHandler and EpochStartTriggerHandler
laurci Jul 29, 2024
f94bc3c
fix more RoundHandler nils in tests
laurci Jul 29, 2024
8d9114d
deep copy header when returning from trigger
laurci Jul 29, 2024
0684c29
epochStartHdr nil error check
laurci Jul 29, 2024
53f4563
propagate SetCurrentHeader errors
laurci Jul 30, 2024
0534fc4
remove log; epoch start hdr check if nil
laurci Jul 30, 2024
1f07ae2
simplify error handling in LastCommitedEpochStartHdr impl
laurci Jul 31, 2024
1877e0c
get epoch start block from storage for sc query
laurci Jul 31, 2024
6f64220
add comment for SetEpochStartHeader
laurci Aug 1, 2024
a333893
rc/1.7.2 update dependencies
laurci Aug 2, 2024
fc46b8b
managed buffer small ints gas cost
laurci Aug 2, 2024
5d877d8
formatting fixes
laurci Aug 2, 2024
4ac3ab5
update vm version
laurci Aug 2, 2024
a6a1af7
chainsimulator tests fixes
laurci Aug 7, 2024
e770491
more chain simulator tests fixing
laurci Aug 7, 2024
2a402b5
add warn logs for error cases in updateEpochStartHeaderFromCurrentHeader
laurci Aug 14, 2024
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: 3 additions & 3 deletions epochStart/bootstrap/disabled/disabledEpochStartTrigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func (e *epochStartTrigger) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *epochStartTrigger) EpochStartHdr() data.HeaderHandler {
return nil
// LastCommitedEpochStartHdr -
func (e *epochStartTrigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
return nil, nil
}

// GetSavedStateKey -
Expand Down
2 changes: 1 addition & 1 deletion epochStart/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type TriggerHandler interface {
Update(round uint64, nonce uint64)
EpochStartRound() uint64
EpochStartMetaHdrHash() []byte
EpochStartHdr() data.HeaderHandler
LastCommitedEpochStartHdr() (data.HeaderHandler, error)
GetSavedStateKey() []byte
LoadState(key []byte) error
SetProcessed(header data.HeaderHandler, body data.BodyHandler)
Expand Down
22 changes: 18 additions & 4 deletions epochStart/metachain/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-logger-go"
logger "github.com/multiversx/mx-chain-logger-go"
)

var log = logger.GetOrCreate("epochStart/metachain")
Expand Down Expand Up @@ -403,9 +403,23 @@ func (t *trigger) EpochStartMetaHdrHash() []byte {
return t.epochStartMetaHash
}

// EpochStartHdr returns the header of the epoch start block
func (t *trigger) EpochStartHdr() data.HeaderHandler {
return t.epochStartMeta
// LastCommitedEpochStartHdr returns the header of the epoch start block
func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()

// marshal + unmarshal deep copy
headerBytes, err := t.marshaller.Marshal(t.epochStartMeta)
if err != nil {
return nil, err
}

header, err := process.UnmarshalMetaHeader(t.marshaller, headerBytes)
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

return header, nil
}

// GetSavedStateKey returns the last saved trigger state key
Expand Down
19 changes: 15 additions & 4 deletions epochStart/shardchain/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-logger-go"
logger "github.com/multiversx/mx-chain-logger-go"
)

var log = logger.GetOrCreate("epochStart/shardchain")
Expand Down Expand Up @@ -1102,12 +1102,23 @@ func (t *trigger) EpochStartMetaHdrHash() []byte {
return t.epochMetaBlockHash
}

// EpochStartHdr returns the epoch start header
func (t *trigger) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr returns the epoch start header
func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()

return t.epochStartShardHeader
// marshal + unmarshal deep copy
headerBytes, err := t.marshaller.Marshal(t.epochStartShardHeader)
if err != nil {
return nil, err
}

header, err := process.UnmarshalShardHeader(t.marshaller, headerBytes)
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

return header, nil
}

// GetSavedStateKey returns the last saved trigger state key
Expand Down
8 changes: 4 additions & 4 deletions factory/mock/epochStartTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}

return nil
return nil, nil
}

// GetSavedStateKey -
Expand Down
2 changes: 1 addition & 1 deletion factory/processing/blockProcessorCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ func (pcf *processComponentsFactory) createVMFactoryMeta(
Counter: counters.NewDisabledCounter(),
MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(),
EpochStartTrigger: epochStartTriggerHandler,
RoundHandler: pcf.coreData.RoundHandler(),
RoundHandler: pcf.coreData.RoundHandler(), // TODO: @laurci - this needs to be replaced when changing the round duration
}

blockChainHookImpl, err := hooks.NewBlockChainHookImpl(argsHook)
Expand Down
8 changes: 4 additions & 4 deletions integrationTests/mock/endOfEpochTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}
return nil
return nil, nil
}

// GetSavedStateKey -
Expand Down
8 changes: 4 additions & 4 deletions node/mock/endOfEpochTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}

return nil
return nil, nil
}

// Revert -
Expand Down
2 changes: 1 addition & 1 deletion process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ type EpochStartTriggerHandler interface {
Epoch() uint32
MetaEpoch() uint32
EpochStartRound() uint64
EpochStartHdr() data.HeaderHandler
LastCommitedEpochStartHdr() (data.HeaderHandler, error)
SetProcessed(header data.HeaderHandler, body data.BodyHandler)
RevertStateToBlock(header data.HeaderHandler) error
EpochStartMetaHdrHash() []byte
Expand Down
8 changes: 4 additions & 4 deletions process/mock/endOfEpochTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func (e *EpochStartTriggerStub) EpochStartRound() uint64 {
return 0
}

// ReceivedHeader -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}
return nil
return nil, nil
}

// Update -
Expand Down
30 changes: 23 additions & 7 deletions process/smartContract/hooks/blockChainHook.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type ArgBlockChainHook struct {
Counter BlockChainHookCounter
MissingTrieNodesNotifier common.MissingTrieNodesNotifier
EpochStartTrigger EpochStartTriggerHandler
RoundHandler RoundHandler
RoundHandler RoundHandler // TODO: @laurci - this needs to be replaced when changing the round duration
}

// BlockChainHookImpl is a wrapper over AccountsAdapter that satisfy vmcommon.BlockchainHook interface
Expand All @@ -84,7 +84,7 @@ type BlockChainHookImpl struct {
enableEpochsHandler common.EnableEpochsHandler
counter BlockChainHookCounter
epochStartTrigger EpochStartTriggerHandler
roundHandler RoundHandler
roundHandler RoundHandler // TODO: @laurci - this needs to be replaced when changing the round duration

mutCurrentHdr sync.RWMutex
currentHdr data.HeaderHandler
Expand Down Expand Up @@ -411,6 +411,7 @@ func (bh *BlockChainHookImpl) LastEpoch() uint32 {

// RoundTime returns the duration of a round
func (bh *BlockChainHookImpl) RoundTime() uint64 {
// TODO: @laurci - this needs to be replaced when changing the round duration
roundDuration := bh.roundHandler.TimeDuration()
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved

return uint64(roundDuration.Milliseconds())
Expand Down Expand Up @@ -801,24 +802,39 @@ func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) {

bh.mutCurrentHdr.Lock()
bh.currentHdr = hdr
bh.updateEpochStartHeader(hdr)
err := bh.updateEpochStartHeader(hdr)
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Debug("BlockChainHookImpl.SetCurrentHeader: updateEpochStartHeader", "error", err)
AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
}

bh.mutCurrentHdr.Unlock()
}

func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) {
func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) error {
bh.mutEpochStartHdr.Lock()
defer bh.mutEpochStartHdr.Unlock()

if hdr.IsStartOfEpochBlock() {
bh.epochStartHdr = hdr
return
return nil
}

if bh.epochStartHdr.GetEpoch() == hdr.GetEpoch() {
return
return nil
}

bh.epochStartHdr = bh.epochStartTrigger.EpochStartHdr()
epochStartHdr, err := bh.epochStartTrigger.LastCommitedEpochStartHdr()
if err != nil {
return err
}

if epochStartHdr.GetEpoch() != hdr.GetEpoch() {
return ErrLastCommitedEpochStartHdrMismatch
}

bh.epochStartHdr = epochStartHdr

return nil
}

// SaveCompiledCode saves the compiled code to cache and storage
Expand Down
3 changes: 3 additions & 0 deletions process/smartContract/hooks/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ var ErrNilEpochStartTriggerHandler = errors.New("nil epoch start trigger handler

// ErrNilRoundHandler signals that a nil round handler was provided
var ErrNilRoundHandler = errors.New("nil round handler")

// ErrLastCommitedEpochStartHdrMismatch signals that the current header epoch and last commited epoch start header epoch do not match
var ErrLastCommitedEpochStartHdrMismatch = errors.New("current header epoch and last commited epoch start header epoch do not match")
2 changes: 1 addition & 1 deletion process/smartContract/hooks/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type BlockChainHookCounter interface {

// EpochStartTriggerHandler defines the operations of an epoch start trigger handler needed by the blockchain hook
type EpochStartTriggerHandler interface {
EpochStartHdr() data.HeaderHandler
LastCommitedEpochStartHdr() (data.HeaderHandler, error)
IsInterfaceNil() bool
}

Expand Down
8 changes: 4 additions & 4 deletions testscommon/epochStartTriggerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte {
return nil
}

// EpochStartHdr -
func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler {
// LastCommitedEpochStartHdr -
func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
if e.EpochStartHdrCalled != nil {
return e.EpochStartHdrCalled()
return e.EpochStartHdrCalled(), nil
}
return nil
return nil, nil
}

// GetSavedStateKey -
Expand Down
Loading