@@ -836,6 +836,10 @@ var StateListMessagesCmd = &cli.Command{
836836 Name : "cids" ,
837837 Usage : "print message CIDs instead of messages" ,
838838 },
839+ & cli.BoolFlag {
840+ Name : "order-by-nonce" ,
841+ Usage : "order messages by nonce" ,
842+ },
839843 },
840844 Action : func (cctx * cli.Context ) error {
841845 api , closer , err := GetFullNodeAPI (cctx )
@@ -888,21 +892,56 @@ var StateListMessagesCmd = &cli.Command{
888892 return err
889893 }
890894
891- for _ , c := range msgs {
892- if cctx .Bool ("cids" ) {
893- fmt .Println (c .String ())
894- continue
895+ // If --order-by-nonce is set and --from is used, fetch messages and sort by nonce
896+ if cctx .Bool ("order-by-nonce" ) && froma != address .Undef {
897+ var sortedMsgs []struct {
898+ Cid cid.Cid
899+ Nonce uint64
900+ Msg * types.Message
895901 }
896-
897- m , err := api .ChainGetMessage (ctx , c )
898- if err != nil {
899- return err
902+ for _ , c := range msgs {
903+ m , err := api .ChainGetMessage (ctx , c )
904+ if err != nil {
905+ return err
906+ }
907+ if m .From == froma {
908+ sortedMsgs = append (sortedMsgs , struct {
909+ Cid cid.Cid
910+ Nonce uint64
911+ Msg * types.Message
912+ }{c , m .Nonce , m })
913+ }
900914 }
901- b , err := json .MarshalIndent (m , "" , " " )
902- if err != nil {
903- return err
915+ sort .Slice (sortedMsgs , func (i , j int ) bool {
916+ return sortedMsgs [i ].Nonce < sortedMsgs [j ].Nonce
917+ })
918+ for _ , sm := range sortedMsgs {
919+ if cctx .Bool ("cids" ) {
920+ fmt .Println (sm .Cid .String ())
921+ continue
922+ }
923+ b , err := json .MarshalIndent (sm .Msg , "" , " " )
924+ if err != nil {
925+ return err
926+ }
927+ fmt .Println (string (b ))
928+ }
929+ } else {
930+ for _ , c := range msgs {
931+ if cctx .Bool ("cids" ) {
932+ fmt .Println (c .String ())
933+ continue
934+ }
935+ m , err := api .ChainGetMessage (ctx , c )
936+ if err != nil {
937+ return err
938+ }
939+ b , err := json .MarshalIndent (m , "" , " " )
940+ if err != nil {
941+ return err
942+ }
943+ fmt .Println (string (b ))
904944 }
905- fmt .Println (string (b ))
906945 }
907946
908947 if end <= 0 {
0 commit comments