This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpluggables.mini
153 lines (128 loc) · 4.76 KB
/
pluggables.mini
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Copyright 2020, Offchain Labs, Inc. All rights reserved.
//
use accounts::Account;
use accounts::account_setContractInfo;
use evmCallStack::evmCallStack_topFrame;
use evmCallStack::evmCallStack_setTopFrameMemory;
use evmCallStack::evmCallFrame_getCalldata;
use evmOps::getEvmOpJumpTable;
use evmOps::evmOp_return;
use evmOps::evmOp_revert;
use core::array::array;
use core::array::array_resize;
use std::bytearray::ByteArray;
use std::bytearray::bytearray_new;
use std::bytearray::bytearray_size;
use std::bytearray::bytearray_setByte;
use std::storageMap::storageMap_new;
const ModuleIdLimit = 4096;
type ModuleFunction = impure func(any, any, bool, impure func(uint, any) -> option<any>) -> (any, any)
type ModuleState = struct {
code: ModuleFunction,
state: any,
neverBeenCalled: bool,
}
type ModuleManager = struct {
capacity: uint,
modules: []option<ModuleState>,
}
var moduleManager: ModuleManager;
public impure func pluggables_init() {
moduleManager = struct {
capacity: 1,
modules: newarray<option<ModuleState>>(1) with { [0] = None<ModuleState> },
};
}
public impure func callPluggable(id: uint, args: any) -> option<any> {
if (id >= moduleManager.capacity) {
return None;
}
let theModule = moduleManager.modules[id]?;
let (retVal, newState) = theModule.code(args, theModule.state, theModule.neverBeenCalled, callPluggable);
moduleManager = moduleManager with {
modules: moduleManager.modules with {
[id] = Some(theModule with { state: newState } with { neverBeenCalled: false })
}
};
return Some(retVal);
}
public impure func pluggableExists(id: uint) -> bool {
return (id < moduleManager.capacity) && (moduleManager.modules[id] != None<ModuleState>);
}
public impure func installPluggable(id: uint, newFunc: ModuleFunction, keepState: bool) -> option<()> {
if (id > const::ModuleIdLimit) {
return None;
}
while (id >= moduleManager.capacity) {
let newCapacity = 8 * moduleManager.capacity;
moduleManager = moduleManager with {
capacity: newCapacity
} with {
modules: unsafecast<[]option<ModuleState>>(
array_resize(
unsafecast<array>(moduleManager.modules),
newCapacity,
None<ModuleState>
)
)
};
}
let newState = unsafecast<any>(());
if (keepState) {
newState = (moduleManager.modules[id]?).state;
}
moduleManager = moduleManager with {
modules: moduleManager.modules with {
[id] = Some(struct { code: newFunc, state: newState, neverBeenCalled: true })
}
};
return Some(());
}
// The next section supports binding pluggables to a contract address, so a pluggable can function as
// a contract.
// A pluggable functioning this way expects to receive args consisting of the 3-tuple
// (callFrame: EvmCallFrame, calldata: ByteArray, evmOpJumpTable: [const::NumEvmOps]impure func()).
// Its should return (success: bool, resultData: ByteArray).
public impure func bindContractAddressToPluggable(account: Account, id: uint) -> Account {
return account_setContractInfo(
account,
bytearray_setByte(bytearray_new(0), 0, 0xfe), // like an "Arbitrum style" precompiled contract
newmap<uint, impure func()>,
makePluggableContractEntryPoint(id),
storageMap_new(),
true, // force initial nonce to be 1
);
}
impure func makePluggableContractEntryPoint(id: uint) -> impure func() {
// make a small code snippet that function-calls to entryPointForPluggableContract(id) (with a dummy return address)
// using a dummy return address is safe because entryPointForPluggableContract will never return
return asm(
const::AVM_nop,
id,
asm(
const::AVM_nop,
0, // dummy return address
asm(
const::AVM_jump,
entryPointForPluggableContract,
asm() func() { errcodept }
) func() { pushinsnimm }
) func() { pushinsnimm }
) impure func() { pushinsnimm };
}
impure func entryPointForPluggableContract(id: uint) {
if let Some(topFrame) = evmCallStack_topFrame() {
if let Some(retVal) = callPluggable(id, (topFrame, evmCallFrame_getCalldata(topFrame), getEvmOpJumpTable())) {
let (success, resultData) = unsafecast<(bool, ByteArray)>(retVal);
if (evmCallStack_setTopFrameMemory(resultData)) {
if (success) {
evmOp_return(0, bytearray_size(resultData));
} else {
evmOp_revert(0, bytearray_size(resultData));
}
}
}
}
evmOp_revert(0, 0);
}