From 8176650cf8389da84dace4c1459e6e0cb5d547c0 Mon Sep 17 00:00:00 2001 From: dyoung Date: Thu, 17 Nov 2022 19:11:54 +0800 Subject: [PATCH] fix pos GetTransactionByNumber return (#199) * fix pos GetTransactionByNumber return * nil --- client_test.go | 8 +++++++ types/pos/pos.go | 40 +++++++++----------------------- types/pos/transaction_payload.go | 14 +++++++---- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/client_test.go b/client_test.go index df006fc..07ddbcd 100644 --- a/client_test.go +++ b/client_test.go @@ -22,6 +22,7 @@ import ( providers "github.com/openweb3/go-rpc-provider/provider_wrapper" "github.com/pkg/errors" . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" ) func _TestNewClient(t *testing.T) { @@ -125,3 +126,10 @@ func TestEstimateGasAndCollateralAlwaysWithGaspriceNil(t *testing.T) { To: defaultAccount, }) } + +func _TestGetPosTxByNum(t *testing.T) { + c := MustNewClient("https://test-internal.confluxrpc.com", ClientOption{Logger: os.Stdout}) + tx, err := c.Pos().GetTransactionByNumber(*types.NewUint64(0x76657)) + assert.NoError(t, err) + fmt.Printf("%v\n", tx) +} diff --git a/types/pos/pos.go b/types/pos/pos.go index cd0608e..d013821 100644 --- a/types/pos/pos.go +++ b/types/pos/pos.go @@ -134,16 +134,6 @@ func (b *Transaction) UnmarshalJSON(data []byte) error { Type string `json:"type"` } - type tmpPayload struct { - ElectionPayload - RetirePayload - // RegisterPayload - UpdateVotingPowerPayload - PivotBlockDecision - DisputePayload - ConflictSignature - } - tmpTx := tmpTransaction{} if err := json.Unmarshal(data, &tmpTx); err != nil { @@ -154,39 +144,31 @@ func (b *Transaction) UnmarshalJSON(data []byte) error { tmpTx.Timestamp, tmpTx.Number, nil, tmpTx.Status, tmpTx.Type} if tmpTx.Payload != nil { - marshaed, err := json.Marshal(tmpTx.Payload) - if err != nil { - return errors.WithStack(err) - } - - payload := tmpPayload{} - err = json.Unmarshal(marshaed, &payload) + marshaled, err := json.Marshal(tmpTx.Payload) if err != nil { return errors.WithStack(err) } realPayload := TransactionPayload{} + realPayload.SetTransactionType(tmpTx.Type) switch tmpTx.Type { case "Election": - realPayload.ElectionPayload = payload.ElectionPayload + err = json.Unmarshal(marshaled, &realPayload.ElectionPayload) case "Retire": - realPayload.RetirePayload = payload.RetirePayload + err = json.Unmarshal(marshaled, &realPayload.RetirePayload) case "Register": - realPayload.RegisterPayload = RegisterPayload{ - PublicKey: payload.ElectionPayload.PublicKey, - VrfPublicKey: payload.ElectionPayload.VrfPublicKey, - } - // fmt.Printf("realPayload.RegisterPayload: %#v\n\n", realPayload.RegisterPayload) + err = json.Unmarshal(marshaled, &realPayload.RegisterPayload) case "UpdateVotingPower": - realPayload.UpdateVotingPowerPayload = payload.UpdateVotingPowerPayload + err = json.Unmarshal(marshaled, &realPayload.UpdateVotingPowerPayload) case "PivotDecision": - realPayload.PivotBlockDecision = payload.PivotBlockDecision + err = json.Unmarshal(marshaled, &realPayload.PivotBlockDecision) case "Dispute": - realPayload.DisputePayload = payload.DisputePayload + err = json.Unmarshal(marshaled, &realPayload.DisputePayload) + } + if err != nil { + return errors.WithStack(err) } - realPayload.SetTransactionType(tmpTx.Type) - // fmt.Printf("tmpTxType:%v,payload:%#v, realPayload %#v", tmpTx.Type, payload, realPayload) b.Payload = &realPayload } diff --git a/types/pos/transaction_payload.go b/types/pos/transaction_payload.go index d5060ac..dd11a1c 100644 --- a/types/pos/transaction_payload.go +++ b/types/pos/transaction_payload.go @@ -73,10 +73,16 @@ type PivotBlockDecision struct { } type DisputePayload struct { - Address Address `json:"address"` - BlsPubKey string `json:"blsPubKey"` - VrfPubKey string `json:"vrfPubKey"` - ConflictingVotes ConflictSignature `json:"conflictingVotes"` + Address Address `json:"address"` + BlsPublicKey string `json:"blsPublicKey"` + VrfPublicKey string `json:"vrfPublicKey"` + ConflictingVotes ConflictingVotes `json:"conflictingVotes"` +} + +type ConflictingVotes struct { + ConflictVoteType string `json:"conflictVoteType"` + First string `json:"first"` + Second string `json:"second"` } type ConflictSignature struct {