Skip to content

Commit

Permalink
Add EnableDeveloperAPI configuration flag (algorand#1191)
Browse files Browse the repository at this point in the history
Add a local config to disable teal/compile in algod by default.
  • Loading branch information
tsachiherman authored Jun 22, 2020
1 parent 0baf481 commit 97143e5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ type Local struct {

// CatchupLedgerDownloadRetryAttempts controls the number of attempt the block fetching would be attempted before giving up catching up to the provided catchpoint.
CatchupBlockDownloadRetryAttempts int `version[9]:"1000"`

// EnableDeveloperAPI enables teal/compile, teal/dryrun API endpoints.
// This functionlity is disabled by default.
EnableDeveloperAPI bool `version[9]:"false"`
}

// Filenames of config files within the configdir (e.g. ~/.algorand)
Expand Down
1 change: 1 addition & 0 deletions config/local_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var defaultLocal = Local{
EnableAgreementTimeMetrics: false,
EnableAssembleStats: false,
EnableBlockService: false,
EnableDeveloperAPI: false,
EnableGossipBlockService: true,
EnableIncomingMessageFilter: false,
EnableLedgerService: false,
Expand Down
5 changes: 5 additions & 0 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type NodeInterface interface {
SuggestedFee() basics.MicroAlgos
StartCatchup(catchpoint string) error
AbortCatchup(catchpoint string) error
Config() config.Local
}

// RegisterParticipationKeys registers participation keys.
Expand Down Expand Up @@ -589,6 +590,10 @@ func (v2 *Handlers) AbortCatchup(ctx echo.Context, catchpoint string) error {
// TealCompile compiles TEAL code to binary, return both binary and hash
// (POST /v2/teal/compile)
func (v2 *Handlers) TealCompile(ctx echo.Context) error {
// return early if teal compile is not allowed in node config
if !v2.Node.Config().EnableDeveloperAPI {
return ctx.String(http.StatusNotFound, "/teal/compile was not enabled in the configuration file by setting the EnableDeveloperAPI to true")
}
buf := new(bytes.Buffer)
ctx.Request().Body = http.MaxBytesReader(nil, ctx.Request().Body, maxTealSourceBytes)
buf.ReadFrom(ctx.Request().Body)
Expand Down
10 changes: 6 additions & 4 deletions daemon/algod/api/server/v2/test/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,15 @@ func TestAbortCatchup(t *testing.T) {
abortCatchupTest(t, badCatchPoint, 400)
}

func tealCompileTest(t *testing.T, bytesToUse []byte, expectedCode int) {
func tealCompileTest(t *testing.T, bytesToUse []byte, expectedCode int, enableDeveloperAPI bool) {
numAccounts := 1
numTransactions := 1
offlineAccounts := true
mockLedger, _, _, _, releasefunc := testingenv(t, numAccounts, numTransactions, offlineAccounts)
defer releasefunc()
dummyShutdownChan := make(chan struct{})
mockNode := makeMockNode(mockLedger, t.Name())
mockNode.config.EnableDeveloperAPI = enableDeveloperAPI
handler := v2.Handlers{
Node: &mockNode,
Log: logging.Base(),
Expand All @@ -316,11 +317,12 @@ func tealCompileTest(t *testing.T, bytesToUse []byte, expectedCode int) {
}

func TestTealCompile(t *testing.T) {
tealCompileTest(t, nil, 200) // nil program should work
tealCompileTest(t, nil, 200, true) // nil program should work
goodProgram := `int 1`
goodProgramBytes := []byte(goodProgram)
tealCompileTest(t, goodProgramBytes, 200)
tealCompileTest(t, goodProgramBytes, 200, true)
tealCompileTest(t, goodProgramBytes, 404, false)
badProgram := "bad program"
badProgramBytes := []byte(badProgram)
tealCompileTest(t, badProgramBytes, 400)
tealCompileTest(t, badProgramBytes, 400, true)
}
5 changes: 3 additions & 2 deletions daemon/algod/api/server/v2/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ var poolAddrResponseGolden = generatedV2.AccountResponse{
type mockNode struct {
ledger *data.Ledger
genesisID string
config config.Local
}

func makeMockNode(ledger *data.Ledger, genesisID string) mockNode {
return mockNode{ledger: ledger, genesisID: genesisID}
return mockNode{ledger: ledger, genesisID: genesisID, config: config.GetDefaultLocal()}
}

func (m mockNode) Ledger() *data.Ledger {
Expand Down Expand Up @@ -116,7 +117,7 @@ func (m mockNode) SuggestedFee() basics.MicroAlgos {

// unused by handlers:
func (m mockNode) Config() config.Local {
return config.GetDefaultLocal()
return m.config
}
func (m mockNode) Start() {}

Expand Down
1 change: 1 addition & 0 deletions installer/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"EnableAgreementTimeMetrics": false,
"EnableAssembleStats": false,
"EnableBlockService": false,
"EnableDeveloperAPI": false,
"EnableGossipBlockService": true,
"EnableIncomingMessageFilter": false,
"EnableLedgerService": false,
Expand Down

0 comments on commit 97143e5

Please sign in to comment.