types extracted to submodule
This commit is contained in:
parent
accf5ba341
commit
0b102f2272
2 changed files with 48 additions and 43 deletions
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"lbc/blockchain/types"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/dgraph-io/badger"
|
"github.com/dgraph-io/badger"
|
||||||
|
|
@ -17,46 +18,14 @@ type PromiseApp struct {
|
||||||
currentBatch *badger.Txn
|
currentBatch *badger.Txn
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommiterTxBody struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
CommiterPubKey string `json:"commiter_pubkey"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PromiseTxBody struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
Timestamp int64 `json:"timestamp,omitempty"` // ← чтобы понимать клиент
|
|
||||||
Title string `json:"title,omitempty"` // ← опционально, если когда-нибудь пригодится
|
|
||||||
Deadline string `json:"deadline,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CommitmentTxBody struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
PromiseID string `json:"promise_id"`
|
|
||||||
CommiterID string `json:"commiter_id"`
|
|
||||||
CommiterSig string `json:"commiter_sig,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type compoundTxRaw struct {
|
type compoundTxRaw struct {
|
||||||
Body json.RawMessage `json:"body"`
|
Body json.RawMessage `json:"body"`
|
||||||
Signature string `json:"signature"`
|
Signature string `json:"signature"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type compoundBody struct {
|
type compoundBody struct {
|
||||||
Promise *PromiseTxBody `json:"promise"`
|
Promise *types.PromiseTxBody `json:"promise"`
|
||||||
Commitment *CommitmentTxBody `json:"commitment"`
|
Commitment *types.CommitmentTxBody `json:"commitment"`
|
||||||
}
|
|
||||||
|
|
||||||
type CompoundTx struct {
|
|
||||||
Body struct {
|
|
||||||
Promise *PromiseTxBody `json:"promise"`
|
|
||||||
Commitment *CommitmentTxBody `json:"commitment"`
|
|
||||||
} `json:"body"`
|
|
||||||
Signature string `json:"signature"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPromiseApp(db *badger.DB) *PromiseApp {
|
func NewPromiseApp(db *badger.DB) *PromiseApp {
|
||||||
|
|
@ -65,7 +34,7 @@ func NewPromiseApp(db *badger.DB) *PromiseApp {
|
||||||
|
|
||||||
func verifyAndExtractBody(db *badger.DB, tx []byte) (map[string]interface{}, error) {
|
func verifyAndExtractBody(db *badger.DB, tx []byte) (map[string]interface{}, error) {
|
||||||
var outer struct {
|
var outer struct {
|
||||||
Body CommiterTxBody `json:"body"`
|
Body types.CommiterTxBody `json:"body"`
|
||||||
Signature string `json:"signature"`
|
Signature string `json:"signature"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,7 +164,7 @@ func (app *PromiseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliver
|
||||||
compound, err := verifyCompoundTx(app.db, req.Tx)
|
compound, err := verifyCompoundTx(app.db, req.Tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
outer := struct {
|
outer := struct {
|
||||||
Body CommiterTxBody `json:"body"`
|
Body types.CommiterTxBody `json:"body"`
|
||||||
Signature string `json:"signature"`
|
Signature string `json:"signature"`
|
||||||
}{}
|
}{}
|
||||||
if err := json.Unmarshal(req.Tx, &outer); err != nil {
|
if err := json.Unmarshal(req.Tx, &outer); err != nil {
|
||||||
|
|
@ -247,7 +216,7 @@ func (app *PromiseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliver
|
||||||
return abci.ResponseDeliverTx{Code: 0}
|
return abci.ResponseDeliverTx{Code: 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyCompoundTx(db *badger.DB, tx []byte) (*CompoundTx, error) {
|
func verifyCompoundTx(db *badger.DB, tx []byte) (*types.CompoundTx, error) {
|
||||||
// 1) Разобрать внешний конверт, body оставить сырым
|
// 1) Разобрать внешний конверт, body оставить сырым
|
||||||
var outerRaw compoundTxRaw
|
var outerRaw compoundTxRaw
|
||||||
if err := json.Unmarshal(tx, &outerRaw); err != nil {
|
if err := json.Unmarshal(tx, &outerRaw); err != nil {
|
||||||
|
|
@ -286,7 +255,7 @@ func verifyCompoundTx(db *badger.DB, tx []byte) (*CompoundTx, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var commiter CommiterTxBody
|
var commiter types.CommiterTxBody
|
||||||
if err := json.Unmarshal(commiterData, &commiter); err != nil {
|
if err := json.Unmarshal(commiterData, &commiter); err != nil {
|
||||||
return nil, errors.New("corrupted commiter record")
|
return nil, errors.New("corrupted commiter record")
|
||||||
}
|
}
|
||||||
|
|
@ -321,10 +290,10 @@ func verifyCompoundTx(db *badger.DB, tx []byte) (*CompoundTx, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6) Вернуть в привычной форме
|
// 6) Вернуть в привычной форме
|
||||||
return &CompoundTx{
|
return &types.CompoundTx{
|
||||||
Body: struct {
|
Body: struct {
|
||||||
Promise *PromiseTxBody `json:"promise"`
|
Promise *types.PromiseTxBody `json:"promise"`
|
||||||
Commitment *CommitmentTxBody `json:"commitment"`
|
Commitment *types.CommitmentTxBody `json:"commitment"`
|
||||||
}{
|
}{
|
||||||
Promise: body.Promise,
|
Promise: body.Promise,
|
||||||
Commitment: body.Commitment,
|
Commitment: body.Commitment,
|
||||||
|
|
|
||||||
36
blockchain/types/types.go
Normal file
36
blockchain/types/types.go
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
// Types subpackage.
|
||||||
|
// You can you use to make an RFC client for abci application.
|
||||||
|
|
||||||
|
type CommiterTxBody struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
CommiterPubKey string `json:"commiter_pubkey"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PromiseTxBody struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Timestamp int64 `json:"timestamp,omitempty"` // ← чтобы понимать клиент
|
||||||
|
Title string `json:"title,omitempty"` // ← опционально, если когда-нибудь пригодится
|
||||||
|
Deadline string `json:"deadline,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommitmentTxBody struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
PromiseID string `json:"promise_id"`
|
||||||
|
CommiterID string `json:"commiter_id"`
|
||||||
|
CommiterSig string `json:"commiter_sig,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CompoundTx struct {
|
||||||
|
Body struct {
|
||||||
|
Promise *PromiseTxBody `json:"promise"`
|
||||||
|
Commitment *CommitmentTxBody `json:"commitment"`
|
||||||
|
} `json:"body"`
|
||||||
|
Signature string `json:"signature"`
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue