Skip to content

Commit

Permalink
Fix transaction decoding issue
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-lashin committed Jul 9, 2018
1 parent 57fd771 commit 49a2d05
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 51 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## TBD
## 0.0.6

- [core] Fix transaction decoding issue
- [api] Update transaction api

## 0.0.5
Expand Down
28 changes: 18 additions & 10 deletions core/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,31 +386,35 @@ func rlpHash(x interface{}) (h types.Hash) {
func DecodeFromBytes(buf []byte) (*Transaction, error) {

var tx Transaction
rlp.Decode(bytes.NewReader(buf), &tx)
err := rlp.Decode(bytes.NewReader(buf), &tx)

if err != nil {
return nil, err
}

switch tx.Type {
case TypeSend:
{
data := SendData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)
}
case TypeRedeemCheck:
{
data := RedeemCheckData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)
}
case TypeConvert:
{
data := ConvertData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)
}
case TypeCreateCoin:
{
data := CreateCoinData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)

if data.InitialReserve == nil || data.InitialAmount == nil {
Expand All @@ -421,37 +425,41 @@ func DecodeFromBytes(buf []byte) (*Transaction, error) {
case TypeDeclareCandidacy:
{
data := DeclareCandidacyData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)
}
case TypeDelegate:
{
data := DelegateData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)
}
case TypeSetCandidateOnline:
{
data := SetCandidateOnData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)
}
case TypeSetCandidateOffline:
{
data := SetCandidateOffData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)
}
case TypeUnbond:
{
data := UnbondData{}
rlp.Decode(bytes.NewReader(tx.Data), &data)
err = rlp.Decode(bytes.NewReader(tx.Data), &data)
tx.SetDecodedData(data)
}
default:
return nil, errors.New("incorrect tx data")
}

if err != nil {
return nil, err
}

