OSDN Git Service

feat(modify genesis-tx): modify genesis-tx (#2043)
authorhauson <32921352+hauson@users.noreply.github.com>
Mon, 19 Jul 2021 10:38:41 +0000 (18:38 +0800)
committerGitHub <noreply@github.com>
Mon, 19 Jul 2021 10:38:41 +0000 (18:38 +0800)
* feat(modify genesis-tx): modify genesis-tx

* feat(only left btm, sup, mag): only left btm, sup, mag

* feat(modify): modify

* feat(modify test): moidfy test

* feat(modify test): modify test

Co-authored-by: 崔浩鑫 <cuihaoxin@cuihaoxindeMacBook-Pro.local>
Co-authored-by: Paladz <yzhu101@uottawa.ca>
config/genesis.go
config/genesis_tx.go
database/store_test.go
wallet/wallet_test.go

index 800cc84..7b27832 100644 (file)
@@ -8,9 +8,17 @@ import (
        "github.com/bytom/bytom/protocol/bc/types"
 )
 
+func toBCTxs(txs []*types.Tx) []*bc.Tx {
+       var bcTxs []*bc.Tx
+       for _, tx := range txs {
+               bcTxs = append(bcTxs, tx.Tx)
+       }
+       return bcTxs
+}
+
 func mainNetGenesisBlock() *types.Block {
-       tx := GenesisTx()
-       merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
+       txs := GenesisTxs()
+       merkleRoot, err := types.TxMerkleRoot(toBCTxs(txs))
        if err != nil {
                log.Panicf("fail on calc genesis tx merkel root")
        }
@@ -24,14 +32,14 @@ func mainNetGenesisBlock() *types.Block {
                                TransactionsMerkleRoot: merkleRoot,
                        },
                },
-               Transactions: []*types.Tx{tx},
+               Transactions: txs,
        }
        return block
 }
 
 func testNetGenesisBlock() *types.Block {
-       tx := GenesisTx()
-       merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
+       txs := GenesisTxs()
+       merkleRoot, err := types.TxMerkleRoot(toBCTxs(txs))
        if err != nil {
                log.Panicf("fail on calc genesis tx merkel root")
        }
@@ -45,14 +53,14 @@ func testNetGenesisBlock() *types.Block {
                                TransactionsMerkleRoot: merkleRoot,
                        },
                },
-               Transactions: []*types.Tx{tx},
+               Transactions: txs,
        }
        return block
 }
 
 func soloNetGenesisBlock() *types.Block {
-       tx := GenesisTx()
-       merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
+       txs := GenesisTxs()
+       merkleRoot, err := types.TxMerkleRoot(toBCTxs(txs))
        if err != nil {
                log.Panicf("fail on calc genesis tx merkel root")
        }
@@ -66,7 +74,7 @@ func soloNetGenesisBlock() *types.Block {
                                TransactionsMerkleRoot: merkleRoot,
                        },
                },
-               Transactions: []*types.Tx{tx},
+               Transactions: txs,
        }
        return block
 }
index a4740b8..89a0a4f 100644 (file)
@@ -6,24 +6,97 @@ import (
        log "github.com/sirupsen/logrus"
 
        "github.com/bytom/bytom/consensus"
+       "github.com/bytom/bytom/protocol/bc"
        "github.com/bytom/bytom/protocol/bc/types"
 )
 
