From 4b8a7fa976744ba0117f6a7a03a6f79c6004c502 Mon Sep 17 00:00:00 2001 From: Hayarobi Park Date: Thu, 31 Oct 2024 18:08:00 +0900 Subject: [PATCH] wip --- chain/chaindb.go | 19 +++++++++++++++++-- chain/chainservice.go | 4 ++++ chain/stubchain.go | 5 +++++ p2p/p2pmock/mock_chainaccessor.go | 16 +++++++++++++++- p2p/p2pmock/readme.txt | 4 ++-- p2p/subproto/blockhash_test.go | 5 +++++ types/blockchain.go | 1 + 7 files changed, 49 insertions(+), 5 deletions(-) diff --git a/chain/chaindb.go b/chain/chaindb.go index b48c11bb1..0151ff003 100644 --- a/chain/chaindb.go +++ b/chain/chaindb.go @@ -757,12 +757,27 @@ func (cdb *ChainDB) Hardfork(hConfig config.HardforkConfig) config.HardforkDbCon if err := json.Unmarshal(data, &c); err != nil { return nil } - // When a new hardkfork height is added, the hardfork config from DB (HardforkDBConfig) + // When a new hardfork height is added, the hardfork config from DB (HardforkDBConfig) // must be modified by using the height from HardforkConfig. Without this, aergosvr fails - // to start, since a harfork heght value not stored on DB is evaluated as 0. + // to start, since a hardfork height value not stored on DB is evaluated as 0. return c.FixDbConfig(hConfig) } +func (cdb *ChainDB) hardforkHeights() config.HardforkDbConfig { + var c config.HardforkDbConfig + data := cdb.store.Get(dbkey.HardFork()) + if len(data) == 0 { + return c + } + // TODO Hardfork status is not changed during the lifetime of server process. + // We can optimize this + if err := json.Unmarshal(data, &c); err != nil { + logger.Error().Msg("Failed to read hardfork from cdb") + return nil + } + return c +} + func (cdb *ChainDB) WriteHardfork(c *config.HardforkConfig) error { data, err := json.Marshal(c) if err != nil { diff --git a/chain/chainservice.go b/chain/chainservice.go index d69fd8af7..686bbfc50 100644 --- a/chain/chainservice.go +++ b/chain/chainservice.go @@ -987,3 +987,7 @@ func (cs *ChainService) ChainID(bno types.BlockNo) *types.ChainID { cid.Version = cs.cfg.Hardfork.Version(bno) return cid } + +func (cs *ChainService) HardforkHeights() map[string]types.BlockNo { + return cs.cdb.hardforkHeights() +} diff --git a/chain/stubchain.go b/chain/stubchain.go index 587a508bd..02023aebc 100644 --- a/chain/stubchain.go +++ b/chain/stubchain.go @@ -218,6 +218,11 @@ func (tchain *StubBlockChain) ChainID(bno types.BlockNo) *types.ChainID { return nil } +func (tchain *StubBlockChain) HardforkHeights() map[string]types.BlockNo { + // FIXME: maybe it should return at least latest hardfork + return nil +} + func (tchain *StubBlockChain) Rollback(ancestor *types.BlockInfo) { prevBest := tchain.Best tchain.Best = int(ancestor.No) diff --git a/p2p/p2pmock/mock_chainaccessor.go b/p2p/p2pmock/mock_chainaccessor.go index e55b42f57..dc32a56c9 100644 --- a/p2p/p2pmock/mock_chainaccessor.go +++ b/p2p/p2pmock/mock_chainaccessor.go @@ -1,7 +1,7 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/aergoio/aergo/v2/types (interfaces: ChainAccessor) -// package p2pmock is a generated GoMock package. +// Package p2pmock is a generated GoMock package. package p2pmock import ( @@ -164,3 +164,17 @@ func (mr *MockChainAccessorMockRecorder) GetSystemValue(arg0 interface{}) *gomoc mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSystemValue", reflect.TypeOf((*MockChainAccessor)(nil).GetSystemValue), arg0) } + +// HardforkHeights mocks base method +func (m *MockChainAccessor) HardforkHeights() map[string]uint64 { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HardforkHeights") + ret0, _ := ret[0].(map[string]uint64) + return ret0 +} + +// HardforkHeights indicates an expected call of HardforkHeights +func (mr *MockChainAccessorMockRecorder) HardforkHeights() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HardforkHeights", reflect.TypeOf((*MockChainAccessor)(nil).HardforkHeights)) +} diff --git a/p2p/p2pmock/readme.txt b/p2p/p2pmock/readme.txt index b4d7d7025..a6744f641 100644 --- a/p2p/p2pmock/readme.txt +++ b/p2p/p2pmock/readme.txt @@ -17,7 +17,7 @@ mockgen -source=p2p/p2pcommon/pool.go -mock_names=WaitingPeerManager=MockWaiting # Manually generated mock classes -The generate decriptions of these mock objects are in p2p/p2pcommon/temptypes.go . So you can use such like `go generate ./p2p/p2pcommon/temptypes.go` command. +The generate descriptions of these mock objects are in p2p/p2pcommon/temptypes.go . So you can use such like `go generate ./p2p/p2pcommon/temptypes.go` command. # mock files which are not generated automatically by go generate ./p2p mockgen github.com/aergoio/aergo/v2/consensus ConsensusAccessor,AergoRaftAccessor | gsed -e 's/^package mock_[a-zA-Z0-9_]\+/package p2pmock/g' > p2p/p2pmock/mock_consensus.go @@ -26,4 +26,4 @@ mockgen -source=types/blockchain.go -package=p2pmock -destination=p2p/p2pmock/mo mockgen io Reader,ReadCloser,Writer,WriteCloser,ReadWriteCloser > p2p/p2pmock/mock_io.go | gsed -e 's/^package mock_[a-zA-Z0-9_]\+/package p2pmock/g' > p2p/p2pmock/mock_io.go -mockgen github.com/aergoio/aergo/v2/types ChainAccessor | sed -e 's/^package mock_mock_[a-zA-Z0-9_]\+/package p2pmock/g' > ../p2pmock/mock_chainaccessor.go \ No newline at end of file +mockgen github.com/aergoio/aergo/v2/types ChainAccessor | sed -e 's/^package mock_[a-zA-Z0-9_]\+/package p2pmock/g' > ../p2pmock/mock_chainaccessor.go \ No newline at end of file diff --git a/p2p/subproto/blockhash_test.go b/p2p/subproto/blockhash_test.go index 551290b69..ef7370148 100644 --- a/p2p/subproto/blockhash_test.go +++ b/p2p/subproto/blockhash_test.go @@ -8,6 +8,7 @@ package subproto import ( "bytes" "crypto/sha256" + "fmt" "sync" "testing" @@ -261,6 +262,10 @@ func (a *testDoubleChainAccessor) getChain() [][]byte { } } +func (a *testDoubleChainAccessor) GetBlockByNo(blockNo types.BlockNo) (*types.Block, error) { + return nil, fmt.Errorf("not implemented") +} + type testDoubleHashesRespFactory struct { lastResp *types.GetHashesResponse lastStatus types.ResultStatus diff --git a/types/blockchain.go b/types/blockchain.go index 372bbdf3b..6100a215a 100644 --- a/types/blockchain.go +++ b/types/blockchain.go @@ -141,6 +141,7 @@ type ChainAccessor interface { // GetEnterpriseConfig always return non-nil object if there is no error, but it can return EnterpriseConfig with empty values GetEnterpriseConfig(key string) (*EnterpriseConfig, error) ChainID(bno BlockNo) *ChainID + HardforkHeights() map[string]BlockNo } type SyncContext struct {