OSDN Git Service

Add write data to chain
authormars <mars@bytom.io>
Tue, 26 Feb 2019 07:41:49 +0000 (15:41 +0800)
committermars <mars@bytom.io>
Tue, 26 Feb 2019 07:43:29 +0000 (15:43 +0800)
api/transact.go
blockchain/txbuilder/actions.go
blockchain/txbuilder/types.go
cmd/vapor/commands/run_node.go
config/config.go
node/node.go

index 1f34cb1..7f8b986 100644 (file)
@@ -35,6 +35,7 @@ func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, erro
                "spend_account":                a.wallet.AccountMgr.DecodeSpendAction,
                "spend_account_unspent_output": a.wallet.AccountMgr.DecodeSpendUTXOAction,
                "dpos_address":                 a.wallet.AccountMgr.DecodeDposAction,
+               "ipfs_data":                    txbuilder.DecodeIpfsDataAction,
        }
        decoder, ok := decoders[action]
        return decoder, ok
index 13268be..9837d61 100644 (file)
@@ -9,6 +9,8 @@ import (
        "os"
        "strings"
 
+       "github.com/vapor/config"
+
        ipfs "github.com/ipfs/go-ipfs-api"
        "github.com/vapor/common"
        "github.com/vapor/consensus"
@@ -148,18 +150,24 @@ const (
        data
 )
 
+// DecodeIpfsDataAction convert input data to action struct
+func DecodeIpfsDataAction(data []byte) (Action, error) {
+       a := new(dataAction)
+       err := stdjson.Unmarshal(data, a)
+       return a, err
+}
+
 type dataAction struct {
-       Type uint32 `json:"type"`
+       bc.AssetAmount
+       Type uint32 `json:"data_type"`
        Data string `json:"data"`
 }
 
 func (a *dataAction) Build(ctx context.Context, b *TemplateBuilder) error {
-
        var r io.Reader
 
        switch a.Type {
        case file:
-               // 检查文件是否存在
                fi, err := os.Stat(a.Data)
                if os.IsNotExist(err) {
                        return err
@@ -171,24 +179,28 @@ func (a *dataAction) Build(ctx context.Context, b *TemplateBuilder) error {
                if err != nil {
                        return err
                }
-
        case data:
                if a.Data == "" {
                        return errors.New("data is empty")
                }
-               // 生成文件对象
                r = strings.NewReader(a.Data)
        default:
        }
 
-       // 连接ipfs节点
-       sh := ipfs.NewShell("localhost:5001")
+       sh := ipfs.NewShell(config.CommonConfig.IpfsAddress)
        cid, err := sh.Add(r)
        if err != nil {
                return err
        }
 
-       fmt.Println(cid)
+       program, err := vmutil.RetireProgram([]byte(cid))
+       if err != nil {
+               return err
+       }
+       out := types.NewTxOutput(*a.AssetId, 0, program)
+       return b.AddOutput(out)
+}
 
-       return nil
+func (a *dataAction) ActionType() string {
+       return "ipfs_data"
 }
index 9de666f..3749f7a 100644 (file)
@@ -71,3 +71,9 @@ type IntegerArgument struct {
 type BoolArgument struct {
        Value bool `json:"value"`
 }
+
+// ContractArgument for smart contract
+type IpfsData struct {
+       Type string `json:"type"`
+       Data string `json:"data"`
+}
index fdf7172..dff5e3f 100644 (file)
@@ -69,6 +69,9 @@ func init() {
 
        runNodeCmd.Flags().String("consensus_config_file", config.ConsensusConfigFile, "consensus configuration file")
 
+       // ipfs address
+       runNodeCmd.Flags().String("IpfsAddress", config.IpfsAddress, "Connect ipfs (eg. 127.0.0.1:5001)")
+
        RootCmd.AddCommand(runNodeCmd)
 }
 
index 13d8629..7e38217 100644 (file)
@@ -92,6 +92,8 @@ type BaseConfig struct {
        Signer        string `mapstructure:"signer"`
 
        ConsensusConfigFile string `mapstructure:"consensus_config_file"`
+
+       IpfsAddress string `mapstructure:"ipfs_addr"`
 }
 
 // Default configurable base parameters.
@@ -103,6 +105,7 @@ func DefaultBaseConfig() BaseConfig {
                DBBackend:         "leveldb",
                DBPath:            "data",
                KeysPath:          "keystore",
+               IpfsAddress:       "127.0.0.1:5001",
        }
 }
 
index d975183..d46f06d 100644 (file)
@@ -344,7 +344,7 @@ func bytomdRPCCheck() bool {
                        json.Unmarshal(tmp, &blockHeader)
                        hash := blockHeader.BlockHeader.Hash()
                        if strings.Compare(consensus.ActiveNetParams.ParentGenesisBlockHash, hash.String()) != 0 {
-                               log.Error("Invalid parent genesis block hash response via RPC. Contacting wrong parent daemon?", consensus.ActiveNetParams.ParentGenesisBlockHash, hash.String())
+                               log.Error("Invalid parent genesis block hash response via RPC. Contacting wrong parent daemon?", consensus.ActiveNetParams.ParentGenesisBlockHash, ":", hash.String())
                                return false
                        }
                        break