Skip to content

Commit

Permalink
docstrings and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
deelawn committed Jan 24, 2024
1 parent 3a43959 commit 02920e5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
24 changes: 21 additions & 3 deletions examples/gno.land/r/gnoland/ghverify/contract.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

const (
// The agent should send this value if it has verified the github handle.
verifiedResult = "OK"
)

Expand All @@ -30,6 +31,8 @@ func init() {

type postGnorkleMessageHandler struct{}

// Handle does post processing after a message is ingested by the oracle feed. It extracts the value to realm
// storage and removes the feed from the oracle.
func (h postGnorkleMessageHandler) Handle(i *gnorkle.Instance, funcType message.FuncType, feed gnorkle.Feed) {
if funcType != message.FuncTypeIngest {
return
Expand All @@ -40,8 +43,11 @@ func (h postGnorkleMessageHandler) Handle(i *gnorkle.Instance, funcType message.
return
}

// The value is consumable, meaning the ingestion occurred, so we can remove the feed from the oracle
// after saving it to realm storage.
defer oracle.RemoveFeed(feed.ID())

// Couldn't verify; nothing to do.
if result.String != verifiedResult {
return
}
Expand All @@ -56,24 +62,33 @@ func (h postGnorkleMessageHandler) Handle(i *gnorkle.Instance, funcType message.
panic("expected ghverify task")
}

handleToAddressMap.Set(task.GithubHandle(), task.GnoAddress())
addressToHandleMap.Set(task.GnoAddress(), task.GithubHandle())
handleToAddressMap.Set(task.githubHandle, task.gnoAddress)
addressToHandleMap.Set(task.gnoAddress, task.githubHandle)
}

// RequestVerification creates a new static feed with a single task that will
// instruct an agent to verify the github handle / gno address pair.
func RequestVerification(githubHandle string) {
oracle.AddFeeds(
static.NewSingleValueFeed(
githubHandle,
"string",
[]feed.Task{NewVerificationTask(string(std.GetOrigCaller()), githubHandle)},
[]feed.Task{
&verificationTask{
gnoAddress: string(std.GetOrigCaller()),
githubHandle: githubHandle,
},
},
),
)
}

// GnorkleEntrypoint is the entrypoint to the gnorkle oracle handler.
func GnorkleEntrypoint(message string) string {
return oracle.HandleMessage(message, postHandler)
}

// SetOwner transfers ownership of the contract to the given address.
func SetOwner(owner string) {
if ownerAddress != string(std.GetOrigCaller()) {
panic("only the owner can set a new owner")
Expand All @@ -87,16 +102,19 @@ func SetOwner(owner string) {
oracle.AddToWhitelist("", []string{ownerAddress})
}

// GetHandleByAddress returns the github handle associated with the given gno address.
func GetHandleByAddress(address string) string {
value, _ := addressToHandleMap.Get(address)
return value.(string)
}

// GetAddressByHandle returns the gno address associated with the given github handle.
func GetAddressByHandle(handle string) string {
value, _ := handleToAddressMap.Get(handle)
return value.(string)
}

// Render returns a json object string will all verified handle -> address mappings.
func Render(_ string) string {
result := "{"
var appendComma bool
Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/r/gnoland/ghverify/contract_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestVerificationLifecycle(t *testing.T) {
// invocation.
std.TestSetOrigCaller(defaultAddress)
result = GnorkleEntrypoint("request")
expResult := `[{"id":"deelawn","type":"0","value_type":"string","tasks":[{"gnoAddress":"` +
string(userAddress) + `","githubHandle":"deelawn"}]}]`
expResult := `[{"id":"deelawn","type":"0","value_type":"string","tasks":[{"gno_address":"` +
string(userAddress) + `","github_handle":"deelawn"}]}]`
if result != expResult {
t.Fatalf("expected request result %s, got %s", expResult, result)
}
Expand Down
18 changes: 2 additions & 16 deletions examples/gno.land/r/gnoland/ghverify/task.gno
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,15 @@ type verificationTask struct {
githubHandle string
}

func NewVerificationTask(gnoAddress, githubHandle string) *verificationTask {
return &verificationTask{
gnoAddress: gnoAddress,
githubHandle: githubHandle,
}
}

// MarshalToJSON marshals the task contents to JSON.
func (t *verificationTask) MarshalToJSON() ([]byte, error) {
buf := new(bytes.Buffer)
w := bufio.NewWriter(buf)

w.Write(
[]byte(`{"gnoAddress":"` + t.gnoAddress + `","githubHandle":"` + t.githubHandle + `"}`),
[]byte(`{"gno_address":"` + t.gnoAddress + `","github_handle":"` + t.githubHandle + `"}`),
)

w.Flush()
return buf.Bytes(), nil
}

func (t *verificationTask) GnoAddress() string {
return t.gnoAddress
}

func (t *verificationTask) GithubHandle() string {
return t.githubHandle
}

0 comments on commit 02920e5

Please sign in to comment.