rlp 방식을 사용하는 이유
- 구현의 단순성과 언제나 일관된 바이트 형태를 보장하기 때문에 사용한다.
- key/value 맵 구조는 순서가 정해져 있지 않다. 따라서 데이터의 순서에 따라 결과가 달라질 수 있다. 그래서 인코딩에 사용할 수 없다.
- float의 경우는 어디까지 소수점을 적용할지, 올림, 반올림은 어떻게 처리할지 등등 이슈가 존재한다.
- 따라서 항상 일관된 결괏값을 보장하기 위해서 rlp 방식을 이용한다.
golnag으로 작성한 example
func Example_단일바이트() {
result, _ := rlp.EncodeToBytes("a")
fmt.Println(toHexs(result))
result, _ = rlp.EncodeToBytes("1")
fmt.Println(toHexs(result))
}
func ExampleShort_55바이트이하() {
result, _ := rlp.EncodeToBytes("foobar")
fmt.Println(toHexs(result))
}
func Example_55바이트초과() {
tartget := strings.Repeat("a", 100)
result, _ := rlp.EncodeToBytes(tartget)
fmt.Println(toHexs(result))
}
func Example_배열의_모든_아이템들의_rlp_인코딩된_값들의_길이가_55보다_작은경우_1() {
target := []string{"ab"}
result, _ := rlp.EncodeToBytes(target)
fmt.Println(toHexs(result))
}
func Example_배열의_모든_아이템들의_rlp_인코딩된_값들의_길이가_55보다_작은경우_2() {
target := []string{"ab", "cd"}
result, _ := rlp.EncodeToBytes(target)
fmt.Println(toHexs(result))
}
func toHexs(bytes []byte) []string {
var result []string
for _, x := range bytes {
result = append(result, fmt.Sprintf("%x", x))
}
return result
}
func Example_첫_바이트가_00_7f() {
var a string
rlp.DecodeBytes([]byte{97}, &a)
fmt.Println(a)
var b string
rlp.DecodeBytes([]byte{98}, &b)
fmt.Println(b)
}
func Example_첫_바이트가_80_b7() {
var ab string
rlp.DecodeBytes([]byte{130, 97, 98}, &ab)
fmt.Println(ab)
}
func Example_첫_바이트가_b8_bf() {
var longA string
rlp.DecodeBytes([]byte{184, 100, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97}, &longA)
fmt.Println(longA)
}
func Example_첫_바이트가_c0_f7() {
var result []string
rlp.DecodeBytes([]byte{198, 130, 97, 98, 130, 99, 100}, &result)
fmt.Println(result)
}