Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add transfer_ticket. Refactoring: receiver #983

Merged
merged 5 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions cmd/api/handlers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"net/http"

"github.com/baking-bad/bcdhub/internal/bcd"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -39,20 +40,24 @@ func GetInfo() gin.HandlerFunc {
if handleError(c, ctx.Storage, err, 0) {
return
}
block, err := ctx.Blocks.Last()
if handleError(c, ctx.Storage, err, 0) {
return
}
balance, err := ctx.Cache.TezosBalance(c, acc.Address, block.Level)
if handleError(c, ctx.Storage, err, 0) {
return
var balance int64
if !(bcd.IsRollupAddressLazy(acc.Address) || bcd.IsSmartRollupAddressLazy(acc.Address)) {
block, err := ctx.Blocks.Last()
if handleError(c, ctx.Storage, err, 0) {
return
}
balance, err = ctx.Cache.TezosBalance(c, acc.Address, block.Level)
if handleError(c, ctx.Storage, err, 0) {
return
}
}
c.SecureJSON(http.StatusOK, AccountInfo{
Address: acc.Address,
Alias: acc.Alias,
TxCount: stats.Count,
Balance: balance,
LastAction: stats.LastAction.UTC(),
Address: acc.Address,
Alias: acc.Alias,
TxCount: stats.Count,
Balance: balance,
LastAction: stats.LastAction.UTC(),
AccountType: acc.Type.String(),
})
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/api/handlers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func ContractsHelpers() gin.HandlerFunc {
c.SecureJSON(http.StatusOK, response)
}
}

func getStringPointer(s string) *string {
return &s
}
73 changes: 59 additions & 14 deletions cmd/api/handlers/operations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"encoding/hex"
"net/http"
"strings"

Expand Down Expand Up @@ -305,11 +306,13 @@ func GetOperationDiff() gin.HandlerFunc {
return
}

bmd, err := ctx.BigMapDiffs.GetForOperation(operation.ID)
if handleError(c, ctx.Storage, err, 0) {
return
var bmd []bigmapdiff.BigMapDiff
if operation.BigMapDiffsCount > 0 {
bmd, err = ctx.BigMapDiffs.GetForOperation(operation.ID)
if handleError(c, ctx.Storage, err, 0) {
return
}
}

if err := setStorageDiff(ctx, operation.DestinationID, operation.DeffatedStorage, &result, bmd, storageType); handleError(c, ctx.Storage, err, 0) {
return
}
Expand Down Expand Up @@ -496,27 +499,55 @@ func prepareOperation(ctx *config.Context, operation operation.Operation, bmd []
}
op.Protocol = proto.Hash

if operation.IsEvent() {
eventType, err := ast.NewTypedAstFromBytes(operation.EventType)
if err := formatErrors(operation.Errors, &op); err != nil {
return op, err
}

switch operation.Kind {
case modelTypes.OperationKindEvent, modelTypes.OperationKindTransferTicket:
payloadType, err := ast.NewTypedAstFromBytes(operation.PayloadType)
if err != nil {
return op, err
}
if err := eventType.SettleFromBytes(operation.EventPayload); err != nil {
if err := payloadType.SettleFromBytes(operation.Payload); err != nil {
return op, err
}
eventMiguel, err := eventType.ToMiguel()
payloadMiguel, err := payloadType.ToMiguel()
if err != nil {
return op, err
}
op.Event = eventMiguel
op.Payload = payloadMiguel
return op, err
case modelTypes.OperationKindSrExecuteOutboxMessage:
if len(operation.Payload) >= 32 {
commitment, err := encoding.EncodeBase58(operation.Payload[:32], []byte(encoding.PrefixSmartRollupCommitment))
if err != nil {
return op, err
}
op.Payload = []*ast.MiguelNode{
{
Prim: "pair",
Type: "namedtuple",
Children: []*ast.MiguelNode{
{
Prim: "string",
Type: "string",
Name: getStringPointer("cemented_commitment"),
Value: commitment,
}, {
Prim: "bytes",
Type: "bytes",
Name: getStringPointer("output_proof"),
Value: hex.EncodeToString(operation.Payload[32:]),
},
},
},
}
}

}

if bcd.IsContract(op.Destination) {
if err := formatErrors(operation.Errors, &op); err != nil {
return op, err
}

if withStorageDiff {
storageType, err := getStorageType(ctx.Contracts, op.Destination, proto.SymLink)
if err != nil {
Expand Down Expand Up @@ -544,6 +575,20 @@ func prepareOperation(ctx *config.Context, operation operation.Operation, bmd []
}
}

if bcd.IsSmartRollupHash(op.Destination) && operation.IsTransaction() && operation.IsCall() && !tezerrors.HasParametersError(op.Errors) {
rollup, err := ctx.SmartRollups.Get(op.Destination)
if err != nil {
return op, err
}
tree, err := ast.NewTypedAstFromBytes(rollup.Type)
if err != nil {
return op, err
}
if err := setParameters(operation.Parameters, tree, &op); err != nil {
return op, err
}
}

return op, nil
}

Expand All @@ -554,7 +599,7 @@ func PrepareOperations(ctx *config.Context, ops []operation.Operation, withStora
var diffs []bigmapdiff.BigMapDiff
var err error

if withStorageDiff {
if withStorageDiff && ops[i].BigMapDiffsCount > 0 {
diffs, err = ctx.BigMapDiffs.GetForOperation(ops[i].ID)
if err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions cmd/api/handlers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,13 @@ type getViewsArgs struct {
type findContract struct {
Tags string `form:"tags" binding:"omitempty"`
}

type smartRollupListRequest struct {
pageableRequest

Sort string `form:"sort" binding:"omitempty,oneof=asc desc"`
}

type getSmartRollupRequest struct {
Address string `uri:"address" binding:"required,smart_rollup"`
}
53 changes: 44 additions & 9 deletions cmd/api/handlers/responses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"encoding/hex"
stdJSON "encoding/json"
"time"

Expand All @@ -13,6 +14,7 @@ import (
"github.com/baking-bad/bcdhub/internal/models/contract"
"github.com/baking-bad/bcdhub/internal/models/operation"
"github.com/baking-bad/bcdhub/internal/models/protocol"
smartrollup "github.com/baking-bad/bcdhub/internal/models/smart_rollup"
"github.com/baking-bad/bcdhub/internal/models/ticket"
"github.com/baking-bad/bcdhub/internal/models/types"
)
Expand All @@ -39,10 +41,12 @@ type Operation struct {
ConsumedGas int64 `json:"consumed_gas,omitempty" extensions:"x-nullable" example:"100"`
StorageSize int64 `json:"storage_size,omitempty" extensions:"x-nullable" example:"200"`
PaidStorageSizeDiff int64 `json:"paid_storage_size_diff,omitempty" extensions:"x-nullable" example:"300"`
TicketUpdatesCount int `json:"ticket_updates_count"`
BigMapDiffsCount int `json:"big_map_diffs_count"`
Errors []*tezerrors.Error `json:"errors,omitempty" extensions:"x-nullable"`
Parameters interface{} `json:"parameters,omitempty" extensions:"x-nullable"`
StorageDiff *ast.MiguelNode `json:"storage_diff,omitempty" extensions:"x-nullable"`
Event []*ast.MiguelNode `json:"event,omitempty" extensions:"x-nullable"`
Payload []*ast.MiguelNode `json:"payload,omitempty" extensions:"x-nullable"`
RawMempool interface{} `json:"rawMempool,omitempty" extensions:"x-nullable"`
Timestamp time.Time `json:"timestamp"`
Protocol string `json:"protocol"`
Expand Down Expand Up @@ -96,6 +100,8 @@ func (o *Operation) FromModel(operation operation.Operation) {
o.StorageSize = operation.StorageSize
o.PaidStorageSizeDiff = operation.PaidStorageSizeDiff
o.AllocatedDestinationContract = operation.AllocatedDestinationContract
o.TicketUpdatesCount = operation.TicketUpdatesCount
o.BigMapDiffsCount = operation.BigMapDiffsCount
}

// ToModel -
Expand Down Expand Up @@ -493,11 +499,12 @@ type Screenshot struct {

// AccountInfo -
type AccountInfo struct {
Address string `json:"address"`
Alias string `json:"alias,omitempty" extensions:"x-nullable"`
Balance int64 `json:"balance"`
TxCount int64 `json:"tx_count"`
LastAction time.Time `json:"last_action"`
Address string `json:"address"`
Alias string `json:"alias,omitempty" extensions:"x-nullable"`
Balance int64 `json:"balance"`
TxCount int64 `json:"tx_count"`
LastAction time.Time `json:"last_action"`
AccountType string `json:"account_type"`
}

// CountResponse -
Expand Down Expand Up @@ -643,7 +650,7 @@ type Event struct {

// NewEvent -
func NewEvent(o operation.Operation) (*Event, error) {
if !o.IsEvent() {
if o.Kind != types.OperationKindEvent {
return nil, nil
}

Expand All @@ -660,11 +667,11 @@ func NewEvent(o operation.Operation) (*Event, error) {
Tag: o.Tag.String(),
}

eventType, err := ast.NewTypedAstFromBytes(o.EventType)
eventType, err := ast.NewTypedAstFromBytes(o.PayloadType)
if err != nil {
return nil, err
}
if err := eventType.SettleFromBytes(o.EventPayload); err != nil {
if err := eventType.SettleFromBytes(o.Payload); err != nil {
return nil, err
}
eventMiguel, err := eventType.ToMiguel()
Expand Down Expand Up @@ -699,3 +706,31 @@ func NewTicketUpdateFromModel(update ticket.TicketUpdate) TicketUpdate {
Amount: update.Amount.String(),
}
}

// SmartRollup -
type SmartRollup struct {
ID int64 `json:"id"`
Level int64 `json:"level"`
Timestamp time.Time `json:"timestamp"`
Size uint64 `json:"size"`
Address string `json:"address"`
GenesisCommitmentHash string `json:"genesis_commitment_hash"`
PvmKind string `json:"pvm_kind"`
Kernel string `json:"kernel"`
Type []ast.Typedef `json:"type"`
}

// NewSmartRollup -
func NewSmartRollup(rollup smartrollup.SmartRollup) SmartRollup {
kernel := hex.EncodeToString(rollup.Kernel)
return SmartRollup{
ID: rollup.ID,
Level: rollup.Level,
Timestamp: rollup.Timestamp,
Size: rollup.Size,
Address: rollup.Address.Address,
GenesisCommitmentHash: rollup.GenesisCommitmentHash,
PvmKind: rollup.PvmKind,
Kernel: kernel,
}
}
98 changes: 98 additions & 0 deletions cmd/api/handlers/smart_rollup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package handlers

import (
"net/http"

"github.com/baking-bad/bcdhub/internal/bcd/ast"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/gin-gonic/gin"
)

// GetSmartRollup godoc
// @Summary Get smart rollup
// @Description Get smart rollup
// @Tags smart-rollups
// @ID get-smart-rollups
// @Param network path string true "network"
// @Param address path string true "expr address of smart rollup" minlength(36) maxlength(36)
// @Accept json
// @Produce json
// @Success 200 {object} SmartRollup
// @Failure 400 {object} Error
// @Failure 404 {object} Error
// @Failure 500 {object} Error
// @Router /v1/smart_rollups/{network}/{address} [get]
func GetSmartRollup() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.MustGet("context").(*config.Context)

var req getSmartRollupRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

rollup, err := ctx.SmartRollups.Get(req.Address)
if handleError(c, ctx.Storage, err, 0) {
return
}

response := NewSmartRollup(rollup)
typ, err := ast.NewTypedAstFromBytes(rollup.Type)
if handleError(c, ctx.Storage, err, 0) {
return
}
docs, err := typ.Docs("")
if handleError(c, ctx.Storage, err, 0) {
return
}
response.Type = docs
c.SecureJSON(http.StatusOK, response)
}
}

// ListSmartRollups godoc
// @Summary List smart rollups
// @Description List smart rollups
// @Tags smart-rollups
// @ID list-smart-rollups
// @Param network path string true "network"
// @Param size query integer false "Constants count" mininum(1) maximum(10)
// @Param offset query integer false "Offset" mininum(1)
// @Param sort query string false "Sort order" Enums(asc, desc)
// @Accept json
// @Produce json
// @Success 200 {array} SmartRollup
// @Failure 400 {object} Error
// @Failure 404 {object} Error
// @Failure 500 {object} Error
// @Router /v1/smart_rollups/{network} [get]
func ListSmartRollups() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.MustGet("context").(*config.Context)

var args smartRollupListRequest
if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

rollups, err := ctx.SmartRollups.List(args.Size, args.Offset, args.Sort)
if handleError(c, ctx.Storage, err, 0) {
return
}
response := make([]SmartRollup, len(rollups))
for i := range rollups {
response[i] = NewSmartRollup(rollups[i])

typ, err := ast.NewTypedAstFromBytes(rollups[i].Type)
if handleError(c, ctx.Storage, err, 0) {
return
}
docs, err := typ.Docs("")
if handleError(c, ctx.Storage, err, 0) {
return
}
response[i].Type = docs
}
c.SecureJSON(http.StatusOK, response)
}
}
Loading