marshal/unmarshal delegation

This commit is contained in:
rigelrozanski 2018-07-03 15:03:35 -04:00
parent f50c7be976
commit ab964da105
1 changed files with 36 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
sdk "github.com/cosmos/cosmos-sdk/types" 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 // 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 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 // two are equal
func (d Delegation) Equal(d2 Delegation) bool { func (d Delegation) Equal(d2 Delegation) bool {
return bytes.Equal(d.DelegatorAddr, d2.DelegatorAddr) && return bytes.Equal(d.DelegatorAddr, d2.DelegatorAddr) &&