Skip to content

Commit 27bb8bd

Browse files
committed
feat: github oracle
1 parent 3c661d8 commit 27bb8bd

File tree

7 files changed

+172
-23
lines changed

7 files changed

+172
-23
lines changed

examples/gno.land/r/demo/teritori/gh/account.gno

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package gh
22

3-
import "errors"
3+
4+
import (
5+
"std"
6+
"errors"
7+
"gno.land/p/demo/avl"
8+
)
49

510
// Account represents a GitHub user account or organization.
611
type Account struct {
@@ -27,7 +32,7 @@ func (a Account) Validate() error {
2732
if a.login == "" {
2833
return errors.New("empty login")
2934
}
30-
if a.kind == "" {
35+
if a.kind == "" || (a.kind != UserAccount && a.kind != OrgAccount){
3136
return errors.New("empty kind")
3237
}
3338
if a.name == "" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package gh
2+
3+
import (
4+
"std"
5+
)
6+
7+
var(
8+
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
9+
)
10+
11+
12+
func assertIsAdmin() {
13+
if std.GetOrigCaller() != adminAddr {
14+
panic("restricted area")
15+
}
16+
}
17+
18+
func setAdminAddress(address std.Address) {
19+
adminAddr = address
20+
}
21+
22+
func SetAdminAddress(address std.Address) {
23+
assertIsAdmin()
24+
setAdminAddress(address)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package gh
2+
3+
import (
4+
"std"
5+
"testing"
6+
7+
"gno.land/p/demo/avl"
8+
"gno.land/p/demo/testutils"
9+
)
10+
11+
func TestAssertIsAdmin(t *testing.T) {
12+
adminAddr = "g1test1234"
13+
randomuser := testutils.TestAddress("g1unknown_player")
14+
defer func() {
15+
if r := recover(); r != nil {
16+
}
17+
}()
18+
std.TestSetOrigCaller(randomuser)
19+
assertIsAdmin()
20+
t.Fatalf("should fail because not admin")
21+
}
22+

examples/gno.land/r/demo/teritori/gh/oracle.gno

+3-11
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import (
1111
var (
1212
accounts avl.Tree // uri -> Account
1313
lastUpdateTime time.Time // used by the bot to only upload the diff
14-
oracleAddr std.Address = "g1eunnckcl6r8ncwj0lrpxu9g5062xcvwxqlrf29"
15-
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
14+
oracleAddr std.Address
1615
)
1716

1817
func OracleLastUpdated() time.Time { return lastUpdateTime }
1918

20-
func OracleUpsertAccount(id, name, kind string) {
19+
func OracleUpsertAccount(id, login, name, kind string) {
2120
assertIsOracle()
2221
lastUpdateTime = time.Now()
2322

@@ -33,6 +32,7 @@ func OracleUpsertAccount(id, name, kind string) {
3332
// update fields
3433
account.name = name
3534
account.kind = kind
35+
account.login = login
3636

3737
if err := account.Validate(); err != nil {
3838
panic(err)
@@ -47,14 +47,6 @@ func AdminSetOracleAddr(new std.Address) {
4747
oracleAddr = new
4848
}
4949

50-
func AdminGetOracleAddr() std.Address { return oracleAddr }
51-
52-
func assertIsAdmin() {
53-
if std.GetOrigCaller() != adminAddr {
54-
panic("restricted area")
55-
}
56-
}
57-
5850
func assertIsOracle() {
5951
if std.GetOrigCaller() != oracleAddr {
6052
panic("restricted area")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package gh
2+
3+
import (
4+
"std"
5+
"testing"
6+
"time"
7+
8+
"gno.land/p/demo/avl"
9+
"gno.land/p/demo/testutils"
10+
)
11+
12+
func TestOracleUpsertUserIsNotOracle(t *testing.T) {
13+
oracleAddr = "g1test1234"
14+
var randomuser = "g1unknown_player"
15+
user := testutils.TestAddress("user")
16+
defer func() {
17+
if r := recover(); r != nil {
18+
}
19+
}()
20+
OracleUpsertAccount("acountID", "villaquiranm","john doe","user")
21+
t.Fatalf("should fail because not admin")
22+
}
23+
24+
func TestOracleUpsertUserOk(t *testing.T) {
25+
oracleAddr = "g1random"
26+
if accounts.Size() != 0 {
27+
t.Fatalf("Accounts is not empty")
28+
}
29+
now := time.Now()
30+
31+
std.TestSetOrigCaller(oracleAddr)
32+
33+
var randomuser = "g1unknown_player"
34+
OracleUpsertAccount("acountID", "villaquiranm","john doe","user")
35+
36+
if accounts.Size() != 1 {
37+
t.Fatalf("User was not created")
38+
}
39+
40+
OracleUpsertAccount("acountID", "villaquiranm","john doe","user")
41+
42+
if accounts.Size() != 1 {
43+
t.Fatalf("User was created more than once")
44+
}
45+
46+
if OracleLastUpdated().Unix() < now.Unix() {
47+
t.Fatalf("OracleLastUpdated was not changed")
48+
}
49+
}
50+
51+
func TestAssertIsOracle(t *testing.T) {
52+
std.TestSetOrigCaller(adminAddr)
53+
AdminSetOracleAddr("g1random123")
54+
defer func() {
55+
if r := recover(); r != nil {
56+
}
57+
}()
58+
assertIsOracle()
59+
t.Fatalf("should fail because user is not oracle")
60+
}

examples/gno.land/r/demo/teritori/gh/public.gno

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package gh
2+
3+
import (
4+
"std"
5+
"gno.land/p/demo/avl"
6+
)
27
var (
3-
addressesToAccount avl.Tree // address -> AccountLink
4-
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
8+
addressToAccount avl.Tree // address -> AccountLink
59
)
10+
11+
func init() {
12+
setAdminAddress(std.GetOrigCaller())
13+
}
14+
615
func LinkAccount(accountID string, address std.Address) {
716
assertIsAdmin()
8-
account := AccountByID(id string)
17+
account := AccountByID(accountID)
918
if account == nil {
1019
panic("account not found")
1120
}
1221

13-
addressesToAccount.Set(address, account)
22+
addressToAccount.Set(address.String(), account)
1423
}
15-
16-
func assertIsAdmin() {
17-
if std.GetOrigCaller() != adminAddr {
18-
panic("restricted area")
19-
}
20-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package gh
2+
3+
import (
4+
"std"
5+
"testing"
6+
7+
"gno.land/p/demo/avl"
8+
"gno.land/p/demo/testutils"
9+
)
10+
11+
func TestLinkAccountCallerNotAdmin(t *testing.T) {
12+
user := testutils.TestAddress("user")
13+
std.TestSetOrigCaller(user)
14+
defer func() {
15+
if r := recover(); r != nil {
16+
}
17+
}()
18+
LinkAccount("acountID", user)
19+
t.Fatalf("should fail because not admin")
20+
}
21+
22+
func TestLinkAccount(t *testing.T) {
23+
adminAddr = "g1test1234"
24+
25+
user := testutils.TestAddress("user")
26+
admin := testutils.TestAddress("admin")
27+
28+
std.TestSetOrigCaller(admin)
29+
setAdminAddress(admin)
30+
AdminSetOracleAddr(admin)
31+
32+
OracleUpsertAccount("123","user", "user", UserAccount)
33+
LinkAccount("123", user)
34+
accountInPublic, ok := addressToAccount.Get(user.String())
35+
if !ok {
36+
t.Fatalf("account not found")
37+
}
38+
account := AccountByID("123")
39+
if accountInPublic != account {
40+
t.Fatalf("account is not same")
41+
}
42+
}

0 commit comments

Comments
 (0)