Skip to content

Commit

Permalink
Fixed single member contracts and improved updater logic
Browse files Browse the repository at this point in the history
- Fixed the bug preventing resolving members of contracts with single members.
- Fixed a bug on updater logic. Updater wrote updated date to db before update check is successful. that prevented users check update after a failed update due to interrupt or network issues.
  • Loading branch information
tuhalf committed Aug 30, 2024
1 parent 34743cb commit b8e9200
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
3 changes: 0 additions & 3 deletions cmd/diode/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ func (dio *Diode) Init() error {
shouldUpdateDiode = diff.Hours() >= 24
}
if shouldUpdateDiode {
lastUpdateAt = time.Now()
lastUpdateAtByt = util.DecodeInt64ToBytes(lastUpdateAt.Unix())
db.DB.Put("last_update_at", lastUpdateAtByt)
doUpdate()
}
}
Expand Down
22 changes: 14 additions & 8 deletions cmd/diode/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ var (
}
)

func updateHandler() (err error) {
func writeLastUpdateAt() {
lastUpdateAt := time.Now()
lastUpdateAtByt := util.DecodeInt64ToBytes(lastUpdateAt.Unix())
db.DB.Put("last_update_at", lastUpdateAtByt)
}

func updateHandler() (err error) {
doUpdate()
return
}
Expand All @@ -54,13 +57,16 @@ func doUpdate() int {
m.Command += ".exe"
}

tarball, ok := download(m)
tarball, ok, err := download(m)
if !ok {
// Will recheck for an update in 24 hours
go func() {
time.Sleep(time.Hour * 24)
doUpdate()
}()
if err == nil {
writeLastUpdateAt()
}
return 0
}

Expand All @@ -85,12 +91,12 @@ func doUpdate() int {

cmd := path.Join(dir, m.Command)
fmt.Printf("Updated, restarting %s...\n", cmd)

writeLastUpdateAt()
update.Restart(cmd)
return 0
}

func download(m *update.Manager) (string, bool) {
func download(m *update.Manager) (string, bool, error) {
cfg := config.AppConfig
ansi.HideCursor()
defer ansi.ShowCursor()
Expand All @@ -101,13 +107,13 @@ func download(m *update.Manager) (string, bool) {
releases, err := m.LatestReleases()
if err != nil {
cfg.PrintInfo(fmt.Sprintf("Error fetching releases: %s", err))
return "", false
return "", false, err
}

// no updates
if len(releases) == 0 {
cfg.PrintInfo("No updates")
return "", false
return "", false, nil
}

// latest release
Expand All @@ -117,7 +123,7 @@ func download(m *update.Manager) (string, bool) {
a := latest.FindZip(runtime.GOOS, runtime.GOARCH)
if a == nil {
cfg.PrintInfo(fmt.Sprintf("No binary for your system (%s_%s)", runtime.GOOS, runtime.GOARCH))
return "", false
return "", false, nil
}

// whitespace
Expand All @@ -129,5 +135,5 @@ func download(m *update.Manager) (string, bool) {
cfg.PrintError("Error downloading", err)
}

return tarball, true
return tarball, true, nil
}
17 changes: 11 additions & 6 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,18 +1038,23 @@ func (client *Client) ResolveMembers(members Address) (addr []Address, err error
blockNumber, _ := client.LastValid()

var raw []byte

owner := client.GetAccountValueAddress(blockNumber, members, contract.OwnerLocation())
addr = append(addr, owner)

raw, err = client.GetAccountValueRaw(blockNumber, members, contract.MemberIndex())
// If this there is no such contract we assume
// this is a normal address
if err != nil {
addr = append(addr, members)
err = nil
return
//if owner is 0x0, return empty array
if owner == [20]byte{} {
return
} else {
addr = append(addr, members)
}
return addr, nil
}

owner := client.GetAccountValueAddress(blockNumber, members, contract.OwnerLocation())
addr = append(addr, owner)

var size big.Int
size.SetBytes(raw)
intSize := size.Int64()
Expand Down
8 changes: 7 additions & 1 deletion rpc/datapool.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,15 @@ func resolveAllPeersOfAddrs(members []Address, client *Client) (peers []Address)
// smart contract, might need to recurse
peers = append(peers, resolveAllPeersOfAddrs(members, client)...)
} else {
fmt.Printf("resolvedPeer: %x\n", maybePeerAddr)
// not a smart contract, instead a real (device) peer
peers = append(peers, maybePeerAddr)
// log peers
peersString := "["
for _, peer := range peers {
peersString += peer.HexString() + ", "
}
peersString = peersString[:len(peersString)-2] + "]"
client.Log().Debug("Resolved all peers of %s. Peers list is %v", maybePeerAddr.HexString(), peersString)
}
}
return peers
Expand Down

0 comments on commit b8e9200

Please sign in to comment.