Skip to content

Commit 93fd844

Browse files
mbobenMarko Boben
and
Marko Boben
authored
Prioritised submitter contract (#26)
* Added prioritised handling of submitter contract * Fork times for submitter contract * Simplified function for checking ftso prioritised contract * Change of default attestors addresses * Updated submitter fork addresses * Updated submitter fork test date * Fixed failing test * Updated patch version * Changed submitter fork time for coston 2 --------- Co-authored-by: Marko Boben <[email protected]>
1 parent 44079ee commit 93fd844

File tree

5 files changed

+119
-24
lines changed

5 files changed

+119
-24
lines changed

avalanchego/version/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
Current = &Semantic{
1515
Major: 1,
1616
Minor: 7,
17-
Patch: 1806,
17+
Patch: 1807,
1818
}
1919
CurrentApp = &Application{
2020
Major: Current.Major,

coreth/core/daemon.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@ package core
66
import (
77
"fmt"
88
"math/big"
9+
"os"
10+
"time"
911

1012
"github.com/ethereum/go-ethereum/common"
1113
"github.com/ethereum/go-ethereum/log"
1214

1315
"github.com/ava-labs/coreth/core/vm"
16+
"github.com/ava-labs/coreth/params"
17+
)
18+
19+
var (
20+
// Define activation times for submitter contract
21+
submitterContractActivationTimeFlare = big.NewInt(time.Date(2024, time.March, 26, 12, 0, 0, 0, time.UTC).Unix())
22+
submitterContractActivationTimeCostwo = big.NewInt(time.Date(2024, time.March, 7, 12, 0, 0, 0, time.UTC).Unix())
23+
24+
// Define ftso and submitter contract addresses
25+
prioritisedFTSOContractAddress = common.HexToAddress("0x1000000000000000000000000000000000000003")
26+
27+
prioritisedSubmitterContractAddress = common.HexToAddress("0x2cA6571Daa15ce734Bbd0Bf27D5C9D16787fc33f")
28+
prioritisedSubmitterContractAddressEnv = common.HexToAddress(os.Getenv("SUBMITTER_CONTRACT_ADDRESS")) // for local and staging chains
1429
)
1530

1631
// Define errors
@@ -73,10 +88,35 @@ func GetDaemonSelector(blockTime *big.Int) []byte {
7388
}
7489
}
7590

76-
func GetPrioritisedFTSOContract(blockTime *big.Int) string {
91+
func isPrioritisedFTSOContract(to *common.Address) bool {
92+
return to != nil && *to == prioritisedFTSOContractAddress
93+
}
94+
95+
func isPrioritisedSubmitterContract(chainID *big.Int, to *common.Address, blockTime *big.Int) bool {
7796
switch {
97+
case to == nil || chainID == nil || blockTime == nil:
98+
return false
99+
case chainID.Cmp(params.FlareChainID) == 0:
100+
return *to == prioritisedSubmitterContractAddress &&
101+
blockTime.Cmp(submitterContractActivationTimeFlare) > 0
102+
case chainID.Cmp(params.CostwoChainID) == 0:
103+
return *to == prioritisedSubmitterContractAddress &&
104+
blockTime.Cmp(submitterContractActivationTimeCostwo) > 0
105+
case chainID.Cmp(params.LocalFlareChainID) == 0 || chainID.Cmp(params.StagingChainID) == 0:
106+
return *to == prioritisedSubmitterContractAddressEnv
78107
default:
79-
return "0x1000000000000000000000000000000000000003"
108+
return false
109+
}
110+
}
111+
112+
func IsPrioritisedContractCall(chainID *big.Int, to *common.Address, ret []byte, blockTime *big.Int) bool {
113+
switch {
114+
case isPrioritisedFTSOContract(to):
115+
return true
116+
case isPrioritisedSubmitterContract(chainID, to, blockTime):
117+
return !isZeroSlice(ret)
118+
default:
119+
return false
80120
}
81121
}
82122

@@ -157,3 +197,12 @@ func atomicDaemonAndMint(evm EVMCaller, log log.Logger) {
157197
log.Warn("Daemon error", "error", daemonErr)
158198
}
159199
}
200+
201+
func isZeroSlice(s []byte) bool {
202+
for i := len(s) - 1; i >= 0; i-- {
203+
if s[i] != 0 {
204+
return false
205+
}
206+
}
207+
return true
208+
}

coreth/core/daemon_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"errors"
88
"math/big"
99
"testing"
10+
"time"
1011

1112
"github.com/ethereum/go-ethereum/common"
1213
"github.com/ethereum/go-ethereum/log"
1314

1415
"github.com/ava-labs/coreth/core/vm"
16+
"github.com/ava-labs/coreth/params"
1517
)
1618

