@@ -2,6 +2,7 @@ package market
2
2
3
3
import (
4
4
"context"
5
+ "encoding/base64"
5
6
"fmt"
6
7
7
8
"github.com/filecoin-project/go-state-types/abi"
@@ -34,74 +35,71 @@ func (DealProposalExtractor) Extract(ctx context.Context, a actorstate.ActorInfo
34
35
return nil , err
35
36
}
36
37
37
- currDealProposals , err := ec .CurrState .Proposals ()
38
- if err != nil {
39
- return nil , fmt .Errorf ("loading current market deal proposals: %w" , err )
40
- }
41
-
38
+ var dealProposals []market.ProposalIDState
39
+ // if this is genesis iterator actors current state.
42
40
if ec .IsGenesis () {
43
- var out marketmodel.MarketDealProposals
41
+ currDealProposals , err := ec .CurrState .Proposals ()
42
+ if err != nil {
43
+ return nil , fmt .Errorf ("loading current market deal proposals: %w" , err )
44
+ }
45
+
44
46
if err := currDealProposals .ForEach (func (id abi.DealID , dp market.DealProposal ) error {
45
- var label string
46
- if dp .Label .IsString () {
47
- var err error
48
- label , err = dp .Label .ToString ()
49
- if err != nil {
50
- return fmt .Errorf ("creating deal proposal label string: %w" , err )
51
- }
52
- } else {
53
- label = ""
54
- }
55
- out = append (out , & marketmodel.MarketDealProposal {
56
- Height : int64 (ec .CurrTs .Height ()),
57
- DealID : uint64 (id ),
58
- StateRoot : ec .CurrTs .ParentState ().String (),
59
- PaddedPieceSize : uint64 (dp .PieceSize ),
60
- UnpaddedPieceSize : uint64 (dp .PieceSize .Unpadded ()),
61
- StartEpoch : int64 (dp .StartEpoch ),
62
- EndEpoch : int64 (dp .EndEpoch ),
63
- ClientID : dp .Client .String (),
64
- ProviderID : dp .Provider .String (),
65
- ClientCollateral : dp .ClientCollateral .String (),
66
- ProviderCollateral : dp .ProviderCollateral .String (),
67
- StoragePricePerEpoch : dp .StoragePricePerEpoch .String (),
68
- PieceCID : dp .PieceCID .String (),
69
- IsVerified : dp .VerifiedDeal ,
70
- Label : SanitizeLabel (label ),
47
+ dealProposals = append (dealProposals , market.ProposalIDState {
48
+ ID : id ,
49
+ Proposal : dp ,
71
50
})
72
51
return nil
73
52
}); err != nil {
74
- return nil , fmt .Errorf ("walking current deal states: %w" , err )
53
+ return nil , err
54
+ }
55
+ } else {
56
+ // else diff the actor against previous state and collect any additions that occurred.
57
+ changed , err := ec .CurrState .ProposalsChanged (ec .PrevState )
58
+ if err != nil {
59
+ return nil , fmt .Errorf ("checking for deal proposal changes: %w" , err )
60
+ }
61
+ if ! changed {
62
+ return nil , nil
75
63
}
76
- return out , nil
77
64
78
- }
65
+ changes , err := market .DiffDealProposals (ctx , ec .Store , ec .PrevState , ec .CurrState )
66
+ if err != nil {
67
+ return nil , fmt .Errorf ("diffing deal proposals: %w" , err )
68
+ }
79
69
80
- changed , err := ec .CurrState .ProposalsChanged (ec .PrevState )
81
- if err != nil {
82
- return nil , fmt .Errorf ("checking for deal proposal changes: %w" , err )
70
+ for _ , change := range changes .Added {
71
+ dealProposals = append (dealProposals , market.ProposalIDState {
72
+ ID : change .ID ,
73
+ Proposal : change .Proposal ,
74
+ })
75
+ }
83
76
}
84
77
85
- if ! changed {
86
- return nil , nil
87
- }
78
+ out := make (marketmodel.MarketDealProposals , len (dealProposals ))
79
+ for idx , add := range dealProposals {
80
+ var isString bool
81
+ var base64Label string
82
+ if add .Proposal .Label .IsString () {
83
+ labelString , err := add .Proposal .Label .ToString ()
84
+ if err != nil {
85
+ return nil , fmt .Errorf ("deal proposal (ID: %d) label is not a string despite claiming it is (developer error?)" , add .ID )
86
+ }
88
87
89
- changes , err := market .DiffDealProposals (ctx , ec .Store , ec .PrevState , ec .CurrState )
90
- if err != nil {
91
- return nil , fmt .Errorf ("diffing deal states: %w" , err )
92
- }
88
+ isString = true
89
+ base64Label = base64 .StdEncoding .EncodeToString ([]byte (SanitizeLabel (labelString )))
93
90
94
- out := make (marketmodel.MarketDealProposals , len (changes .Added ))
95
- for idx , add := range changes .Added {
96
- var label string
97
- if add .Proposal .Label .IsString () {
98
- var err error
99
- label , err = add .Proposal .Label .ToString ()
91
+ } else if add .Proposal .Label .IsBytes () {
92
+ labelBytes , err := add .Proposal .Label .ToBytes ()
100
93
if err != nil {
101
- return nil , fmt .Errorf ("creating deal proposal label string : %w " , err )
94
+ return nil , fmt .Errorf ("deal proposal (ID : %d) label is not bytes despit claiming it is (developer error?) " , add . ID )
102
95
}
96
+
97
+ isString = false
98
+ base64Label = base64 .StdEncoding .EncodeToString (labelBytes )
99
+
103
100
} else {
104
- label = ""
101
+ // TODO this should never happen, but if it does it indicates a logic.
102
+ return nil , fmt .Errorf ("deal proposal (ID: %d) label is neither bytes nor string (DEVELOPER ERROR)" , add .ID )
105
103
}
106
104
out [idx ] = & marketmodel.MarketDealProposal {
107
105
Height : int64 (ec .CurrTs .Height ()),
@@ -118,7 +116,8 @@ func (DealProposalExtractor) Extract(ctx context.Context, a actorstate.ActorInfo
118
116
StoragePricePerEpoch : add .Proposal .StoragePricePerEpoch .String (),
119
117
PieceCID : add .Proposal .PieceCID .String (),
120
118
IsVerified : add .Proposal .VerifiedDeal ,
121
- Label : SanitizeLabel (label ),
119
+ Label : base64Label ,
120
+ IsString : isString ,
122
121
}
123
122
}
124
123
return out , nil
0 commit comments