OSDN Git Service

Merge pull request #776 from Bytom/spend_genesis
authorYongfeng LI <wliyongfeng@gmail.com>
Sun, 22 Apr 2018 01:51:02 +0000 (09:51 +0800)
committerGitHub <noreply@github.com>
Sun, 22 Apr 2018 01:51:02 +0000 (09:51 +0800)
genesis is ready for mainnet

config/genesis.go
config/genesis_test.go
consensus/difficulty/difficulty.go
consensus/general.go
database/leveldb/store_test.go
node/node.go
protocol/block_test.go
protocol/protocol.go
wallet/wallet.go

index f7912c1..d08d8b5 100644 (file)
@@ -1,6 +1,8 @@
 package config
 
 import (
+       "encoding/hex"
+
        log "github.com/sirupsen/logrus"
 
        "github.com/bytom/consensus"
@@ -8,60 +10,52 @@ import (
        "github.com/bytom/protocol/bc/types"
 )
 
-// GenerateGenesisTx will return genesis transaction
-func GenerateGenesisTx() *types.Tx {
+func genesisTx() *types.Tx {
+       contract, err := hex.DecodeString("00149514bf92cac8791dcc4cd7fd3ef4167ffc477f62")
+       if err != nil {
+               log.Panicf("fail on decode genesis tx output control program")
+       }
+
        txData := types.TxData{
-               Version:        1,
-               SerializedSize: 63,
+               Version: 1,
                Inputs: []*types.TxInput{
                        types.NewCoinbaseInput([]byte("May 4th Be With You")),
                },
                Outputs: []*types.TxOutput{
-                       &types.TxOutput{
-                               AssetVersion: 1,
-                               OutputCommitment: types.OutputCommitment{
-                                       AssetAmount: bc.AssetAmount{
-                                               AssetId: consensus.BTMAssetID,
-                                               Amount:  consensus.InitialBlockSubsidy,
-                                       },
-                                       VMVersion:      1,
-                                       ControlProgram: []byte{81},
-                               },
-                       },
+                       types.NewTxOutput(*consensus.BTMAssetID, consensus.InitialBlockSubsidy, contract),
                },
        }
-
        return types.NewTx(txData)
 }
 
-// GenerateGenesisBlock will return genesis block
-func GenerateGenesisBlock() *types.Block {
-       genesisCoinbaseTx := GenerateGenesisTx()
-       merkleRoot, err := bc.TxMerkleRoot([]*bc.Tx{genesisCoinbaseTx.Tx})
-       if err != nil {
-               log.Panicf("Fatal create tx merkelRoot")
-       }
-
+// GenesisBlock will return genesis block
+func GenesisBlock() *types.Block {
+       tx := genesisTx()
        txStatus := bc.NewTransactionStatus()
        txStatus.SetStatus(0, false)
        txStatusHash, err := bc.TxStatusMerkleRoot(txStatus.VerifyStatus)
        if err != nil {
-               log.Panicf("Fatal create tx status gmerkelRoot")
+               log.Panicf("fail on calc genesis tx status merkle root")
+       }
+
+       merkleRoot, err := bc.TxMerkleRoot([]*bc.Tx{tx.Tx})
+       if err != nil {
+               log.Panicf("fail on calc genesis tx merkel root")
        }
 
        block := &types.Block{
                BlockHeader: types.BlockHeader{
                        Version:   1,
                        Height:    0,
-                       Nonce:     4216236,
+                       Nonce:     2083236893,
                        Timestamp: 1524202000,
+                       Bits:      2089670227111054243,
                        BlockCommitment: types.BlockCommitment{
                                TransactionsMerkleRoot: merkleRoot,
                                TransactionStatusHash:  txStatusHash,
                        },
-                       Bits: 2089670227111054243,
                },
-               Transactions: []*types.Tx{genesisCoinbaseTx},
+               Transactions: []*types.Tx{tx},
        }
        return block
 }
index 4acab69..3c30938 100644 (file)
@@ -1,16 +1,5 @@
 package config
 
-import (
-       "testing"
-)
-
-// test genesis
-func TestGenerateGenesisTx(t *testing.T) {
-       if tx := GenerateGenesisTx(); tx == nil {
-               t.Errorf("Generate genesis tx failed")
-       }
-}
-
 /*func TestGenerateGenesisBlock(t *testing.T) {
        block := GenerateGenesisBlock()
        nonce := block.Nonce
index 5f82682..5f5249a 100644 (file)
@@ -126,9 +126,7 @@ func CheckProofOfWork(hash, seed *bc.Hash, bits uint64) bool {
 // for next block, when a lower difficulty Int actually reflects a more difficult
 // mining progress.
 func CalcNextRequiredDifficulty(lastBH, compareBH *types.BlockHeader) uint64 {
-       if lastBH == nil {
-               return consensus.PowMinBits
-       } else if (lastBH.Height)%consensus.BlocksPerRetarget != 0 || lastBH.Height == 0 {
+       if (lastBH.Height)%consensus.BlocksPerRetarget != 0 || lastBH.Height == 0 {
                return lastBH.Bits
        }
 
index b6ed2fa..bc3a39e 100644 (file)
@@ -19,10 +19,9 @@ const (
        CoinbasePendingBlockNumber = uint64(100)
        subsidyReductionInterval   = uint64(840000)
        baseSubsidy                = uint64(41250000000)
-       InitialBlockSubsidy        = uint64(1470000041250000000)
+       InitialBlockSubsidy        = uint64(147000041250000000)
 
        // config for pow mining
-       PowMinBits            = uint64(2305843009213861724)
        BlocksPerRetarget     = uint64(2016)
        TargetSecondsPerBlock = uint64(150)
        SeedPerRetarget       = uint64(256)
@@ -85,6 +84,7 @@ type Params struct {
        Bech32HRPSegwit string
 }
 
+// ActiveNetParams is ...
 var ActiveNetParams = MainNetParams
 
 // NetParams is the correspondence between chain_id and Params
index 97e2e7b..74fbc9d 100644 (file)
@@ -15,7 +15,7 @@ func TestLoadBlockIndex(t *testing.T) {
        testDB := dbm.NewDB("testdb", "leveldb", "temp")
        store := NewStore(testDB)
 
-       block := config.GenerateGenesisBlock()
+       block := config.GenesisBlock()
        txStatus := bc.NewTransactionStatus()
 
        if err := store.SaveBlock(block, txStatus); err != nil {
index 3061d68..5f82e93 100644 (file)
@@ -60,16 +60,15 @@ func NewNode(config *cfg.Config) *Node {
        ctx := context.Background()
        initActiveNetParams(config)
        // Get store
-       txDB := dbm.NewDB("txdb", config.DBBackend, config.DBDir())
-       store := leveldb.NewStore(txDB)
+       coreDB := dbm.NewDB("core", config.DBBackend, config.DBDir())
+       store := leveldb.NewStore(coreDB)
 
        tokenDB := dbm.NewDB("accesstoken", config.DBBackend, config.DBDir())
        accessTokens := accesstoken.NewStore(tokenDB)
 
        // Make event switch
        eventSwitch := types.NewEventSwitch()
-       _, err := eventSwitch.Start()
-       if err != nil {
+       if _, err := eventSwitch.Start(); err != nil {
                cmn.Exit(cmn.Fmt("Failed to start switch: %v", err))
        }
 
index e321e93..58a27bd 100644 (file)
@@ -10,7 +10,7 @@ import (
 
 func TestCalcReorganizeNodes(t *testing.T) {
        c := &Chain{index: state.NewBlockIndex()}
-       header := config.GenerateGenesisBlock().BlockHeader
+       header := config.GenesisBlock().BlockHeader
        initNode, err := state.NewBlockNode(&header, nil)
        if err != nil {
                t.Fatal(err)
index b3763ce..015cb94 100644 (file)
@@ -60,7 +60,7 @@ func NewChain(store Store, txPool *TxPool) (*Chain, error) {
 }
 
 func (c *Chain) initChainStatus() error {
-       genesisBlock := config.GenerateGenesisBlock()
+       genesisBlock := config.GenesisBlock()
        txStatus := bc.NewTransactionStatus()
        for i := range genesisBlock.Transactions {
                txStatus.SetStatus(i, false)
index 7aafa1f..b80cf1b 100644 (file)
@@ -174,9 +174,9 @@ func (w *Wallet) RescanBlocks() {
 func (w *Wallet) getRescanNotification() {
        select {
        case <-w.rescanCh:
-               w.status.WorkHeight = 0
-               block, _ := w.chain.GetBlockByHeight(w.status.WorkHeight)
-               w.status.WorkHash = block.Hash()
+               block, _ := w.chain.GetBlockByHeight(0)
+               w.status.WorkHash = bc.Hash{}
+               w.AttachBlock(block)
        default:
                return
        }