@@ -53,7 +53,7 @@ def replace_between(filename, content, start_pattern, stop_pattern=None):
5353
5454 start_idx = original .find (start_pattern )
5555 if start_idx == - 1 :
56- raise ValueError ("Start pattern not found" )
56+ raise ValueError (f "Start pattern ' { start_pattern } ' not found in { filename } " )
5757
5858 start_idx += len (start_pattern )
5959 stop_idx = len (original )
@@ -67,18 +67,44 @@ def replace_between(filename, content, start_pattern, stop_pattern=None):
6767 with open (filename , 'w' , encoding = 'utf-8' ) as f :
6868 f .write (updated )
6969
70+ def find_line (filename , s ):
71+ """
72+ Returns the line from `filename` that contains `s`
73+ Args:
74+ filename (str): Path to the file to modify.
75+ s (str): Name of the substring to look for
76+ """
77+ with open (filename , 'r' , encoding = 'utf-8' ) as f :
78+ original = f .read ()
79+
80+ start_idx = original .find (s )
81+ if start_idx == - 1 :
82+ return ""
83+ stop_idx = original .find ("\n " , start_idx )
84+
85+ return original [start_idx :stop_idx ]
7086
7187SDK = "../go-algorand-sdk/"
7288
7389def sdkize (input ):
7490 # allocbounds are not used by the SDK. It's confusing to leave them in.
7591 input = re .sub (",allocbound=.*\" " , '"' , input )
7692
93+ # protocol.ConsensusVersion and protocolConsensusVxx constants are
94+ # the only things that stays in the protocol package. So we "hide"
95+ # them from the replacements below, then switch it back
96+ input = input .replace ("protocol.ConsensusV" , "protocolConsensusV" )
97+ input = input .replace ("protocol.ConsensusFuture" , "protocolConsensusFuture" )
98+
7799 # All types are in the same package in the SDK
78100 input = input .replace ("basics." , "" )
79101 input = input .replace ("crypto." , "" )
80102 input = input .replace ("protocol." , "" )
81103
104+ # and go back...
105+ input = input .replace ("protocolConsensusV" , "protocol.ConsensusV" )
106+ input = input .replace ("protocolConsensusFuture" , "protocol.ConsensusFuture" )
107+
82108 # keyreg
83109 input = input .replace ("OneTimeSignatureVerifier" , "VotePK" )
84110 input = input .replace ("VRFVerifier" , "VRFPK" )
@@ -97,36 +123,65 @@ def export(src, dst, start, stop):
97123 replace_between (SDK + dst , x , start , stop )
98124 subprocess .run (["gofmt" , "-w" , SDK + dst ])
99125
126+ def export_type (name , src , dst ):
127+ export_thing ("type {thing} " , name , src , dst )
128+
129+ def export_var (name , src , dst ):
130+ export_thing ("var {thing} " , name , src , dst )
131+
132+ def export_func (name , src , dst ):
133+ export_thing ("func {thing}(" , name , src , dst )
134+
135+ def export_thing (pattern , name , src , dst ):
136+ start = pattern .format (thing = name )
137+ line = find_line (src , start )
138+ if line == "" :
139+ raise ValueError (f"Unable to find { name } in { src } " )
140+ stop = "\n }\n " if line .endswith ("{" ) else "\n "
141+ x = extract_between (src , start , stop )
142+ x = sdkize (x )
143+ if dst .endswith (".go" ): # explicit dst
144+ dst = f"{ SDK } { dst } "
145+ else :
146+ dst = f"{ SDK } types/{ dst } .go"
147+ replace_between (dst , x , start , stop )
148+ subprocess .run (["gofmt" , "-w" , dst ])
100149
101150if __name__ == "__main__" :
102- # Replace the entire file, starting with "type ConsensusParams"
103- consensus = extract_between ("config/consensus.go" , "type ConsensusParams" )
104- replace_between (SDK + "protocol/config/consensus.go" , consensus , "type ConsensusParams" )
105-
106- # Common tranbsaction types
107- export ("data/transactions/transaction.go" , "types/transaction.go" ,
108- "type Header " , "\n }" )
109- export ("data/transactions/transaction.go" , "types/transaction.go" ,
110- "type Transaction " , "\n }" )
111- export ("data/transactions/signedtxn.go" , "types/transaction.go" ,
112- "type SignedTxn " , "\n }" )
113-
114- # The transaction types
115- export ("data/transactions/payment.go" , "types/transaction.go" ,
116- "type PaymentTxnFields " , "\n }" )
117- export ("data/transactions/keyreg.go" , "types/transaction.go" ,
118- "type KeyregTxnFields " , "\n }" )
119-
120- export ("data/transactions/asset.go" , "types/transaction.go" ,
121- "type AssetConfigTxnFields " , "\n }" )
122- export ("data/transactions/asset.go" , "types/transaction.go" ,
123- "type AssetTransferTxnFields " , "\n }" )
124- export ("data/transactions/asset.go" , "types/transaction.go" ,
125- "type AssetFreezeTxnFields " , "\n }" )
126-
127- export ("data/transactions/application.go" , "types/applications.go" ,
128- "type ApplicationCallTxnFields " , "\n }" )
151+ src = "config/consensus.go"
152+ dst = "protocol/config/consensus.go"
153+ export_type ("ConsensusParams" , src , dst )
154+ export_type ("ProposerPayoutRules" , src , dst )
155+ export_type ("BonusPlan" , src , dst )
156+ export_type ("PaysetCommitType" , src , dst )
157+ export_type ("ConsensusProtocols" , src , dst )
158+ export_var ("Consensus" , src , dst )
159+ export_func ("initConsensusProtocols" , src , dst )
160+ export_type ("Global" , src , dst )
161+ export_var ("Protocol" , src , dst )
162+ # do _not_ export init(), since go-algorand sets bounds, SDK does not
163+
164+ # Common transaction types
165+ export_type ("Header" , "data/transactions/transaction.go" , "transaction" )
166+ export_type ("Transaction" , "data/transactions/transaction.go" , "transaction" )
167+ export_type ("SignedTxn" , "data/transactions/signedtxn.go" , "transaction" )
168+
169+ # The transaction types themselves
170+ # payment
171+ export_type ("PaymentTxnFields" , "data/transactions/payment.go" , "transaction" )
172+ # keyreg
173+ export_type ("KeyregTxnFields" , "data/transactions/keyreg.go" , "transaction" )
174+ # assets
175+ export_type ("AssetConfigTxnFields" , "data/transactions/asset.go" , "transaction" )
176+ export_type ("AssetTransferTxnFields" , "data/transactions/asset.go" , "transaction" )
177+ export_type ("AssetFreezeTxnFields" , "data/transactions/asset.go" , "transaction" )
178+ export_type ("AssetIndex" , "data/basics/userBalance.go" , "asset" )
179+ export_type ("AssetParams" , "data/basics/userBalance.go" , "asset" )
180+ # apps
181+ export_type ("ApplicationCallTxnFields" , "data/transactions/application.go" , "applications" )
182+ export_type ("AppIndex" , "data/basics/userBalance.go" , "applications" )
129183
130184 # StateDelta. Eventually need to deal with all types from ledgercore.StateDelta down
131- export ("data/basics/userBalance.go" , "types/statedelta.go" ,
132- "type AppParams " , "\n }" )
185+ export_type ("AppParams" , "data/basics/userBalance.go" , "statedelta" )
186+ export_type ("TealKeyValue" , "data/basics/teal.go" , "statedelta" )
187+ export_type ("TealValue" , "data/basics/teal.go" , "statedelta" )
0 commit comments