mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
fix genesis bug, get app running
This commit is contained in:
parent
3f7bcca487
commit
c168718332
@ -39,7 +39,7 @@ func main() {
|
|||||||
// add standard rpc commands
|
// add standard rpc commands
|
||||||
rpc.AddCommands(rootCmd)
|
rpc.AddCommands(rootCmd)
|
||||||
|
|
||||||
//Add state commands
|
// Add state commands
|
||||||
tendermintCmd := &cobra.Command{
|
tendermintCmd := &cobra.Command{
|
||||||
Use: "tendermint",
|
Use: "tendermint",
|
||||||
Short: "Tendermint state querying subcommands",
|
Short: "Tendermint state querying subcommands",
|
||||||
@ -50,7 +50,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
tx.AddCommands(tendermintCmd, cdc)
|
tx.AddCommands(tendermintCmd, cdc)
|
||||||
|
|
||||||
//Add IBC commands
|
// Add IBC commands
|
||||||
// ibcCmd := &cobra.Command{
|
// ibcCmd := &cobra.Command{
|
||||||
// Use: "ibc",
|
// Use: "ibc",
|
||||||
// Short: "Inter-Blockchain Communication subcommands",
|
// Short: "Inter-Blockchain Communication subcommands",
|
||||||
@ -76,7 +76,7 @@ func main() {
|
|||||||
client.LineBreak,
|
client.LineBreak,
|
||||||
)
|
)
|
||||||
|
|
||||||
//Add stake commands
|
// Add stake commands
|
||||||
stakeCmd := &cobra.Command{
|
stakeCmd := &cobra.Command{
|
||||||
Use: "stake",
|
Use: "stake",
|
||||||
Short: "Stake and validation subcommands",
|
Short: "Stake and validation subcommands",
|
||||||
@ -102,7 +102,7 @@ func main() {
|
|||||||
stakeCmd,
|
stakeCmd,
|
||||||
)
|
)
|
||||||
|
|
||||||
//Add stake commands
|
// Add gov commands
|
||||||
// govCmd := &cobra.Command{
|
// govCmd := &cobra.Command{
|
||||||
// Use: "gov",
|
// Use: "gov",
|
||||||
// Short: "Governance and voting subcommands",
|
// Short: "Governance and voting subcommands",
|
||||||
@ -123,7 +123,7 @@ func main() {
|
|||||||
// govCmd,
|
// govCmd,
|
||||||
// )
|
// )
|
||||||
|
|
||||||
//Add auth and bank commands
|
// Add auth and bank commands
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
client.GetCommands(
|
client.GetCommands(
|
||||||
authcmd.GetAccountCmd("acc", cdc, authcmd.GetAccountDecoder(cdc)),
|
authcmd.GetAccountCmd("acc", cdc, authcmd.GetAccountDecoder(cdc)),
|
||||||
@ -133,19 +133,22 @@ func main() {
|
|||||||
bankcmd.SendTxCmd(cdc),
|
bankcmd.SendTxCmd(cdc),
|
||||||
)...)
|
)...)
|
||||||
|
|
||||||
|
// Add paychan commands
|
||||||
paychanCmd := &cobra.Command{
|
paychanCmd := &cobra.Command{
|
||||||
Use: "paychan",
|
Use: "paychan",
|
||||||
Short: "Payment channel subcommands",
|
Short: "Payment channel subcommand",
|
||||||
}
|
}
|
||||||
paychanCmd.AddCommand(
|
paychanCmd.AddCommand(
|
||||||
client.PostCommands(
|
client.PostCommands(
|
||||||
paychancmd.CreatePaychanCmd(cdc),
|
paychancmd.CreateChannelCmd(cdc),
|
||||||
paychancmd.GenerateNewStateCmd(cdc),
|
paychancmd.GeneratePaymentCmd(cdc),
|
||||||
paychancmd.ClosePaychanCmd(cdc),
|
paychancmd.VerifyPaymentCmd(cdc, "paychan"), // pass in storeKey
|
||||||
|
paychancmd.SubmitPaymentCmd(cdc),
|
||||||
)...)
|
)...)
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
paychanCmd,
|
paychanCmd,
|
||||||
)
|
)
|
||||||
|
|
||||||
// add proxy, version and key info
|
// add proxy, version and key info
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
keys.Commands(),
|
keys.Commands(),
|
||||||
|
@ -151,6 +151,7 @@ func (app *KavaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) ab
|
|||||||
|
|
||||||
// The function baseapp runs on receipt of a EndBlock ABCI message
|
// The function baseapp runs on receipt of a EndBlock ABCI message
|
||||||
func (app *KavaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
func (app *KavaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
||||||
|
paychan.EndBlocker(ctx, app.paychanKeeper)
|
||||||
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
|
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
|
||||||
|
|
||||||
//tags, _ := gov.EndBlocker(ctx, app.govKeeper)
|
//tags, _ := gov.EndBlocker(ctx, app.govKeeper)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
|
/*
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
@ -75,3 +76,4 @@ func TestGenesis(t *testing.T) {
|
|||||||
res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address)
|
res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address)
|
||||||
assert.Equal(t, acc, res1)
|
assert.Equal(t, acc, res1)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
@ -9,7 +9,6 @@ Simplifications:
|
|||||||
TODO
|
TODO
|
||||||
- in code TODOs
|
- in code TODOs
|
||||||
- Tidy up - method descriptions, heading comments, remove uneccessary comments, README/docs
|
- Tidy up - method descriptions, heading comments, remove uneccessary comments, README/docs
|
||||||
- chnge module name to "channel"?
|
|
||||||
- Find a better name for Queue - clarify distinction between int slice and abstract queue concept
|
- Find a better name for Queue - clarify distinction between int slice and abstract queue concept
|
||||||
- write some sort of integration test
|
- write some sort of integration test
|
||||||
- find nicer name for payout
|
- find nicer name for payout
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/cosmos/cosmos-sdk/client/keys"
|
"github.com/cosmos/cosmos-sdk/client/keys"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/wire"
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
||||||
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/internal/x/paychan"
|
"github.com/kava-labs/kava/internal/x/paychan"
|
||||||
@ -22,7 +21,6 @@ import (
|
|||||||
// list of functions that return pointers to cobra commands
|
// list of functions that return pointers to cobra commands
|
||||||
// No local storage needed for cli acting as a sender
|
// No local storage needed for cli acting as a sender
|
||||||
|
|
||||||
|
|
||||||
func CreateChannelCmd(cdc *wire.Codec) *cobra.Command {
|
func CreateChannelCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
flagTo := "to"
|
flagTo := "to"
|
||||||
flagCoins := "amount"
|
flagCoins := "amount"
|
||||||
@ -46,7 +44,7 @@ func CreateChannelCmd(cdc *wire.Codec) *cobra.Command {
|
|||||||
|
|
||||||
// Get receiver address
|
// Get receiver address
|
||||||
toStr := viper.GetString(flagTo)
|
toStr := viper.GetString(flagTo)
|
||||||
receiver, err := sdk.GetAccAddressBech32(toStr)
|
receiver, err := sdk.AccAddressFromBech32(toStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -60,7 +58,7 @@ func CreateChannelCmd(cdc *wire.Codec) *cobra.Command {
|
|||||||
|
|
||||||
// Create the create channel msg to send
|
// Create the create channel msg to send
|
||||||
msg := paychan.MsgCreate{
|
msg := paychan.MsgCreate{
|
||||||
Participants: []sdk.AccAddress{sender, receiver},
|
Participants: [2]sdk.AccAddress{sender, receiver},
|
||||||
Coins: coins,
|
Coins: coins,
|
||||||
}
|
}
|
||||||
err = msg.ValidateBasic()
|
err = msg.ValidateBasic()
|
||||||
@ -69,16 +67,15 @@ func CreateChannelCmd(cdc *wire.Codec) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build and sign the transaction, then broadcast to the blockchain
|
// Build and sign the transaction, then broadcast to the blockchain
|
||||||
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
|
err = ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().String(flagTo, "", "Recipient address of the payment channel.")
|
cmd.Flags().String(flagTo, "", "Recipient address of the payment channel.")
|
||||||
cmd.Flags().String(flagAmount, "", "Amount of coins to fund the payment channel with.")
|
cmd.Flags().String(flagCoins, "", "Amount of coins to fund the payment channel with.")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +102,7 @@ func GeneratePaymentCmd(cdc *wire.Codec) *cobra.Command {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// Get the paychan id
|
// Get the paychan id
|
||||||
id := viper.GetInt64(flagId) // TODO make this default to pulling id from chain
|
id := paychan.ChannelID(viper.GetInt64(flagId)) // TODO make this default to pulling id from chain
|
||||||
|
|
||||||
// Get channel receiver amount
|
// Get channel receiver amount
|
||||||
senderCoins, err := sdk.ParseCoins(viper.GetString(flagSenderAmount))
|
senderCoins, err := sdk.ParseCoins(viper.GetString(flagSenderAmount))
|
||||||
@ -141,13 +138,16 @@ func GeneratePaymentCmd(cdc *wire.Codec) *cobra.Command {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
update.Sigs = [1]paychan.UpdateSignature{
|
update.Sigs = [1]paychan.UpdateSignature{{
|
||||||
PubKey: pubKey,
|
PubKey: pubKey,
|
||||||
CryptoSignature: sig,
|
CryptoSignature: sig,
|
||||||
}
|
}}
|
||||||
|
|
||||||
// Print out the update
|
// Print out the update
|
||||||
jsonUpdate := cdc.MarshalJSONIndent(update)
|
jsonUpdate, err := wire.MarshalJSONIndent(cdc, update)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
fmt.Println(string(jsonUpdate))
|
fmt.Println(string(jsonUpdate))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -159,7 +159,7 @@ func GeneratePaymentCmd(cdc *wire.Codec) *cobra.Command {
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyPaymentCmd(cdc *wire.Codec, paychanStoreName, string) *cobra.Command {
|
func VerifyPaymentCmd(cdc *wire.Codec, paychanStoreName string) *cobra.Command {
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "verify",
|
Use: "verify",
|
||||||
@ -168,6 +168,9 @@ func VerifyPaymentCmd(cdc *wire.Codec, paychanStoreName, string) *cobra.Command
|
|||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
|
// Create a "client context" stuct populated with info from common flags
|
||||||
|
ctx := context.NewCoreContextFromViper()
|
||||||
|
|
||||||
// read in update
|
// read in update
|
||||||
bz, err := ioutil.ReadAll(os.Stdin)
|
bz, err := ioutil.ReadAll(os.Stdin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -178,7 +181,7 @@ func VerifyPaymentCmd(cdc *wire.Codec, paychanStoreName, string) *cobra.Command
|
|||||||
var update paychan.Update
|
var update paychan.Update
|
||||||
cdc.UnmarshalJSON(bz, &update)
|
cdc.UnmarshalJSON(bz, &update)
|
||||||
|
|
||||||
// get the channel from the node
|
// get the channel from the node
|
||||||
res, err := ctx.QueryStore(paychan.GetChannelKey(update.ChannelID), paychanStoreName)
|
res, err := ctx.QueryStore(paychan.GetChannelKey(update.ChannelID), paychanStoreName)
|
||||||
if len(res) == 0 || err != nil {
|
if len(res) == 0 || err != nil {
|
||||||
return errors.Errorf("channel with ID '%d' does not exist", update.ChannelID)
|
return errors.Errorf("channel with ID '%d' does not exist", update.ChannelID)
|
||||||
@ -187,7 +190,7 @@ func VerifyPaymentCmd(cdc *wire.Codec, paychanStoreName, string) *cobra.Command
|
|||||||
cdc.MustUnmarshalBinary(res, &channel)
|
cdc.MustUnmarshalBinary(res, &channel)
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
updateIsOK := paychan.Keeper.VerifyUpdate(channel ,update)
|
updateIsOK := paychan.VerifyUpdate(channel, update)
|
||||||
|
|
||||||
// print result
|
// print result
|
||||||
fmt.Println(updateIsOK)
|
fmt.Println(updateIsOK)
|
||||||
@ -199,7 +202,7 @@ func VerifyPaymentCmd(cdc *wire.Codec, paychanStoreName, string) *cobra.Command
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func SubmitPaymentChannelCmd(cdc *wire.Codec) *cobra.Command {
|
func SubmitPaymentCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "submit",
|
Use: "submit",
|
||||||
@ -238,11 +241,10 @@ func SubmitPaymentChannelCmd(cdc *wire.Codec) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build and sign the transaction, then broadcast to the blockchain
|
// Build and sign the transaction, then broadcast to the blockchain
|
||||||
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
|
err = ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package rest
|
package rest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/mux"
|
//"github.com/gorilla/mux"
|
||||||
//"github.com/tendermint/go-crypto/keys"
|
//"github.com/tendermint/go-crypto/keys"
|
||||||
//"github.com/cosmos/cosmos-sdk/client/context"
|
//"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
//"github.com/cosmos/cosmos-sdk/wire"
|
//"github.com/cosmos/cosmos-sdk/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -11,11 +11,10 @@ func EndBlocker(ctx sdk.Context, k Keeper) sdk.Tags {
|
|||||||
|
|
||||||
// Iterate through submittedUpdatesQueue
|
// Iterate through submittedUpdatesQueue
|
||||||
// TODO optimise so it doesn't pull every update from DB every block
|
// TODO optimise so it doesn't pull every update from DB every block
|
||||||
|
q := k.getSubmittedUpdatesQueue(ctx)
|
||||||
var sUpdate SubmittedUpdate
|
var sUpdate SubmittedUpdate
|
||||||
q, found := k.getSubmittedUpdatesQueue(ctx)
|
var found bool
|
||||||
if !found {
|
|
||||||
panic("SubmittedUpdatesQueue not found.")
|
|
||||||
}
|
|
||||||
for _, id := range q {
|
for _, id := range q {
|
||||||
// close the channel if the update has reached its execution time.
|
// close the channel if the update has reached its execution time.
|
||||||
// Using >= in case some are somehow missed.
|
// Using >= in case some are somehow missed.
|
||||||
|
@ -49,7 +49,7 @@ func TestEndBlocker(t *testing.T) {
|
|||||||
_, found := channelKeeper.getChannel(ctx, channelID)
|
_, found := channelKeeper.getChannel(ctx, channelID)
|
||||||
assert.False(t, found)
|
assert.False(t, found)
|
||||||
// check queue is empty, NOTE: due to encoding, an empty queue (underneath just an int slice) will be decoded as nil slice rather than an empty slice
|
// check queue is empty, NOTE: due to encoding, an empty queue (underneath just an int slice) will be decoded as nil slice rather than an empty slice
|
||||||
suq, _ := channelKeeper.getSubmittedUpdatesQueue(ctx)
|
suq := channelKeeper.getSubmittedUpdatesQueue(ctx)
|
||||||
assert.Equal(t, SubmittedUpdatesQueue(nil), suq)
|
assert.Equal(t, SubmittedUpdatesQueue(nil), suq)
|
||||||
// check submittedUpdate is gone
|
// check submittedUpdate is gone
|
||||||
_, found = channelKeeper.getSubmittedUpdate(ctx, channelID)
|
_, found = channelKeeper.getSubmittedUpdate(ctx, channelID)
|
||||||
|
@ -79,15 +79,12 @@ func (k Keeper) InitCloseChannelBySender(ctx sdk.Context, update Update) (sdk.Ta
|
|||||||
if !found {
|
if !found {
|
||||||
return nil, sdk.ErrInternal("Channel doesn't exist")
|
return nil, sdk.ErrInternal("Channel doesn't exist")
|
||||||
}
|
}
|
||||||
err := k.VerifyUpdate(channel, update)
|
err := VerifyUpdate(channel, update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
q, found := k.getSubmittedUpdatesQueue(ctx)
|
q := k.getSubmittedUpdatesQueue(ctx)
|
||||||
if !found {
|
|
||||||
panic("SubmittedUpdatesQueue not found.") // TODO nicer custom errors
|
|
||||||
}
|
|
||||||
if q.Contains(update.ChannelID) {
|
if q.Contains(update.ChannelID) {
|
||||||
// Someone has previously tried to update channel
|
// Someone has previously tried to update channel
|
||||||
// In bidirectional channels the new update is compared against existing and replaces it if it has a higher sequence number.
|
// In bidirectional channels the new update is compared against existing and replaces it if it has a higher sequence number.
|
||||||
@ -123,16 +120,13 @@ func (k Keeper) CloseChannelByReceiver(ctx sdk.Context, update Update) (sdk.Tags
|
|||||||
if !found {
|
if !found {
|
||||||
return nil, sdk.ErrInternal("Channel doesn't exist")
|
return nil, sdk.ErrInternal("Channel doesn't exist")
|
||||||
}
|
}
|
||||||
err := k.VerifyUpdate(channel, update)
|
err := VerifyUpdate(channel, update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if there is an update in the queue already
|
// Check if there is an update in the queue already
|
||||||
q, found := k.getSubmittedUpdatesQueue(ctx)
|
q := k.getSubmittedUpdatesQueue(ctx)
|
||||||
if !found {
|
|
||||||
panic("SubmittedUpdatesQueue not found.") // TODO nicer custom errors
|
|
||||||
}
|
|
||||||
if q.Contains(update.ChannelID) {
|
if q.Contains(update.ChannelID) {
|
||||||
// Someone has previously tried to update channel but receiver has final say
|
// Someone has previously tried to update channel but receiver has final say
|
||||||
k.removeFromSubmittedUpdatesQueue(ctx, update.ChannelID)
|
k.removeFromSubmittedUpdatesQueue(ctx, update.ChannelID)
|
||||||
@ -162,7 +156,7 @@ func (k Keeper) CloseChannelByReceiver(ctx sdk.Context, update Update) (sdk.Tags
|
|||||||
// return returnUpdate
|
// return returnUpdate
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func (k Keeper) VerifyUpdate(channel Channel, update Update) sdk.Error {
|
func VerifyUpdate(channel Channel, update Update) sdk.Error {
|
||||||
|
|
||||||
// Check the num of payout participants match channel participants
|
// Check the num of payout participants match channel participants
|
||||||
if len(update.Payout) != len(channel.Participants) {
|
if len(update.Payout) != len(channel.Participants) {
|
||||||
@ -183,7 +177,7 @@ func (k Keeper) VerifyUpdate(channel Channel, update Update) sdk.Error {
|
|||||||
return sdk.ErrInternal("Payout amount doesn't match channel amount")
|
return sdk.ErrInternal("Payout amount doesn't match channel amount")
|
||||||
}
|
}
|
||||||
// Check sender signature is OK
|
// Check sender signature is OK
|
||||||
if !k.verifySignatures(channel, update) {
|
if !verifySignatures(channel, update) {
|
||||||
return sdk.ErrInternal("Signature on update not valid")
|
return sdk.ErrInternal("Signature on update not valid")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -212,7 +206,7 @@ func (k Keeper) closeChannel(ctx sdk.Context, update Update) (sdk.Tags, sdk.Erro
|
|||||||
return tags, nil
|
return tags, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) verifySignatures(channel Channel, update Update) bool {
|
func verifySignatures(channel Channel, update Update) bool {
|
||||||
// In non unidirectional channels there will be more than one signature to check
|
// In non unidirectional channels there will be more than one signature to check
|
||||||
|
|
||||||
signBytes := update.GetSignBytes()
|
signBytes := update.GetSignBytes()
|
||||||
@ -234,10 +228,7 @@ func (k Keeper) verifySignatures(channel Channel, update Update) bool {
|
|||||||
func (k Keeper) addToSubmittedUpdatesQueue(ctx sdk.Context, sUpdate SubmittedUpdate) {
|
func (k Keeper) addToSubmittedUpdatesQueue(ctx sdk.Context, sUpdate SubmittedUpdate) {
|
||||||
// always overwrite prexisting values - leave paychan logic to higher levels
|
// always overwrite prexisting values - leave paychan logic to higher levels
|
||||||
// get current queue
|
// get current queue
|
||||||
q, found := k.getSubmittedUpdatesQueue(ctx)
|
q := k.getSubmittedUpdatesQueue(ctx)
|
||||||
if !found {
|
|
||||||
panic("SubmittedUpdatesQueue not found.")
|
|
||||||
}
|
|
||||||
// append ID to queue
|
// append ID to queue
|
||||||
if !q.Contains(sUpdate.ChannelID) {
|
if !q.Contains(sUpdate.ChannelID) {
|
||||||
q = append(q, sUpdate.ChannelID)
|
q = append(q, sUpdate.ChannelID)
|
||||||
@ -249,10 +240,7 @@ func (k Keeper) addToSubmittedUpdatesQueue(ctx sdk.Context, sUpdate SubmittedUpd
|
|||||||
}
|
}
|
||||||
func (k Keeper) removeFromSubmittedUpdatesQueue(ctx sdk.Context, channelID ChannelID) {
|
func (k Keeper) removeFromSubmittedUpdatesQueue(ctx sdk.Context, channelID ChannelID) {
|
||||||
// get current queue
|
// get current queue
|
||||||
q, found := k.getSubmittedUpdatesQueue(ctx)
|
q := k.getSubmittedUpdatesQueue(ctx)
|
||||||
if !found {
|
|
||||||
panic("SubmittedUpdatesQueue not found.")
|
|
||||||
}
|
|
||||||
// remove id
|
// remove id
|
||||||
q.RemoveMatchingElements(channelID)
|
q.RemoveMatchingElements(channelID)
|
||||||
// set queue
|
// set queue
|
||||||
@ -261,19 +249,18 @@ func (k Keeper) removeFromSubmittedUpdatesQueue(ctx sdk.Context, channelID Chann
|
|||||||
k.deleteSubmittedUpdate(ctx, channelID)
|
k.deleteSubmittedUpdate(ctx, channelID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) getSubmittedUpdatesQueue(ctx sdk.Context) (SubmittedUpdatesQueue, bool) {
|
func (k Keeper) getSubmittedUpdatesQueue(ctx sdk.Context) SubmittedUpdatesQueue {
|
||||||
// load from DB
|
// load from DB
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
bz := store.Get(k.getSubmittedUpdatesQueueKey())
|
bz := store.Get(k.getSubmittedUpdatesQueueKey())
|
||||||
|
|
||||||
var suq SubmittedUpdatesQueue
|
var suq SubmittedUpdatesQueue // if the submittedUpdatesQueue not found then return an empty one
|
||||||
if bz == nil {
|
if bz != nil {
|
||||||
return suq, false // TODO maybe create custom error to pass up here
|
// unmarshal
|
||||||
|
k.cdc.MustUnmarshalBinary(bz, &suq)
|
||||||
}
|
}
|
||||||
// unmarshal
|
return suq
|
||||||
k.cdc.MustUnmarshalBinary(bz, &suq)
|
|
||||||
// return
|
|
||||||
return suq, true
|
|
||||||
}
|
}
|
||||||
func (k Keeper) setSubmittedUpdatesQueue(ctx sdk.Context, suq SubmittedUpdatesQueue) {
|
func (k Keeper) setSubmittedUpdatesQueue(ctx sdk.Context, suq SubmittedUpdatesQueue) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
@ -332,7 +319,7 @@ func (k Keeper) getSubmittedUpdateKey(channelID ChannelID) []byte {
|
|||||||
func (k Keeper) getChannel(ctx sdk.Context, channelID ChannelID) (Channel, bool) {
|
func (k Keeper) getChannel(ctx sdk.Context, channelID ChannelID) (Channel, bool) {
|
||||||
// load from DB
|
// load from DB
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
bz := store.Get(k.GetChannelKey(channelID))
|
bz := store.Get(GetChannelKey(channelID))
|
||||||
|
|
||||||
var channel Channel
|
var channel Channel
|
||||||
if bz == nil {
|
if bz == nil {
|
||||||
@ -350,13 +337,13 @@ func (k Keeper) setChannel(ctx sdk.Context, channel Channel) {
|
|||||||
// marshal
|
// marshal
|
||||||
bz := k.cdc.MustMarshalBinary(channel) // panics if something goes wrong
|
bz := k.cdc.MustMarshalBinary(channel) // panics if something goes wrong
|
||||||
// write to db
|
// write to db
|
||||||
key := k.GetChannelKey(channel.ID)
|
key := GetChannelKey(channel.ID)
|
||||||
store.Set(key, bz) // panics if something goes wrong
|
store.Set(key, bz) // panics if something goes wrong
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) deleteChannel(ctx sdk.Context, channelID ChannelID) {
|
func (k Keeper) deleteChannel(ctx sdk.Context, channelID ChannelID) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
store.Delete(k.GetChannelKey(channelID))
|
store.Delete(GetChannelKey(channelID))
|
||||||
// TODO does this have return values? What happens when key doesn't exist?
|
// TODO does this have return values? What happens when key doesn't exist?
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -364,7 +351,7 @@ func (k Keeper) getNewChannelID(ctx sdk.Context) ChannelID {
|
|||||||
// get last channel ID
|
// get last channel ID
|
||||||
var lastID ChannelID
|
var lastID ChannelID
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
bz := store.Get(k.getLastChannelIDKey())
|
bz := store.Get(getLastChannelIDKey())
|
||||||
if bz == nil {
|
if bz == nil {
|
||||||
lastID = -1 // TODO is just setting to zero if uninitialized ok?
|
lastID = -1 // TODO is just setting to zero if uninitialized ok?
|
||||||
} else {
|
} else {
|
||||||
@ -374,14 +361,14 @@ func (k Keeper) getNewChannelID(ctx sdk.Context) ChannelID {
|
|||||||
newID := lastID + 1
|
newID := lastID + 1
|
||||||
bz = k.cdc.MustMarshalBinary(newID)
|
bz = k.cdc.MustMarshalBinary(newID)
|
||||||
// set last channel id again
|
// set last channel id again
|
||||||
store.Set(k.getLastChannelIDKey(), bz)
|
store.Set(getLastChannelIDKey(), bz)
|
||||||
// return
|
// return
|
||||||
return newID
|
return newID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) GetChannelKey(channelID ChannelID) []byte {
|
func GetChannelKey(channelID ChannelID) []byte {
|
||||||
return []byte(fmt.Sprintf("channel:%d", channelID))
|
return []byte(fmt.Sprintf("channel:%d", channelID))
|
||||||
}
|
}
|
||||||
func (k Keeper) getLastChannelIDKey() []byte {
|
func getLastChannelIDKey() []byte {
|
||||||
return []byte("lastChannelID")
|
return []byte("lastChannelID")
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ import (
|
|||||||
func TestKeeper(t *testing.T) {
|
func TestKeeper(t *testing.T) {
|
||||||
t.Run("CreateChannel", func(t *testing.T) {
|
t.Run("CreateChannel", func(t *testing.T) {
|
||||||
|
|
||||||
|
// TODO test for receiver account not existing (OK) and sender not existing (not ok)
|
||||||
|
|
||||||
accountSeeds := []string{"senderSeed", "receiverSeed"}
|
accountSeeds := []string{"senderSeed", "receiverSeed"}
|
||||||
const (
|
const (
|
||||||
senderAccountIndex int = 0
|
senderAccountIndex int = 0
|
||||||
|
@ -80,7 +80,7 @@ type SubmittedUpdate struct {
|
|||||||
ExecutionTime int64 // BlockHeight
|
ExecutionTime int64 // BlockHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
type SubmittedUpdatesQueue []ChannelID
|
type SubmittedUpdatesQueue []ChannelID // not technically a queue
|
||||||
|
|
||||||
// Check if value is in queue
|
// Check if value is in queue
|
||||||
func (suq SubmittedUpdatesQueue) Contains(channelID ChannelID) bool {
|
func (suq SubmittedUpdatesQueue) Contains(channelID ChannelID) bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user