This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
modify.go
91 lines (78 loc) · 2.54 KB
/
modify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package actions
import (
"context"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/utils"
"github.com/ava-labs/indexvm/auth"
"github.com/ava-labs/indexvm/storage"
)
var _ chain.Action = (*Modify)(nil)
// Modify will update the Royalty of any claimed content or claim unclaimed
// content
//
// This allows content creators to update previously indexed info without
// unindexing im.
type Modify struct {
// Content is the content to update
Content ids.ID `json:"content"`
// Royaly is the new value to apply to the content
Royalty uint64 `json:"royalty"`
}
func (m *Modify) StateKeys(chain.Auth) [][]byte {
return [][]byte{storage.PrefixContentKey(m.Content)}
}
func (m *Modify) Execute(
ctx context.Context,
r chain.Rules,
db chain.Database,
_ int64,
rauth chain.Auth,
_ ids.ID,
) (*chain.Result, error) {
actor := auth.GetActor(rauth)
unitsUsed := m.MaxUnits(r) // max units == units
if m.Content == ids.Empty {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputContentEmpty}, nil
}
if m.Royalty == 0 {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputInvalidObject}, nil
}
owner, oldRoyalty, exists, err := storage.GetContent(ctx, db, m.Content)
if err != nil {
return &chain.Result{Success: false, Units: unitsUsed, Output: utils.ErrBytes(err)}, nil
}
if !exists {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputContentMissing}, nil
}
if owner != actor {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputWrongOwner}, nil
}
if oldRoyalty == m.Royalty {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputInvalidObject}, nil
}
if err := storage.SetContent(ctx, db, m.Content, actor, m.Royalty); err != nil {
return &chain.Result{Success: false, Units: unitsUsed, Output: utils.ErrBytes(err)}, nil
}
return &chain.Result{Success: true, Units: unitsUsed}, nil
}
func (*Modify) MaxUnits(chain.Rules) uint64 {
return consts.IDLen + consts.Uint64Len
}
func (m *Modify) Marshal(p *codec.Packer) {
p.PackID(m.Content)
p.PackUint64(m.Royalty)
}
func UnmarshalModify(p *codec.Packer) (chain.Action, error) {
var modify Modify
p.UnpackID(true, &modify.Content)
modify.Royalty = p.UnpackUint64(true)
return &modify, p.Err()
}
func (*Modify) ValidRange(chain.Rules) (int64, int64) {
return -1, -1
}