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
1 change: 1 addition & 0 deletions process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ type BlockChainHookHandler interface {
IsLimitedTransfer(tokenID []byte) bool
NumberOfShards() uint32
SetCurrentHeader(hdr data.HeaderHandler) error
SetEpochStartHeader(hdr data.HeaderHandler)
SaveCompiledCode(codeHash []byte, code []byte)
GetCompiledCode(codeHash []byte) (bool, []byte)
IsPayable(sndAddress []byte, recvAddress []byte) (bool, error)
Expand Down
11 changes: 9 additions & 2 deletions process/smartContract/hooks/blockChainHook.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,10 @@ func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) error {
defer bh.mutCurrentHdr.Unlock()

bh.currentHdr = hdr
return bh.updateEpochStartHeader(hdr)
return bh.updateEpochStartHeaderFromCurrentHeader(hdr)
}

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

Expand Down Expand Up @@ -838,6 +838,13 @@ func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) err
return nil
}

func (bh *BlockChainHookImpl) SetEpochStartHeader(header data.HeaderHandler) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added comment

bh.mutEpochStartHdr.Lock()
defer bh.mutEpochStartHdr.Unlock()

bh.epochStartHdr = header
}

// SaveCompiledCode saves the compiled code to cache and storage
func (bh *BlockChainHookImpl) SaveCompiledCode(codeHash []byte, code []byte) {
bh.compiledScPool.Put(codeHash, code, len(code))
Expand Down
49 changes: 49 additions & 0 deletions process/smartContract/scQueryService.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var logQueryService = logger.GetOrCreate("process/smartcontract.queryService")
// MaxGasLimitPerQuery - each unit is the equivalent of 1 nanosecond processing time
const MaxGasLimitPerQuery = 300_000_000_000

// EpochStartHdrCacheSize - the size of the cache for the EpochStartHdr
const EpochStartHdrCacheSize = 5

// SCQueryService can execute Get functions over SC to fetch stored values
type SCQueryService struct {
vmContainer process.VirtualMachinesContainer
Expand All @@ -53,6 +56,7 @@ type SCQueryService struct {
hasher hashing.Hasher
uint64ByteSliceConverter typeConverters.Uint64ByteSliceConverter
isInHistoricalBalancesMode bool
epochStartHdrCache map[uint32]data.HeaderHandler
}

// ArgsNewSCQueryService defines the arguments needed for the sc query service
Expand Down Expand Up @@ -105,6 +109,7 @@ func NewSCQueryService(
hasher: args.Hasher,
uint64ByteSliceConverter: args.Uint64ByteSliceConverter,
isInHistoricalBalancesMode: args.IsInHistoricalBalancesMode,
epochStartHdrCache: make(map[uint32]data.HeaderHandler, EpochStartHdrCacheSize),
}, nil
}

Expand Down Expand Up @@ -207,6 +212,12 @@ func (service *SCQueryService) executeScCall(query *process.SCQuery, gasPrice ui
return nil, nil, err
}

AdoAdoAdo marked this conversation as resolved.
Show resolved Hide resolved
epochStartHdr, err := service.getEpochStartBlockHdr(blockHeader.GetEpoch())
if err != nil {
return nil, nil, err
}
service.blockChainHook.SetEpochStartHeader(epochStartHdr)

err = service.blockChainHook.SetCurrentHeader(blockHeader)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -322,6 +333,44 @@ func (service *SCQueryService) getBlockHeaderByHash(headerHash []byte) (data.Hea
return header, nil
}

func (service *SCQueryService) getEpochStartBlockHdr(epoch uint32) (data.HeaderHandler, error) {
if header, ok := service.epochStartHdrCache[epoch]; ok {
return header, nil
}

storer, err := service.storageService.GetStorer(dataRetriever.BlockHeaderUnit)
if err != nil {
return nil, err
}

identifier := core.EpochStartIdentifier(epoch)
headerBytes, err := storer.GetFromEpoch([]byte(identifier), epoch)
if err != nil {
return nil, err
}

shardId := service.shardCoordinator.SelfId()

header, err := process.UnmarshalHeader(shardId, service.marshaller, headerBytes)
if err != nil {
return nil, err
}

if len(service.epochStartHdrCache) >= EpochStartHdrCacheSize {
oldestEpoch := uint32(1<<32 - 1)
for e := range service.epochStartHdrCache {
if e < oldestEpoch {
oldestEpoch = e
}
}

delete(service.epochStartHdrCache, oldestEpoch)
}

service.epochStartHdrCache[epoch] = header
return header, nil
}

func (service *SCQueryService) getOptionalEpochByHash(hash []byte) (core.OptionalUint32, error) {
if !service.historyRepository.IsEnabled() {
return core.OptionalUint32{}, nil
Expand Down
11 changes: 11 additions & 0 deletions process/smartContract/scQueryService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/data/transaction"
Expand Down Expand Up @@ -412,6 +413,15 @@ func TestExecuteQuery_ShouldReceiveQueryCorrectly(t *testing.T) {
GetStorerCalled: func(unitType dataRetriever.UnitType) (storage.Storer, error) {
return &storageStubs.StorerStub{
GetFromEpochCalled: func(key []byte, epoch uint32) ([]byte, error) {
if string(key) == core.EpochStartIdentifier(epoch) {
hdr := &block.Header{
RootHash: []byte("epoch start"),
EpochStartMetaHash: []byte("meta"),
}
buff, _ := argsNewSCQuery.Marshaller.Marshal(hdr)
return buff, nil
}

counter++
if counter > 2 {
return nil, fmt.Errorf("no scheduled")
Expand Down Expand Up @@ -469,6 +479,7 @@ func TestExecuteQuery_ShouldReceiveQueryCorrectly(t *testing.T) {
}

_, _, err := target.ExecuteQuery(&query)
assert.Nil(t, err)
assert.True(t, runWasCalled)
assert.True(t, recreateTrieFromEpochWasCalled)
assert.False(t, recreateTrieWasCalled)
Expand Down
8 changes: 8 additions & 0 deletions testscommon/blockChainHookStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type BlockChainHookStub struct {
GetSnapshotCalled func() int
RevertToSnapshotCalled func(snapshot int) error
SetCurrentHeaderCalled func(hdr data.HeaderHandler) error
SetEpochStartHeaderCalled func(hdr data.HeaderHandler)
DeleteCompiledCodeCalled func(codeHash []byte)
SaveNFTMetaDataToSystemAccountCalled func(tx data.TransactionHandler) error
CloseCalled func() error
Expand Down Expand Up @@ -330,6 +331,13 @@ func (stub *BlockChainHookStub) SetCurrentHeader(hdr data.HeaderHandler) error {
return nil
}

// SetEpochStartHeader -
func (stub *BlockChainHookStub) SetEpochStartHeader(hdr data.HeaderHandler) {
if stub.SetEpochStartHeaderCalled != nil {
stub.SetEpochStartHeaderCalled(hdr)
}
}

// SaveCompiledCode -
func (stub *BlockChainHookStub) SaveCompiledCode(codeHash []byte, code []byte) {
if stub.SaveCompiledCodeCalled != nil {
Expand Down
Loading