goat-systems / go-tezos

Go Tezos Is a Go library that exposes and builds upon the Tezos RPC.
MIT License
71 stars 45 forks source link

panic when forging endorsement #166

Closed utdrmac closed 3 years ago

utdrmac commented 3 years ago

Simplest example:

package main

import (
  "fmt"
  "github.com/goat-systems/go-tezos/forge"
  "github.com/goat-systems/go-tezos/rpc"
)

func main() {

    endorsement := rpc.Endorsement{
        Kind: rpc.ENDORSEMENT,
        Level: 12345,
    }

    endorsementBytes, err := forge.Encode("BKxWMXLfmd7RmT4xTyF9wpeMeiq1H4mw4RnruY9kBxkzCQKieqD", endorsement.ToContent())
    if err != nil {
        panic(fmt.Sprintf("ERROR: %s", err.Error()))
    }

    fmt.Println("Bytes:", endorsementBytes)
}

Result:

$ go run test.go
panic: runtime error: slice bounds out of range [:4] with capacity 1

goroutine 1 [running]:
github.com/goat-systems/go-tezos/forge.forgeInt32(...)
    /home/drmac/go/src/github.com/goat-systems/go-tezos/forge/forge.go:837
github.com/goat-systems/go-tezos/forge.forgeEndorsement(0x62d63b, 0xb, 0x3039, 0x0, 0x26, 0x0, 0x0, 0x2, 0x58000000d4)
    /home/drmac/go/src/github.com/goat-systems/go-tezos/forge/forge.go:591 +0x70a
github.com/goat-systems/go-tezos/forge.Encode(0x637ed6, 0x33, 0xc00029fe00, 0x1, 0x1, 0x8, 0xc0002b08a0, 0x0, 0x60b320)
    /home/drmac/go/src/github.com/goat-systems/go-tezos/forge/forge.go:215 +0xfa7
main.main()
    /home/drmac/go/src/goendorse/test.go:16 +0x1ab
exit status 2
utdrmac commented 3 years ago

If the purpose is to create a BigEndian representation of the level, can you use this? https://play.golang.org/p/xf1nYhprdmu

func forgeInt32(value int, l int) []byte {
    bigE := make([]byte, 4)
    binary.BigEndian.PutUint32(bigE, uint32(value))
    return bigE
}

I modified my fork to use the above and it appears to be working as expected.

DefinitelyNotAGoat commented 3 years ago

Great, thanks again @utdrmac!

DefinitelyNotAGoat commented 3 years ago

Fixed in V4. Thanks again!