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

fix(zcn): removed redundant json marshal on sc.Input: #707

Merged
merged 2 commits into from
Jun 1, 2022
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
10 changes: 1 addition & 9 deletions code/go/0chain.net/blobbercore/allocation/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,6 @@ type finalizeRequest struct {
AllocationID string `json:"allocation_id"`
}

func (fr *finalizeRequest) marshal() string {
var b, err = json.Marshal(fr)
if err != nil {
panic(err) // must never happens
}
return string(b)
}

func sendFinalizeAllocation(a *Allocation) {
var tx, err = transaction.NewTransactionEntity()
if err != nil {
Expand All @@ -267,7 +259,7 @@ func sendFinalizeAllocation(a *Allocation) {
err = tx.ExecuteSmartContract(
transaction.STORAGE_CONTRACT_ADDRESS,
transaction.FINALIZE_ALLOCATION,
request.marshal(),
request,
0)
if err != nil {
Logger.Error("sending finalize allocation", zap.Error(err))
Expand Down
7 changes: 1 addition & 6 deletions code/go/0chain.net/blobbercore/challenge/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ func (cr *ChallengeEntity) SubmitChallengeToBC(ctx context.Context) (*transactio
sn.ChallengeID = cr.ChallengeID
sn.ValidationTickets = cr.ValidationTickets

snBytes, err := json.Marshal(sn)
if err != nil {
return nil, err
}

err = txn.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS, transaction.CHALLENGE_RESPONSE, string(snBytes), 0)
err = txn.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS, transaction.CHALLENGE_RESPONSE, sn, 0)
if err != nil {
Logger.Info("Failed submitting challenge to the mining network", zap.String("err:", err.Error()))
return nil, err
Expand Down
8 changes: 1 addition & 7 deletions code/go/0chain.net/blobbercore/handler/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handler

import (
"context"
"encoding/json"
"errors"
"sync"
"time"
Expand Down Expand Up @@ -185,15 +184,10 @@ func sendSmartContractBlobberUpdate(ctx context.Context) (string, error) {
return "", err
}

snBytes, err := json.Marshal(sn)
if err != nil {
return "", err
}

logging.Logger.Info("Adding or updating on the blockchain")

err = txn.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS,
transaction.UPDATE_BLOBBER_SC_NAME, string(snBytes), 0)
transaction.UPDATE_BLOBBER_SC_NAME, sn, 0)
if err != nil {
logging.Logger.Error("Failed to set blobber on the blockchain",
zap.String("err:", err.Error()))
Expand Down
9 changes: 1 addition & 8 deletions code/go/0chain.net/blobbercore/readmarker/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package readmarker

import (
"context"
"encoding/json"
"time"

"github.com/0chain/blobber/code/go/0chain.net/core/chain"
Expand Down Expand Up @@ -50,13 +49,7 @@ func (rme *ReadMarkerEntity) RedeemReadMarker(ctx context.Context) (err error) {
ReadMarker: rme.LatestRM,
}

var snBytes []byte
if snBytes, err = json.Marshal(sn); err != nil {
zLogger.Logger.Error("Error encoding SC input", zap.Error(err), zap.Any("scdata", sn))
return common.NewErrorf("redeem_read_marker", "encoding SC data: %v", err)
}

err = tx.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS, transaction.READ_REDEEM, string(snBytes), 0)
err = tx.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS, transaction.READ_REDEEM, sn, 0)
if err != nil {
zLogger.Logger.Info("Failed submitting read redeem", zap.Error(err))
return common.NewErrorf("redeem_read_marker", "sending transaction: %v", err)
Expand Down
15 changes: 1 addition & 14 deletions code/go/0chain.net/blobbercore/writemarker/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package writemarker

import (
"context"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -98,19 +97,7 @@ func (wme *WriteMarkerEntity) RedeemMarker(ctx context.Context) error {
sn.PrevAllocationRoot = wme.WM.PreviousAllocationRoot
sn.WriteMarker = &wme.WM

snBytes, err := json.Marshal(sn)
if err != nil {
Logger.Error("Error encoding sc input", zap.String("err:", err.Error()), zap.Any("scdata", sn))
wme.Status = Failed
wme.StatusMessage = "Error encoding sc input. " + err.Error()
wme.ReedeemRetries++
if err := wme.UpdateStatus(ctx, Failed, "Error encoding sc input. "+err.Error(), ""); err != nil {
Logger.Error("WriteMarkerEntity_UpdateStatus", zap.Error(err))
}
return err
}

err = txn.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS, transaction.CLOSE_CONNECTION_SC_NAME, string(snBytes), 0)
err = txn.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS, transaction.CLOSE_CONNECTION_SC_NAME, sn, 0)
if err != nil {
Logger.Error("Failed during sending close connection to the miner. ", zap.String("err:", err.Error()))
wme.Status = Failed
Expand Down
4 changes: 2 additions & 2 deletions code/go/0chain.net/blobbercore/writemarker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func redeemWriteMarker(allocationObj *allocation.Allocation, wm *WriteMarkerEnti

defer func() {
if shouldRollback {
if err := db.Rollback(); err != nil {
if rollbackErr := db.Rollback().Error; rollbackErr != nil {
logging.Logger.Error("Error rollback on redeeming the write marker.",
zap.Any("wm", wm.WM.AllocationID), zap.Any("error", err))
zap.Any("wm", wm.WM.AllocationID), zap.Error(rollbackErr))
}
}
}()
Expand Down
6 changes: 3 additions & 3 deletions code/go/0chain.net/core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ func (t *Transaction) Verify() error {
var objmap map[string]json.RawMessage
err = json.Unmarshal([]byte(output), &objmap)
if err != nil {
return common.NewError("transaction_verify_error", "Error unmarshaling verify output. "+err.Error())
return common.NewError("transaction_verify_error", "Error unmarshaling verify output: "+string(output)+" "+err.Error())
}

err = json.Unmarshal(objmap["txn"], t)
if err != nil {
var confirmation map[string]json.RawMessage
err = json.Unmarshal(objmap["confirmation"], &confirmation)
if err != nil {
return common.NewError("transaction_verify_error", "Error unmarshaling verify output. "+err.Error())
return common.NewError("transaction_verify_error", "Error unmarshaling verify output->confirmation: "+string(output)+" "+err.Error())
}
err = json.Unmarshal(confirmation["txn"], t)
if err != nil {
return common.NewError("transaction_verify_error", "Error unmarshaling verify output. "+err.Error())
return common.NewError("transaction_verify_error", "Error unmarshaling verify output->confirmation->txn: "+string(output)+" "+err.Error())
}
}
return nil
Expand Down
6 changes: 1 addition & 5 deletions code/go/0chain.net/validatorcore/storage/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,8 @@ func (sp *ValidatorProtocolImpl) RegisterValidator(ctx context.Context) (string,
sn.StakePoolSettings.NumDelegates = config.Configuration.NumDelegates
sn.StakePoolSettings.ServiceCharge = config.Configuration.ServiceCharge

snBytes, err := json.Marshal(sn)
if err != nil {
return "", err
}
Logger.Info("Adding validator to the blockchain.")
err = txn.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS, transaction.ADD_VALIDATOR_SC_NAME, string(snBytes), 0)
err = txn.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS, transaction.ADD_VALIDATOR_SC_NAME, sn, 0)
if err != nil {
Logger.Info("Failed during registering validator to the mining network", zap.String("err:", err.Error()))
return "", err
Expand Down