1719
// Define a mock structure to spy and mock values for daemon calls
@@ -476,3 +478,31 @@ func TestDaemonShouldNotMintMoreThanLimit(t *testing.T) {
476478
t.Errorf("Add balance call count not as expected. got %d want 1", defaultEVMMock.mockEVMCallerData.addBalanceCalls)
477479
}
478480
}
481+
482+
func TestPrioritisedContract(t *testing.T) {
483+
address := common.HexToAddress("0x123456789aBCdEF123456789aBCdef123456789A")
484+
preForkTime := big.NewInt(time.Date(2024, time.March, 20, 12, 0, 0, 0, time.UTC).Unix())
485+
postForkTime := big.NewInt(time.Date(2024, time.March, 27, 12, 0, 0, 0, time.UTC).Unix())
486+
ret0 := [32]byte{}
487+
ret1 := [32]byte{}
488+
ret1[31] = 1
489+
490+
if IsPrioritisedContractCall(params.FlareChainID, &address, nil, preForkTime) {
491+
t.Errorf("Expected false for wrong address")
492+
}
493+
if !IsPrioritisedContractCall(params.FlareChainID, &prioritisedFTSOContractAddress, nil, preForkTime) {
494+
t.Errorf("Expected true for FTSO contract")
495+
}
496+
if IsPrioritisedContractCall(params.FlareChainID, &prioritisedSubmitterContractAddress, ret1[:], preForkTime) {
497+
t.Errorf("Expected false for submitter contract before activation")
498+
}
499+
if !IsPrioritisedContractCall(params.FlareChainID, &prioritisedSubmitterContractAddress, ret1[:], postForkTime) {
500+
t.Errorf("Expected true for submitter contract after activation")
501+
}
502+
if IsPrioritisedContractCall(params.FlareChainID, &prioritisedSubmitterContractAddress, ret0[:], postForkTime) {
503+
t.Errorf("Expected false for submitter contract with wrong return value")
504+
}
505+
if IsPrioritisedContractCall(params.FlareChainID, &prioritisedSubmitterContractAddress, nil, postForkTime) {
506+
t.Errorf("Expected false for submitter contract with no return value")
507+
}
508+
}

