Skip to content

Commit

Permalink
Merge branch 'dev' into gosec
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Sep 18, 2020
2 parents 9697030 + 71ff97e commit ff51276
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 55 deletions.
6 changes: 3 additions & 3 deletions .ci/run_e2e_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ AVALANCHE_IMAGE=$(docker image ls --format="{{.Repository}}" | head -n 1)
DOCKER_REPO="avaplatform"

E2E_TESTING_REMOTE="https://github.com/ava-labs/avalanche-testing.git"
E2E_TAG="v0.9.3-dev.2"
E2E_TAG="v0.9.3-dev.3"

mkdir -p "$E2E_TEST_HOME"
git clone "$E2E_TESTING_REMOTE" "$E2E_TEST_HOME"
Expand All @@ -19,8 +19,8 @@ go mod edit -replace github.com/ava-labs/avalanchego="$AVALANCHE_HOME"
bash "./scripts/rebuild_initializer_binary.sh"


TESTING_CONTROLLER_IMAGE="$DOCKER_REPO/avalanche-testing_controller:everest-v0.9.2-dev"
BYZANTINE_IMAGE="$DOCKER_REPO/avalanche-byzantine:testing-ci-stable"
TESTING_CONTROLLER_IMAGE="$DOCKER_REPO/avalanche-testing_controller:everest-dev.1"
BYZANTINE_IMAGE="$DOCKER_REPO/avalanche-byzantine:everest-dev.1"

docker pull "$TESTING_CONTROLLER_IMAGE"

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/AppsFlyer/go-sundheit v0.2.0
github.com/Microsoft/go-winio v0.4.14
github.com/ava-labs/coreth v0.3.0-rc.4 // indirect
github.com/ava-labs/coreth v0.3.0-rc.6 // indirect
github.com/ava-labs/go-ethereum v1.9.3
github.com/btcsuite/btcutil v1.0.2
github.com/decred/dcrd/dcrec/secp256k1/v3 v3.0.0-20200627015759-01fd2de07837
Expand Down
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,9 @@ github.com/ava-labs/avalanchego v0.6.1-rc.1/go.mod h1:TT6uA1BETZpVMR0xiFtE8I5Mv4
github.com/ava-labs/avalanchego v0.8.0-beta/go.mod h1:quYojL1hu0ue2glUT1ng28kADs9R94zGdEvfW0/HRg8=
github.com/ava-labs/avalanchego v0.8.3/go.mod h1:6zPzQv7m6vSvdKAwH+lLTga0IMd/0+HLMT5OULrpFcU=
github.com/ava-labs/coreth v0.2.14-rc.1/go.mod h1:Zhb60GFIB7G5AnUCks0Jo4Rezx/EovL8o+z51aBF1A8=
github.com/ava-labs/coreth v0.2.15-rc.2 h1:4aMeRsluvcxngtubJiLfRIj+TkAQJoIO5cuh9uFKVxI=
github.com/ava-labs/coreth v0.2.15-rc.2/go.mod h1:+sK2XGKCNA48uzeHWe4iBzmiOBYmYvnnzLtOkQeQfkk=
github.com/ava-labs/coreth v0.2.15-rc.3 h1:sGmqa3C0eiLlkMFSV8X1AZzSP5t+0+smIA/FeYi1wV0=
github.com/ava-labs/coreth v0.2.15-rc.3/go.mod h1:+sK2XGKCNA48uzeHWe4iBzmiOBYmYvnnzLtOkQeQfkk=
github.com/ava-labs/coreth v0.2.15-rc.4 h1:Fx/QTRC3fgO7lquQSOW1N/Wwd8zbQAh7n3Zcy6qQ3bk=
github.com/ava-labs/coreth v0.2.15-rc.4/go.mod h1:+sK2XGKCNA48uzeHWe4iBzmiOBYmYvnnzLtOkQeQfkk=
github.com/ava-labs/coreth v0.3.0-rc.2 h1:1TacYjPymREwVPDZBsTIrUPUdQDBdSCZrzyYaVMGvi8=
github.com/ava-labs/coreth v0.3.0-rc.2/go.mod h1:kbztUh9/VFQwAGLfQ5nqJsZR4NrFClUCWvkDhUMOYf0=
github.com/ava-labs/coreth v0.3.0-rc.4 h1:R03rHfCY/r4uiPOsc0BHtfuDoHN83c11VFCnmh5mhfM=
github.com/ava-labs/coreth v0.3.0-rc.4/go.mod h1:kbztUh9/VFQwAGLfQ5nqJsZR4NrFClUCWvkDhUMOYf0=
github.com/ava-labs/coreth v0.3.0-rc.5 h1:TMM8hQfCm5T0QFXRx+CYz62hF4mz73K23L0jS6TCN0U=
github.com/ava-labs/coreth v0.3.0-rc.5/go.mod h1:kbztUh9/VFQwAGLfQ5nqJsZR4NrFClUCWvkDhUMOYf0=
github.com/ava-labs/gecko v0.6.1-rc.1 h1:BhWmoDGA0Obs5ZbEdpNqw/3rx9ZMPmjcZu1oD+yySLY=
github.com/ava-labs/gecko v0.6.1-rc.1/go.mod h1:TT6uA1BETZpVMR0xiFtE8I5Mv4DULlS+lAL3xuYKnpA=
github.com/ava-labs/go-ethereum v1.9.3 h1:GmnMZ/dlvVAPFmWBzEpRJX49pUAymPfoASLNRJqR0AY=
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_coreth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BUILD_DIR="$AVALANCHE_PATH/build" # Where binaries go
PLUGIN_DIR="$BUILD_DIR/plugins" # Where plugin binaries (namely coreth) go
BINARY_PATH="$PLUGIN_DIR/evm"

