-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UTs for
PreExecutor.PreExecute()
(#1858)
- Loading branch information
1 parent
4440a22
commit 296bad6
Showing
5 changed files
with
187 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package chain_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ava-labs/avalanchego/utils/set" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/hypersdk/chain" | ||
"github.com/ava-labs/hypersdk/genesis" | ||
"github.com/ava-labs/hypersdk/internal/validitywindow" | ||
"github.com/ava-labs/hypersdk/internal/validitywindow/validitywindowtest" | ||
"github.com/ava-labs/hypersdk/state" | ||
"github.com/ava-labs/hypersdk/state/metadata" | ||
"github.com/ava-labs/hypersdk/utils" | ||
) | ||
|
||
var ( | ||
feeKey = string(chain.FeeKey([]byte{2})) | ||
|
||
errMockAuth = errors.New("mock auth error") | ||
errMockValidityWindow = errors.New("mock validity window error") | ||
) | ||
|
||
func TestPreExecutor(t *testing.T) { | ||
testRules := genesis.NewDefaultRules() | ||
ruleFactory := genesis.ImmutableRuleFactory{ | ||
Rules: testRules, | ||
} | ||
validTx := &chain.Transaction{ | ||
TransactionData: chain.TransactionData{ | ||
Base: &chain.Base{ | ||
Timestamp: utils.UnixRMilli( | ||
time.Now().UnixMilli(), | ||
testRules.GetValidityWindow(), | ||
), | ||
}, | ||
}, | ||
Auth: &mockAuth{ | ||
start: -1, | ||
end: -1, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
state map[string][]byte | ||
tx *chain.Transaction | ||
validityWindow chain.ValidityWindow | ||
err error | ||
}{ | ||
{ | ||
name: "valid tx", | ||
state: map[string][]byte{ | ||
feeKey: {}, | ||
}, | ||
tx: validTx, | ||
validityWindow: &validitywindowtest.MockTimeValidityWindow[*chain.Transaction]{}, | ||
}, | ||
{ | ||
name: "raw fee missing", | ||
tx: validTx, | ||
err: chain.ErrFailedToFetchFee, | ||
}, | ||
{ | ||
name: "validity window error", | ||
tx: validTx, | ||
state: map[string][]byte{ | ||
feeKey: {}, | ||
}, | ||
validityWindow: &validitywindowtest.MockTimeValidityWindow[*chain.Transaction]{ | ||
OnIsRepeat: func(context.Context, validitywindow.ExecutionBlock[*chain.Transaction], []*chain.Transaction, int64) (set.Bits, error) { | ||
return set.NewBits(), errMockValidityWindow | ||
}, | ||
}, | ||
err: errMockValidityWindow, | ||
}, | ||
{ | ||
name: "duplicate tx", | ||
tx: validTx, | ||
state: map[string][]byte{ | ||
feeKey: {}, | ||
}, | ||
validityWindow: &validitywindowtest.MockTimeValidityWindow[*chain.Transaction]{ | ||
OnIsRepeat: func(context.Context, validitywindow.ExecutionBlock[*chain.Transaction], []*chain.Transaction, int64) (set.Bits, error) { | ||
return set.NewBits(0), nil | ||
}, | ||
}, | ||
err: chain.ErrDuplicateTx, | ||
}, | ||
{ | ||
name: "invalid state keys", | ||
state: map[string][]byte{ | ||
feeKey: {}, | ||
}, | ||
tx: &chain.Transaction{ | ||
TransactionData: chain.TransactionData{ | ||
Actions: []chain.Action{ | ||
&mockAction{ | ||
stateKeys: state.Keys{ | ||
"": state.None, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Auth: &mockAuth{}, | ||
}, | ||
validityWindow: &validitywindowtest.MockTimeValidityWindow[*chain.Transaction]{}, | ||
err: chain.ErrInvalidKeyValue, | ||
}, | ||
{ | ||
name: "verify auth error", | ||
state: map[string][]byte{ | ||
feeKey: {}, | ||
}, | ||
tx: &chain.Transaction{ | ||
TransactionData: chain.TransactionData{ | ||
Base: &chain.Base{}, | ||
}, | ||
Auth: &mockAuth{ | ||
verifyError: errMockAuth, | ||
}, | ||
}, | ||
validityWindow: &validitywindowtest.MockTimeValidityWindow[*chain.Transaction]{}, | ||
err: errMockAuth, | ||
}, | ||
{ | ||
name: "tx pre-execute error", | ||
state: map[string][]byte{ | ||
feeKey: {}, | ||
}, | ||
tx: &chain.Transaction{ | ||
TransactionData: chain.TransactionData{ | ||
Base: &chain.Base{}, | ||
}, | ||
Auth: &mockAuth{}, | ||
}, | ||
validityWindow: &validitywindowtest.MockTimeValidityWindow[*chain.Transaction]{}, | ||
err: chain.ErrTimestampTooLate, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := require.New(t) | ||
ctx := context.Background() | ||
|
||
preExecutor := chain.NewPreExecutor( | ||
&ruleFactory, | ||
tt.validityWindow, | ||
metadata.NewDefaultManager(), | ||
&mockBalanceHandler{}, | ||
) | ||
|
||
r.ErrorIs( | ||
preExecutor.PreExecute( | ||
ctx, | ||
nil, | ||
state.ImmutableStorage(tt.state), | ||
tt.tx, | ||
), tt.err, | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters