-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.go
60 lines (52 loc) · 1.88 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package blockmgr
import (
"math/big"
"time"
"github.com/drep-project/DREP-Chain/chain/utils"
chainBlock "github.com/drep-project/DREP-Chain/chain/block"
chainStore "github.com/drep-project/DREP-Chain/chain/store"
"github.com/drep-project/DREP-Chain/common"
"github.com/drep-project/DREP-Chain/crypto"
"github.com/drep-project/DREP-Chain/params"
"github.com/drep-project/DREP-Chain/types"
)
// GenerateTemplate blockchain t
func (blockMgr *BlockMgr) GenerateTemplate(trieStore chainStore.StoreInterface, leaderAddr crypto.CommonAddress, blockInterval int) (*types.Block, *big.Int, error) {
parent, err := blockMgr.ChainService.GetHighestBlock()
if err != nil {
return nil, nil, err
}
newGasLimit := blockMgr.ChainService.CalcGasLimit(parent.Header, params.MinGasLimit, params.MaxGasLimit)
height := blockMgr.ChainService.BestChain().Height() + 1
txs := blockMgr.transactionPool.GetPending(newGasLimit)
previousHash := blockMgr.ChainService.BestChain().Tip().Hash
timestamp := uint64(time.Now().Unix())
blockHeader := &types.BlockHeader{
Version: common.Version,
PreviousHash: *previousHash,
ChainId: blockMgr.ChainService.ChainID(),
GasLimit: *newGasLimit,
Timestamp: timestamp,
Height: height,
StateRoot: []byte{},
TxRoot: []byte{},
MinerAddr: leaderAddr,
}
block := &types.Block{
Header: blockHeader,
Data: &types.BlockData{
TxCount: uint64(len(txs)),
TxList: txs,
},
}
gp := new(utils.GasPool).AddGas(newGasLimit.Uint64())
//process transaction
chainStore := &chainStore.ChainStore{blockMgr.DatabaseService.LevelDb()}
context := chainBlock.NewBlockExecuteContext(trieStore, gp, chainStore, block)
templateValidator := NewTemplateBlockValidator(blockMgr.ChainService)
err = templateValidator.ExecuteBlock(context, blockInterval)
if err != nil {
return nil, nil, err
}
return context.Block, context.GasFee, nil
}