github.com/annchain/OG@v0.0.9/og/data_loader.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package og
    15  
    16  import (
    17  	"github.com/annchain/OG/arefactor/common/goroutine"
    18  	types2 "github.com/annchain/OG/arefactor/og/types"
    19  	"github.com/annchain/OG/common"
    20  	"github.com/annchain/OG/common/math"
    21  	"github.com/annchain/OG/og/types"
    22  	core2 "github.com/annchain/OG/ogcore/ledger"
    23  	"github.com/annchain/OG/ogcore/pool"
    24  
    25  	"github.com/sirupsen/logrus"
    26  )
    27  
    28  type DataLoader struct {
    29  	Dag    *core2.Dag
    30  	TxPool *pool.TxPool
    31  }
    32  
    33  func (d *DataLoader) Start() {
    34  	goroutine.New(d.LoadLocalData)
    35  }
    36  
    37  func (d *DataLoader) Stop() {
    38  	logrus.Info("dataLoader received stop signal. Quiting...")
    39  }
    40  
    41  func (d *DataLoader) Name() string {
    42  	return "DataLoader"
    43  }
    44  
    45  // LoadLocalData will load all necessary Data (db, status, etc) from local database.
    46  // If there is no Data or Data corrupted, rebuild.
    47  func (d *DataLoader) LoadLocalData() {
    48  	genesis := d.Dag.Genesis()
    49  	if genesis == nil {
    50  		// write genesis and flush it to database
    51  		genesis = d.GenerateGenesis()
    52  		genesisBalance := d.GenerateGenesisBalance()
    53  		d.Dag.Init(genesis, genesisBalance)
    54  		// init tips
    55  		d.TxPool.Init(d.Dag.LatestSequencer())
    56  	}
    57  }
    58  
    59  func (d *DataLoader) GenerateGenesis() *types.Sequencer {
    60  	from := common.HexToAddress("0x00")
    61  	return &types.Sequencer{
    62  		Issuer: &from,
    63  		TxBase: types.TxBase{
    64  			Type:         types.TxBaseTypeSequencer,
    65  			Hash:         types2.HexToHash("0x00"),
    66  			Height:       0,
    67  			AccountNonce: 0,
    68  		},
    69  	}
    70  }
    71  func (loader *DataLoader) GenerateGenesisBalance() map[common.Address]*math.BigInt {
    72  	return map[common.Address]*math.BigInt{}
    73  }