From c3b4c505c40362a3c80f6a635e9b5468cd6c67ed Mon Sep 17 00:00:00 2001 From: hauson <32921352+hauson@users.noreply.github.com> Date: Mon, 19 Jul 2021 18:38:41 +0800 Subject: [PATCH] feat(modify genesis-tx): modify genesis-tx (#2043) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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: 崔浩鑫 Co-authored-by: Paladz --- config/genesis.go | 26 +++++++++----- config/genesis_tx.go | 95 ++++++++++++++++++++++++++++++++++++++++++++------ database/store_test.go | 3 ++ wallet/wallet_test.go | 2 +- 4 files changed, 105 insertions(+), 21 deletions(-) diff --git a/config/genesis.go b/config/genesis.go index 800cc847..7b278325 100644 --- a/config/genesis.go +++ b/config/genesis.go @@ -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 } diff --git a/config/genesis_tx.go b/config/genesis_tx.go index a4740b82..89a0a4fc 100644 --- a/config/genesis_tx.go +++ b/config/genesis_tx.go @@ -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, + }, } diff --git a/database/store_test.go b/database/store_test.go index cd3141ea..da88e930 100644 --- a/database/store_test.go +++ b/database/store_test.go @@ -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) } diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index b8a79933..010176c0 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -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), } } -- 2.11.0