github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/action_migrate.go (about)

     1  // Copyright (C) 2013-2018, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
     2  // Use of this source code is governed by GPLv3 found in the LICENSE file
     3  //----------------------------------------------------------------------------------------
     4  //
     5  package holochain
     6  
     7  import (
     8  	. "github.com/holochain/holochain-proto/hash"
     9  	peer "github.com/libp2p/go-libp2p-peer"
    10  )
    11  
    12  //------------------------------------------------------------
    13  // Migrate Action
    14  
    15  type ActionMigrate struct {
    16  	entry  MigrateEntry
    17  	header *Header
    18  }
    19  
    20  func (a *ActionMigrate) Name() string {
    21  	return "migrate"
    22  }
    23  
    24  func (a *ActionMigrate) Entry() Entry {
    25  	j, err := a.entry.ToJSON()
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  	return &GobEntry{C: j}
    30  }
    31  
    32  func (a *ActionMigrate) EntryType() string {
    33  	return MigrateEntryType
    34  }
    35  
    36  func (a *ActionMigrate) SetHeader(header *Header) {
    37  	a.header = header
    38  }
    39  
    40  func (a *ActionMigrate) GetHeader() (header *Header) {
    41  	return a.header
    42  }
    43  
    44  func (action *ActionMigrate) Share(h *Holochain, def *EntryDef) (err error) {
    45  	err = h.dht.Change(action.header.EntryLink, PUT_REQUEST, HoldReq{EntryHash: action.header.EntryLink})
    46  	return
    47  }
    48  
    49  func (action *ActionMigrate) SysValidation(h *Holochain, def *EntryDef, pkg *Package, sources []peer.ID) (err error) {
    50  	// correct entry def
    51  	if def != MigrateEntryDef {
    52  		err = ErrEntryDefInvalid
    53  		return
    54  	}
    55  	// has a header
    56  	if action.header == nil {
    57  		err = ErrActionMissingHeader
    58  		return
    59  	}
    60  	// entry is valid
    61  	err = sysValidateEntry(h, def, action.Entry(), pkg)
    62  	// @TODO should migration only be valid if peer ID is node owner?
    63  	return
    64  }
    65  
    66  func (a *ActionMigrate) CheckValidationRequest(def *EntryDef) (err error) {
    67  	// intentionally left blank ;)
    68  	return
    69  }
    70  
    71  func (a *ActionMigrate) Receive(dht *DHT, msg *Message) (response interface{}, err error) {
    72  	// this is always an error because there is no action message for migrate
    73  	err = ErrActionReceiveInvalid
    74  	return
    75  }
    76  
    77  //------------------------------------------------------------
    78  // Migrate API fn
    79  
    80  type APIFnMigrate struct {
    81  	action ActionMigrate
    82  }
    83  
    84  func (fn *APIFnMigrate) Name() string {
    85  	return fn.action.Name()
    86  }
    87  
    88  func (fn *APIFnMigrate) Args() []Arg {
    89  	return []Arg{{Name: "migrationType",
    90  		Type: StringArg},
    91  		{Name: "DNAHash",
    92  			Type: HashArg},
    93  		{Name: "Key",
    94  			Type: HashArg},
    95  		{Name: "data",
    96  			Type: StringArg}}
    97  }
    98  
    99  func (fn *APIFnMigrate) Call(h *Holochain) (response interface{}, err error) {
   100  	var hash Hash
   101  	response, err = h.commitAndShare(&fn.action, hash)
   102  	return
   103  }