coreth/core/state_connector.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,30 @@ func FinaliseRoundSelector(chainID *big.Int, blockTime *big.Int) []byte {
115115
func GetDefaultAttestors(chainID *big.Int, blockTime *big.Int) []common.Address {
116116
switch {
117117
case chainID.Cmp(params.FlareChainID) == 0:
118-
return []common.Address{
119-
common.HexToAddress("0x0988Cf4828F4e4eD0cE7c07467E70e19095Ee152"),
120-
common.HexToAddress("0x6BC7DCa62010D418eB72CCdc58561e00C5868Ef1"),
121-
common.HexToAddress("0xE34Bb361536610a9DCcEa5292262e36AfF65c06c"),
122-
common.HexToAddress("0x8A3D627D86A81F5D21683F4963565C63DB5e1309"),
123-
common.HexToAddress("0x2D3e7e4b19bDc920fd9C57BD3072A31F5a59FeC8"),
124-
common.HexToAddress("0x6455dC38fdF739b6fE021b30C7D9672C1c6DEb5c"),
125-
common.HexToAddress("0x49893c5Dfc035F4eE4E46faC014f6D4bC80F7f92"),
126-
common.HexToAddress("0x08e8b2Af4874e920de27723576A13d66008Af523"),
127-
common.HexToAddress("0x5D2f75392DdDa69a2818021dd6a64937904c8352"),
118+
if blockTime.Cmp(submitterContractActivationTimeFlare) > 0 {
119+
return []common.Address{
120+
common.HexToAddress("0x4E07E1F3DB3Dc9BAd56Cc829747cc0148234329F"),
121+
common.HexToAddress("0xB264Fad6Fdc65767998f93501945aB8F9108809d"),
122+
common.HexToAddress("0x366BeC54195bfD45DBB34b79Ad2dEC4010598947"),
123+
common.HexToAddress("0x2665B179d5fCE1118f06e23B5d6E7617c5Ff733A"),
124+
common.HexToAddress("0x65cBaFaDD7C914179aabcE9C35f918a4E36AfFf9"),
125+
common.HexToAddress("0x7eC6a7C7c4Ef003A75DC6c06352B48B37Ac2191B"),
126+
common.HexToAddress("0xEa9bC2F98eFFC6A27E2C31733c1905961826f73B"),
127+
common.HexToAddress("0xA4aA75a9B49c7f2B4be62b2999d7103E78D004C7"),
128+
common.HexToAddress("0x4DF8436D7578C2d3bc73d33B6644913e131B70FC"),
129+
}
130+
} else {
131+
return []common.Address{
132+
common.HexToAddress("0x0988Cf4828F4e4eD0cE7c07467E70e19095Ee152"),
133+
common.HexToAddress("0x6BC7DCa62010D418eB72CCdc58561e00C5868Ef1"),
134+
common.HexToAddress("0xE34Bb361536610a9DCcEa5292262e36AfF65c06c"),
135+
common.HexToAddress("0x8A3D627D86A81F5D21683F4963565C63DB5e1309"),
136+
common.HexToAddress("0x2D3e7e4b19bDc920fd9C57BD3072A31F5a59FeC8"),
137+
common.HexToAddress("0x6455dC38fdF739b6fE021b30C7D9672C1c6DEb5c"),
138+
common.HexToAddress("0x49893c5Dfc035F4eE4E46faC014f6D4bC80F7f92"),
139+
common.HexToAddress("0x08e8b2Af4874e920de27723576A13d66008Af523"),
140+
common.HexToAddress("0x5D2f75392DdDa69a2818021dd6a64937904c8352"),
141+
}
128142
}
129143
case chainID.Cmp(params.SongbirdChainID) == 0:
130144
return []common.Address{

coreth/core/state_transition.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ The state transitioning model does all the necessary work to work out a valid ne
5656
3) Create a new state object if the recipient is \0*32
5757
4) Value transfer
5858
== If contract creation ==
59-
4a) Attempt to run transaction data
60-
4b) If valid, use result as code for the new state object
59+
60+
4a) Attempt to run transaction data
61+
4b) If valid, use result as code for the new state object
62+
6163
== end ==
6264
5) Run Script section
6365
6) Derive new state root
@@ -300,13 +302,13 @@ func (st *StateTransition) preCheck() error {
300302
// TransitionDb will transition the state by applying the current message and
301303
// returning the evm execution result with following fields.
302304
//
303-
// - used gas:
304-
// total gas used (including gas being refunded)
305-
// - returndata:
306-
// the returned data from evm
307-
// - concrete execution error:
308-
// various **EVM** error which aborts the execution,
309-
// e.g. ErrOutOfGas, ErrExecutionReverted
305+
// - used gas:
306+
// total gas used (including gas being refunded)
307+
// - returndata:
308+
// the returned data from evm
309+
// - concrete execution error:
310+
// various **EVM** error which aborts the execution,
311+
// e.g. ErrOutOfGas, ErrExecutionReverted
310312
//
311313
// However if any consensus issue encountered, return the error directly with
312314
// nil evm execution result.
@@ -417,8 +419,8 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
417419
}
418420

419421
st.refundGas(rules.IsApricotPhase1)
420-
if vmerr == nil && msg.To() != nil && *msg.To() == common.HexToAddress(GetPrioritisedFTSOContract(timestamp)) && st.initialGas <= GetMaxFTSOGasLimit(timestamp) {
421-
nominalGasUsed := uint64(params.TxGas) // 21000
422+
if vmerr == nil && IsPrioritisedContractCall(chainID, msg.To(), ret, timestamp) && st.initialGas <= GetMaxFTSOGasLimit(timestamp) {
423+
nominalGasUsed := params.TxGas // 21000
422424
nominalGasPrice := uint64(params.ApricotPhase4MinBaseFee) // 25_000_000_000; the max base fee is 1_000_000_000_000
423425
nominalFee := new(big.Int).Mul(new(big.Int).SetUint64(nominalGasUsed), new(big.Int).SetUint64(nominalGasPrice))
424426
actualGasUsed := st.gasUsed()

0 commit comments

Comments
 (0)