Merge branch 'oracle_uuid'

This commit is contained in:
RustamOper05 2024-01-14 21:47:16 +03:00
commit 6bf5e6095c
8 changed files with 82 additions and 22 deletions

View file

@ -1,8 +1,13 @@
package Comment package Comment
import ( import (
<<<<<<< HEAD
//guuid "github.com/google/uuid" //guuid "github.com/google/uuid"
=======
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
>>>>>>> oracle_uuid
"github.com/nspcc-dev/neo-go/pkg/interop/native/std" "github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/storage"
) )
@ -17,8 +22,13 @@ type Comment struct {
func CreateNewComment(authorId string, postId string, text string) { func CreateNewComment(authorId string, postId string, text string) {
ctx := storage.GetContext() ctx := storage.GetContext()
newComment := Comment{ newComment := Comment{
<<<<<<< HEAD
id: "1", // guuid.New().String(), id: "1", // guuid.New().String(),
=======
id: "none",
>>>>>>> oracle_uuid
authorId: authorId, authorId: authorId,
postId: postId, postId: postId,
text: text, text: text,
@ -26,11 +36,32 @@ func CreateNewComment(authorId string, postId string, text string) {
dislikes: 0, dislikes: 0,
} }
storeURL := "https://www.uuidgenerator.net/api/version7"
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
newComment.id = storage.Get(ctx, "lastCommentId").(string)
comments := GetByPostId(postId) comments := GetByPostId(postId)
comments = append(comments, newComment) comments = append(comments, newComment)
storage.Put(ctx, postId+"_comment", std.Serialize(comments)) storage.Put(ctx, postId+"_comment", std.Serialize(comments))
} }
func cbGetUUID(url string, commentData any, code int, result []byte) {
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(oracle.Hash) {
panic("not called from the oracle contract")
}
if code != oracle.Success {
panic("request failed for " + url + " with code " + std.Itoa(code, 10))
}
runtime.Log("result for " + url + " is: " + string(result))
runtime.Log("Last Comment id is: " + string(result))
ctx := storage.GetContext()
storage.Put(ctx, "lastCommentId", string(result))
}
func GetByPostId(postId string) []Comment { func GetByPostId(postId string) []Comment {
ctx := storage.GetContext() ctx := storage.GetContext()
return std.Deserialize(storage.Get(ctx, postId+"_comment").([]byte)).([]Comment) return std.Deserialize(storage.Get(ctx, postId+"_comment").([]byte)).([]Comment)

View file

@ -1 +0,0 @@
{"name":"comment","abi":{"methods":[{"name":"createNewComment","offset":0,"parameters":[{"name":"authorId","type":"String"},{"name":"postId","type":"String"},{"name":"text","type":"String"}],"returntype":"Void","safe":false},{"name":"getByAuthorId","offset":109,"parameters":[{"name":"postId","type":"String"},{"name":"authorId","type":"String"}],"returntype":"Array","safe":false},{"name":"getByPostId","offset":61,"parameters":[{"name":"postId","type":"String"}],"returntype":"Array","safe":false},{"name":"getComment","offset":157,"parameters":[{"name":"commentId","type":"String"},{"name":"postId","type":"String"}],"returntype":"Array","safe":false},{"name":"rateComment","offset":259,"parameters":[{"name":"isLike","type":"Boolean"},{"name":"postId","type":"String"},{"name":"commentId","type":"String"}],"returntype":"Void","safe":false},{"name":"updateComment","offset":292,"parameters":[{"name":"comment","type":"Array"},{"name":"postId","type":"String"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}

Binary file not shown.

View file

@ -1,10 +1,9 @@
package Post package Post
import ( import (
"strconv" "github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
guuid "github.com/google/uuid"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std" "github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/storage"
) )
@ -23,13 +22,6 @@ const (
lastIndex = "_lastIndex" lastIndex = "_lastIndex"
) )
/*
func _deploy(data interface{}, isUpdate bool, userId string) {
if isUpdate {
return
}
}
*/
func NewPost(authorId string, text string, postName string) { func NewPost(authorId string, text string, postName string) {
ctx := storage.GetContext() ctx := storage.GetContext()
@ -40,22 +32,48 @@ func NewPost(authorId string, text string, postName string) {
likes: 0, likes: 0,
category: "none", category: "none",
postName: postName, postName: postName,
id: guuid.New().String(), id: "none",
} }
storeURL := "https://www.uuidgenerator.net/api/version7"
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
post.id = storage.Get(ctx, "lastPostId").(string)
storage.Put(ctx, post.id, std.Serialize(post)) storage.Put(ctx, post.id, std.Serialize(post))
updatePostIndex(authorId) updatePostIndex(authorId)
storage.Put(ctx, authorId+strconv.Itoa(getPostIndex(authorId)), post.id) lastPostIndex := storage.Get(ctx, authorId+lastIndex)
storage.Put(ctx, authorId+lastPostIndex.(string), post.id)
} }
func CbGetUUID(url string, postData any, code int, result []byte) {
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(oracle.Hash) {
panic("not called from the oracle contract")
}
if code != oracle.Success {
panic("request failed for " + url + " with code " + std.Itoa(code, 10))
}
runtime.Log("result for " + url + " is: " + string(result))
ctx := storage.GetContext()
storage.Put(ctx, "lastPostId", string(result))
}
func GetPost(postId string) Post { func GetPost(postId string) Post {
ctx := storage.GetReadOnlyContext() ctx := storage.GetReadOnlyContext()
data := storage.Get(ctx, postId) data := storage.Get(ctx, postId)
if data == nil { if data == nil {
<<<<<<< HEAD
panic("user not found") panic("user not found")
=======
panic("post not found")
>>>>>>> oracle_uuid
} }
return std.Deserialize(data.([]byte)).(Post) return std.Deserialize(data.([]byte)).(Post)
} }
func RatePost(isLike bool, postId string) { func RatePost(isLike bool, postId string) {
@ -79,5 +97,4 @@ func updatePostIndex(userId string) {
ctx := storage.GetContext() ctx := storage.GetContext()
index := getPostIndex(userId) index := getPostIndex(userId)
storage.Put(ctx, userId+lastIndex, index+1) storage.Put(ctx, userId+lastIndex, index+1)
} }

6
Post/post_contract.yml Normal file
View file

@ -0,0 +1,6 @@
name: Post
safemethods: []
supportedstandards: []
events:
permissions:
- methods: "*"

View file

@ -3,6 +3,7 @@ package Users
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std" "github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/storage"
) )
@ -14,6 +15,10 @@ type User struct {
ownerHash interop.Hash160 ownerHash interop.Hash160
} }
const (
lastIndex = "_lastIndex"
)
func NewUser(name string, surname string, login string, password string, owner interop.Hash160) { func NewUser(name string, surname string, login string, password string, owner interop.Hash160) {
ctx := storage.GetContext() ctx := storage.GetContext()
@ -44,7 +49,8 @@ func GetUser(ctx storage.Context, login string) User {
} }
func saveUser(ctx storage.Context, userLogin string, user User) { func saveUser(ctx storage.Context, userLogin string, user User) {
storage.Put(ctx, user.ownerHash, std.Serialize(user)) runtime.Log("User " + userLogin + " was created")
storage.Put(ctx, string(user.ownerHash)+"LastPost", -1)
storage.Put(ctx, string(user.ownerHash)+"LastCom", -1) storage.Put(ctx, userLogin+lastIndex, -1)
storage.Put(ctx, userLogin, std.Serialize(user))
} }

4
go.mod
View file

@ -2,9 +2,13 @@ module blog
go 1.21.5 go 1.21.5
<<<<<<< HEAD
require ( require (
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502 github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502
github.com/rs/xid v1.5.0 github.com/rs/xid v1.5.0
) )
require github.com/google/uuid v1.5.0 require github.com/google/uuid v1.5.0
=======
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502
>>>>>>> oracle_uuid

3
go.sum
View file

@ -1,5 +1,2 @@
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502 h1:HwR9fWkdJXCbsTnrb2Rm92xkDGdEM0VHvZV+7XJUXM0= github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502 h1:HwR9fWkdJXCbsTnrb2Rm92xkDGdEM0VHvZV+7XJUXM0=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag= github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=