aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <obscuren@obscura.com>2013-12-28 04:23:40 +0800
committerobscuren <obscuren@obscura.com>2013-12-28 04:23:40 +0800
commitdca9ee79b3a27ba2cc5d026d445ae627a53617b0 (patch)
treef0568e37910e9e9921ab8f69a0bbf2fb1d02fbef
parent8301d154ae4bb6082d588bd42c6e913be63618ef (diff)
downloadgo-tangerine-dca9ee79b3a27ba2cc5d026d445ae627a53617b0.tar.gz
go-tangerine-dca9ee79b3a27ba2cc5d026d445ae627a53617b0.tar.zst
go-tangerine-dca9ee79b3a27ba2cc5d026d445ae627a53617b0.zip
Changed Tx serialization to return bytes instead of a string
-rw-r--r--serialization.go8
-rw-r--r--transaction.go16
2 files changed, 13 insertions, 11 deletions
diff --git a/serialization.go b/serialization.go
index 613dcdcc5..5a92a434f 100644
--- a/serialization.go
+++ b/serialization.go
@@ -28,6 +28,10 @@ func NumToVarInt(x int) string {
func RlpEncode(object interface{}) string {
if str, ok := object.(string); ok {
return "\x00" + NumToVarInt(len(str)) + str
+ } else if num, ok := object.(uint32); ok {
+ return RlpEncode(Uitoa(num))
+ } else if byt, ok := object.([]byte); ok {
+ return RlpEncode(string(byt))
} else if slice, ok := object.([]interface{}); ok {
var buffer bytes.Buffer
for _, val := range slice {
@@ -53,7 +57,7 @@ func RlpEncode(object interface{}) string {
}
type RlpSerializer interface {
- MarshalRls() []byte
- UnmarshalRls([]byte)
+ MarshalRlp() []byte
+ UnmarshalRlp([]byte)
}
diff --git a/transaction.go b/transaction.go
index 26d7df258..5a268b566 100644
--- a/transaction.go
+++ b/transaction.go
@@ -6,7 +6,6 @@ import (
"encoding/hex"
"crypto/sha256"
_ "bytes"
- "strconv"
)
/*
@@ -35,14 +34,17 @@ var Period3Reward *big.Int = new(big.Int)
var Period4Reward *big.Int = new(big.Int)
type Transaction struct {
+ RlpSerializer
+
sender string
recipient uint32
value uint32
fee uint32
data []string
memory []int
- signature string
+ // To be removed
+ signature string
addr string
}
@@ -61,18 +63,14 @@ func NewTransaction(to uint32, value uint32, data []string) *Transaction {
tx.data[i] = instr
}
- b:= []byte(tx.Serialize())
+ b:= []byte(tx.MarshalRlp())
hash := sha256.Sum256(b)
tx.addr = hex.EncodeToString(hash[0:19])
return &tx
}
-func Uitoa(i uint32) string {
- return strconv.FormatUint(uint64(i), 10)
-}
-
-func (tx *Transaction) Serialize() string {
+func (tx *Transaction) MarshalRlp() []byte {
// Prepare the transaction for serialization
preEnc := []interface{}{
"0", // TODO last Tx
@@ -84,7 +82,7 @@ func (tx *Transaction) Serialize() string {
tx.data,
}
- return RlpEncode(preEnc)
+ return []byte(RlpEncode(preEnc))
}
func InitFees() {