CORETH_VER="0.3.0-rc.4" # Should match coreth version in go.mod
CORETH_VER="0.3.0-rc.6" # Should match coreth version in go.mod
CORETH_PATH="$GOPATH/pkg/mod/github.com/ava-labs/coreth@v$CORETH_VER"

if [[ $# -eq 2 ]]; then
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/add_delegator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func (tx *UnsignedAddDelegatorTx) SemanticVerify(
return nil, nil, nil, nil, permError{fmt.Errorf("chain timestamp (%s) not before validator's start time (%s)",
currentTimestamp,
validatorStartTime)}
} else if validatorStartTime.After(currentTimestamp.Add(maxFutureStartTime)) {
return nil, nil, nil, nil, permError{fmt.Errorf("validator start time (%s) more than two weeks after current chain timestamp (%s)", validatorStartTime, currentTimestamp)}
// } else if validatorStartTime.After(currentTimestamp.Add(maxFutureStartTime)) {
// return nil, nil, nil, nil, permError{fmt.Errorf("validator start time (%s) more than two weeks after current chain timestamp (%s)", validatorStartTime, currentTimestamp)}
}

// Ensure that the period this delegator delegates is a subset of the time
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/add_subnet_validator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func (tx *UnsignedAddSubnetValidatorTx) SemanticVerify(
return nil, nil, nil, nil, permError{fmt.Errorf("validator's start time (%s) is at or after current chain timestamp (%s)",
currentTimestamp,
validatorStartTime)}
} else if validatorStartTime.After(currentTimestamp.Add(maxFutureStartTime)) {
return nil, nil, nil, nil, permError{fmt.Errorf("validator start time (%s) more than two weeks after current chain timestamp (%s)", validatorStartTime, currentTimestamp)}
// } else if validatorStartTime.After(currentTimestamp.Add(maxFutureStartTime)) {
// return nil, nil, nil, nil, permError{fmt.Errorf("validator start time (%s) more than two weeks after current chain timestamp (%s)", validatorStartTime, currentTimestamp)}
}

// Ensure that the period this validator validates the specified subnet is a
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/add_validator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ func (tx *UnsignedAddValidatorTx) SemanticVerify(
return nil, nil, nil, nil, permError{fmt.Errorf("validator's start time (%s) at or before current timestamp (%s)",
startTime,
currentTime)}
} else if startTime.After(currentTime.Add(maxFutureStartTime)) {
return nil, nil, nil, nil, permError{fmt.Errorf("validator start time (%s) more than two weeks after current chain timestamp (%s)", startTime, currentTime)}
// } else if startTime.After(currentTime.Add(maxFutureStartTime)) {
// return nil, nil, nil, nil, permError{fmt.Errorf("validator start time (%s) more than two weeks after current chain timestamp (%s)", startTime, currentTime)}
}

_, isValidator, err := vm.isValidator(db, constants.PrimaryNetworkID, tx.Validator.NodeID)
Expand Down
32 changes: 16 additions & 16 deletions vms/platformvm/add_validator_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,22 +387,22 @@ func TestAddValidatorTxSemanticVerify(t *testing.T) {
}
vDB.Abort()

// Case: Validator's start time too far in the future
if tx, err := vm.newAddValidatorTx(
vm.minValidatorStake,
uint64(defaultValidateStartTime.Add(maxFutureStartTime).Unix()+1),
uint64(defaultValidateStartTime.Add(maxFutureStartTime).Add(defaultMinStakingDuration).Unix()+1),
nodeID,
nodeID,
PercentDenominator,
[]*crypto.PrivateKeySECP256K1R{keys[0]},
ids.ShortEmpty, // change addr
); err != nil {
t.Fatal(err)
} else if _, _, _, _, err := tx.UnsignedTx.(UnsignedProposalTx).SemanticVerify(vm, vDB, tx); err == nil {
t.Fatal("should've errored because start time too far in the future")
}
vDB.Abort()
// // Case: Validator's start time too far in the future
// if tx, err := vm.newAddValidatorTx(
// vm.minValidatorStake,
// uint64(defaultValidateStartTime.Add(maxFutureStartTime).Unix()+1),
// uint64(defaultValidateStartTime.Add(maxFutureStartTime).Add(defaultMinStakingDuration).Unix()+1),
// nodeID,
// nodeID,
// PercentDenominator,
// []*crypto.PrivateKeySECP256K1R{keys[0]},
// ids.ShortEmpty, // change addr
// ); err != nil {
// t.Fatal(err)
// } else if _, _, _, _, err := tx.UnsignedTx.(UnsignedProposalTx).SemanticVerify(vm, vDB, tx); err == nil {
// t.Fatal("should've errored because start time too far in the future")
// }
// vDB.Abort()

// Case: Validator already validating primary network
if tx, err := vm.newAddValidatorTx(
Expand Down
94 changes: 75 additions & 19 deletions vms/platformvm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,18 @@ func (service *Service) ImportKey(r *http.Request, args *ImportKeyArgs, reply *a
******************************************************
*/

// GetBalanceArgs ...
type GetBalanceArgs struct {
// Address to get the balance of
Address string `json:"address"`
}

// GetBalanceResponse ...
type GetBalanceResponse struct {
// Balance, in nAVAX, of the address
Balance json.Uint64 `json:"balance"`
UTXOIDs []*avax.UTXOID `json:"utxoIDs"`
Balance json.Uint64 `json:"balance"`
Unlocked json.Uint64 `json:"unlocked"`
LockedStakeable json.Uint64 `json:"lockedStakeable"`
LockedNotStakeable json.Uint64 `json:"lockedNotStakeable"`
UTXOIDs []*avax.UTXOID `json:"utxoIDs"`
}

// GetBalance gets the balance of an address
func (service *Service) GetBalance(_ *http.Request, args *GetBalanceArgs, response *GetBalanceResponse) error {
func (service *Service) GetBalance(_ *http.Request, args *api.JsonAddress, response *GetBalanceResponse) error {
service.vm.SnowmanVM.Ctx.Log.Info("Platform: GetBalance called for address %s", args.Address)

// Parse to address
Expand All @@ -187,20 +184,75 @@ func (service *Service) GetBalance(_ *http.Request, args *GetBalanceArgs, respon
return fmt.Errorf("couldn't get UTXO set of %s: %w", addr, err)
}

balance := uint64(0)
currentTime := service.vm.clock.Unix()

unlocked := uint64(0)
lockedStakeable := uint64(0)
lockedNotStakeable := uint64(0)

utxoFor:
for _, utxo := range utxos {
out, ok := utxo.Out.(*secp256k1fx.TransferOutput)
if !ok {
// TODO: support looking up tokens that are locked.
continue
}
balance, err = math.Add64(balance, out.Amount())
if err != nil {
return errors.New("overflow while calculating balance")
switch out := utxo.Out.(type) {
case *secp256k1fx.TransferOutput:
if out.Locktime <= currentTime {
newBalance, err := math.Add64(unlocked, out.Amount())
if err != nil {
return errors.New("overflow while calculating unlocked balance")
}
unlocked = newBalance
} else {
newBalance, err := math.Add64(lockedNotStakeable, out.Amount())
if err != nil {
return errors.New("overflow while calculating locked not stakeable balance")
}
lockedNotStakeable = newBalance
}
case *StakeableLockOut:
innerOut, ok := out.TransferableOut.(*secp256k1fx.TransferOutput)
if !ok {
service.vm.SnowmanVM.Ctx.Log.Warn("Unexpected Output type in UTXO: %T",
out.TransferableOut)
continue utxoFor
}
if innerOut.Locktime > currentTime {
newBalance, err := math.Add64(lockedNotStakeable, out.Amount())
if err != nil {
return errors.New("overflow while calculating locked not stakeable balance")
}
lockedNotStakeable = newBalance
} else if out.Locktime <= currentTime {
newBalance, err := math.Add64(unlocked, out.Amount())
if err != nil {
return errors.New("overflow while calculating unlocked balance")
}
unlocked = newBalance
} else {
newBalance, err := math.Add64(lockedStakeable, out.Amount())
if err != nil {
return errors.New("overflow while calculating unlocked stakeable balance")
}
lockedStakeable = newBalance
}
default:
continue utxoFor
}

response.UTXOIDs = append(response.UTXOIDs, &utxo.UTXOID)
}

lockedBalance, err := math.Add64(lockedStakeable, lockedNotStakeable)
if err != nil {
return errors.New("overflow while calculating locked balance")
}
balance, err := math.Add64(unlocked, lockedBalance)
if err != nil {
return errors.New("overflow while calculating total balance")
}

response.Balance = json.Uint64(balance)
response.Unlocked = json.Uint64(unlocked)
response.LockedStakeable = json.Uint64(lockedStakeable)
response.LockedNotStakeable = json.Uint64(lockedNotStakeable)
return nil
}

Expand Down Expand Up @@ -1885,7 +1937,11 @@ func (service *Service) GetStake(_ *http.Request, args *api.JsonAddresses, respo
if !stake.AssetID().Equals(service.vm.Ctx.AVAXAssetID) {
continue
}
secpOut, ok := stake.Out.(*secp256k1fx.TransferOutput)
out := stake.Out
if lockedOut, ok := out.(*StakeableLockOut); ok {
out = lockedOut.TransferableOut
}
secpOut, ok := out.(*secp256k1fx.TransferOutput)
if !ok {
continue
}
Expand Down
32 changes: 32 additions & 0 deletions vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,38 @@ func TestGetTx(t *testing.T) {
}
}

// Test method GetBalance
func TestGetBalance(t *testing.T) {
service := defaultService(t)
defaultAddress(t, service)
service.vm.Ctx.Lock.Lock()
defer func() { service.vm.Shutdown(); service.vm.Ctx.Lock.Unlock() }()

// Ensure GetStake is correct for each of the genesis validators
genesis, _ := defaultGenesis()
for _, utxo := range genesis.UTXOs {
request := api.JsonAddress{
Address: fmt.Sprintf("P-%s", utxo.Address),
}
reply := GetBalanceResponse{}
if err := service.GetBalance(nil, &request, &reply); err != nil {
t.Fatal(err)
}
if reply.Balance != cjson.Uint64(defaultBalance) {
t.Fatalf("Wrong balance. Expected %d ; Returned %d", reply.Balance, defaultBalance)
}
if reply.Unlocked != cjson.Uint64(defaultBalance) {
t.Fatalf("Wrong unlocked balance. Expected %d ; Returned %d", reply.Unlocked, defaultBalance)
}
if reply.LockedStakeable != 0 {
t.Fatalf("Wrong locked stakeable balance. Expected %d ; Returned %d", reply.LockedStakeable, 0)
}
if reply.LockedNotStakeable != 0 {
t.Fatalf("Wrong locked not stakeable balance. Expected %d ; Returned %d", reply.LockedNotStakeable, 0)
}
}
}

// Test method GetStake
func TestGetStake(t *testing.T) {
service := defaultService(t)
Expand Down

0 comments on commit ff51276

Please sign in to comment.