Skip to content

Commit 0b3a4db

Browse files
committed
Add to the export_sdk_types script to keep SDK in sync
1 parent f65c7cf commit 0b3a4db

File tree

4 files changed

+101
-39
lines changed

4 files changed

+101
-39
lines changed

config/bounds/bounds.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,7 @@ const EncodedMaxAppParams = 64
148148
// config.Consensus[protocol.ConsensusCurrentVersion].MaxLocalSchemaEntries and
149149
// config.Consensus[protocol.ConsensusCurrentVersion].MaxGlobalSchemaEntries
150150
const EncodedMaxKeyValueEntries = 1024
151+
152+
// MaxConsensusVersionLen must be larger than any URL length of any consensus
153+
// version (which is currently URL+hash=89)
154+
const MaxConsensusVersionLen = 128

protocol/consensus.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ import (
2323
// ConsensusVersion is a string that identifies a version of the
2424
// consensus protocol.
2525
//
26-
//msgp:allocbound ConsensusVersion maxConsensusVersionLen
26+
//msgp:allocbound ConsensusVersion bounds.MaxConsensusVersionLen
2727
type ConsensusVersion string
2828

29-
// maxConsensusVersionLen is used for generating MaxSize functions on types that contain ConsensusVersion
30-
// as it's member. 128 is slightly larger than the existing URL length of consensus version URL+hash=89
31-
const maxConsensusVersionLen = 128
32-
3329
// DEPRECATEDConsensusV0 is a baseline version of the Algorand consensus protocol.
3430
// at the time versioning was introduced.
3531
// It is now deprecated.

protocol/msgp_gen.go

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/export_sdk_types.py

Lines changed: 91 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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,17 +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

7187
SDK="../go-algorand-sdk/"
7288

7389
def sdkize(input):
7490
# allocbounds are not used by the SDK. It's confusing to leave them in.
7591
input = re.sub(",allocbound=.*\"", '"', input)
92+
input = re.sub("^//msgp:allocbound.*\n", '', input, flags=re.MULTILINE)
93+
94+
# protocol.ConsensusVersion and protocolConsensusVxx constants are
95+
# the only things that stays in the protocol package. So we "hide"
96+
# them from the replacements below, then switch it back
97+
input = input.replace("protocol.ConsensusV", "protocolConsensusV")
98+
input = input.replace("protocol.ConsensusFuture", "protocolConsensusFuture")
7699

77100
# All types are in the same package in the SDK
78101
input = input.replace("basics.", "")
79102
input = input.replace("crypto.", "")
80-
input = input.replace("protocol.", "")
103+
input = re.sub(r'protocol\.\b', r'', input)
104+
105+
# and go back...
106+
input = input.replace("protocolConsensusV", "protocol.ConsensusV")
107+
input = input.replace("protocolConsensusFuture", "protocol.ConsensusFuture")
81108

82109
# keyreg
83110
input = input.replace("OneTimeSignatureVerifier", "VotePK")
@@ -97,36 +124,69 @@ def export(src, dst, start, stop):
97124
replace_between(SDK+dst, x, start, stop)
98125
subprocess.run(["gofmt", "-w", SDK+dst])
99126

127+
def export_type(name, src, dst):
128+
export_thing("type {thing} ", name, src, dst)
129+
130+
def export_var(name, src, dst):
131+
export_thing("var {thing} ", name, src, dst)
132+
133+
def export_func(name, src, dst):
134+
export_thing("func {thing}(", name, src, dst)
135+
136+
def export_thing(pattern, name, src, dst):
137+
start = pattern.format(thing=name)
138+
line = find_line(src, start)
139+
if line == "":
140+
raise ValueError(f"Unable to find {name} in {src}")
141+
stop = "\n}\n" if line.endswith("{") else "\n"
142+
x = extract_between(src, start, stop)
143+
x = sdkize(x)
144+
if dst.endswith(".go"): # explicit dst
145+
dst = f"{SDK}{dst}"
146+
else:
147+
dst = f"{SDK}types/{dst}.go"
148+
replace_between(dst, x, start, stop)
149+
subprocess.run(["gofmt", "-w", dst])
100150

101151
if __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}")
152+
# Replace the entire file, after "import" (basically just relicense it)
153+
versions = extract_between("protocol/consensus.go", "import")
154+
replace_between(SDK+"protocol/consensus.go", sdkize(versions), "import")
155+
156+
src = "config/consensus.go"
157+
dst = "protocol/config/consensus.go"
158+
export_type("ConsensusParams", src, dst)
159+
export_type("ProposerPayoutRules", src, dst)
160+
export_type("BonusPlan", src, dst)
161+
export_type("PaysetCommitType", src, dst)
162+
export_type("ConsensusProtocols", src, dst)
163+
export_var("Consensus", src, dst)
164+
export_func("initConsensusProtocols", src, dst)
165+
export_type("Global", src, dst)
166+
export_var("Protocol", src, dst)
167+
# do _not_ export init(), since go-algorand sets bounds, SDK does not
168+
169+
# Common transaction types
170+
export_type("Header", "data/transactions/transaction.go", "transaction")
171+
export_type("Transaction", "data/transactions/transaction.go", "transaction")
172+
export_type("SignedTxn", "data/transactions/signedtxn.go", "transaction")
173+
174+
# The transaction types themselves
175+
# payment
176+
export_type("PaymentTxnFields", "data/transactions/payment.go", "transaction")
177+
# keyreg
178+
export_type("KeyregTxnFields", "data/transactions/keyreg.go", "transaction")
179+
# assets
180+
export_type("AssetConfigTxnFields", "data/transactions/asset.go", "transaction")
181+
export_type("AssetTransferTxnFields", "data/transactions/asset.go", "transaction")
182+
export_type("AssetFreezeTxnFields", "data/transactions/asset.go", "transaction")
183+
export_type("AssetIndex", "data/basics/userBalance.go", "asset")
184+
export_type("AssetParams", "data/basics/userBalance.go", "asset")
185+
# apps
186+
export_type("ApplicationCallTxnFields", "data/transactions/application.go", "applications")
187+
export_type("AppIndex", "data/basics/userBalance.go", "applications")
129188

130189
# 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}")
190+
export_type("AppParams", "data/basics/userBalance.go", "statedelta")
191+
export_type("TealKeyValue", "data/basics/teal.go", "statedelta")
192+
export_type("TealValue", "data/basics/teal.go", "statedelta")

0 commit comments

Comments
 (0)