if err := h.UnmarshalJSON(rawHash.Key); err != nil {
return nil, errors.Wrap(err, "unmarshaling Mainchain hash")
}
- mainchain[uint64(i)] = h
+ mainchain[uint64(i+1)] = h
}
return mainchain, nil
func saveMainchain(db dbm.DB, mainchain map[uint64]*bc.Hash, hash *bc.Hash) error {
var mainchainList storage.Mainchain
- for i := 0; i < len(mainchain); i++ {
+ for i := 1; i <= len(mainchain); i++ {
rawHash := &storage.Mainchain_Hash{Key: mainchain[uint64(i)].Bytes()}
mainchainList.Hashs = append(mainchainList.Hashs, rawHash)
}
}
func BlockSubsidy(height uint64) uint64 {
- if height == 0 {
+ if height == 1 {
return initialBlockSubsidy
}
return baseSubsidy >> uint(height/subsidyReductionInterval)
}
func InitBlock() []byte {
- return []byte("0301000000000000000000000000000000000000000000000000000000000000000000ece090e7eb2b4078a79ed5c640a026361c4af77a37342e503cc68493229996e11dd9be38b18f5b492159980684155da19e87de0d1b37b35c1a1123770ec1dcc710aabe77607cce00b1c5a181808080802e0107010700ece090e7eb2b000001012cffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8080ccdee2a69fb314010151000000")
+ return []byte("0301010000000000000000000000000000000000000000000000000000000000000000cecccaebf42b406b03545ed2b38a578e5e6b0796d4ebdd8a6dd72210873fcc026c7319de578ffc492159980684155da19e87de0d1b37b35c1a1123770ec1dcc710aabe77607cced7bb1993fcb680808080801e0107010700cecccaebf42b000001012cffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8080ccdee2a69fb314010151000000")
}
"testing"
"time"
+ "github.com/bytom/consensus"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/legacy"
"github.com/bytom/protocol/state"
)
func TestNewInitBlock(t *testing.T) {
- coinbaseTx, err := createCoinbaseTx(0, 0, []byte{})
+ coinbaseTx, err := createCoinbaseTx(0, 1, []byte{})
if err != nil {
t.Error(err)
}
b := &legacy.Block{
BlockHeader: legacy.BlockHeader{
Version: 1,
- Height: 0,
+ Height: 1,
PreviousBlockHash: bc.Hash{},
TimestampMS: bc.Millis(time.Now()),
BlockCommitment: legacy.BlockCommitment{
TransactionsMerkleRoot: merkleRoot,
AssetsMerkleRoot: snap.Tree.RootHash(),
},
- Bits: uint64(3314649325747331761),
- Nonce: 0,
+ Bits: uint64(2161727821138738707),
},
Transactions: []*legacy.Tx{coinbaseTx},
}
+ for i := uint64(0); i <= 10000000000000; i++ {
+ b.Nonce = i
+ hash := b.Hash()
+
+ if consensus.CheckProofOfWork(&hash, b.Bits) {
+ break
+ }
+ }
+
rawBlock, err := b.MarshalText()
if err != nil {
t.Error(err)
txPool := protocol.NewTxPool()
chain, err := protocol.NewChain(genesisBlock.Hash(), store, txPool)
- if chain.Height() < 1 {
- if _, err := chain.ProcessBlock(genesisBlock); err != nil {
- cmn.Exit(cmn.Fmt("Failed to add genesisBlock to Chain: %v", err))
+ if chain.Height() == 0 {
+ if err := chain.SaveBlock(genesisBlock); err != nil {
+ cmn.Exit(cmn.Fmt("Failed to save genesisBlock to store: %v", err))
+ }
+ if err := chain.ConnectBlock(genesisBlock); err != nil {
+ cmn.Exit(cmn.Fmt("Failed to connect genesisBlock to chain: %v", err))
}
}
hash, ok := c.state.mainChain[height]
c.state.cond.L.Unlock()
if !ok {
- return nil, nil
+ return nil, errors.New("can't find block in given hight")
}
return c.GetBlockByHash(hash)
}
// ApplyValidBlock creates an updated snapshot without validating the
// block.
-func (c *Chain) connectBlock(block *legacy.Block) error {
+func (c *Chain) ConnectBlock(block *legacy.Block) error {
newSnapshot := state.Copy(c.state.snapshot)
if err := newSnapshot.ApplyBlock(legacy.MapBlock(block)); err != nil {
return err
}
func (c *Chain) SaveBlock(block *legacy.Block) error {
- preBlock, err := c.GetBlockByHash(&block.PreviousBlockHash)
- if err != nil {
+ preBlock, _ := c.GetBlockByHash(&block.PreviousBlockHash)
+ if err := c.ValidateBlock(block, preBlock); err != nil {
return err
}
- if err := c.ValidateBlock(block, preBlock); err != nil {
+ if err := c.store.SaveBlock(block); err != nil {
return err
}
- c.store.SaveBlock(block)
preorphans, ok := c.orphanManage.preOrphans[block.Hash()]
if !ok {
c.state.cond.L.Lock()
if c.state.block.Hash() == block.PreviousBlockHash {
defer c.state.cond.L.Unlock()
- return false, c.connectBlock(block)
+ return false, c.ConnectBlock(block)
}
if block.Height > c.state.height && block.Bits >= c.state.block.Bits {
storeStatus := store.GetStoreStatus()
c.state.height = storeStatus.Height
- if c.state.height < 1 {
+ if c.state.height == 0 {
c.state.snapshot = state.Empty()
c.state.mainChain = make(map[uint64]*bc.Hash)
return c, nil