From ab964da10568b7ff1872dc754160c87312e940bf Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 3 Jul 2018 15:03:35 -0400 Subject: [PATCH] marshal/unmarshal delegation --- x/stake/types/delegation.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index eb38a50a7a..705dcb6cd9 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -5,6 +5,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" ) // Delegation represents the bond with tokens held by an account. It is @@ -17,6 +18,41 @@ type Delegation struct { Height int64 `json:"height"` // Last height bond updated } +type delegationValue struct { + Shares sdk.Rat + Height int64 +} + +// return the delegation without fields contained within the key for the store +func MarshalDelegation(cdc *wire.Codec, delegation Delegation) []byte { + val := delegationValue{ + delegation.Shares, + delegation.Height, + } + return cdc.MustMarshalBinary(val) +} + +// return the delegation without fields contained within the key for the store +func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) Delegation { + var storeValue delegationValue + cdc.MustUnmarshalBinary(value, &storeValue) + + addrs := IndexKey[1:] // remove prefix bytes + split := len(addrs) / 2 + if (len(addrs) % 2) != 0 { + panic("key length not even") + } + delAddr := sdk.Address{addrs[:split]} + valAddr := sdk.Address{addrs[split:]} + + return Delegation{ + DelegatorAddr: delAddr, + ValidatorAddr: valAddr, + Shares: storeValue.Shares, + Height: storeValue.Height, + } +} + // two are equal func (d Delegation) Equal(d2 Delegation) bool { return bytes.Equal(d.DelegatorAddr, d2.DelegatorAddr) &&