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

     1  package holochain
     2  
     3  import (
     4  	. "github.com/holochain/holochain-proto/hash"
     5  	peer "github.com/libp2p/go-libp2p-peer"
     6  )
     7  
     8  //------------------------------------------------------------
     9  // Commit
    10  
    11  type APIFnCommit struct {
    12  	action ActionCommit
    13  }
    14  
    15  func (fn *APIFnCommit) Name() string {
    16  	return fn.action.Name()
    17  }
    18  
    19  func (fn *APIFnCommit) Args() []Arg {
    20  	return []Arg{{Name: "entryType", Type: StringArg}, {Name: "entry", Type: EntryArg}}
    21  }
    22  
    23  func (fn *APIFnCommit) Call(h *Holochain) (response interface{}, err error) {
    24  	response, err = h.commitAndShare(&fn.action, NullHash())
    25  	return
    26  }
    27  
    28  func (fn *APIFnCommit) SetAction(a *ActionCommit) {
    29  	fn.action = *a
    30  }
    31  
    32  type ActionCommit struct {
    33  	entryType string
    34  	entry     Entry
    35  	header    *Header
    36  }
    37  
    38  func NewCommitAction(entryType string, entry Entry) *ActionCommit {
    39  	a := ActionCommit{entryType: entryType, entry: entry}
    40  	return &a
    41  }
    42  
    43  func (a *ActionCommit) Entry() Entry {
    44  	return a.entry
    45  }
    46  
    47  func (a *ActionCommit) EntryType() string {
    48  	return a.entryType
    49  }
    50  
    51  func (a *ActionCommit) Name() string {
    52  	return "commit"
    53  }
    54  
    55  func (a *ActionCommit) SetHeader(header *Header) {
    56  	a.header = header
    57  }
    58  
    59  func (a *ActionCommit) GetHeader() (header *Header) {
    60  	return a.header
    61  }
    62  
    63  func (a *ActionCommit) Share(h *Holochain, def *EntryDef) (err error) {
    64  	if def.DataFormat == DataFormatLinks {
    65  		// if this is a Link entry we have to send the DHT Link message
    66  		var le LinksEntry
    67  		entryStr := a.entry.Content().(string)
    68  		le, err = LinksEntryFromJSON(entryStr)
    69  		if err != nil {
    70  			return
    71  		}
    72  
    73  		bases := make(map[string]bool)
    74  		for _, l := range le.Links {
    75  			_, exists := bases[l.Base]
    76  			if !exists {
    77  				b, _ := NewHash(l.Base)
    78  				h.dht.Change(b, LINK_REQUEST, HoldReq{RelatedHash: b, EntryHash: a.header.EntryLink})
    79  				//TODO errors from the send??
    80  				bases[l.Base] = true
    81  			}
    82  		}
    83  	}
    84  	if def.isSharingPublic() {
    85  		// otherwise we check to see if it's a public entry and if so send the DHT put message
    86  		err = h.dht.Change(a.header.EntryLink, PUT_REQUEST, HoldReq{EntryHash: a.header.EntryLink})
    87  		if err == ErrEmptyRoutingTable {
    88  			// will still have committed locally and can gossip later
    89  			err = nil
    90  		}
    91  	}
    92  	return
    93  }
    94  
    95  func (a *ActionCommit) SysValidation(h *Holochain, def *EntryDef, pkg *Package, sources []peer.ID) (err error) {
    96  	err = sysValidateEntry(h, def, a.entry, pkg)
    97  	return
    98  }
    99  
   100  func (a *ActionCommit) Receive(dht *DHT, msg *Message) (response interface{}, err error) {
   101  	err = NonDHTAction
   102  	return
   103  }
   104  
   105  func (a *ActionCommit) CheckValidationRequest(def *EntryDef) (err error) {
   106  	return
   107  }