-// GenesisTx make genesis block txs
-func GenesisTx() *types.Tx {
-       contract, err := hex.DecodeString("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4")
+// AssetIssue asset issue params
+type AssetIssue struct {
+       NonceStr           string
+       IssuanceProgramStr string
+       AssetDefinitionStr string
+       AssetIDStr         string
+       Amount             uint64
+}
+
+func (a *AssetIssue) nonce() []byte {
+       bs, err := hex.DecodeString(a.NonceStr)
+       if err != nil {
+               panic(err)
+       }
+
+       return bs
+}
+
+func (a *AssetIssue) issuanceProgram() []byte {
+       bs, err := hex.DecodeString(a.IssuanceProgramStr)
+       if err != nil {
+               panic(err)
+       }
+
+       return bs
+}
+
+func (a *AssetIssue) assetDefinition() []byte {
+       bs, err := hex.DecodeString(a.AssetDefinitionStr)
+       if err != nil {
+               panic(err)
+       }
+
+       return bs
+}
+
+func (a *AssetIssue) id() bc.AssetID {
+       var bs [32]byte
+       bytes, err := hex.DecodeString(a.AssetIDStr)
+       if err != nil {
+               panic(err)
+       }
+
+       copy(bs[:], bytes)
+       return bc.NewAssetID(bs)
+}
+
+// GenesisTxs make genesis block txs
+func GenesisTxs() []*types.Tx {
+       contract, err := hex.DecodeString("00148dea804c8f5a27518be2696e43dca0352b9a93cc")
        if err != nil {
                log.Panicf("fail on decode genesis tx output control program")
        }
 
-       txData := types.TxData{
+       var txs []*types.Tx
+       firstTxData := types.TxData{
                Version: 1,
-               Inputs: []*types.TxInput{
-                       types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018.")),
-               },
-               Outputs: []*types.TxOutput{
-                       types.NewOriginalTxOutput(*consensus.BTMAssetID, 167959666678580395, contract, nil),
-               },
+               Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018."))},
+               Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 167959666678580395, contract, nil)},
+       }
+       txs = append(txs, types.NewTx(firstTxData))
+
+       inputs := []*types.TxInput{}
+       outputs := []*types.TxOutput{}
+       for _, asset := range assetIssues {
+               inputs = append(inputs, types.NewIssuanceInput(asset.nonce(), asset.Amount, asset.issuanceProgram(), nil, asset.assetDefinition()))
+               outputs = append(outputs, types.NewOriginalTxOutput(asset.id(), asset.Amount, contract, nil))
        }
-       return types.NewTx(txData)
+
+       secondTxData := types.TxData{Version: 1, Inputs: inputs, Outputs: outputs}
+       txs = append(txs, types.NewTx(secondTxData))
+       return txs
+}
+
+var assetIssues = []*AssetIssue{
+       {
+               NonceStr:           "f57f15fbbdf5f70d",
+               IssuanceProgramStr: "0354df07cda069ae203c939b7ba615a49adf57b5b4c9c37e80666436970f8507d56a272539cabcb9e15151ad",
+               AssetDefinitionStr: "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022535550222c0a20202271756f72756d223a20312c0a20202272656973737565223a202266616c7365222c0a20202273796d626f6c223a2022535550220a7d",
+               AssetIDStr:         "47fcd4d7c22d1d38931a6cd7767156babbd5f05bbbb3f7d3900635b56eb1b67e",
+               Amount:             9877601291045,
+       },
+       {
+               NonceStr:           "8e972359c6441299",
+               IssuanceProgramStr: "ae20d66ab117eca2bba6aefed569e52d6bf68a7a4ad7d775cbc01f7b2cfcd798f7b22031ab3c2147c330c5e360b4e585047d1dea5f529476ad5aff475ecdd55541923120851b4a24975df6dbeb4f8e5348542764f85bed67b763875325aa5e45116751b35253ad",
+               AssetDefinitionStr: "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a20224d4147222c0a20202271756f72756d223a20322c0a20202272656973737565223a202274727565222c0a20202273796d626f6c223a20224d4147220a7d",
+               AssetIDStr:         "a0a71c215764e342d10d003be6369baf4145d9c7977f7b8f6bf446e628d8b8b8",
+               Amount:             100000000000000,
+       },
 }
index cd3141e..da88e93 100644 (file)
@@ -75,6 +75,9 @@ func TestSaveBlock(t *testing.T) {
 
        gotBlock.Transactions[0].Tx.SerializedSize = 0
        gotBlock.Transactions[0].SerializedSize = 0
+       gotBlock.Transactions[1].Tx.SerializedSize = 0
+       gotBlock.Transactions[1].SerializedSize = 0
+
        if !testutil.DeepEqual(block, gotBlock) {
                t.Errorf("got block:%v, expect block:%v", gotBlock, block)
        }
index b8a7993..010176c 100644 (file)
@@ -387,6 +387,6 @@ func mockSingleBlock(tx *types.Tx) *types.Block {
                        Version: 1,
                        Height:  1,
                },
-               Transactions: []*types.Tx{config.GenesisTx(), tx},
+               Transactions: append(config.GenesisTxs(), tx),
        }
 }