-
Notifications
You must be signed in to change notification settings - Fork 10
/
txnscope_setcontroller.go
74 lines (68 loc) · 2.01 KB
/
txnscope_setcontroller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package impl
import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/textileio/go-tableland/pkg/tables"
"github.com/textileio/go-tableland/pkg/tables/impl/ethereum"
)
func (ts *txnScope) executeSetControllerEvent(
ctx context.Context,
e *ethereum.ContractSetController,
) (eventExecutionResult, error) {
if e.TableId == nil {
return eventExecutionResult{Error: &tableIDIsEmpty}, nil
}
tableID := tables.TableID(*e.TableId)
if err := ts.setController(ctx, tableID, e.Controller); err != nil {
var dbErr *errQueryExecution
if errors.As(err, &dbErr) {
err := fmt.Sprintf("set controller execution failed (code: %s, msg: %s)", dbErr.Code, dbErr.Msg)
return eventExecutionResult{Error: &err}, nil
}
return eventExecutionResult{}, fmt.Errorf("executing set controller: %s", err)
}
return eventExecutionResult{TableID: &tableID}, nil
}
// SetController sets and unsets the controller of a table.
func (ts *txnScope) setController(
ctx context.Context,
id tables.TableID,
controller common.Address,
) error {
if controller == common.HexToAddress("0x0") {
if _, err := ts.txn.ExecContext(ctx,
`DELETE FROM system_controller WHERE chain_id = ?1 AND table_id = ?2;`,
ts.scopeVars.ChainID,
id.String(),
); err != nil {
if code, ok := isErrCausedByQuery(err); ok {
return &errQueryExecution{
Code: "SQLITE_" + code,
Msg: err.Error(),
}
}
return fmt.Errorf("deleting entry from system controller: %s", err)
}
} else {
if _, err := ts.txn.ExecContext(ctx,
`INSERT INTO system_controller ("chain_id", "table_id", "controller")
VALUES (?1, ?2, ?3)
ON CONFLICT ("chain_id", "table_id")
DO UPDATE set controller = ?3;`,
ts.scopeVars.ChainID,
id.String(),
controller.Hex(),
); err != nil {
if code, ok := isErrCausedByQuery(err); ok {
return &errQueryExecution{
Code: "SQLITE_" + code,
Msg: err.Error(),
}
}
return fmt.Errorf("inserting new entry into system controller: %s", err)
}
}
return nil
}