Skip to content

Commit

Permalink
Change subscribe meta internal type from string to bytes
Browse files Browse the repository at this point in the history
Protobuf bytes and string have the same encoding but different
restrictions: string can now only contain utf-8 characters.
However, the current chain data contains non-utf-8 string in meta
generated using old pb version, which will cause new version fail
to save certain blocks. Changing internal type to bytes will
resolve the issue without breaking compatibility.

Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Oct 9, 2020
1 parent c75ce11 commit 52c7188
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion chain/bvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (bvs *BlockValidationState) VerifyTransactionWithBlock(txn *transaction.Tra
defer func() {
if e == nil {
bvs.addChange(func() {
bvs.subscriptions[key] = subscriptionInfo{new: !subscribed, meta: subscribePayload.Meta}
bvs.subscriptions[key] = subscriptionInfo{new: !subscribed, meta: string(subscribePayload.Meta)}
})
}
}()
Expand Down
4 changes: 2 additions & 2 deletions chain/store/stateGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/nknorg/nkn/v2/block"
"github.com/nknorg/nkn/v2/chain"
"github.com/nknorg/nkn/v2/common"
"github.com/nknorg/nkn/v2/config"
"github.com/nknorg/nkn/v2/crypto"
"github.com/nknorg/nkn/v2/pb"
"github.com/nknorg/nkn/v2/program"
"github.com/nknorg/nkn/v2/transaction"
"github.com/nknorg/nkn/v2/config"
)

func (cs *ChainStore) spendTransaction(states *StateDB, txn *transaction.Transaction, totalFee common.Fixed64, genesis bool, height uint32) error {
Expand Down Expand Up @@ -123,7 +123,7 @@ func (cs *ChainStore) spendTransaction(states *StateDB, txn *transaction.Transac
}
case pb.PayloadType_SUBSCRIBE_TYPE:
subscribePayload := pl.(*pb.Subscribe)
if err = states.subscribe(subscribePayload.Topic, subscribePayload.Bucket, subscribePayload.Subscriber, subscribePayload.Identifier, subscribePayload.Meta, height+subscribePayload.Duration); err != nil {
if err = states.subscribe(subscribePayload.Topic, subscribePayload.Bucket, subscribePayload.Subscriber, subscribePayload.Identifier, string(subscribePayload.Meta), height+subscribePayload.Duration); err != nil {
return err
}
case pb.PayloadType_UNSUBSCRIBE_TYPE:
Expand Down
2 changes: 1 addition & 1 deletion pb/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (m *Subscribe) ToMap() map[string]interface{} {
"topic": m.Topic,
"bucket": m.Bucket,
"duration": m.Duration,
"meta": m.Meta,
"meta": string(m.Meta),
}
}

Expand Down
86 changes: 43 additions & 43 deletions pb/transaction.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pb/transaction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ message Subscribe {
string topic = 3;
uint32 bucket = 4 [deprecated = true];
uint32 duration = 5;
string meta = 6;
bytes meta = 6;
}

message Unsubscribe {
Expand Down
2 changes: 1 addition & 1 deletion transaction/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewSubscribe(subscriber []byte, id, topic string, duration uint32, meta str
Identifier: id,
Topic: topic,
Duration: duration,
Meta: meta,
Meta: []byte(meta),
}
}

Expand Down

0 comments on commit 52c7188

Please sign in to comment.