OSDN Git Service

refactor: use derived xpubs for federation (#289)
[bytom/vapor.git] / config / genesis.go
index bd19407..4c8e62e 100644 (file)
@@ -1,66 +1,70 @@
 package config
 
 import (
-       "crypto/sha256"
        "encoding/hex"
 
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/consensus"
        "github.com/vapor/crypto"
-       "github.com/vapor/crypto/ed25519"
        "github.com/vapor/crypto/ed25519/chainkd"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/vm/vmutil"
 )
 
-func commitToArguments() (res *[32]byte) {
-       var fedpegPubkeys []ed25519.PublicKey
-       var signBlockPubkeys []ed25519.PublicKey
-       for _, xpub := range consensus.ActiveNetParams.FedpegXPubs {
-               fedpegPubkeys = append(fedpegPubkeys, xpub.PublicKey())
-       }
-       fedpegScript, _ := vmutil.P2SPMultiSigProgram(fedpegPubkeys, len(fedpegPubkeys))
+var FedAddressPath = [][]byte{
+       []byte{0x2C, 0x00, 0x00, 0x00},
+       []byte{0x99, 0x00, 0x00, 0x00},
+       []byte{0x01, 0x00, 0x00, 0x00},
+       []byte{0x00, 0x00, 0x00, 0x00},
+       []byte{0x01, 0x00, 0x00, 0x00},
+}
 
-       for _, xpub := range consensus.ActiveNetParams.SignBlockXPubs {
-               signBlockPubkeys = append(signBlockPubkeys, xpub.PublicKey())
+func FederationPMultiSigScript(c *Config) []byte {
+       xpubs := c.Federation.Xpubs
+       derivedXPubs := chainkd.DeriveXPubs(xpubs, FedAddressPath)
+       program, err := vmutil.P2SPMultiSigProgram(chainkd.XPubKeys(derivedXPubs), c.Federation.Quorum)
+       if err != nil {
+               log.Panicf("fail to generate federation scirpt for federation: %v", err)
        }
-       signBlockScript, _ := vmutil.P2SPMultiSigProgram(signBlockPubkeys, len(signBlockPubkeys))
 
-       hasher := sha256.New()
-       hasher.Write(fedpegScript)
-       hasher.Write(signBlockScript)
-       resSlice := hasher.Sum(nil)
-       res = new([32]byte)
-       copy(res[:], resSlice)
-       return
+       return program
 }
 
-func genesisTx() *types.Tx {
+func FederationWScript(c *Config) []byte {
+       script := FederationPMultiSigScript(c)
+       scriptHash := crypto.Sha256(script)
+       wscript, err := vmutil.P2WSHProgram(scriptHash)
+       if err != nil {
+               log.Panicf("Fail converts scriptHash to witness: %v", err)
+       }
+
+       return wscript
+}
 
+func GenesisTx() *types.Tx {
        contract, err := hex.DecodeString("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4")
        if err != nil {
                log.Panicf("fail on decode genesis tx output control program")
        }
 
-       coinbaseInput := commitToArguments()
+       coinbaseInput := FederationWScript(CommonConfig)
+
        txData := types.TxData{
                Version: 1,
                Inputs: []*types.TxInput{
-                       // Any consensus-related values that are command-line set can be added here for anti-footgun
                        types.NewCoinbaseInput(coinbaseInput[:]),
-                       //types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018.")),
                },
                Outputs: []*types.TxOutput{
-                       types.NewTxOutput(*consensus.BTMAssetID, consensus.InitialBlockSubsidy, contract),
+                       types.NewIntraChainOutput(*consensus.BTMAssetID, consensus.BlockSubsidy(0), contract),
                },
        }
        return types.NewTx(txData)
 }
 
 func mainNetGenesisBlock() *types.Block {
-       tx := genesisTx()
+       tx := GenesisTx()
        txStatus := bc.NewTransactionStatus()
        if err := txStatus.SetStatus(0, false); err != nil {
                log.Panicf(err.Error())
@@ -79,7 +83,7 @@ func mainNetGenesisBlock() *types.Block {
                BlockHeader: types.BlockHeader{
                        Version:   1,
                        Height:    0,
-                       Timestamp: 1524549600,
+                       Timestamp: 1562840600003,
                        BlockCommitment: types.BlockCommitment{
                                TransactionsMerkleRoot: merkleRoot,
                                TransactionStatusHash:  txStatusHash,
@@ -87,26 +91,11 @@ func mainNetGenesisBlock() *types.Block {
                },
                Transactions: []*types.Tx{tx},
        }
-
-       var xPrv chainkd.XPrv
-       if consensus.ActiveNetParams.Signer == "" {
-               log.Panicf("Signer is empty")
-       }
-       copy(xPrv[:], []byte(consensus.ActiveNetParams.Signer))
-       msg, _ := block.MarshalText()
-       sign := xPrv.Sign(msg)
-       pubHash := crypto.Ripemd160(xPrv.XPub().PublicKey())
-       control, err := vmutil.P2WPKHProgram([]byte(pubHash))
-       if err != nil {
-               log.Panicf(err.Error())
-       }
-       block.Proof.Sign = sign
-       block.Proof.ControlProgram = control
        return block
 }
 
 func testNetGenesisBlock() *types.Block {
-       tx := genesisTx()
+       tx := GenesisTx()
        txStatus := bc.NewTransactionStatus()
        if err := txStatus.SetStatus(0, false); err != nil {
                log.Panicf(err.Error())
@@ -125,7 +114,7 @@ func testNetGenesisBlock() *types.Block {
                BlockHeader: types.BlockHeader{
                        Version:   1,
                        Height:    0,
-                       Timestamp: 1528945000,
+                       Timestamp: 1562840600001,
                        BlockCommitment: types.BlockCommitment{
                                TransactionsMerkleRoot: merkleRoot,
                                TransactionStatusHash:  txStatusHash,
@@ -137,7 +126,7 @@ func testNetGenesisBlock() *types.Block {
 }
 
 func soloNetGenesisBlock() *types.Block {
-       tx := genesisTx()
+       tx := GenesisTx()
        txStatus := bc.NewTransactionStatus()
        if err := txStatus.SetStatus(0, false); err != nil {
                log.Panicf(err.Error())
@@ -156,7 +145,7 @@ func soloNetGenesisBlock() *types.Block {
                BlockHeader: types.BlockHeader{
                        Version:   1,
                        Height:    0,
-                       Timestamp: 1528945000,
+                       Timestamp: 1562840600000,
                        BlockCommitment: types.BlockCommitment{
                                TransactionsMerkleRoot: merkleRoot,
                                TransactionStatusHash:  txStatusHash,