Skip to content

Instantly share code, notes, and snippets.

@dshulyak
Created May 6, 2019 08:32
Show Gist options
  • Save dshulyak/f3101ae6ad7234addece4d2ee228ddba to your computer and use it in GitHub Desktop.
Save dshulyak/f3101ae6ad7234addece4d2ee228ddba to your computer and use it in GitHub Desktop.
rlp.go
package main
import (
"fmt"
"github.com/ethereum/go-ethereum/rlp"
)
type Envelope struct {
A uint
}
func encodeSeparately(envelopes ...Envelope) [][]byte {
rst := [][]byte{}
for _, e := range envelopes {
buf, err := rlp.EncodeToBytes(e)
if err != nil {
panic(err)
}
rst = append(rst, buf)
}
return rst
}
func main() {
first := Envelope{A: 10}
second := Envelope{A: 20}
data := []Envelope{first, second}
buf1, err := rlp.EncodeToBytes(data)
if err != nil {
panic(err)
}
buf2, err := rlp.EncodeToBytes(encodeSeparately(first, second))
if err != nil {
panic(err)
}
fmt.Println(buf1, buf2)
var rst []Envelope
err = rlp.DecodeBytes(buf1, &rst)
if err != nil {
panic(err)
}
fmt.Println(rst)
err = rlp.DecodeBytes(buf2, &rst)
if err != nil {
panic(err)
}
fmt.Println(rst)
}
@dshulyak
Copy link
Author

dshulyak commented May 6, 2019

[196 193 10 193 20] [198 130 193 10 130 193 20]
[{10} {20}]
panic: rlp: expected input list for main.Envelope, decoding into ([]main.Envelope)[0]

goroutine 1 [running]:
main.main()
/home/dd/status/src/github.com/status-im/status-go/triage.go:48 +0x493
exit status 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment