Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/base'
Browse files Browse the repository at this point in the history
# Conflicts:
#	chain/taskreset.go
  • Loading branch information
mksong76 committed Jul 29, 2022
2 parents 6b036e9 + 8286593 commit b2bb29f
Show file tree
Hide file tree
Showing 53 changed files with 1,252 additions and 200 deletions.
57 changes: 57 additions & 0 deletions block/blockdatafactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package block

import (
"io"

"github.com/icon-project/goloop/chain/base"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/module"
)

type blockDataFactory struct {
sm module.ServiceManager
handlers handlerList
}

func NewBlockDataFactory(
c module.Chain,
sm module.ServiceManager,
handlers []base.BlockHandler,
) (module.BlockDataFactory, error) {
if handlers == nil {
handlers = []base.BlockHandler{NewBlockV2Handler(c)}
}

return &blockDataFactory{
sm: sm,
handlers: handlers,
}, nil
}

func (f *blockDataFactory) NewBlockDataFromReader(r io.Reader) (module.BlockData, error) {
v, r, err := PeekVersion(r)
if err != nil {
return nil, err
}
h, ok := f.handlers.forVersion(v)
if !ok {
return nil, errors.UnsupportedError.Errorf("unsupported block version %d", v)
}
return h.NewBlockDataFromReader(r)
}
33 changes: 19 additions & 14 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,8 @@ func (m *manager) _importBlockByID(src db.Database, id []byte) (module.Block, er

func (m *manager) finalizePrunedBlock() error {
s := m.chain.GenesisStorage()
g := new(gs.PrunedGenesis)
if err := json.Unmarshal(s.Genesis(), g); err != nil {
g, err := gs.NewPrunedGenesis(s.Genesis())
if err != nil {
return transaction.InvalidGenesisError.Wrap(err, "InvalidGenesis")
}
d := gs.NewDatabaseWithStorage(s)
Expand Down Expand Up @@ -1303,7 +1303,7 @@ func (m *manager) getTransactionLocator(id []byte) (*transactionLocator, error)
loc := new(transactionLocator)
err = tlb.Get(db.Raw(id), loc)
if err != nil {
return nil, errors.ErrNotFound
return nil, errors.NotFoundError.Errorf("not found tx id=%x", id)
}
return loc, nil
}
Expand Down Expand Up @@ -1515,14 +1515,15 @@ func hasBits(v int, bits int) bool {
return (v & bits) == bits
}

func (m *manager) ExportGenesis(blk module.Block, gsw module.GenesisStorageWriter) error {
func (m *manager) ExportGenesis(blk module.BlockData, votes module.CommitVoteSet, gsw module.GenesisStorageWriter) error {
height := blk.Height()

var votes module.CommitVoteSet
if nblk, err := m.GetBlockByHeight(height + 1); err != nil {
return errors.Wrapf(err, "fail to get next block(height=%d) for votes", height+1)
} else {
votes = nblk.Votes()
if votes == nil {
if nblk, err := m.GetBlockByHeight(height + 1); err != nil {
return errors.Wrapf(err, "fail to get next block(height=%d) for votes", height+1)
} else {
votes = nblk.Votes()
}
}

cid, err := m.sm.GetChainID(blk.Result())
Expand Down Expand Up @@ -1550,14 +1551,11 @@ func (m *manager) ExportGenesis(blk module.Block, gsw module.GenesisStorageWrite
if err := gsw.WriteGenesis(g); err != nil {
return errors.Wrap(err, "fail to write genesis")
}
defer func() {
m.log.Must(gsw.Close())
}()

if _, err := gsw.WriteData(votes.Bytes()); err != nil {
return errors.Wrap(err, "fail to write votes")
}
return m._exportBlocks(height, height, gs.NewDatabaseWithWriter(gsw), exportHashable, nil)
return nil
}

func (m *manager) ExportBlocks(from, to int64, dst db.Database, on func(h int64) error) error {
Expand Down Expand Up @@ -1829,10 +1827,17 @@ func GetLastHeightOf(dbase db.Database) int64 {
}

func ResetDB(d db.Database, c codec.Codec, height int64) error {
bk, err := d.GetBucket(db.ChainProperty)
return SetLastHeight(d, c, height)
}

func SetLastHeight(dbase db.Database, c codec.Codec, height int64) error {
bk, err := dbase.GetBucket(db.ChainProperty)
if err != nil {
return err
}
if c == nil {
c = dbCodec
}
err = bk.Set([]byte(keyLastBlockHeight), c.MustMarshalToBytes(height))
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion block/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (ti *transitionImpl) propose(bi module.BlockInfo, csi module.ConsensusInfo,
}

func (ti *transitionImpl) sync(result []byte, vlHash []byte, cb transitionCallback) (*transition, error) {
cmtr := ti._chainContext.sm.CreateSyncTransition(ti._mtransition, result, vlHash)
cmtr := ti._chainContext.sm.CreateSyncTransition(ti._mtransition, result, vlHash, false)
if cmtr == nil {
return nil, errors.New("fail to createSyncTransition")
}
Expand Down
107 changes: 107 additions & 0 deletions block/unsafefinalize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2022 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package block

import (
"github.com/icon-project/goloop/chain/base"
"github.com/icon-project/goloop/common"
"github.com/icon-project/goloop/common/db"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/common/log"
"github.com/icon-project/goloop/module"
"github.com/icon-project/goloop/service"
)

type finalizeRequest struct {
sm ServiceManager
syncTr module.Transition
dbase db.Database
resCh chan error
cancelCh <-chan struct{}
}

func (r *finalizeRequest) finalize(blk module.BlockData) error {
ntr, err := r.sm.CreateTransition(r.syncTr, blk.NormalTransactions(), blk, nil, true)
if err != nil {
return nil
}
if err = service.FinalizeTransition(ntr, module.FinalizeNormalTransaction, false); err != nil {
return err
}
if err = service.FinalizeTransition(r.syncTr, module.FinalizePatchTransaction|module.FinalizeResult, false); err != nil {
return err
}

if err = blk.(base.BlockVersionSpec).FinalizeHeader(r.dbase); err != nil {
return err
}
if err = WriteTransactionLocators(r.dbase, blk.Height(), blk.PatchTransactions(), blk.NormalTransactions()); err != nil {
return err
}
return nil
}

func (r *finalizeRequest) OnValidate(t module.Transition, err error) {
if err != nil {
log.Warnf("unexpected error during forced finalization: %+v", err)
r.resCh <- err
}
}

func (r *finalizeRequest) OnExecute(t module.Transition, err error) {
r.resCh <- err
}

func UnsafeFinalize(
sm ServiceManager,
c module.Chain,
blk module.BlockData,
cancelCh <-chan struct{},
) error {
initTr, err := sm.CreateInitialTransition(nil, nil)
if err != nil {
return err
}
bi := common.NewBlockInfo(blk.Height()-1, blk.Timestamp()-1)
tr, err := sm.CreateTransition(initTr, nil, bi, nil, true)
if err != nil {
return err
}
tr = sm.PatchTransition(tr, blk.PatchTransactions(), blk)
syncTr := sm.CreateSyncTransition(tr, blk.Result(), blk.NextValidatorsHash(), true)
r := &finalizeRequest{
sm: sm,
syncTr: syncTr,
dbase: c.Database(),
resCh: make(chan error, 2),
cancelCh: cancelCh,
}
canceler, err := syncTr.Execute(r)
if err != nil {
return err
}
select {
case err := <-r.resCh:
if err != nil {
return err
}
return r.finalize(blk)
case <-r.cancelCh:
canceler()
return errors.Errorf("sync canceled height=%d hash=%x", blk.Height(), blk.Height())
}
}
11 changes: 8 additions & 3 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func (c *singleChain) _runTask(task chainTask, wait bool) error {

func (c *singleChain) _waitResultOf(task chainTask) error {
result := task.Wait()
c.logger.Infof("DONE %s err=%v", task.String(), result)
c.logger.Infof("DONE %s err=%+v", task.String(), result)

if result == nil {
c._transitOrTerminate(Finished, nil, Started, Stopping)
Expand Down Expand Up @@ -639,8 +639,13 @@ func (c *singleChain) Verify() error {
return errors.UnsupportedError.New("UnsupportedFeatureVerify")
}

func (c *singleChain) Reset() error {
task := newTaskReset(c)
func (c *singleChain) Reset(gs string, height int64, blockHash []byte) error {
if len(gs) == 0 {
chainDir := c.cfg.AbsBaseDir()
const chainGenesisZipFileName = "genesis.zip"
gs = path.Join(chainDir, chainGenesisZipFileName)
}
task := newTaskReset(c, gs, height, blockHash)
return c._runTask(task, false)
}

Expand Down
6 changes: 4 additions & 2 deletions chain/gs/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type genesisStorage struct {

func (gs *genesisStorage) ensureTypeAndIDs() error {
if gs.cid == 0 {
if pg, err := newPrunedGenesis(gs.Genesis()); err == nil {
if pg, err := NewPrunedGenesis(gs.Genesis()); err == nil {
gs.cid = int(pg.CID.Value)
gs.nid = int(pg.NID.Value)
gs.gType = module.GenesisPruned
Expand Down Expand Up @@ -348,9 +348,11 @@ func WriteFromPath(w io.Writer, p string) error {
writer: gsw,
path: genesisDir,
}, genesisObj)
if err != nil {
return errors.Wrap(err, "Fail to process content")
}

// write genesis data at last

genesis, err = json.Marshal(genesisObj)
if err != nil {
return errors.Wrap(err, "Fail to marshal JSON")
Expand Down
2 changes: 1 addition & 1 deletion chain/gs/pruned.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (g *PrunedGenesis) Verify() error {
return nil
}

func newPrunedGenesis(js []byte) (*PrunedGenesis, error) {
func NewPrunedGenesis(js []byte) (*PrunedGenesis, error) {
g := new(PrunedGenesis)
if err := json.Unmarshal(js, g); err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions chain/imports/servicemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func (m *managerForImport) PatchTransition(
}
}

func (m *managerForImport) CreateSyncTransition(transition module.Transition, result []byte, vlHash []byte) module.Transition {
otr := m.ServiceManager.CreateSyncTransition(unwrap(transition), result, vlHash)
func (m *managerForImport) CreateSyncTransition(transition module.Transition, result []byte, vlHash []byte, noBuffer bool) module.Transition {
otr := m.ServiceManager.CreateSyncTransition(unwrap(transition), result, vlHash, noBuffer)
if otr == nil {
return nil
}
Expand Down
Loading

0 comments on commit b2bb29f

Please sign in to comment.