if tx.S == nil || tx.R == nil || tx.V == nil {
return nil, errors.New("incorrect tx signature")
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.4"
services:
minter:
image: minterteam/minter:0.0.5
image: minterteam/minter:0.0.6
command: --tendermint_addr=tcp://tendermint:46657
volumes:
- ~/.minter:/minter
Expand Down
43 changes: 18 additions & 25 deletions rlp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ import (
)

var (
// EOL is returned when the end of the current list
// has been reached during streaming.
EOL = errors.New("rlp: end of list")

// Actual Errors
ErrExpectedString = errors.New("rlp: expected String or Byte")
ErrExpectedList = errors.New("rlp: expected List")
ErrCanonInt = errors.New("rlp: non-canonical integer format")
ErrCanonSize = errors.New("rlp: non-canonical size information")
ErrElemTooLarge = errors.New("rlp: element is larger than containing list")
ErrValueTooLarge = errors.New("rlp: value size exceeds available input length")
ErrMoreThanOneValue = errors.New("rlp: input contains more than one value")

// internal errors
errNotInList = errors.New("rlp: call of ListEnd outside of any list")
errNotAtEOL = errors.New("rlp: call of ListEnd not positioned at EOL")
errUintOverflow = errors.New("rlp: uint overflow")
errNoPointer = errors.New("rlp: interface given to Decode must be a pointer")
errDecodeIntoNil = errors.New("rlp: pointer given to Decode must not be nil")
)
Expand Down Expand Up @@ -274,9 +291,8 @@ func makeListDecoder(typ reflect.Type, tag tags) (decoder, error) {
if etype.Kind() == reflect.Uint8 && !reflect.PtrTo(etype).Implements(decoderInterface) {
if typ.Kind() == reflect.Array {
return decodeByteArray, nil
} else {
return decodeByteSlice, nil
}
return decodeByteSlice, nil
}
etypeinfo, err := cachedTypeInfo1(etype, tags{})
if err != nil {
Expand Down Expand Up @@ -555,29 +571,6 @@ func (k Kind) String() string {
}
}

var (
// EOL is returned when the end of the current list
// has been reached during streaming.
EOL = errors.New("rlp: end of list")

// Actual Errors
ErrExpectedString = errors.New("rlp: expected String or Byte")
ErrExpectedList = errors.New("rlp: expected List")
ErrCanonInt = errors.New("rlp: non-canonical integer format")
ErrCanonSize = errors.New("rlp: non-canonical size information")
ErrElemTooLarge = errors.New("rlp: element is larger than containing list")
ErrValueTooLarge = errors.New("rlp: value size exceeds available input length")

// This error is reported by DecodeBytes if the slice contains
// additional data after the first RLP value.
ErrMoreThanOneValue = errors.New("rlp: input contains more than one value")

// internal errors
errNotInList = errors.New("rlp: call of ListEnd outside of any list")
errNotAtEOL = errors.New("rlp: call of ListEnd not positioned at EOL")
errUintOverflow = errors.New("rlp: uint overflow")
)

// ByteReader must be implemented by any input reader for a Stream. It
// is implemented by e.g. bufio.Reader and bytes.Reader.
type ByteReader interface {
Expand Down
21 changes: 9 additions & 12 deletions rlp/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Encode(w io.Writer, val interface{}) error {
return eb.toWriter(w)
}

// EncodeBytes returns the RLP encoding of val.
// EncodeToBytes returns the RLP encoding of val.
// Please see the documentation of Encode for the encoding rules.
func EncodeToBytes(val interface{}) ([]byte, error) {
eb := encbufPool.Get().(*encbuf)
Expand All @@ -104,7 +104,7 @@ func EncodeToBytes(val interface{}) ([]byte, error) {
return eb.toBytes(), nil
}

// EncodeReader returns a reader from which the RLP encoding of val
// EncodeToReader returns a reader from which the RLP encoding of val
// can be read. The returned size is the total size of the encoded
// data.
//
Expand Down Expand Up @@ -151,11 +151,10 @@ func puthead(buf []byte, smalltag, largetag byte, size uint64) int {
if size < 56 {
buf[0] = smalltag + byte(size)
return 1
} else {
sizesize := putint(buf[1:], size)
buf[0] = largetag + byte(sizesize)
return sizesize + 1
}
sizesize := putint(buf[1:], size)
buf[0] = largetag + byte(sizesize)
return sizesize + 1
}

// encbufs are pooled.
Expand Down Expand Up @@ -218,7 +217,7 @@ func (w *encbuf) list() *listhead {
func (w *encbuf) listEnd(lh *listhead) {
lh.size = w.size() - lh.offset - lh.size
if lh.size < 56 {
w.lhsize += 1 // length encoded into kind tag
w.lhsize++ // length encoded into kind tag
} else {
w.lhsize += 1 + intsize(uint64(lh.size))
}
Expand Down Expand Up @@ -322,10 +321,9 @@ func (r *encReader) next() []byte {
p := r.buf.str[r.strpos:head.offset]
r.strpos += sizebefore
return p
} else {
r.lhpos++
return head.encode(r.buf.sizebuf)
}
r.lhpos++
return head.encode(r.buf.sizebuf)

case r.strpos < len(r.buf.str):
// String data at the end, after all list headers.
Expand Down Expand Up @@ -576,9 +574,8 @@ func makePtrWriter(typ reflect.Type) (writer, error) {
writer := func(val reflect.Value, w *encbuf) error {
if val.IsNil() {
return nilfunc(w)
} else {
return etypeinfo.writer(val.Elem(), w)
}
return etypeinfo.writer(val.Elem(), w)
}
return writer, err
}
Expand Down
4 changes: 2 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package version
const (
Maj = "0"
Min = "0"
Fix = "5"
Fix = "6"
)

var (
// Must be a string because scripts like dist.sh read this file.
Version = "0.0.5"
Version = "0.0.6"

// GitCommit is the current HEAD set using ldflags.
GitCommit string
Expand Down

0 comments on commit 49a2d05

Please sign in to comment.